authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2018-09-13 15:46:34-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2018-09-13 15:46:34-04:00
log7c3636aaa38e8efa77b73ba94362802517ea739e
tree68aa09c19e1ccd907ffb286cba02c7aab9399738
parent9ac9633b105b737f2700f1930768a6ea6d4d75c5
signaturelock-open Commit is signed but in an unrecognized format.

remove the scope parameter of setFloatMode

also document that scopes inherit this value. See #367 See #1283

6 files changed, 47 insertions(+), 63 deletions(-)

doc/langref.html.in+11-7
...@@ -734,7 +734,7 @@ export fn foo_strict(x: f64) f64 {...@@ -734,7 +734,7 @@ export fn foo_strict(x: f64) f64 {
734}734}
735735
736export fn foo_optimized(x: f64) f64 {736export fn foo_optimized(x: f64) f64 {
737 @setFloatMode(this, builtin.FloatMode.Optimized);737 @setFloatMode(builtin.FloatMode.Optimized);
738 return x + big - big;738 return x + big - big;
739}739}
740 {#code_end#}740 {#code_end#}
...@@ -6030,17 +6030,20 @@ test "foo" {...@@ -6030,17 +6030,20 @@ test "foo" {
6030 {#see_also|comptime#}6030 {#see_also|comptime#}
6031 {#header_close#}6031 {#header_close#}
6032 {#header_open|@setFloatMode#}6032 {#header_open|@setFloatMode#}
6033 <pre><code class="zig">@setFloatMode(scope, mode: @import("builtin").FloatMode)</code></pre>6033 <pre><code class="zig">@setFloatMode(mode: @import("builtin").FloatMode)</code></pre>
6034 <p>6034 <p>
6035 Sets the floating point mode for a given scope. Possible values are:6035 Sets the floating point mode of the current scope. Possible values are:
6036 </p>6036 </p>
6037 {#code_begin|syntax#}6037 {#code_begin|syntax#}
6038pub const FloatMode = enum {6038pub const FloatMode = enum {
6039 Optimized,
6040 Strict,6039 Strict,
6040 Optimized,
6041};6041};
6042 {#code_end#}6042 {#code_end#}
6043 <ul>6043 <ul>
6044 <li>
6045 <code>Strict</code> (default) - Floating point operations follow strict IEEE compliance.
6046 </li>
6044 <li>6047 <li>
6045 <code>Optimized</code> - Floating point operations may do all of the following:6048 <code>Optimized</code> - Floating point operations may do all of the following:
6046 <ul>6049 <ul>
...@@ -6053,10 +6056,11 @@ pub const FloatMode = enum {...@@ -6053,10 +6056,11 @@ pub const FloatMode = enum {
6053 </ul>6056 </ul>
6054 This is equivalent to <code>-ffast-math</code> in GCC.6057 This is equivalent to <code>-ffast-math</code> in GCC.
6055 </li>6058 </li>
6056 <li>
6057 <code>Strict</code> (default) - Floating point operations follow strict IEEE compliance.
6058 </li>
6059 </ul>6059 </ul>
6060 <p>
6061 The floating point mode is inherited by child scopes, and can be overridden in any scope.
6062 You can set the floating point mode in a struct or module scope by using a comptime block.
6063 </p>
6060 {#see_also|Floating Point Operations#}6064 {#see_also|Floating Point Operations#}
6061 {#header_close#}6065 {#header_close#}
6062 {#header_open|@setGlobalLinkage#}6066 {#header_open|@setGlobalLinkage#}
src/all_types.hpp+1-1
...@@ -3287,8 +3287,8 @@ static const size_t stack_trace_ptr_count = 30;...@@ -3287,8 +3287,8 @@ static const size_t stack_trace_ptr_count = 30;
32873287
32883288
3289enum FloatMode {3289enum FloatMode {
3290 FloatModeOptimized,
3291 FloatModeStrict,3290 FloatModeStrict,
3291 FloatModeOptimized,
3292};3292};
32933293
3294enum FnWalkId {3294enum FnWalkId {
src/codegen.cpp+6-6
...@@ -6729,7 +6729,7 @@ static void define_builtin_fns(CodeGen *g) {...@@ -6729,7 +6729,7 @@ static void define_builtin_fns(CodeGen *g) {
6729 create_builtin_fn(g, BuiltinFnIdIntType, "IntType", 2); // TODO rename to Int6729 create_builtin_fn(g, BuiltinFnIdIntType, "IntType", 2); // TODO rename to Int
6730 create_builtin_fn(g, BuiltinFnIdSetCold, "setCold", 1);6730 create_builtin_fn(g, BuiltinFnIdSetCold, "setCold", 1);
6731 create_builtin_fn(g, BuiltinFnIdSetRuntimeSafety, "setRuntimeSafety", 1);6731 create_builtin_fn(g, BuiltinFnIdSetRuntimeSafety, "setRuntimeSafety", 1);
6732 create_builtin_fn(g, BuiltinFnIdSetFloatMode, "setFloatMode", 2);6732 create_builtin_fn(g, BuiltinFnIdSetFloatMode, "setFloatMode", 1);
6733 create_builtin_fn(g, BuiltinFnIdPanic, "panic", 1);6733 create_builtin_fn(g, BuiltinFnIdPanic, "panic", 1);
6734 create_builtin_fn(g, BuiltinFnIdPtrCast, "ptrCast", 2);6734 create_builtin_fn(g, BuiltinFnIdPtrCast, "ptrCast", 2);
6735 create_builtin_fn(g, BuiltinFnIdBitCast, "bitCast", 2);6735 create_builtin_fn(g, BuiltinFnIdBitCast, "bitCast", 2);
...@@ -7115,11 +7115,11 @@ Buf *codegen_generate_builtin_source(CodeGen *g) {...@@ -7115,11 +7115,11 @@ Buf *codegen_generate_builtin_source(CodeGen *g) {
7115 {7115 {
7116 buf_appendf(contents,7116 buf_appendf(contents,
7117 "pub const FloatMode = enum {\n"7117 "pub const FloatMode = enum {\n"
7118 " Optimized,\n"
7119 " Strict,\n"7118 " Strict,\n"
7119 " Optimized,\n"
7120 "};\n\n");7120 "};\n\n");
7121 assert(FloatModeOptimized == 0);7121 assert(FloatModeStrict == 0);
7122 assert(FloatModeStrict == 1);7122 assert(FloatModeOptimized == 1);
7123 }7123 }
7124 {7124 {
7125 buf_appendf(contents,7125 buf_appendf(contents,
...@@ -7127,8 +7127,8 @@ Buf *codegen_generate_builtin_source(CodeGen *g) {...@@ -7127,8 +7127,8 @@ Buf *codegen_generate_builtin_source(CodeGen *g) {
7127 " Big,\n"7127 " Big,\n"
7128 " Little,\n"7128 " Little,\n"
7129 "};\n\n");7129 "};\n\n");
7130 assert(FloatModeOptimized == 0);7130 //assert(EndianBig == 0);
7131 assert(FloatModeStrict == 1);7131 //assert(EndianLittle == 1);
7132 }7132 }
7133 {7133 {
7134 const char *endian_str = g->is_big_endian ? "Endian.Big" : "Endian.Little";7134 const char *endian_str = g->is_big_endian ? "Endian.Big" : "Endian.Little";
src/ir.cpp+26-46
...@@ -1577,13 +1577,11 @@ static IrInstruction *ir_build_set_runtime_safety(IrBuilder *irb, Scope *scope,...@@ -1577,13 +1577,11 @@ static IrInstruction *ir_build_set_runtime_safety(IrBuilder *irb, Scope *scope,
1577}1577}
15781578
1579static IrInstruction *ir_build_set_float_mode(IrBuilder *irb, Scope *scope, AstNode *source_node,1579static IrInstruction *ir_build_set_float_mode(IrBuilder *irb, Scope *scope, AstNode *source_node,
1580 IrInstruction *scope_value, IrInstruction *mode_value)1580 IrInstruction *mode_value)
1581{1581{
1582 IrInstructionSetFloatMode *instruction = ir_build_instruction<IrInstructionSetFloatMode>(irb, scope, source_node);1582 IrInstructionSetFloatMode *instruction = ir_build_instruction<IrInstructionSetFloatMode>(irb, scope, source_node);
1583 instruction->scope_value = scope_value;
1584 instruction->mode_value = mode_value;1583 instruction->mode_value = mode_value;
15851584
1586 ir_ref_instruction(scope_value, irb->current_basic_block);
1587 ir_ref_instruction(mode_value, irb->current_basic_block);1585 ir_ref_instruction(mode_value, irb->current_basic_block);
15881586
1589 return &instruction->base;1587 return &instruction->base;
...@@ -3959,12 +3957,7 @@ static IrInstruction *ir_gen_builtin_fn_call(IrBuilder *irb, Scope *scope, AstNo...@@ -3959,12 +3957,7 @@ static IrInstruction *ir_gen_builtin_fn_call(IrBuilder *irb, Scope *scope, AstNo
3959 if (arg0_value == irb->codegen->invalid_instruction)3957 if (arg0_value == irb->codegen->invalid_instruction)
3960 return arg0_value;3958 return arg0_value;
39613959
3962 AstNode *arg1_node = node->data.fn_call_expr.params.at(1);3960 IrInstruction *set_float_mode = ir_build_set_float_mode(irb, scope, node, arg0_value);
3963 IrInstruction *arg1_value = ir_gen_node(irb, arg1_node, scope);
3964 if (arg1_value == irb->codegen->invalid_instruction)
3965 return arg1_value;
3966
3967 IrInstruction *set_float_mode = ir_build_set_float_mode(irb, scope, node, arg0_value, arg1_value);
3968 return ir_lval_wrap(irb, scope, set_float_mode, lval);3961 return ir_lval_wrap(irb, scope, set_float_mode, lval);
3969 }3962 }
3970 case BuiltinFnIdSizeof:3963 case BuiltinFnIdSizeof:
...@@ -15379,6 +15372,7 @@ static ZigType *ir_analyze_instruction_set_cold(IrAnalyze *ira, IrInstructionSet...@@ -15379,6 +15372,7 @@ static ZigType *ir_analyze_instruction_set_cold(IrAnalyze *ira, IrInstructionSet
15379 ir_build_const_from(ira, &instruction->base);15372 ir_build_const_from(ira, &instruction->base);
15380 return ira->codegen->builtin_types.entry_void;15373 return ira->codegen->builtin_types.entry_void;
15381}15374}
15375
15382static ZigType *ir_analyze_instruction_set_runtime_safety(IrAnalyze *ira,15376static ZigType *ir_analyze_instruction_set_runtime_safety(IrAnalyze *ira,
15383 IrInstructionSetRuntimeSafety *set_runtime_safety_instruction)15377 IrInstructionSetRuntimeSafety *set_runtime_safety_instruction)
15384{15378{
...@@ -15439,14 +15433,6 @@ static ZigType *ir_analyze_instruction_set_runtime_safety(IrAnalyze *ira,...@@ -15439,14 +15433,6 @@ static ZigType *ir_analyze_instruction_set_runtime_safety(IrAnalyze *ira,
15439static ZigType *ir_analyze_instruction_set_float_mode(IrAnalyze *ira,15433static ZigType *ir_analyze_instruction_set_float_mode(IrAnalyze *ira,
15440 IrInstructionSetFloatMode *instruction)15434 IrInstructionSetFloatMode *instruction)
15441{15435{
15442 IrInstruction *target_instruction = instruction->scope_value->other;
15443 ZigType *target_type = target_instruction->value.type;
15444 if (type_is_invalid(target_type))
15445 return ira->codegen->builtin_types.entry_invalid;
15446 ConstExprValue *target_val = ir_resolve_const(ira, target_instruction, UndefBad);
15447 if (!target_val)
15448 return ira->codegen->builtin_types.entry_invalid;
15449
15450 if (ira->new_irb.exec->is_inline) {15436 if (ira->new_irb.exec->is_inline) {
15451 // ignore setFloatMode when running functions at compile time15437 // ignore setFloatMode when running functions at compile time
15452 ir_build_const_from(ira, &instruction->base);15438 ir_build_const_from(ira, &instruction->base);
...@@ -15455,40 +15441,34 @@ static ZigType *ir_analyze_instruction_set_float_mode(IrAnalyze *ira,...@@ -15455,40 +15441,34 @@ static ZigType *ir_analyze_instruction_set_float_mode(IrAnalyze *ira,
1545515441
15456 bool *fast_math_on_ptr;15442 bool *fast_math_on_ptr;
15457 AstNode **fast_math_set_node_ptr;15443 AstNode **fast_math_set_node_ptr;
15458 if (target_type->id == ZigTypeIdBlock) {15444
15459 ScopeBlock *block_scope = (ScopeBlock *)target_val->data.x_block;15445 Scope *scope = instruction->base.scope;
15460 fast_math_on_ptr = &block_scope->fast_math_on;15446 while (scope != nullptr) {
15461 fast_math_set_node_ptr = &block_scope->fast_math_set_node;15447 if (scope->id == ScopeIdBlock) {
15462 } else if (target_type->id == ZigTypeIdFn) {15448 ScopeBlock *block_scope = (ScopeBlock *)scope;
15463 assert(target_val->data.x_ptr.special == ConstPtrSpecialFunction);15449 fast_math_on_ptr = &block_scope->fast_math_on;
15464 ZigFn *target_fn = target_val->data.x_ptr.data.fn.fn_entry;15450 fast_math_set_node_ptr = &block_scope->fast_math_set_node;
15465 assert(target_fn->def_scope);15451 break;
15466 fast_math_on_ptr = &target_fn->def_scope->fast_math_on;15452 } else if (scope->id == ScopeIdFnDef) {
15467 fast_math_set_node_ptr = &target_fn->def_scope->fast_math_set_node;15453 ScopeFnDef *def_scope = (ScopeFnDef *)scope;
15468 } else if (target_type->id == ZigTypeIdMetaType) {15454 ZigFn *target_fn = def_scope->fn_entry;
15469 ScopeDecls *decls_scope;15455 assert(target_fn->def_scope != nullptr);
15470 ZigType *type_arg = target_val->data.x_type;15456 fast_math_on_ptr = &target_fn->def_scope->fast_math_on;
15471 if (type_arg->id == ZigTypeIdStruct) {15457 fast_math_set_node_ptr = &target_fn->def_scope->fast_math_set_node;
15472 decls_scope = type_arg->data.structure.decls_scope;15458 break;
15473 } else if (type_arg->id == ZigTypeIdEnum) {15459 } else if (scope->id == ScopeIdDecls) {
15474 decls_scope = type_arg->data.enumeration.decls_scope;15460 ScopeDecls *decls_scope = (ScopeDecls *)scope;
15475 } else if (type_arg->id == ZigTypeIdUnion) {15461 fast_math_on_ptr = &decls_scope->fast_math_on;
15476 decls_scope = type_arg->data.unionation.decls_scope;15462 fast_math_set_node_ptr = &decls_scope->fast_math_set_node;
15463 break;
15477 } else {15464 } else {
15478 ir_add_error_node(ira, target_instruction->source_node,15465 scope = scope->parent;
15479 buf_sprintf("expected scope reference, found type '%s'", buf_ptr(&type_arg->name)));15466 continue;
15480 return ira->codegen->builtin_types.entry_invalid;
15481 }15467 }
15482 fast_math_on_ptr = &decls_scope->fast_math_on;
15483 fast_math_set_node_ptr = &decls_scope->fast_math_set_node;
15484 } else {
15485 ir_add_error_node(ira, target_instruction->source_node,
15486 buf_sprintf("expected scope reference, found type '%s'", buf_ptr(&target_type->name)));
15487 return ira->codegen->builtin_types.entry_invalid;
15488 }15468 }
15469 assert(scope != nullptr);
1548915470
15490 IrInstruction *float_mode_value = instruction->mode_value->other;15471 IrInstruction *float_mode_value = instruction->mode_value->other;
15491
15492 FloatMode float_mode_scalar;15472 FloatMode float_mode_scalar;
15493 if (!ir_resolve_float_mode(ira, float_mode_value, &float_mode_scalar))15473 if (!ir_resolve_float_mode(ira, float_mode_value, &float_mode_scalar))
15494 return ira->codegen->builtin_types.entry_invalid;15474 return ira->codegen->builtin_types.entry_invalid;
test/cases/eval.zig+1-1
...@@ -275,7 +275,7 @@ test "eval @setFloatMode at compile-time" {...@@ -275,7 +275,7 @@ test "eval @setFloatMode at compile-time" {
275}275}
276276
277fn fnWithFloatMode() f32 {277fn fnWithFloatMode() f32 {
278 @setFloatMode(this, builtin.FloatMode.Strict);278 @setFloatMode(builtin.FloatMode.Strict);
279 return 1234.0;279 return 1234.0;
280}280}
281281
test/compile_errors.zig+2-2
...@@ -3996,8 +3996,8 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {...@@ -3996,8 +3996,8 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
3996 cases.add(3996 cases.add(
3997 "@setFloatMode twice for same scope",3997 "@setFloatMode twice for same scope",
3998 \\export fn foo() void {3998 \\export fn foo() void {
3999 \\ @setFloatMode(this, @import("builtin").FloatMode.Optimized);3999 \\ @setFloatMode(@import("builtin").FloatMode.Optimized);
4000 \\ @setFloatMode(this, @import("builtin").FloatMode.Optimized);4000 \\ @setFloatMode(@import("builtin").FloatMode.Optimized);
4001 \\}4001 \\}
4002 ,4002 ,
4003 ".tmp_source.zig:3:5: error: float mode set twice for same scope",4003 ".tmp_source.zig:3:5: error: float mode set twice for same scope",