[Libre-soc-dev] New instruction

Jacob Lifshay programmerjake at gmail.com
Tue Mar 2 17:52:49 GMT 2021


On Tue, Mar 2, 2021, 03:06 lkcl <luke.leighton at gmail.com> wrote:

> (top-posting to preserve message, hope you don't mind alain)
>
> we shouuuuld already have that covered, through v3.0
>
>
> Floating Round to Integer Nearest X-form
>

the one you wanted is the round towards zero version, though even that is
not correct for javascript.


> 4.6.7.3 Floating Round to Integer Instructions
>
> v3.0C p163
>

Except that Javascript has different semantics for conversion from f64 ->
i32:
it is a wrapping conversion instead of the saturating conversion used by
most cpus.
https://262.ecma-international.org/#sec-toint32

translation to C (assuming I didn't make a mistake):
int32_t js_cvt(double v) {
    if(!isfinite(v))
        return 0;
    v = trunc(v);
    v = fmod(v, 1LL << 32);
    if(v < 0)
        v += 1LL << 32;
    // now v is an integer in the range 0 to 2^32 - 1
    if(v >= (1LL << 31))
        v -= 1LL << 32;
    // now v is an integer in the range -(2^31) to 2^31 - 1
    return (int32_t)v;
}

Jacob


More information about the Libre-soc-dev mailing list