Skip to content

Commit 7c3636a

Browse files
committed
remove the scope parameter of setFloatMode
also document that scopes inherit this value. See #367 See #1283
1 parent 9ac9633 commit 7c3636a

File tree

6 files changed

+47
-63
lines changed

6 files changed

+47
-63
lines changed

doc/langref.html.in

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -734,7 +734,7 @@ export fn foo_strict(x: f64) f64 {
734734
}
735735

736736
export fn foo_optimized(x: f64) f64 {
737-
@setFloatMode(this, builtin.FloatMode.Optimized);
737+
@setFloatMode(builtin.FloatMode.Optimized);
738738
return x + big - big;
739739
}
740740
{#code_end#}
@@ -6030,17 +6030,20 @@ test "foo" {
60306030
{#see_also|comptime#}
60316031
{#header_close#}
60326032
{#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>
60346034
<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:
60366036
</p>
60376037
{#code_begin|syntax#}
60386038
pub const FloatMode = enum {
6039-
Optimized,
60406039
Strict,
6040+
Optimized,
60416041
};
60426042
{#code_end#}
60436043
<ul>
6044+
<li>
6045+
<code>Strict</code> (default) - Floating point operations follow strict IEEE compliance.
6046+
</li>
60446047
<li>
60456048
<code>Optimized</code> - Floating point operations may do all of the following:
60466049
<ul>
@@ -6053,10 +6056,11 @@ pub const FloatMode = enum {
60536056
</ul>
60546057
This is equivalent to <code>-ffast-math</code> in GCC.
60556058
</li>
6056-
<li>
6057-
<code>Strict</code> (default) - Floating point operations follow strict IEEE compliance.
6058-
</li>
60596059
</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>
60606064
{#see_also|Floating Point Operations#}
60616065
{#header_close#}
60626066
{#header_open|@setGlobalLinkage#}

src/all_types.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3287,8 +3287,8 @@ static const size_t stack_trace_ptr_count = 30;
32873287

32883288

32893289
enum FloatMode {
3290-
FloatModeOptimized,
32913290
FloatModeStrict,
3291+
FloatModeOptimized,
32923292
};
32933293

32943294
enum FnWalkId {

src/codegen.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6729,7 +6729,7 @@ static void define_builtin_fns(CodeGen *g) {
67296729
create_builtin_fn(g, BuiltinFnIdIntType, "IntType", 2); // TODO rename to Int
67306730
create_builtin_fn(g, BuiltinFnIdSetCold, "setCold", 1);
67316731
create_builtin_fn(g, BuiltinFnIdSetRuntimeSafety, "setRuntimeSafety", 1);
6732-
create_builtin_fn(g, BuiltinFnIdSetFloatMode, "setFloatMode", 2);
6732+
create_builtin_fn(g, BuiltinFnIdSetFloatMode, "setFloatMode", 1);
67336733
create_builtin_fn(g, BuiltinFnIdPanic, "panic", 1);
67346734
create_builtin_fn(g, BuiltinFnIdPtrCast, "ptrCast", 2);
67356735
create_builtin_fn(g, BuiltinFnIdBitCast, "bitCast", 2);
@@ -7115,20 +7115,20 @@ Buf *codegen_generate_builtin_source(CodeGen *g) {
71157115
{
71167116
buf_appendf(contents,
71177117
"pub const FloatMode = enum {\n"
7118-
" Optimized,\n"
71197118
" Strict,\n"
7119+
" Optimized,\n"
71207120
"};\n\n");
7121-
assert(FloatModeOptimized == 0);
7122-
assert(FloatModeStrict == 1);
7121+
assert(FloatModeStrict == 0);
7122+
assert(FloatModeOptimized == 1);
71237123
}
71247124
{
71257125
buf_appendf(contents,
71267126
"pub const Endian = enum {\n"
71277127
" Big,\n"
71287128
" Little,\n"
71297129
"};\n\n");
7130-
assert(FloatModeOptimized == 0);
7131-
assert(FloatModeStrict == 1);
7130+
//assert(EndianBig == 0);
7131+
//assert(EndianLittle == 1);
71327132
}
71337133
{
71347134
const char *endian_str = g->is_big_endian ? "Endian.Big" : "Endian.Little";

src/ir.cpp

Lines changed: 26 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1577,13 +1577,11 @@ static IrInstruction *ir_build_set_runtime_safety(IrBuilder *irb, Scope *scope,
15771577
}
15781578

15791579
static IrInstruction *ir_build_set_float_mode(IrBuilder *irb, Scope *scope, AstNode *source_node,
1580-
IrInstruction *scope_value, IrInstruction *mode_value)
1580+
IrInstruction *mode_value)
15811581
{
15821582
IrInstructionSetFloatMode *instruction = ir_build_instruction<IrInstructionSetFloatMode>(irb, scope, source_node);
1583-
instruction->scope_value = scope_value;
15841583
instruction->mode_value = mode_value;
15851584

1586-
ir_ref_instruction(scope_value, irb->current_basic_block);
15871585
ir_ref_instruction(mode_value, irb->current_basic_block);
15881586

15891587
return &instruction->base;
@@ -3959,12 +3957,7 @@ static IrInstruction *ir_gen_builtin_fn_call(IrBuilder *irb, Scope *scope, AstNo
39593957
if (arg0_value == irb->codegen->invalid_instruction)
39603958
return arg0_value;
39613959

3962-
AstNode *arg1_node = node->data.fn_call_expr.params.at(1);
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);
3960+
IrInstruction *set_float_mode = ir_build_set_float_mode(irb, scope, node, arg0_value);
39683961
return ir_lval_wrap(irb, scope, set_float_mode, lval);
39693962
}
39703963
case BuiltinFnIdSizeof:
@@ -15379,6 +15372,7 @@ static ZigType *ir_analyze_instruction_set_cold(IrAnalyze *ira, IrInstructionSet
1537915372
ir_build_const_from(ira, &instruction->base);
1538015373
return ira->codegen->builtin_types.entry_void;
1538115374
}
15375+
1538215376
static ZigType *ir_analyze_instruction_set_runtime_safety(IrAnalyze *ira,
1538315377
IrInstructionSetRuntimeSafety *set_runtime_safety_instruction)
1538415378
{
@@ -15439,14 +15433,6 @@ static ZigType *ir_analyze_instruction_set_runtime_safety(IrAnalyze *ira,
1543915433
static ZigType *ir_analyze_instruction_set_float_mode(IrAnalyze *ira,
1544015434
IrInstructionSetFloatMode *instruction)
1544115435
{
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-
1545015436
if (ira->new_irb.exec->is_inline) {
1545115437
// ignore setFloatMode when running functions at compile time
1545215438
ir_build_const_from(ira, &instruction->base);
@@ -15455,40 +15441,34 @@ static ZigType *ir_analyze_instruction_set_float_mode(IrAnalyze *ira,
1545515441

1545615442
bool *fast_math_on_ptr;
1545715443
AstNode **fast_math_set_node_ptr;
15458-
if (target_type->id == ZigTypeIdBlock) {
15459-
ScopeBlock *block_scope = (ScopeBlock *)target_val->data.x_block;
15460-
fast_math_on_ptr = &block_scope->fast_math_on;
15461-
fast_math_set_node_ptr = &block_scope->fast_math_set_node;
15462-
} else if (target_type->id == ZigTypeIdFn) {
15463-
assert(target_val->data.x_ptr.special == ConstPtrSpecialFunction);
15464-
ZigFn *target_fn = target_val->data.x_ptr.data.fn.fn_entry;
15465-
assert(target_fn->def_scope);
15466-
fast_math_on_ptr = &target_fn->def_scope->fast_math_on;
15467-
fast_math_set_node_ptr = &target_fn->def_scope->fast_math_set_node;
15468-
} else if (target_type->id == ZigTypeIdMetaType) {
15469-
ScopeDecls *decls_scope;
15470-
ZigType *type_arg = target_val->data.x_type;
15471-
if (type_arg->id == ZigTypeIdStruct) {
15472-
decls_scope = type_arg->data.structure.decls_scope;
15473-
} else if (type_arg->id == ZigTypeIdEnum) {
15474-
decls_scope = type_arg->data.enumeration.decls_scope;
15475-
} else if (type_arg->id == ZigTypeIdUnion) {
15476-
decls_scope = type_arg->data.unionation.decls_scope;
15444+
15445+
Scope *scope = instruction->base.scope;
15446+
while (scope != nullptr) {
15447+
if (scope->id == ScopeIdBlock) {
15448+
ScopeBlock *block_scope = (ScopeBlock *)scope;
15449+
fast_math_on_ptr = &block_scope->fast_math_on;
15450+
fast_math_set_node_ptr = &block_scope->fast_math_set_node;
15451+
break;
15452+
} else if (scope->id == ScopeIdFnDef) {
15453+
ScopeFnDef *def_scope = (ScopeFnDef *)scope;
15454+
ZigFn *target_fn = def_scope->fn_entry;
15455+
assert(target_fn->def_scope != nullptr);
15456+
fast_math_on_ptr = &target_fn->def_scope->fast_math_on;
15457+
fast_math_set_node_ptr = &target_fn->def_scope->fast_math_set_node;
15458+
break;
15459+
} else if (scope->id == ScopeIdDecls) {
15460+
ScopeDecls *decls_scope = (ScopeDecls *)scope;
15461+
fast_math_on_ptr = &decls_scope->fast_math_on;
15462+
fast_math_set_node_ptr = &decls_scope->fast_math_set_node;
15463+
break;
1547715464
} else {
15478-
ir_add_error_node(ira, target_instruction->source_node,
15479-
buf_sprintf("expected scope reference, found type '%s'", buf_ptr(&type_arg->name)));
15480-
return ira->codegen->builtin_types.entry_invalid;
15465+
scope = scope->parent;
15466+
continue;
1548115467
}
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;
1548815468
}
15469+
assert(scope != nullptr);
1548915470

1549015471
IrInstruction *float_mode_value = instruction->mode_value->other;
15491-
1549215472
FloatMode float_mode_scalar;
1549315473
if (!ir_resolve_float_mode(ira, float_mode_value, &float_mode_scalar))
1549415474
return ira->codegen->builtin_types.entry_invalid;

test/cases/eval.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,7 @@ test "eval @setFloatMode at compile-time" {
275275
}
276276

277277
fn fnWithFloatMode() f32 {
278-
@setFloatMode(this, builtin.FloatMode.Strict);
278+
@setFloatMode(builtin.FloatMode.Strict);
279279
return 1234.0;
280280
}
281281

test/compile_errors.zig

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3996,8 +3996,8 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
39963996
cases.add(
39973997
"@setFloatMode twice for same scope",
39983998
\\export fn foo() void {
3999-
\\ @setFloatMode(this, @import("builtin").FloatMode.Optimized);
4000-
\\ @setFloatMode(this, @import("builtin").FloatMode.Optimized);
3999+
\\ @setFloatMode(@import("builtin").FloatMode.Optimized);
4000+
\\ @setFloatMode(@import("builtin").FloatMode.Optimized);
40014001
\\}
40024002
,
40034003
".tmp_source.zig:3:5: error: float mode set twice for same scope",

0 commit comments

Comments
 (0)