authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2018-08-23 08:26:09-04:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2018-08-23 08:26:09-04:00
log327482c3a41a30d5ea63eedd3bfb13553c8c2d00
treeef668f8342632858ee20e6d9a05d9c93d8e5b704
parent68dcdf1c867a918aa0bb7b5c4cb42df185e1c8f9
parent353419f82d3575dc45631750a8cf08aa4826ec4c
signaturebadge-question-mark Signed by PGP key 4AEE18F83AFDEB23

Merge pull request #1402 from ziglang/default-fp-ieee-strict

Default to strict IEEE floating point

18 files changed, 17 insertions(+), 61 deletions(-)

doc/langref.html.in+5-5
......@@ -744,19 +744,19 @@ const yet_another_hex_float = 0x103.70P-5;
744744 {#code_end#}
745745 {#header_close#}
746746 {#header_open|Floating Point Operations#}
747 <p>By default floating point operations use <code>Optimized</code> mode,
748 but you can switch to <code>Strict</code> mode on a per-block basis:</p>
747 <p>By default floating point operations use <code>Strict</code> mode,
748 but you can switch to <code>Optimized</code> mode on a per-block basis:</p>
749749 {#code_begin|obj|foo#}
750750 {#code_release_fast#}
751751const builtin = @import("builtin");
752752const big = f64(1 << 40);
753753
754754export fn foo_strict(x: f64) f64 {
755 @setFloatMode(this, builtin.FloatMode.Strict);
756755 return x + big - big;
757756}
758757
759758export fn foo_optimized(x: f64) f64 {
759 @setFloatMode(this, builtin.FloatMode.Optimized);
760760 return x + big - big;
761761}
762762 {#code_end#}
......@@ -5948,7 +5948,7 @@ pub const FloatMode = enum {
59485948 {#code_end#}
59495949 <ul>
59505950 <li>
5951 <code>Optimized</code> (default) - Floating point operations may do all of the following:
5951 <code>Optimized</code> - Floating point operations may do all of the following:
59525952 <ul>
59535953 <li>Assume the arguments and result are not NaN. Optimizations are required to retain defined behavior over NaNs, but the value of the result is undefined.</li>
59545954 <li>Assume the arguments and result are not +/-Inf. Optimizations are required to retain defined behavior over +/-Inf, but the value of the result is undefined.</li>
......@@ -5960,7 +5960,7 @@ pub const FloatMode = enum {
59605960 This is equivalent to <code>-ffast-math</code> in GCC.
59615961 </li>
59625962 <li>
5963 <code>Strict</code> - Floating point operations follow strict IEEE compliance.
5963 <code>Strict</code> (default) - Floating point operations follow strict IEEE compliance.
59645964 </li>
59655965 </ul>
59665966 {#see_also|Floating Point Operations#}
src/all_types.hpp+2-2
......@@ -1852,7 +1852,7 @@ struct ScopeDecls {
18521852 HashMap<Buf *, Tld *, buf_hash, buf_eql_buf> decl_table;
18531853 bool safety_off;
18541854 AstNode *safety_set_node;
1855 bool fast_math_off;
1855 bool fast_math_on;
18561856 AstNode *fast_math_set_node;
18571857 ImportTableEntry *import;
18581858 // If this is a scope from a container, this is the type entry, otherwise null
......@@ -1872,7 +1872,7 @@ struct ScopeBlock {
18721872
18731873 bool safety_off;
18741874 AstNode *safety_set_node;
1875 bool fast_math_off;
1875 bool fast_math_on;
18761876 AstNode *fast_math_set_node;
18771877};
18781878
src/codegen.cpp+3-3
......@@ -829,15 +829,15 @@ static bool ir_want_fast_math(CodeGen *g, IrInstruction *instruction) {
829829 if (scope->id == ScopeIdBlock) {
830830 ScopeBlock *block_scope = (ScopeBlock *)scope;
831831 if (block_scope->fast_math_set_node)
832 return !block_scope->fast_math_off;
832 return block_scope->fast_math_on;
833833 } else if (scope->id == ScopeIdDecls) {
834834 ScopeDecls *decls_scope = (ScopeDecls *)scope;
835835 if (decls_scope->fast_math_set_node)
836 return !decls_scope->fast_math_off;
836 return decls_scope->fast_math_on;
837837 }
838838 scope = scope->parent;
839839 }
840 return true;
840 return false;
841841}
842842
843843static bool ir_want_runtime_safety(CodeGen *g, IrInstruction *instruction) {
src/ir.cpp+5-5
......@@ -15200,17 +15200,17 @@ static TypeTableEntry *ir_analyze_instruction_set_float_mode(IrAnalyze *ira,
1520015200 return ira->codegen->builtin_types.entry_void;
1520115201 }
1520215202
15203 bool *fast_math_off_ptr;
15203 bool *fast_math_on_ptr;
1520415204 AstNode **fast_math_set_node_ptr;
1520515205 if (target_type->id == TypeTableEntryIdBlock) {
1520615206 ScopeBlock *block_scope = (ScopeBlock *)target_val->data.x_block;
15207 fast_math_off_ptr = &block_scope->fast_math_off;
15207 fast_math_on_ptr = &block_scope->fast_math_on;
1520815208 fast_math_set_node_ptr = &block_scope->fast_math_set_node;
1520915209 } else if (target_type->id == TypeTableEntryIdFn) {
1521015210 assert(target_val->data.x_ptr.special == ConstPtrSpecialFunction);
1521115211 FnTableEntry *target_fn = target_val->data.x_ptr.data.fn.fn_entry;
1521215212 assert(target_fn->def_scope);
15213 fast_math_off_ptr = &target_fn->def_scope->fast_math_off;
15213 fast_math_on_ptr = &target_fn->def_scope->fast_math_on;
1521415214 fast_math_set_node_ptr = &target_fn->def_scope->fast_math_set_node;
1521515215 } else if (target_type->id == TypeTableEntryIdMetaType) {
1521615216 ScopeDecls *decls_scope;
......@@ -15226,7 +15226,7 @@ static TypeTableEntry *ir_analyze_instruction_set_float_mode(IrAnalyze *ira,
1522615226 buf_sprintf("expected scope reference, found type '%s'", buf_ptr(&type_arg->name)));
1522715227 return ira->codegen->builtin_types.entry_invalid;
1522815228 }
15229 fast_math_off_ptr = &decls_scope->fast_math_off;
15229 fast_math_on_ptr = &decls_scope->fast_math_on;
1523015230 fast_math_set_node_ptr = &decls_scope->fast_math_set_node;
1523115231 } else {
1523215232 ir_add_error_node(ira, target_instruction->source_node,
......@@ -15248,7 +15248,7 @@ static TypeTableEntry *ir_analyze_instruction_set_float_mode(IrAnalyze *ira,
1524815248 return ira->codegen->builtin_types.entry_invalid;
1524915249 }
1525015250 *fast_math_set_node_ptr = source_node;
15251 *fast_math_off_ptr = (float_mode_scalar == FloatModeStrict);
15251 *fast_math_on_ptr = (float_mode_scalar == FloatModeOptimized);
1525215252
1525315253 ir_build_const_from(ira, &instruction->base);
1525415254 return ira->codegen->builtin_types.entry_void;
std/fmt/errol/index.zig-4
......@@ -253,11 +253,7 @@ fn gethi(in: f64) f64 {
253253/// Normalize the number by factoring in the error.
254254/// @hp: The float pair.
255255fn hpNormalize(hp: *HP) void {
256 // Required to avoid segfaults causing buffer overrun during errol3 digit output termination.
257 @setFloatMode(this, @import("builtin").FloatMode.Strict);
258
259256 const val = hp.val;
260
261257 hp.val += hp.off;
262258 hp.off += val - hp.val;
263259}
std/math/ceil.zig-2
......@@ -61,10 +61,8 @@ fn ceil64(x: f64) f64 {
6161 }
6262
6363 if (u >> 63 != 0) {
64 @setFloatMode(this, builtin.FloatMode.Strict);
6564 y = x - math.f64_toint + math.f64_toint - x;
6665 } else {
67 @setFloatMode(this, builtin.FloatMode.Strict);
6866 y = x + math.f64_toint - math.f64_toint - x;
6967 }
7068
std/math/complex/exp.zig-2
......@@ -17,8 +17,6 @@ pub fn exp(z: var) @typeOf(z) {
1717}
1818
1919fn exp32(z: Complex(f32)) Complex(f32) {
20 @setFloatMode(this, @import("builtin").FloatMode.Strict);
21
2220 const exp_overflow = 0x42b17218; // max_exp * ln2 ~= 88.72283955
2321 const cexp_overflow = 0x43400074; // (max_exp - min_denom_exp) * ln2
2422
std/math/cos.zig-2
......@@ -37,8 +37,6 @@ const C5 = 4.16666666666665929218E-2;
3737//
3838// This may have slight differences on some edge cases and may need to replaced if so.
3939fn cos32(x_: f32) f32 {
40 @setFloatMode(this, @import("builtin").FloatMode.Strict);
41
4240 const pi4a = 7.85398125648498535156e-1;
4341 const pi4b = 3.77489470793079817668E-8;
4442 const pi4c = 2.69515142907905952645E-15;
std/math/exp.zig-4
......@@ -18,8 +18,6 @@ pub fn exp(x: var) @typeOf(x) {
1818}
1919
2020fn exp32(x_: f32) f32 {
21 @setFloatMode(this, builtin.FloatMode.Strict);
22
2321 const half = []f32{ 0.5, -0.5 };
2422 const ln2hi = 6.9314575195e-1;
2523 const ln2lo = 1.4286067653e-6;
......@@ -95,8 +93,6 @@ fn exp32(x_: f32) f32 {
9593}
9694
9795fn exp64(x_: f64) f64 {
98 @setFloatMode(this, builtin.FloatMode.Strict);
99
10096 const half = []const f64{ 0.5, -0.5 };
10197 const ln2hi: f64 = 6.93147180369123816490e-01;
10298 const ln2lo: f64 = 1.90821492927058770002e-10;
std/math/exp2.zig-4
......@@ -36,8 +36,6 @@ const exp2ft = []const f64{
3636};
3737
3838fn exp2_32(x: f32) f32 {
39 @setFloatMode(this, @import("builtin").FloatMode.Strict);
40
4139 const tblsiz = @intCast(u32, exp2ft.len);
4240 const redux: f32 = 0x1.8p23 / @intToFloat(f32, tblsiz);
4341 const P1: f32 = 0x1.62e430p-1;
......@@ -353,8 +351,6 @@ const exp2dt = []f64{
353351};
354352
355353fn exp2_64(x: f64) f64 {
356 @setFloatMode(this, @import("builtin").FloatMode.Strict);
357
358354 const tblsiz = @intCast(u32, exp2dt.len / 2);
359355 const redux: f64 = 0x1.8p52 / @intToFloat(f64, tblsiz);
360356 const P1: f64 = 0x1.62e42fefa39efp-1;
std/math/expm1.zig-4
......@@ -19,8 +19,6 @@ pub fn expm1(x: var) @typeOf(x) {
1919}
2020
2121fn expm1_32(x_: f32) f32 {
22 @setFloatMode(this, builtin.FloatMode.Strict);
23
2422 if (math.isNan(x_))
2523 return math.nan(f32);
2624
......@@ -149,8 +147,6 @@ fn expm1_32(x_: f32) f32 {
149147}
150148
151149fn expm1_64(x_: f64) f64 {
152 @setFloatMode(this, builtin.FloatMode.Strict);
153
154150 if (math.isNan(x_))
155151 return math.nan(f64);
156152
std/math/floor.zig-2
......@@ -97,10 +97,8 @@ fn floor64(x: f64) f64 {
9797 }
9898
9999 if (u >> 63 != 0) {
100 @setFloatMode(this, builtin.FloatMode.Strict);
101100 y = x - math.f64_toint + math.f64_toint - x;
102101 } else {
103 @setFloatMode(this, builtin.FloatMode.Strict);
104102 y = x + math.f64_toint - math.f64_toint - x;
105103 }
106104
std/math/ln.zig-4
......@@ -35,8 +35,6 @@ pub fn ln(x: var) @typeOf(x) {
3535}
3636
3737pub fn ln_32(x_: f32) f32 {
38 @setFloatMode(this, @import("builtin").FloatMode.Strict);
39
4038 const ln2_hi: f32 = 6.9313812256e-01;
4139 const ln2_lo: f32 = 9.0580006145e-06;
4240 const Lg1: f32 = 0xaaaaaa.0p-24;
......@@ -89,8 +87,6 @@ pub fn ln_32(x_: f32) f32 {
8987}
9088
9189pub fn ln_64(x_: f64) f64 {
92 @setFloatMode(this, @import("builtin").FloatMode.Strict);
93
9490 const ln2_hi: f64 = 6.93147180369123816490e-01;
9591 const ln2_lo: f64 = 1.90821492927058770002e-10;
9692 const Lg1: f64 = 6.666666666666735130e-01;
std/math/pow.zig-2
......@@ -28,8 +28,6 @@ const assert = std.debug.assert;
2828
2929// This implementation is taken from the go stlib, musl is a bit more complex.
3030pub fn pow(comptime T: type, x: T, y: T) T {
31 @setFloatMode(this, @import("builtin").FloatMode.Strict);
32
3331 if (T != f32 and T != f64) {
3432 @compileError("pow not implemented for " ++ @typeName(T));
3533 }
std/math/round.zig+2-10
......@@ -35,11 +35,7 @@ fn round32(x_: f32) f32 {
3535 return 0 * @bitCast(f32, u);
3636 }
3737
38 {
39 @setFloatMode(this, builtin.FloatMode.Strict);
40 y = x + math.f32_toint - math.f32_toint - x;
41 }
42
38 y = x + math.f32_toint - math.f32_toint - x;
4339 if (y > 0.5) {
4440 y = y + x - 1;
4541 } else if (y <= -0.5) {
......@@ -72,11 +68,7 @@ fn round64(x_: f64) f64 {
7268 return 0 * @bitCast(f64, u);
7369 }
7470
75 {
76 @setFloatMode(this, builtin.FloatMode.Strict);
77 y = x + math.f64_toint - math.f64_toint - x;
78 }
79
71 y = x + math.f64_toint - math.f64_toint - x;
8072 if (y > 0.5) {
8173 y = y + x - 1;
8274 } else if (y <= -0.5) {
std/math/sin.zig-2
......@@ -38,8 +38,6 @@ const C5 = 4.16666666666665929218E-2;
3838//
3939// This may have slight differences on some edge cases and may need to replaced if so.
4040fn sin32(x_: f32) f32 {
41 @setFloatMode(this, @import("builtin").FloatMode.Strict);
42
4341 const pi4a = 7.85398125648498535156e-1;
4442 const pi4b = 3.77489470793079817668E-8;
4543 const pi4c = 2.69515142907905952645E-15;
std/math/sinh.zig-2
......@@ -54,8 +54,6 @@ fn sinh32(x: f32) f32 {
5454}
5555
5656fn sinh64(x: f64) f64 {
57 @setFloatMode(this, @import("builtin").FloatMode.Strict);
58
5957 const u = @bitCast(u64, x);
6058 const w = @intCast(u32, u >> 32);
6159 const ax = @bitCast(f64, u & (@maxValue(u64) >> 1));
std/math/tan.zig-2
......@@ -31,8 +31,6 @@ const Tq4 = -5.38695755929454629881E7;
3131//
3232// This may have slight differences on some edge cases and may need to replaced if so.
3333fn tan32(x_: f32) f32 {
34 @setFloatMode(this, @import("builtin").FloatMode.Strict);
35
3634 const pi4a = 7.85398125648498535156e-1;
3735 const pi4b = 3.77489470793079817668E-8;
3836 const pi4c = 2.69515142907905952645E-15;