authorgravatar for 124872+jedisct1@users.noreply.github.comFrank Denis <124872+jedisct1@users.noreply.github.com> 2020-08-25 22:43:42+02:00
committergravatar for 124872+jedisct1@users.noreply.github.comFrank Denis <124872+jedisct1@users.noreply.github.com> 2020-08-26 10:50:34+02:00
log0bd53dd2033c60d3446abfb83209237c6eb6c9e2
treec9e3936f62f3526acee4e23d97c9997d973e19df
parentff2e82f382b253ca50977d16fe58865e0de3223f

Rename blackBox, move it to std.mem.forceEval()


3 files changed, 19 insertions(+), 40 deletions(-)

lib/std/crypto/benchmark.zig+6-13
......@@ -7,6 +7,7 @@
77
88const builtin = @import("builtin");
99const std = @import("std");
10const mem = std.mem;
1011const time = std.time;
1112const Timer = time.Timer;
1213const crypto = std.crypto;
......@@ -21,14 +22,6 @@ const Crypto = struct {
2122 name: []const u8,
2223};
2324
24fn blackBox(x: anytype) void {
25 asm volatile (""
26 :
27 : [x] "rm" (x)
28 : "memory"
29 );
30}
31
3225const 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+2-27
......@@ -5,6 +5,7 @@
55// and substantial portions of the software.
66const std = @import("std.zig");
77const assert = std.debug.assert;
8const mem = std.mem;
89const 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.
112112pub 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
141116pub fn raiseInvalid() void {
lib/std/mem.zig+11
......@@ -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.
2164pub fn forceEval(val: anytype) void {
2165 asm volatile (""
2166 :
2167 : [val] "rm" (val)
2168 : "memory"
2169 );
2170}
2171
21612172test "alignForward" {
21622173 testing.expect(alignForward(1, 1) == 1);
21632174 testing.expect(alignForward(2, 1) == 2);