f32/f64toParts in repl only

This commit is contained in:
Fabian Schmalzried 2024-03-16 14:41:20 +01:00
parent 64e9899cad
commit 473b8ef2d3
No known key found for this signature in database
GPG key ID: D691D5DA4CEF42E7
12 changed files with 72 additions and 6 deletions

View file

@ -111,6 +111,8 @@ comptime {
exportNumFn(num.lessThanOrEqualU128, "less_than_or_equal.u128");
exportNumFn(num.greaterThanU128, "greater_than.u128");
exportNumFn(num.greaterThanOrEqualU128, "greater_than_or_equal.u128");
exportNumFn(num.f32ToParts, "f32_to_parts");
exportNumFn(num.f64ToParts, "f64_to_parts");
inline for (INTEGERS, 0..) |T, i| {
num.exportPow(T, ROC_BUILTINS ++ "." ++ NUM ++ ".pow_int.");

View file

@ -15,6 +15,18 @@ pub fn NumParseResult(comptime T: type) type {
};
}
pub const F32Parts = extern struct {
fraction: u32,
exponent: u8,
sign: bool,
};
pub const F64Parts = extern struct {
fraction: u64,
exponent: u16,
sign: bool,
};
pub const U256 = struct {
hi: u128,
lo: u128,
@ -630,3 +642,21 @@ pub fn exportCountOneBits(comptime T: type, comptime name: []const u8) void {
}.func;
@export(f, .{ .name = name ++ @typeName(T), .linkage = .Strong });
}
pub fn f32ToParts(self: f32) callconv(.C) F32Parts {
const u32Value = @as(u32, @bitCast(self));
return F32Parts{
.fraction = u32Value & 0x7fffff,
.exponent = @truncate(u32Value >> 23 & 0xff),
.sign = u32Value >> 31 & 1 == 1,
};
}
pub fn f64ToParts(self: f64) callconv(.C) F64Parts {
const u64Value = @as(u64, @bitCast(self));
return F64Parts{
.fraction = u64Value & 0xfffffffffffff,
.exponent = @truncate(u64Value >> 52 & 0x7ff),
.sign = u64Value >> 63 & 1 == 1,
};
}