-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
add toBits
and fromBits
to math
#16403
Conversation
I don't think this should be in |
It's the basis of As to
Float64bits in GoLang/VLang toBits in Rust |
So which is better? func toBits*(x: float64): uint64 =
## Returns the IEEE 754 binary representation of float64.
cast[ptr uint64](unsafeAddr x)[]
func toBits2*(x: float64): uint64 =
## Returns the IEEE 754 binary representation of float64.
cast[uint64](x)
echo toBits(12.4)
echo toBits2(12.4) N_LIB_PRIVATE N_NIMCALL(NU64, toBits)(NF x) {
NU64 result;
result = (NU64)0;
result = (*((NU64*) ((&x))));
return result;
}
N_LIB_PRIVATE N_NIMCALL(NU64, toBits2)(NF x) {
NU64 result;
union { NF source; NU64 dest; } LOC1;
result = (NU64)0;
LOC1.source = x;
result = LOC1.dest;
return result;
} I prefer |
can you check generated asm in both cases? if |
This doesn't work in VM |
we can probably do something similar to maybe this can be unified with a custom type: when defined js:
type Bits = array[2, uint32]
else:
type Bits = uint
proc toBits(x: float): Bits =
# now we can support c,vm,js EDIT: see also proc asBigInt(x: float): int64 =
# result is a `BigInt` type in js, but we cheat the type system
# and say it is a `int64` type.
# TODO refactor it using bigInt once jsBigInt is ready, pending pr #1640
asm """
const buffer = new ArrayBuffer(8);
const floatBuffer = new Float64Array(buffer);
const uintBuffer = new BigUint64Array(buffer);
floatBuffer[0] = `x`;
`result` = uintBuffer[0];""" (with safari support caveat for |
It's not easy to support JS backend.
Todo in the following PR: