2019-10-16 10:19:03 +02:00
|
|
|
module math
|
2021-05-08 12:32:29 +02:00
|
|
|
|
2019-10-16 10:19:03 +02:00
|
|
|
// f32_bits returns the IEEE 754 binary representation of f,
|
|
|
|
// with the sign bit of f and the result in the same bit position.
|
|
|
|
// f32_bits(f32_from_bits(x)) == x.
|
|
|
|
pub fn f32_bits(f f32) u32 {
|
2021-08-21 16:18:57 +02:00
|
|
|
p := u32(0)
|
|
|
|
#let buffer = new ArrayBuffer(4)
|
|
|
|
#let floatArr = new Float32Array(buffer)
|
|
|
|
#floatArr[0] = f.val
|
|
|
|
#let uintArr = new Uint32Array(buffer)
|
|
|
|
#p.val = uintArr[0]
|
|
|
|
|
2020-03-19 10:24:51 +01:00
|
|
|
return p
|
2019-10-16 10:19:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// f32_from_bits returns the floating-point number corresponding
|
|
|
|
// to the IEEE 754 binary representation b, with the sign bit of b
|
|
|
|
// and the result in the same bit position.
|
|
|
|
// f32_from_bits(f32_bits(x)) == x.
|
|
|
|
pub fn f32_from_bits(b u32) f32 {
|
2021-08-21 16:18:57 +02:00
|
|
|
p := f32(0.0)
|
|
|
|
#let buffer = new ArrayBuffer(4)
|
|
|
|
#let floatArr = new Float32Array(buffer)
|
|
|
|
#let uintArr = new Uint32Array(buffer)
|
|
|
|
#uintArr[0] = Number(b.val)
|
|
|
|
#p.val = floatArr[0]
|
|
|
|
|
2020-03-19 10:24:51 +01:00
|
|
|
return p
|
2019-10-16 10:19:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// f64_bits returns the IEEE 754 binary representation of f,
|
|
|
|
// with the sign bit of f and the result in the same bit position,
|
|
|
|
// and f64_bits(f64_from_bits(x)) == x.
|
|
|
|
pub fn f64_bits(f f64) u64 {
|
2021-08-21 16:18:57 +02:00
|
|
|
p := u64(0)
|
|
|
|
#let buffer = new ArrayBuffer(8)
|
|
|
|
#let floatArr = new Float64Array(buffer)
|
|
|
|
#floatArr[0] = f.val
|
|
|
|
#let uintArr = new BigUint64Array(buffer)
|
|
|
|
#p.val = uintArr[0]
|
|
|
|
|
2020-03-19 10:24:51 +01:00
|
|
|
return p
|
2019-10-16 10:19:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// f64_from_bits returns the floating-point number corresponding
|
|
|
|
// to the IEEE 754 binary representation b, with the sign bit of b
|
|
|
|
// and the result in the same bit position.
|
|
|
|
// f64_from_bits(f64_bits(x)) == x.
|
|
|
|
pub fn f64_from_bits(b u64) f64 {
|
2021-08-21 16:18:57 +02:00
|
|
|
p := 0.0
|
|
|
|
#let buffer = new ArrayBuffer(8)
|
|
|
|
#let floatArr = new Float64Array(buffer)
|
|
|
|
#let uintArr = new BigUint64Array(buffer)
|
2021-10-08 16:44:55 +02:00
|
|
|
#uintArr[0] = b.val
|
2021-08-21 16:18:57 +02:00
|
|
|
#p.val = floatArr[0]
|
|
|
|
|
2020-03-19 10:24:51 +01:00
|
|
|
return p
|
2019-10-16 10:19:03 +02:00
|
|
|
}
|