Skip to content

Commit 0bd53dd

Browse files
committed
Rename blackBox, move it to std.mem.forceEval()
1 parent ff2e82f commit 0bd53dd

File tree

3 files changed

+19
-40
lines changed

3 files changed

+19
-40
lines changed

lib/std/crypto/benchmark.zig

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
const builtin = @import("builtin");
99
const std = @import("std");
10+
const mem = std.mem;
1011
const time = std.time;
1112
const Timer = time.Timer;
1213
const crypto = std.crypto;
@@ -21,14 +22,6 @@ const Crypto = struct {
2122
name: []const u8,
2223
};
2324

24-
fn blackBox(x: anytype) void {
25-
asm volatile (""
26-
:
27-
: [x] "rm" (x)
28-
: "memory"
29-
);
30-
}
31-
3225
const hashes = [_]Crypto{
3326
Crypto{ .ty = crypto.hash.Md5, .name = "md5" },
3427
Crypto{ .ty = crypto.hash.Sha1, .name = "sha1" },
@@ -54,7 +47,7 @@ pub fn benchmarkHash(comptime Hash: anytype, comptime bytes: comptime_int) !u64
5447
while (offset < bytes) : (offset += block.len) {
5548
h.update(block[0..]);
5649
}
57-
blackBox(&h);
50+
mem.forceEval(&h);
5851
const end = timer.read();
5952

6053
const elapsed_s = @intToFloat(f64, end - start) / time.ns_per_s;
@@ -89,7 +82,7 @@ pub fn benchmarkMac(comptime Mac: anytype, comptime bytes: comptime_int) !u64 {
8982
const start = timer.lap();
9083
while (offset < bytes) : (offset += in.len) {
9184
Mac.create(mac[0..], in[0..], key[0..]);
92-
blackBox(&mac);
85+
mem.forceEval(&mac);
9386
}
9487
const end = timer.read();
9588

@@ -116,7 +109,7 @@ pub fn benchmarkKeyExchange(comptime DhKeyExchange: anytype, comptime exchange_c
116109
var i: usize = 0;
117110
while (i < exchange_count) : (i += 1) {
118111
_ = DhKeyExchange.create(out[0..], out[0..], in[0..]);
119-
blackBox(&out);
112+
mem.forceEval(&out);
120113
}
121114
}
122115
const end = timer.read();
@@ -141,7 +134,7 @@ pub fn benchmarkSignature(comptime Signature: anytype, comptime signatures_count
141134
var i: usize = 0;
142135
while (i < signatures_count) : (i += 1) {
143136
const s = try Signature.sign(&msg, key_pair, null);
144-
blackBox(&s);
137+
mem.forceEval(&s);
145138
}
146139
}
147140
const end = timer.read();
@@ -177,7 +170,7 @@ pub fn benchmarkAead(comptime Aead: anytype, comptime bytes: comptime_int) !u64
177170
Aead.encrypt(in[0..], tag[0..], in[0..], &[_]u8{}, nonce, key);
178171
Aead.decrypt(in[0..], in[0..], tag, &[_]u8{}, nonce, key) catch unreachable;
179172
}
180-
blackBox(&in);
173+
mem.forceEval(&in);
181174
const end = timer.read();
182175

183176
const elapsed_s = @intToFloat(f64, end - start) / time.ns_per_s;

lib/std/math.zig

Lines changed: 2 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
// and substantial portions of the software.
66
const std = @import("std.zig");
77
const assert = std.debug.assert;
8+
const mem = std.mem;
89
const testing = std.testing;
910

1011
/// Euler's number (e)
@@ -108,34 +109,8 @@ pub fn approxEq(comptime T: type, x: T, y: T, epsilon: T) bool {
108109
return fabs(x - y) < epsilon;
109110
}
110111

111-
// TODO: Hide the following in an internal module.
112112
pub fn forceEval(value: anytype) void {
113-
const T = @TypeOf(value);
114-
switch (T) {
115-
f16 => {
116-
var x: f16 = undefined;
117-
const p = @ptrCast(*volatile f16, &x);
118-
p.* = x;
119-
},
120-
f32 => {
121-
var x: f32 = undefined;
122-
const p = @ptrCast(*volatile f32, &x);
123-
p.* = x;
124-
},
125-
f64 => {
126-
var x: f64 = undefined;
127-
const p = @ptrCast(*volatile f64, &x);
128-
p.* = x;
129-
},
130-
f128 => {
131-
var x: f128 = undefined;
132-
const p = @ptrCast(*volatile f128, &x);
133-
p.* = x;
134-
},
135-
else => {
136-
@compileError("forceEval not implemented for " ++ @typeName(T));
137-
},
138-
}
113+
mem.forceEval(value);
139114
}
140115

141116
pub fn raiseInvalid() void {

lib/std/mem.zig

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2158,6 +2158,17 @@ pub fn alignForwardGeneric(comptime T: type, addr: T, alignment: T) T {
21582158
return alignBackwardGeneric(T, addr + (alignment - 1), alignment);
21592159
}
21602160

2161+
/// Force an evaluation of the expression; this tries to prevent
2162+
/// the compiler from optimizing the computation away even if the
2163+
/// result eventually gets discarded.
2164+
pub fn forceEval(val: anytype) void {
2165+
asm volatile (""
2166+
:
2167+
: [val] "rm" (val)
2168+
: "memory"
2169+
);
2170+
}
2171+
21612172
test "alignForward" {
21622173
testing.expect(alignForward(1, 1) == 1);
21632174
testing.expect(alignForward(2, 1) == 2);

0 commit comments

Comments
 (0)