From efe34243c674a06ead171adcce67a71efdf057e3 Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Tue, 12 Jul 2022 23:29:21 -0700 Subject: std.math: add `inline` to some functions These functions semantically benefit from being inline; it makes sense that `isInf(x)` where `x` is comptime-known should have a comptime-known result. --- lib/std/math/float.zig | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) (limited to 'lib/std/math/float.zig') diff --git a/lib/std/math/float.zig b/lib/std/math/float.zig index 72c7f086ac..1e44778576 100644 --- a/lib/std/math/float.zig +++ b/lib/std/math/float.zig @@ -3,19 +3,19 @@ const assert = std.debug.assert; const expect = std.testing.expect; /// Creates a raw "1.0" mantissa for floating point type T. Used to dedupe f80 logic. -fn mantissaOne(comptime T: type) comptime_int { +inline fn mantissaOne(comptime T: type) comptime_int { return if (@typeInfo(T).Float.bits == 80) 1 << floatFractionalBits(T) else 0; } /// Creates floating point type T from an unbiased exponent and raw mantissa. -fn reconstructFloat(comptime T: type, exponent: comptime_int, mantissa: comptime_int) T { +inline fn reconstructFloat(comptime T: type, exponent: comptime_int, mantissa: comptime_int) T { const TBits = std.meta.Int(.unsigned, @bitSizeOf(T)); const biased_exponent = @as(TBits, exponent + floatExponentMax(T)); return @bitCast(T, (biased_exponent << floatMantissaBits(T)) | @as(TBits, mantissa)); } /// Returns the number of bits in the exponent of floating point type T. -pub fn floatExponentBits(comptime T: type) comptime_int { +pub inline fn floatExponentBits(comptime T: type) comptime_int { assert(@typeInfo(T) == .Float); return switch (@typeInfo(T).Float.bits) { @@ -29,7 +29,7 @@ pub fn floatExponentBits(comptime T: type) comptime_int { } /// Returns the number of bits in the mantissa of floating point type T. -pub fn floatMantissaBits(comptime T: type) comptime_int { +pub inline fn floatMantissaBits(comptime T: type) comptime_int { assert(@typeInfo(T) == .Float); return switch (@typeInfo(T).Float.bits) { @@ -43,7 +43,7 @@ pub fn floatMantissaBits(comptime T: type) comptime_int { } /// Returns the number of fractional bits in the mantissa of floating point type T. -pub fn floatFractionalBits(comptime T: type) comptime_int { +pub inline fn floatFractionalBits(comptime T: type) comptime_int { assert(@typeInfo(T) == .Float); // standard IEEE floats have an implicit 0.m or 1.m integer part @@ -61,39 +61,39 @@ pub fn floatFractionalBits(comptime T: type) comptime_int { /// Returns the minimum exponent that can represent /// a normalised value in floating point type T. -pub fn floatExponentMin(comptime T: type) comptime_int { +pub inline fn floatExponentMin(comptime T: type) comptime_int { return -floatExponentMax(T) + 1; } /// Returns the maximum exponent that can represent /// a normalised value in floating point type T. -pub fn floatExponentMax(comptime T: type) comptime_int { +pub inline fn floatExponentMax(comptime T: type) comptime_int { return (1 << (floatExponentBits(T) - 1)) - 1; } /// Returns the smallest subnormal number representable in floating point type T. -pub fn floatTrueMin(comptime T: type) T { +pub inline fn floatTrueMin(comptime T: type) T { return reconstructFloat(T, floatExponentMin(T) - 1, 1); } /// Returns the smallest normal number representable in floating point type T. -pub fn floatMin(comptime T: type) T { +pub inline fn floatMin(comptime T: type) T { return reconstructFloat(T, floatExponentMin(T), mantissaOne(T)); } /// Returns the largest normal number representable in floating point type T. -pub fn floatMax(comptime T: type) T { +pub inline fn floatMax(comptime T: type) T { const all1s_mantissa = (1 << floatMantissaBits(T)) - 1; return reconstructFloat(T, floatExponentMax(T), all1s_mantissa); } /// Returns the machine epsilon of floating point type T. -pub fn floatEps(comptime T: type) T { +pub inline fn floatEps(comptime T: type) T { return reconstructFloat(T, -floatFractionalBits(T), mantissaOne(T)); } /// Returns the value inf for floating point type T. -pub fn inf(comptime T: type) T { +pub inline fn inf(comptime T: type) T { return reconstructFloat(T, floatExponentMax(T) + 1, mantissaOne(T)); } -- cgit v1.2.3 From 92bc3cbe27792be0300fb5f104c011a11f3cf40f Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Wed, 13 Jul 2022 13:14:37 -0700 Subject: stage2: fix comptime bitcast involving f80 * Sema: implement comptime bitcast of f80 with integer-like types bitwise rather than taking a round trip through memory layout. * Type: introduce `isAbiInt`. * Value: comptime memory write of f80 writes 0 bytes for padding instead of leaving the memory uninitialized. * Value: floatReadFromMemory has a more general implementation, checking the endianness rather than checking for specific architectures. This fixes behavior test failures occurring on MIPS. --- lib/std/math/float.zig | 10 +++++----- src/Sema.zig | 42 ++++++++++++++++++++++++++++++++++++++++++ src/type.zig | 10 ++++++++++ src/value.zig | 17 +++++++---------- 4 files changed, 64 insertions(+), 15 deletions(-) (limited to 'lib/std/math/float.zig') diff --git a/lib/std/math/float.zig b/lib/std/math/float.zig index 1e44778576..30e456fcbd 100644 --- a/lib/std/math/float.zig +++ b/lib/std/math/float.zig @@ -9,14 +9,14 @@ inline fn mantissaOne(comptime T: type) comptime_int { /// Creates floating point type T from an unbiased exponent and raw mantissa. inline fn reconstructFloat(comptime T: type, exponent: comptime_int, mantissa: comptime_int) T { - const TBits = std.meta.Int(.unsigned, @bitSizeOf(T)); + const TBits = @Type(.{ .Int = .{ .signedness = .unsigned, .bits = @bitSizeOf(T) } }); const biased_exponent = @as(TBits, exponent + floatExponentMax(T)); return @bitCast(T, (biased_exponent << floatMantissaBits(T)) | @as(TBits, mantissa)); } /// Returns the number of bits in the exponent of floating point type T. pub inline fn floatExponentBits(comptime T: type) comptime_int { - assert(@typeInfo(T) == .Float); + comptime assert(@typeInfo(T) == .Float); return switch (@typeInfo(T).Float.bits) { 16 => 5, @@ -30,7 +30,7 @@ pub inline fn floatExponentBits(comptime T: type) comptime_int { /// Returns the number of bits in the mantissa of floating point type T. pub inline fn floatMantissaBits(comptime T: type) comptime_int { - assert(@typeInfo(T) == .Float); + comptime assert(@typeInfo(T) == .Float); return switch (@typeInfo(T).Float.bits) { 16 => 10, @@ -44,7 +44,7 @@ pub inline fn floatMantissaBits(comptime T: type) comptime_int { /// Returns the number of fractional bits in the mantissa of floating point type T. pub inline fn floatFractionalBits(comptime T: type) comptime_int { - assert(@typeInfo(T) == .Float); + comptime assert(@typeInfo(T) == .Float); // standard IEEE floats have an implicit 0.m or 1.m integer part // f80 is special and has an explicitly stored bit in the MSB @@ -97,7 +97,7 @@ pub inline fn inf(comptime T: type) T { return reconstructFloat(T, floatExponentMax(T) + 1, mantissaOne(T)); } -test "std.math.float" { +test "float bits" { inline for ([_]type{ f16, f32, f64, f80, f128, c_longdouble }) |T| { // (1 +) for the sign bit, since it is separate from the other bits const size = 1 + floatExponentBits(T) + floatMantissaBits(T); diff --git a/src/Sema.zig b/src/Sema.zig index b139c3f89e..29840820d0 100644 --- a/src/Sema.zig +++ b/src/Sema.zig @@ -22571,6 +22571,48 @@ fn bitCastVal( const target = sema.mod.getTarget(); if (old_ty.eql(new_ty, sema.mod)) return val; + // Some conversions have a bitwise definition that ignores in-memory layout, + // such as converting between f80 and u80. + + if (old_ty.eql(Type.f80, sema.mod) and new_ty.isAbiInt()) { + const float = val.toFloat(f80); + switch (new_ty.intInfo(target).signedness) { + .signed => { + const int = @bitCast(i80, float); + const limbs = try sema.arena.alloc(std.math.big.Limb, 2); + const big_int = std.math.big.int.Mutable.init(limbs, int); + return Value.fromBigInt(sema.arena, big_int.toConst()); + }, + .unsigned => { + const int = @bitCast(u80, float); + const limbs = try sema.arena.alloc(std.math.big.Limb, 2); + const big_int = std.math.big.int.Mutable.init(limbs, int); + return Value.fromBigInt(sema.arena, big_int.toConst()); + }, + } + } + + if (new_ty.eql(Type.f80, sema.mod) and old_ty.isAbiInt()) { + var bigint_space: Value.BigIntSpace = undefined; + var bigint = try val.toBigIntAdvanced(&bigint_space, target, sema.kit(block, src)); + switch (old_ty.intInfo(target).signedness) { + .signed => { + // This conversion cannot fail because we already checked bit size before + // calling bitCastVal. + const int = bigint.to(i80) catch unreachable; + const float = @bitCast(f80, int); + return Value.Tag.float_80.create(sema.arena, float); + }, + .unsigned => { + // This conversion cannot fail because we already checked bit size before + // calling bitCastVal. + const int = bigint.to(u80) catch unreachable; + const float = @bitCast(f80, int); + return Value.Tag.float_80.create(sema.arena, float); + }, + } + } + // For types with well-defined memory layouts, we serialize them a byte buffer, // then deserialize to the new type. const abi_size = try sema.usizeCast(block, src, old_ty.abiSize(target)); diff --git a/src/type.zig b/src/type.zig index 765f1da18c..0744a50579 100644 --- a/src/type.zig +++ b/src/type.zig @@ -4439,6 +4439,16 @@ pub const Type = extern union { }; } + /// Returns true for integers, enums, error sets, and packed structs. + /// If this function returns true, then intInfo() can be called on the type. + pub fn isAbiInt(ty: Type) bool { + return switch (ty.zigTypeTag()) { + .Int, .Enum, .ErrorSet => true, + .Struct => ty.containerLayout() == .Packed, + else => false, + }; + } + /// Asserts the type is an integer, enum, error set, or vector of one of them. pub fn intInfo(self: Type, target: Target) struct { signedness: std.builtin.Signedness, bits: u16 } { var ty = self; diff --git a/src/value.zig b/src/value.zig index 04999c778a..b52e67e31c 100644 --- a/src/value.zig +++ b/src/value.zig @@ -1468,8 +1468,7 @@ pub const Value = extern union { const repr = std.math.break_f80(f); std.mem.writeInt(u64, buffer[0..8], repr.fraction, endian); std.mem.writeInt(u16, buffer[8..10], repr.exp, endian); - // TODO set the rest of the bytes to undefined. should we use 0xaa - // or is there a different way? + std.mem.set(u8, buffer[10..], 0); return; } const Int = @Type(.{ .Int = .{ @@ -1481,20 +1480,18 @@ pub const Value = extern union { } fn floatReadFromMemory(comptime F: type, target: Target, buffer: []const u8) F { + const endian = target.cpu.arch.endian(); if (F == f80) { - switch (target.cpu.arch) { - .i386, .x86_64 => return std.math.make_f80(.{ - .fraction = std.mem.readIntLittle(u64, buffer[0..8]), - .exp = std.mem.readIntLittle(u16, buffer[8..10]), - }), - else => {}, - } + return std.math.make_f80(.{ + .fraction = readInt(u64, buffer[0..8], endian), + .exp = readInt(u16, buffer[8..10], endian), + }); } const Int = @Type(.{ .Int = .{ .signedness = .unsigned, .bits = @typeInfo(F).Float.bits, } }); - const int = readInt(Int, buffer[0..@sizeOf(Int)], target.cpu.arch.endian()); + const int = readInt(Int, buffer[0..@sizeOf(Int)], endian); return @bitCast(F, int); } -- cgit v1.2.3