@@ -3,20 +3,20 @@ const assert = std.debug.assert;
33const expect = std .testing .expect ;
44
55/// Creates a raw "1.0" mantissa for floating point type T. Used to dedupe f80 logic.
6- fn mantissaOne (comptime T : type ) comptime_int {
6+ inline fn mantissaOne (comptime T : type ) comptime_int {
77 return if (@typeInfo (T ).Float .bits == 80 ) 1 << floatFractionalBits (T ) else 0 ;
88}
99
1010/// Creates floating point type T from an unbiased exponent and raw mantissa.
11- fn reconstructFloat (comptime T : type , exponent : comptime_int , mantissa : comptime_int ) T {
12- const TBits = std . meta .Int ( . unsigned , @bitSizeOf (T ));
11+ inline fn reconstructFloat (comptime T : type , exponent : comptime_int , mantissa : comptime_int ) T {
12+ const TBits = @Type (.{ .Int = .{ . signedness = . unsigned , . bits = @bitSizeOf (T ) } } );
1313 const biased_exponent = @as (TBits , exponent + floatExponentMax (T ));
1414 return @bitCast (T , (biased_exponent << floatMantissaBits (T )) | @as (TBits , mantissa ));
1515}
1616
1717/// Returns the number of bits in the exponent of floating point type T.
18- pub fn floatExponentBits (comptime T : type ) comptime_int {
19- assert (@typeInfo (T ) == .Float );
18+ pub inline fn floatExponentBits (comptime T : type ) comptime_int {
19+ comptime assert (@typeInfo (T ) == .Float );
2020
2121 return switch (@typeInfo (T ).Float .bits ) {
2222 16 = > 5 ,
@@ -29,8 +29,8 @@ pub fn floatExponentBits(comptime T: type) comptime_int {
2929}
3030
3131/// Returns the number of bits in the mantissa of floating point type T.
32- pub fn floatMantissaBits (comptime T : type ) comptime_int {
33- assert (@typeInfo (T ) == .Float );
32+ pub inline fn floatMantissaBits (comptime T : type ) comptime_int {
33+ comptime assert (@typeInfo (T ) == .Float );
3434
3535 return switch (@typeInfo (T ).Float .bits ) {
3636 16 = > 10 ,
@@ -43,8 +43,8 @@ pub fn floatMantissaBits(comptime T: type) comptime_int {
4343}
4444
4545/// Returns the number of fractional bits in the mantissa of floating point type T.
46- pub fn floatFractionalBits (comptime T : type ) comptime_int {
47- assert (@typeInfo (T ) == .Float );
46+ pub inline fn floatFractionalBits (comptime T : type ) comptime_int {
47+ comptime assert (@typeInfo (T ) == .Float );
4848
4949 // standard IEEE floats have an implicit 0.m or 1.m integer part
5050 // f80 is special and has an explicitly stored bit in the MSB
@@ -61,43 +61,43 @@ pub fn floatFractionalBits(comptime T: type) comptime_int {
6161
6262/// Returns the minimum exponent that can represent
6363/// a normalised value in floating point type T.
64- pub fn floatExponentMin (comptime T : type ) comptime_int {
64+ pub inline fn floatExponentMin (comptime T : type ) comptime_int {
6565 return - floatExponentMax (T ) + 1 ;
6666}
6767
6868/// Returns the maximum exponent that can represent
6969/// a normalised value in floating point type T.
70- pub fn floatExponentMax (comptime T : type ) comptime_int {
70+ pub inline fn floatExponentMax (comptime T : type ) comptime_int {
7171 return (1 << (floatExponentBits (T ) - 1 )) - 1 ;
7272}
7373
7474/// Returns the smallest subnormal number representable in floating point type T.
75- pub fn floatTrueMin (comptime T : type ) T {
75+ pub inline fn floatTrueMin (comptime T : type ) T {
7676 return reconstructFloat (T , floatExponentMin (T ) - 1 , 1 );
7777}
7878
7979/// Returns the smallest normal number representable in floating point type T.
80- pub fn floatMin (comptime T : type ) T {
80+ pub inline fn floatMin (comptime T : type ) T {
8181 return reconstructFloat (T , floatExponentMin (T ), mantissaOne (T ));
8282}
8383
8484/// Returns the largest normal number representable in floating point type T.
85- pub fn floatMax (comptime T : type ) T {
85+ pub inline fn floatMax (comptime T : type ) T {
8686 const all1s_mantissa = (1 << floatMantissaBits (T )) - 1 ;
8787 return reconstructFloat (T , floatExponentMax (T ), all1s_mantissa );
8888}
8989
9090/// Returns the machine epsilon of floating point type T.
91- pub fn floatEps (comptime T : type ) T {
91+ pub inline fn floatEps (comptime T : type ) T {
9292 return reconstructFloat (T , - floatFractionalBits (T ), mantissaOne (T ));
9393}
9494
9595/// Returns the value inf for floating point type T.
96- pub fn inf (comptime T : type ) T {
96+ pub inline fn inf (comptime T : type ) T {
9797 return reconstructFloat (T , floatExponentMax (T ) + 1 , mantissaOne (T ));
9898}
9999
100- test "std.math. float" {
100+ test "float bits " {
101101 inline for ([_ ]type { f16 , f32 , f64 , f80 , f128 , c_longdouble }) | T | {
102102 // (1 +) for the sign bit, since it is separate from the other bits
103103 const size = 1 + floatExponentBits (T ) + floatMantissaBits (T );
0 commit comments