cut high bits for to large fraction an exponent

This commit is contained in:
Fabian Schmalzried 2024-03-26 14:31:23 +01:00
parent 834015a758
commit 546cb17fb9
No known key found for this signature in database
GPG Key ID: D691D5DA4CEF42E7
2 changed files with 5 additions and 2 deletions

View File

@ -662,9 +662,9 @@ pub fn f64ToParts(self: f64) callconv(.C) F64Parts {
}
pub fn f32FromParts(parts: F32Parts) callconv(.C) f32 {
return @as(f32, @bitCast(parts.fraction | (@as(u32, parts.exponent) << 23) | (@as(u32, @intFromBool(parts.sign)) << 31)));
return @as(f32, @bitCast(parts.fraction & 0x7fffff | (@as(u32, parts.exponent) << 23) | (@as(u32, @intFromBool(parts.sign)) << 31)));
}
pub fn f64FromParts(parts: F64Parts) callconv(.C) f64 {
return @as(f64, @bitCast(parts.fraction | (@as(u64, parts.exponent) << 52) | (@as(u64, @intFromBool(parts.sign)) << 63)));
return @as(f64, @bitCast(parts.fraction & 0xfffffffffffff | (@as(u64, parts.exponent & 0x7ff) << 52) | (@as(u64, @intFromBool(parts.sign)) << 63)));
}

View File

@ -1428,7 +1428,10 @@ f32ToParts : F32 -> { sign : Bool, exponent : U8, fraction : U32 }
f64ToParts : F64 -> { sign : Bool, exponent : U16, fraction : U64 }
## Combine parts of a [F32] according to IEEE 754 standard.
## The fraction should not be bigger than 0x007F_FFFF, any bigger value will be truncated.
f32FromParts : { sign : Bool, exponent : U8, fraction : U32 } -> F32
## Combine parts of a [F64] according to IEEE 754 standard.
## The fraction should not be bigger than 0x000F_FFFF_FFFF_FFFF, any bigger value will be truncated.
## The exponent should not be bigger than 0x07FF, any bigger value will be truncated.
f64FromParts : { sign : Bool, exponent : U16, fraction : U64 } -> F64