Zig 0.17.0-dev (Split by item)

Shortcut keys
?
Show these tips
S
Focus search field
Esc
Clear focus & close tips
U
Jump to source code
↑
Move up in search results
↓
Move down in search results
↵
Go to active search result
This is an example of documentation generated by ZigDoc, an alternative to Zig's built-in Auto Doc feature. See also examples in other modes/formats. The project being documented here (as the example) is the Zig standard library. Public Function

doNotOptimizeAway

Public Function
std.mem.doNotOptimizeAway
pub fn doNotOptimizeAway(val: anytype) void

Force an evaluation of the expression; this tries to prevent the compiler from optimizing the computation away even if the result eventually gets discarded.

Params

  • val

File

mem.zig:4827
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
pub fn doNotOptimizeAway(val: anytype) void {
    if (@inComptime()) return;

    if (builtin.zig_backend == .stage2_c and builtin.abi == .msvc) {
        _ = @atomicRmw(*const anyopaque, @as(*volatile *const anyopaque, &struct {
            var escape: *const anyopaque = undefined;
        }.escape), .Xchg, &val, .acq_rel); // TODO: syncscope("singlethreaded")
        return;
    }

    switch (@typeInfo(@TypeOf(val))) {
        .void, .null, .comptime_int, .comptime_float => return,
        .@"enum" => doNotOptimizeAway(@backingInt(val)),
        .bool => doNotOptimizeAway(@intFromBool(val)),
        .int => |int| {
            // SPIR-V targets do not have registers per se, they have values
            // tied to IDs that can be passed to valid instructions. Some
            // SPIR-V targets do not define c_long, so we just allow any sized
            // integer on these targets
            const val_fits_in_gp_register = builtin.target.cpu.arch.isSpirV() or fits: {
                const max_gp_register_bits = @bitSizeOf(c_long);
                break :fits int.bits <= max_gp_register_bits;
            };
            if (val_fits_in_gp_register) {
                const val2 = @as(
                    @Int(int.signedness, @max(8, std.math.ceilPowerOfTwoAssert(u16, int.bits))),
                    val,
                );
                asm volatile (""
                    :
                    : [_] "r" (val2),
                );
            } else {
                doNotOptimizeAway(&val);
            }
        },
        .float => |float| switch (float.bits) {
            else => comptime unreachable,
            16, 80, 128 => doNotOptimizeAway(&val),
            32, 64 => asm volatile (""
                :
                : [_] "rm" (val),
            ),
        },
        .pointer => asm volatile (""
            :
            : [_] "m" (val),
            : .{ .memory = true }),
        .array => |array| if (array.len * @sizeOf(array.child) <= 64) {
            for (val) |v| doNotOptimizeAway(v);
        } else doNotOptimizeAway(&val),
        else => doNotOptimizeAway(&val),
    }
}