authorgravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2022-01-19 18:47:51+02:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2022-01-28 11:45:04-07:00
log67d04a988a06f52f4abca848f3579a8037070afe
treef59135df73028b28f8a4618ab3f6bba26fab04f6
parenta31a749c42505e53308c0f2426db283ea130e776

std: add f80 bits


5 files changed, 29 insertions(+), 0 deletions(-)

doc/langref.html.in+6
...@@ -737,6 +737,11 @@ pub fn main() void {...@@ -737,6 +737,11 @@ pub fn main() void {
737 <td><code class="c">double</code></td>737 <td><code class="c">double</code></td>
738 <td>64-bit floating point (52-bit mantissa) IEEE-754-2008 binary64</td>738 <td>64-bit floating point (52-bit mantissa) IEEE-754-2008 binary64</td>
739 </tr>739 </tr>
740 <tr>
741 <th scope="row">{#syntax#}f80{#endsyntax#}</th>
742 <td><code class="c">double</code></td>
743 <td>64-bit floating point (64-bit mantissa) IEEE-754-2008 80-bit extended precision</td>
744 </tr>
740 <tr>745 <tr>
741 <th scope="row">{#syntax#}f128{#endsyntax#}</th>746 <th scope="row">{#syntax#}f128{#endsyntax#}</th>
742 <td><code class="c">_Float128</code></td>747 <td><code class="c">_Float128</code></td>
...@@ -1500,6 +1505,7 @@ fn divide(a: i32, b: i32) i32 {...@@ -1500,6 +1505,7 @@ fn divide(a: i32, b: i32) i32 {
1500 <li>{#syntax#}f16{#endsyntax#} - IEEE-754-2008 binary16</li>1505 <li>{#syntax#}f16{#endsyntax#} - IEEE-754-2008 binary16</li>
1501 <li>{#syntax#}f32{#endsyntax#} - IEEE-754-2008 binary32</li>1506 <li>{#syntax#}f32{#endsyntax#} - IEEE-754-2008 binary32</li>
1502 <li>{#syntax#}f64{#endsyntax#} - IEEE-754-2008 binary64</li>1507 <li>{#syntax#}f64{#endsyntax#} - IEEE-754-2008 binary64</li>
1508 <li>{#syntax#}f80{#endsyntax#} - IEEE-754-2008 80-bit extended precision</li>
1503 <li>{#syntax#}f128{#endsyntax#} - IEEE-754-2008 binary128</li>1509 <li>{#syntax#}f128{#endsyntax#} - IEEE-754-2008 binary128</li>
1504 <li>{#syntax#}c_longdouble{#endsyntax#} - matches <code class="c">long double</code> for the target C ABI</li>1510 <li>{#syntax#}c_longdouble{#endsyntax#} - matches <code class="c">long double</code> for the target C ABI</li>
1505 </ul>1511 </ul>
lib/std/math.zig+18
...@@ -43,7 +43,21 @@ pub const f128_max = @bitCast(f128, @as(u128, 0x7FFEFFFFFFFFFFFFFFFFFFFFFFFFFFFF...@@ -43,7 +43,21 @@ pub const f128_max = @bitCast(f128, @as(u128, 0x7FFEFFFFFFFFFFFFFFFFFFFFFFFFFFFF
43pub const f128_epsilon = @bitCast(f128, @as(u128, 0x3F8F0000000000000000000000000000));43pub const f128_epsilon = @bitCast(f128, @as(u128, 0x3F8F0000000000000000000000000000));
44pub const f128_toint = 1.0 / f128_epsilon;44pub const f128_toint = 1.0 / f128_epsilon;
4545
46const F80Repr = if (@import("builtin").cpu.arch.endian() == .Little) extern struct {
47 fraction: u64,
48 exp: u16,
49} else extern struct {
50 exp: u16,
51 fraction: u64,
52};
53
46// float.h details54// float.h details
55pub const f80_true_min = @ptrCast(*const f80, &F80Repr{ .fraction = 1, .exp = 0 }).*;
56pub const f80_min = @ptrCast(*const f80, &F80Repr{ .fraction = 0x8000000000000000, .exp = 1 }).*;
57pub const f80_max = @ptrCast(*const f80, &F80Repr{ .fraction = 0xFFFFFFFFFFFFFFFF, .exp = 0x7FFE }).*;
58pub const f80_epsilon = @ptrCast(*const f80, &F80Repr{ .fraction = 0x8000000000000000, .exp = 0x3FC0 }).*;
59pub const f80_toint = 1.0 / f80_epsilon;
60
47pub const f64_true_min = 4.94065645841246544177e-324;61pub const f64_true_min = 4.94065645841246544177e-324;
48pub const f64_min = 2.2250738585072014e-308;62pub const f64_min = 2.2250738585072014e-308;
49pub const f64_max = 1.79769313486231570815e+308;63pub const f64_max = 1.79769313486231570815e+308;
...@@ -91,6 +105,10 @@ pub const qnan_f64 = @bitCast(f64, qnan_u64);...@@ -91,6 +105,10 @@ pub const qnan_f64 = @bitCast(f64, qnan_u64);
91pub const inf_u64 = @as(u64, 0x7FF << 52);105pub const inf_u64 = @as(u64, 0x7FF << 52);
92pub const inf_f64 = @bitCast(f64, inf_u64);106pub const inf_f64 = @bitCast(f64, inf_u64);
93107
108pub const inf_f80 = @ptrCast(*const f80, &F80Repr{ .fraction = 0x8000000000000000, .exp = 0x7fff }).*;
109pub const nan_f80 = @ptrCast(*const f80, &F80Repr{ .fraction = 0xA000000000000000, .exp = 0x7fff }).*;
110pub const qnan_f80 = @ptrCast(*const f80, &F80Repr{ .fraction = 0xC000000000000000, .exp = 0x7fff }).*;
111
94pub const nan_u128 = @as(u128, 0x7fff0000000000000000000000000001);112pub const nan_u128 = @as(u128, 0x7fff0000000000000000000000000001);
95pub const nan_f128 = @bitCast(f128, nan_u128);113pub const nan_f128 = @bitCast(f128, nan_u128);
96114
lib/std/math/epsilon.zig+1
...@@ -8,6 +8,7 @@ pub fn epsilon(comptime T: type) T {...@@ -8,6 +8,7 @@ pub fn epsilon(comptime T: type) T {
8 f16 => math.f16_epsilon,8 f16 => math.f16_epsilon,
9 f32 => math.f32_epsilon,9 f32 => math.f32_epsilon,
10 f64 => math.f64_epsilon,10 f64 => math.f64_epsilon,
11 f80 => math.f80_epsilon,
11 f128 => math.f128_epsilon,12 f128 => math.f128_epsilon,
12 else => @compileError("epsilon not implemented for " ++ @typeName(T)),13 else => @compileError("epsilon not implemented for " ++ @typeName(T)),
13 };14 };
lib/std/math/inf.zig+1
...@@ -7,6 +7,7 @@ pub fn inf(comptime T: type) T {...@@ -7,6 +7,7 @@ pub fn inf(comptime T: type) T {
7 f16 => math.inf_f16,7 f16 => math.inf_f16,
8 f32 => math.inf_f32,8 f32 => math.inf_f32,
9 f64 => math.inf_f64,9 f64 => math.inf_f64,
10 f80 => math.inf_f80,
10 f128 => math.inf_f128,11 f128 => math.inf_f128,
11 else => @compileError("inf not implemented for " ++ @typeName(T)),12 else => @compileError("inf not implemented for " ++ @typeName(T)),
12 };13 };
lib/std/math/nan.zig+3
...@@ -6,6 +6,7 @@ pub fn nan(comptime T: type) T {...@@ -6,6 +6,7 @@ pub fn nan(comptime T: type) T {
6 f16 => math.nan_f16,6 f16 => math.nan_f16,
7 f32 => math.nan_f32,7 f32 => math.nan_f32,
8 f64 => math.nan_f64,8 f64 => math.nan_f64,
9 f80 => math.nan_f80,
9 f128 => math.nan_f128,10 f128 => math.nan_f128,
10 else => @compileError("nan not implemented for " ++ @typeName(T)),11 else => @compileError("nan not implemented for " ++ @typeName(T)),
11 };12 };
...@@ -19,6 +20,8 @@ pub fn snan(comptime T: type) T {...@@ -19,6 +20,8 @@ pub fn snan(comptime T: type) T {
19 f16 => @bitCast(f16, math.nan_u16),20 f16 => @bitCast(f16, math.nan_u16),
20 f32 => @bitCast(f32, math.nan_u32),21 f32 => @bitCast(f32, math.nan_u32),
21 f64 => @bitCast(f64, math.nan_u64),22 f64 => @bitCast(f64, math.nan_u64),
23 f80 => @bitCast(f80, math.nan_u80),
24 f128 => @bitCast(f128, math.nan_u128),
22 else => @compileError("snan not implemented for " ++ @typeName(T)),25 else => @compileError("snan not implemented for " ++ @typeName(T)),
23 };26 };
24}27}