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 @@...@@ -7,6 +7,7 @@
77
8const builtin = @import("builtin");8const builtin = @import("builtin");
9const std = @import("std");9const std = @import("std");
10const mem = std.mem;
10const time = std.time;11const time = std.time;
11const Timer = time.Timer;12const Timer = time.Timer;
12const crypto = std.crypto;13const crypto = std.crypto;
...@@ -21,14 +22,6 @@ const Crypto = struct {...@@ -21,14 +22,6 @@ const Crypto = struct {
21 name: []const u8,22 name: []const u8,
22};23};
2324
24fn blackBox(x: anytype) void {
25 asm volatile (""
26 :
27 : [x] "rm" (x)
28 : "memory"
29 );
30}
31
32const hashes = [_]Crypto{25const hashes = [_]Crypto{
33 Crypto{ .ty = crypto.hash.Md5, .name = "md5" },26 Crypto{ .ty = crypto.hash.Md5, .name = "md5" },
34 Crypto{ .ty = crypto.hash.Sha1, .name = "sha1" },27 Crypto{ .ty = crypto.hash.Sha1, .name = "sha1" },
...@@ -54,7 +47,7 @@ pub fn benchmarkHash(comptime Hash: anytype, comptime bytes: comptime_int) !u64...@@ -54,7 +47,7 @@ pub fn benchmarkHash(comptime Hash: anytype, comptime bytes: comptime_int) !u64
54 while (offset < bytes) : (offset += block.len) {47 while (offset < bytes) : (offset += block.len) {
55 h.update(block[0..]);48 h.update(block[0..]);
56 }49 }
57 blackBox(&h);50 mem.forceEval(&h);
58 const end = timer.read();51 const end = timer.read();
5952
60 const elapsed_s = @intToFloat(f64, end - start) / time.ns_per_s;53 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 {...@@ -89,7 +82,7 @@ pub fn benchmarkMac(comptime Mac: anytype, comptime bytes: comptime_int) !u64 {
89 const start = timer.lap();82 const start = timer.lap();
90 while (offset < bytes) : (offset += in.len) {83 while (offset < bytes) : (offset += in.len) {
91 Mac.create(mac[0..], in[0..], key[0..]);84 Mac.create(mac[0..], in[0..], key[0..]);
92 blackBox(&mac);85 mem.forceEval(&mac);
93 }86 }
94 const end = timer.read();87 const end = timer.read();
9588
...@@ -116,7 +109,7 @@ pub fn benchmarkKeyExchange(comptime DhKeyExchange: anytype, comptime exchange_c...@@ -116,7 +109,7 @@ pub fn benchmarkKeyExchange(comptime DhKeyExchange: anytype, comptime exchange_c
116 var i: usize = 0;109 var i: usize = 0;
117 while (i < exchange_count) : (i += 1) {110 while (i < exchange_count) : (i += 1) {
118 _ = DhKeyExchange.create(out[0..], out[0..], in[0..]);111 _ = DhKeyExchange.create(out[0..], out[0..], in[0..]);
119 blackBox(&out);112 mem.forceEval(&out);
120 }113 }
121 }114 }
122 const end = timer.read();115 const end = timer.read();
...@@ -141,7 +134,7 @@ pub fn benchmarkSignature(comptime Signature: anytype, comptime signatures_count...@@ -141,7 +134,7 @@ pub fn benchmarkSignature(comptime Signature: anytype, comptime signatures_count
141 var i: usize = 0;134 var i: usize = 0;
142 while (i < signatures_count) : (i += 1) {135 while (i < signatures_count) : (i += 1) {
143 const s = try Signature.sign(&msg, key_pair, null);136 const s = try Signature.sign(&msg, key_pair, null);
144 blackBox(&s);137 mem.forceEval(&s);
145 }138 }
146 }139 }
147 const end = timer.read();140 const end = timer.read();
...@@ -177,7 +170,7 @@ pub fn benchmarkAead(comptime Aead: anytype, comptime bytes: comptime_int) !u64...@@ -177,7 +170,7 @@ pub fn benchmarkAead(comptime Aead: anytype, comptime bytes: comptime_int) !u64
177 Aead.encrypt(in[0..], tag[0..], in[0..], &[_]u8{}, nonce, key);170 Aead.encrypt(in[0..], tag[0..], in[0..], &[_]u8{}, nonce, key);
178 Aead.decrypt(in[0..], in[0..], tag, &[_]u8{}, nonce, key) catch unreachable;171 Aead.decrypt(in[0..], in[0..], tag, &[_]u8{}, nonce, key) catch unreachable;
179 }172 }
180 blackBox(&in);173 mem.forceEval(&in);
181 const end = timer.read();174 const end = timer.read();
182175
183 const elapsed_s = @intToFloat(f64, end - start) / time.ns_per_s;176 const elapsed_s = @intToFloat(f64, end - start) / time.ns_per_s;
lib/std/math.zig+2-27
...@@ -5,6 +5,7 @@...@@ -5,6 +5,7 @@
5// and substantial portions of the software.5// and substantial portions of the software.
6const std = @import("std.zig");6const std = @import("std.zig");
7const assert = std.debug.assert;7const assert = std.debug.assert;
8const mem = std.mem;
8const testing = std.testing;9const testing = std.testing;
910
10/// Euler's number (e)11/// Euler's number (e)
...@@ -108,34 +109,8 @@ pub fn approxEq(comptime T: type, x: T, y: T, epsilon: T) bool {...@@ -108,34 +109,8 @@ pub fn approxEq(comptime T: type, x: T, y: T, epsilon: T) bool {
108 return fabs(x - y) < epsilon;109 return fabs(x - y) < epsilon;
109}110}
110111
111// TODO: Hide the following in an internal module.
112pub fn forceEval(value: anytype) void {112pub fn forceEval(value: anytype) void {
113 const T = @TypeOf(value);113 mem.forceEval(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 }
139}114}
140115
141pub fn raiseInvalid() void {116pub fn raiseInvalid() void {
lib/std/mem.zig+11
...@@ -2158,6 +2158,17 @@ pub fn alignForwardGeneric(comptime T: type, addr: T, alignment: T) T {...@@ -2158,6 +2158,17 @@ pub fn alignForwardGeneric(comptime T: type, addr: T, alignment: T) T {
2158 return alignBackwardGeneric(T, addr + (alignment - 1), alignment);2158 return alignBackwardGeneric(T, addr + (alignment - 1), alignment);
2159}2159}
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
2161test "alignForward" {2172test "alignForward" {
2162 testing.expect(alignForward(1, 1) == 1);2173 testing.expect(alignForward(1, 1) == 1);
2163 testing.expect(alignForward(2, 1) == 2);2174 testing.expect(alignForward(2, 1) == 2);