mirror of
https://codeberg.org/ziglang/zig.git
synced 2025-12-06 13:54:21 +00:00
std.math: rename make_f80 to F80.toFloat and break_f80 to F80.fromFloat
This commit is contained in:
parent
28383d4d98
commit
227fb4875f
8 changed files with 23 additions and 23 deletions
|
|
@ -61,8 +61,8 @@ pub inline fn cmpf2(comptime T: type, comptime RT: type, a: T, b: T) RT {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub inline fn cmp_f80(comptime RT: type, a: f80, b: f80) RT {
|
pub inline fn cmp_f80(comptime RT: type, a: f80, b: f80) RT {
|
||||||
const a_rep = std.math.break_f80(a);
|
const a_rep = std.math.F80.fromFloat(a);
|
||||||
const b_rep = std.math.break_f80(b);
|
const b_rep = std.math.F80.fromFloat(b);
|
||||||
const sig_bits = std.math.floatMantissaBits(f80);
|
const sig_bits = std.math.floatMantissaBits(f80);
|
||||||
const int_bit = 0x8000000000000000;
|
const int_bit = 0x8000000000000000;
|
||||||
const sign_bit = 0x8000;
|
const sign_bit = 0x8000;
|
||||||
|
|
|
||||||
|
|
@ -131,7 +131,7 @@ pub inline fn extend_f80(comptime src_t: type, a: std.meta.Int(.unsigned, @typeI
|
||||||
}
|
}
|
||||||
|
|
||||||
dst.exp |= sign;
|
dst.exp |= sign;
|
||||||
return std.math.make_f80(dst);
|
return dst.toFloat();
|
||||||
}
|
}
|
||||||
|
|
||||||
test {
|
test {
|
||||||
|
|
|
||||||
|
|
@ -18,7 +18,7 @@ fn __extendxftf2(a: f80) callconv(.C) f128 {
|
||||||
const dst_min_normal = @as(u128, 1) << dst_sig_bits;
|
const dst_min_normal = @as(u128, 1) << dst_sig_bits;
|
||||||
|
|
||||||
// Break a into a sign and representation of the absolute value
|
// Break a into a sign and representation of the absolute value
|
||||||
var a_rep = std.math.break_f80(a);
|
var a_rep = std.math.F80.fromFloat(a);
|
||||||
const sign = a_rep.exp & 0x8000;
|
const sign = a_rep.exp & 0x8000;
|
||||||
a_rep.exp &= 0x7FFF;
|
a_rep.exp &= 0x7FFF;
|
||||||
var abs_result: u128 = undefined;
|
var abs_result: u128 = undefined;
|
||||||
|
|
|
||||||
|
|
@ -8,8 +8,8 @@ comptime {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn __subxf3(a: f80, b: f80) callconv(.C) f80 {
|
fn __subxf3(a: f80, b: f80) callconv(.C) f80 {
|
||||||
var b_rep = std.math.break_f80(b);
|
var b_rep = std.math.F80.fromFloat(b);
|
||||||
b_rep.exp ^= 0x8000;
|
b_rep.exp ^= 0x8000;
|
||||||
const neg_b = std.math.make_f80(b_rep);
|
const neg_b = b_rep.toFloat();
|
||||||
return a + neg_b;
|
return a + neg_b;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -121,7 +121,7 @@ pub inline fn trunc_f80(comptime dst_t: type, a: f80) dst_t {
|
||||||
const dst_nan_mask = dst_qnan - 1;
|
const dst_nan_mask = dst_qnan - 1;
|
||||||
|
|
||||||
// Break a into a sign and representation of the absolute value
|
// Break a into a sign and representation of the absolute value
|
||||||
var a_rep = std.math.break_f80(a);
|
var a_rep = std.math.F80.fromFloat(a);
|
||||||
const sign = a_rep.exp & 0x8000;
|
const sign = a_rep.exp & 0x8000;
|
||||||
a_rep.exp &= 0x7FFF;
|
a_rep.exp &= 0x7FFF;
|
||||||
a_rep.fraction &= 0x7FFFFFFFFFFFFFFF;
|
a_rep.fraction &= 0x7FFFFFFFFFFFFFFF;
|
||||||
|
|
|
||||||
|
|
@ -64,5 +64,5 @@ pub fn __trunctfxf2(a: f128) callconv(.C) f80 {
|
||||||
}
|
}
|
||||||
|
|
||||||
res.exp |= sign;
|
res.exp |= sign;
|
||||||
return math.make_f80(res);
|
return res.toFloat();
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1720,21 +1720,21 @@ pub fn comptimeMod(num: anytype, comptime denom: comptime_int) IntFittingRange(0
|
||||||
pub const F80 = struct {
|
pub const F80 = struct {
|
||||||
fraction: u64,
|
fraction: u64,
|
||||||
exp: u16,
|
exp: u16,
|
||||||
|
|
||||||
|
pub fn toFloat(self: F80) f80 {
|
||||||
|
const int = (@as(u80, self.exp) << 64) | self.fraction;
|
||||||
|
return @as(f80, @bitCast(int));
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn fromFloat(x: f80) F80 {
|
||||||
|
const int = @as(u80, @bitCast(x));
|
||||||
|
return .{
|
||||||
|
.fraction = @as(u64, @truncate(int)),
|
||||||
|
.exp = @as(u16, @truncate(int >> 64)),
|
||||||
|
};
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
pub fn make_f80(repr: F80) f80 {
|
|
||||||
const int = (@as(u80, repr.exp) << 64) | repr.fraction;
|
|
||||||
return @as(f80, @bitCast(int));
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn break_f80(x: f80) F80 {
|
|
||||||
const int = @as(u80, @bitCast(x));
|
|
||||||
return .{
|
|
||||||
.fraction = @as(u64, @truncate(int)),
|
|
||||||
.exp = @as(u16, @truncate(int >> 64)),
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Returns -1, 0, or 1.
|
/// Returns -1, 0, or 1.
|
||||||
/// Supports integer and float types and vectors of integer and float types.
|
/// Supports integer and float types and vectors of integer and float types.
|
||||||
/// Unsigned integer types will always return 0 or 1.
|
/// Unsigned integer types will always return 0 or 1.
|
||||||
|
|
|
||||||
|
|
@ -61,7 +61,7 @@ fn nextAfterFloat(comptime T: type, x: T, y: T) T {
|
||||||
const integer_bit_mask = 1 << math.floatFractionalBits(f80);
|
const integer_bit_mask = 1 << math.floatFractionalBits(f80);
|
||||||
const exponent_bits_mask = (1 << math.floatExponentBits(f80)) - 1;
|
const exponent_bits_mask = (1 << math.floatExponentBits(f80)) - 1;
|
||||||
|
|
||||||
var x_parts = math.break_f80(x);
|
var x_parts = math.F80.fromFloat(x);
|
||||||
|
|
||||||
// Bitwise increment/decrement the fractional part while also taking care to update the
|
// Bitwise increment/decrement the fractional part while also taking care to update the
|
||||||
// exponent if we overflow the fractional part. This might flip the integer bit; this is
|
// exponent if we overflow the fractional part. This might flip the integer bit; this is
|
||||||
|
|
@ -88,7 +88,7 @@ fn nextAfterFloat(comptime T: type, x: T, y: T) T {
|
||||||
// set to cleared (if the old value was normal) or remained cleared (if the old value was
|
// set to cleared (if the old value was normal) or remained cleared (if the old value was
|
||||||
// subnormal), both of which are the outcomes we want.
|
// subnormal), both of which are the outcomes we want.
|
||||||
|
|
||||||
return math.make_f80(x_parts);
|
return x_parts.toFloat();
|
||||||
} else {
|
} else {
|
||||||
const Bits = std.meta.Int(.unsigned, @bitSizeOf(T));
|
const Bits = std.meta.Int(.unsigned, @bitSizeOf(T));
|
||||||
var x_bits: Bits = @bitCast(x);
|
var x_bits: Bits = @bitCast(x);
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue