# [libre-riscv-dev] [Bug 74] preliminary exploratory software emulation of FP SQRT

bugzilla-daemon at libre-riscv.org bugzilla-daemon at libre-riscv.org
Sun Apr 28 13:30:05 BST 2019

```http://bugs.libre-riscv.org/show_bug.cgi?id=74

--- Comment #15 from Luke Kenneth Casson Leighton <lkcl at lkcl.net> ---
(In reply to Aleksandar Kostovic from comment #13)
> i see, works the same as the simpler function...

it does... except... try sqrt(65536)...

try this:

for Q in range(1, int(1e7)):
print(Q, sqrt(Q), sqrtsimple(Q), int(Q**0.5))
assert int(Q**0.5) == sqrtsimple(Q), "Q sqrtsimpl fail %d" % Q
assert int(Q**0.5) == sqrt(Q), "Q sqrt fail %d" % Q

> really need to find an algorithms that would suit floating point math.

that *is* the algorithm that suits FP math!

congratulations, sqrt() and sqrtsimple() - if it worked - handle the
mantissa.

now you just need to do the exponent.

> any useful links or pseudo code i could use?

you're already doing it.  don't worry, keep going, ok?

you know how to do the square root of an exponent, right?

you just... divide it by 2.  yes, really!

dang this is actually hard to find proper simple illustrations, online...
a whole lot of modern total bullshit about a new-fangled concept called

google search pulls up results that are such total bullshit i'm just going
to write it out for you.

the square root of a number when it is written in exponent form (e^x)
is basically that you multiply the exponent (x) by 0.5.

exactly as with that x ** 0.5 thing you discovered that python does,
remember?

so:

sqrt( e ^ x ) == e ^ (x*0.5)

therefore, very very simply (far simpler than you're imagining that it is),
to take the square root of the exponent of an IEEE754 FP number...

you DIVIDE THE EXPONENT BY TWO!

that's just a shift operation!

that's it.

that's all it is.

so with that in mind, does comment #3 now make a bit more sense?
http://bugs.libre-riscv.org/show_bug.cgi?id=74#c3

obviously, in IEEE754 FP, exponent is also an integer, so if it is
an odd integer, you lose information.  to adjust for that, you add
1 to the exponent and shift the mantissa.  now the exponent is
even, *now* it will divide cleanly by 2 (shift).

basically you are nearly there.  it really is this simple.

you should be able to write the function which takes e and m as arguments,
(from comment #3) that handles the core algorithm, in about... 6 lines.

--
You are receiving this mail because:
You are on the CC list for the bug.
```