authorgravatar for jacobly@ziglang.orgJacob Young <jacobly@ziglang.org> 2023-05-10 13:20:13-04:00
committergravatar for jacobly@ziglang.orgJacob Young <jacobly@ziglang.org> 2023-05-10 15:16:50-04:00
log716d6a026f468cdb6b84a3e7f1004904b9ac6ecc
treeed51fea6f94d8c06702beace4ce50ec8c7dd5290
parentc5b96c7447efde0d10de9689f03d151afcafbad5

std: revert `comptime_float` support

Removed since I'm aware of some design considerations of `comptime_float`.

4 files changed, 21 insertions(+), 28 deletions(-)

lib/std/math.zig-1
...@@ -37,7 +37,6 @@ pub const sqrt2 = 1.414213562373095048801688724209698079;...@@ -37,7 +37,6 @@ pub const sqrt2 = 1.414213562373095048801688724209698079;
37/// 1/sqrt(2)37/// 1/sqrt(2)
38pub const sqrt1_2 = 0.707106781186547524400844362104849039;38pub const sqrt1_2 = 0.707106781186547524400844362104849039;
3939
40pub const floatBits = @import("math/float.zig").floatBits;
41pub const floatExponentBits = @import("math/float.zig").floatExponentBits;40pub const floatExponentBits = @import("math/float.zig").floatExponentBits;
42pub const floatMantissaBits = @import("math/float.zig").floatMantissaBits;41pub const floatMantissaBits = @import("math/float.zig").floatMantissaBits;
43pub const floatFractionalBits = @import("math/float.zig").floatFractionalBits;42pub const floatFractionalBits = @import("math/float.zig").floatFractionalBits;
lib/std/math/copysign.zig+7-8
...@@ -4,17 +4,16 @@ const expect = std.testing.expect;...@@ -4,17 +4,16 @@ const expect = std.testing.expect;
44
5/// Returns a value with the magnitude of `magnitude` and the sign of `sign`.5/// Returns a value with the magnitude of `magnitude` and the sign of `sign`.
6pub fn copysign(magnitude: anytype, sign: @TypeOf(magnitude)) @TypeOf(magnitude) {6pub fn copysign(magnitude: anytype, sign: @TypeOf(magnitude)) @TypeOf(magnitude) {
7 const bits = math.floatBits(@TypeOf(magnitude));7 const T = @TypeOf(magnitude);
8 const FBits = @Type(.{ .Float = .{ .bits = bits } });8 const TBits = std.meta.Int(.unsigned, @typeInfo(T).Float.bits);
9 const TBits = @Type(.{ .Int = .{ .signedness = .unsigned, .bits = bits } });9 const sign_bit_mask = @as(TBits, 1) << (@bitSizeOf(T) - 1);
10 const sign_bit_mask = @as(TBits, 1) << (bits - 1);10 const mag = @bitCast(TBits, magnitude) & ~sign_bit_mask;
11 const mag = @bitCast(TBits, @as(FBits, magnitude)) & ~sign_bit_mask;11 const sgn = @bitCast(TBits, sign) & sign_bit_mask;
12 const sgn = @bitCast(TBits, @as(FBits, sign)) & sign_bit_mask;12 return @bitCast(T, mag | sgn);
13 return @bitCast(FBits, mag | sgn);
14}13}
1514
16test "math.copysign" {15test "math.copysign" {
17 inline for ([_]type{ f16, f32, f64, f80, f128, c_longdouble, comptime_float }) |T| {16 inline for ([_]type{ f16, f32, f64, f80, f128 }) |T| {
18 try expect(copysign(@as(T, 1.0), @as(T, 1.0)) == 1.0);17 try expect(copysign(@as(T, 1.0), @as(T, 1.0)) == 1.0);
19 try expect(copysign(@as(T, 2.0), @as(T, -2.0)) == -2.0);18 try expect(copysign(@as(T, 2.0), @as(T, -2.0)) == -2.0);
20 try expect(copysign(@as(T, -3.0), @as(T, 3.0)) == 3.0);19 try expect(copysign(@as(T, -3.0), @as(T, 3.0)) == 3.0);
lib/std/math/float.zig+12-17
...@@ -4,29 +4,21 @@ const expect = std.testing.expect;...@@ -4,29 +4,21 @@ const expect = std.testing.expect;
44
5/// Creates a raw "1.0" mantissa for floating point type T. Used to dedupe f80 logic.5/// Creates a raw "1.0" mantissa for floating point type T. Used to dedupe f80 logic.
6inline fn mantissaOne(comptime T: type) comptime_int {6inline fn mantissaOne(comptime T: type) comptime_int {
7 return 1 << floatFractionalBits(T) & ((1 << floatMantissaBits(T)) - 1);7 return if (@typeInfo(T).Float.bits == 80) 1 << floatFractionalBits(T) else 0;
8}8}
99
10/// Creates floating point type T from an unbiased exponent and raw mantissa.10/// Creates floating point type T from an unbiased exponent and raw mantissa.
11inline fn reconstructFloat(comptime T: type, comptime exponent: comptime_int, comptime mantissa: comptime_int) T {11inline fn reconstructFloat(comptime T: type, comptime exponent: comptime_int, comptime mantissa: comptime_int) T {
12 const FBits = @Type(.{ .Float = .{ .bits = floatBits(T) } });12 const TBits = @Type(.{ .Int = .{ .signedness = .unsigned, .bits = @bitSizeOf(T) } });
13 const TBits = @Type(.{ .Int = .{ .signedness = .unsigned, .bits = floatBits(T) } });
14 const biased_exponent = @as(TBits, exponent + floatExponentMax(T));13 const biased_exponent = @as(TBits, exponent + floatExponentMax(T));
15 return @bitCast(FBits, (biased_exponent << floatMantissaBits(T)) | @as(TBits, mantissa));14 return @bitCast(T, (biased_exponent << floatMantissaBits(T)) | @as(TBits, mantissa));
16}
17
18/// Returns the number of bits in floating point type T.
19pub inline fn floatBits(comptime T: type) comptime_int {
20 return switch (@typeInfo(T)) {
21 .Float => |info| info.bits,
22 .ComptimeFloat => 128,
23 else => @compileError(@typeName(T) ++ " is not a floating point type"),
24 };
25}15}
2616
27/// Returns the number of bits in the exponent of floating point type T.17/// Returns the number of bits in the exponent of floating point type T.
28pub inline fn floatExponentBits(comptime T: type) comptime_int {18pub inline fn floatExponentBits(comptime T: type) comptime_int {
29 return switch (floatBits(T)) {19 comptime assert(@typeInfo(T) == .Float);
20
21 return switch (@typeInfo(T).Float.bits) {
30 16 => 5,22 16 => 5,
31 32 => 8,23 32 => 8,
32 64 => 11,24 64 => 11,
...@@ -38,7 +30,9 @@ pub inline fn floatExponentBits(comptime T: type) comptime_int {...@@ -38,7 +30,9 @@ pub inline fn floatExponentBits(comptime T: type) comptime_int {
3830
39/// Returns the number of bits in the mantissa of floating point type T.31/// Returns the number of bits in the mantissa of floating point type T.
40pub inline fn floatMantissaBits(comptime T: type) comptime_int {32pub inline fn floatMantissaBits(comptime T: type) comptime_int {
41 return switch (floatBits(T)) {33 comptime assert(@typeInfo(T) == .Float);
34
35 return switch (@typeInfo(T).Float.bits) {
42 16 => 10,36 16 => 10,
43 32 => 23,37 32 => 23,
44 64 => 52,38 64 => 52,
...@@ -50,10 +44,12 @@ pub inline fn floatMantissaBits(comptime T: type) comptime_int {...@@ -50,10 +44,12 @@ pub inline fn floatMantissaBits(comptime T: type) comptime_int {
5044
51/// Returns the number of fractional bits in the mantissa of floating point type T.45/// Returns the number of fractional bits in the mantissa of floating point type T.
52pub inline fn floatFractionalBits(comptime T: type) comptime_int {46pub inline fn floatFractionalBits(comptime T: type) comptime_int {
47 comptime assert(@typeInfo(T) == .Float);
48
53 // standard IEEE floats have an implicit 0.m or 1.m integer part49 // standard IEEE floats have an implicit 0.m or 1.m integer part
54 // f80 is special and has an explicitly stored bit in the MSB50 // f80 is special and has an explicitly stored bit in the MSB
55 // this function corresponds to `MANT_DIG - 1' from C51 // this function corresponds to `MANT_DIG - 1' from C
56 return switch (floatBits(T)) {52 return switch (@typeInfo(T).Float.bits) {
57 16 => 10,53 16 => 10,
58 32 => 23,54 32 => 23,
59 64 => 52,55 64 => 52,
...@@ -105,7 +101,6 @@ test "float bits" {...@@ -105,7 +101,6 @@ test "float bits" {
105 inline for ([_]type{ f16, f32, f64, f80, f128, c_longdouble }) |T| {101 inline for ([_]type{ f16, f32, f64, f80, f128, c_longdouble }) |T| {
106 // (1 +) for the sign bit, since it is separate from the other bits102 // (1 +) for the sign bit, since it is separate from the other bits
107 const size = 1 + floatExponentBits(T) + floatMantissaBits(T);103 const size = 1 + floatExponentBits(T) + floatMantissaBits(T);
108 try expect(floatBits(T) == size);
109 try expect(@bitSizeOf(T) == size);104 try expect(@bitSizeOf(T) == size);
110105
111 // for machine epsilon, assert expmin <= -prec <= expmax106 // for machine epsilon, assert expmin <= -prec <= expmax
lib/std/math/nan.zig+2-2
...@@ -2,13 +2,13 @@ const math = @import("../math.zig");...@@ -2,13 +2,13 @@ const math = @import("../math.zig");
22
3/// Returns the nan representation for type T.3/// Returns the nan representation for type T.
4pub inline fn nan(comptime T: type) T {4pub inline fn nan(comptime T: type) T {
5 return switch (math.floatBits(T)) {5 return switch (@typeInfo(T).Float.bits) {
6 16 => math.nan_f16,6 16 => math.nan_f16,
7 32 => math.nan_f32,7 32 => math.nan_f32,
8 64 => math.nan_f64,8 64 => math.nan_f64,
9 80 => math.nan_f80,9 80 => math.nan_f80,
10 128 => math.nan_f128,10 128 => math.nan_f128,
11 else => @compileError("unknown floating point type " ++ @typeName(T)),11 else => @compileError("unreachable"),
12 };12 };
13}13}
1414