Skip to content

Commit 21e26d4

Browse files
committed
Upgrade to zig 0.16.0
1 parent 3a5955b commit 21e26d4

3 files changed

Lines changed: 58 additions & 72 deletions

File tree

build.zig.zon

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
.name = .zmath,
33
.fingerprint = 0xfd23d422bd223cc2,
44
.version = "0.11.0-dev",
5-
.minimum_zig_version = "0.15.1",
5+
.minimum_zig_version = "0.16.0",
66
.paths = .{
77
"build.zig",
88
"build.zig.zon",

src/benchmark.zig

Lines changed: 53 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -49,43 +49,41 @@
4949
/// wave benchmark (SOA) - scalar version: 3.7832s, zmath version: 0.3642s
5050
///
5151
/// -------------------------------------------------------------------------------------------------
52-
pub fn main() !void {
53-
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
54-
defer _ = gpa.deinit();
55-
const allocator = gpa.allocator();
52+
pub fn main(init: std.process.Init) !void {
53+
const allocator = init.gpa;
5654

5755
// m = mul(ma, mb); data set fits in L1 cache; AOS data layout.
58-
try mat4MulBenchmark(allocator, 100_000);
56+
try mat4MulBenchmark(init.io, allocator, 100_000);
5957

6058
// v = 0.01 * cross3(va, vb) + vec3(1.0); data set fits in L1 cache; AOS data layout.
61-
try cross3ScaleBiasBenchmark(allocator, 10_000);
59+
try cross3ScaleBiasBenchmark(init.io, allocator, 10_000);
6260

6361
// v = dot3(va, vb) * (0.1 * cross3(va, vb) + vec3(1.0)); data set fits in L1 cache; AOS data layout.
64-
try cross3Dot3ScaleBiasBenchmark(allocator, 10_000);
62+
try cross3Dot3ScaleBiasBenchmark(init.io, allocator, 10_000);
6563

6664
// q = qmul(qa, qb); data set fits in L1 cache; AOS data layout.
67-
try quatBenchmark(allocator, 10_000);
65+
try quatBenchmark(init.io, allocator, 10_000);
6866

6967
// d = sqrt(x * x + z * z); y = sin(d - t); SOA layout.
70-
try waveBenchmark(allocator, 1_000);
68+
try waveBenchmark(init.io, allocator, 1_000);
7169
}
7270

7371
const std = @import("std");
7472
const time = std.time;
75-
const Timer = time.Timer;
73+
const Timer = std.Io.Timestamp;
7674
const zm = @import("zmath");
7775

7876
var prng = std.Random.DefaultPrng.init(0);
7977
const random = prng.random();
8078

81-
noinline fn mat4MulBenchmark(allocator: std.mem.Allocator, comptime count: comptime_int) !void {
79+
noinline fn mat4MulBenchmark(io: std.Io, allocator: std.mem.Allocator, comptime count: comptime_int) !void {
8280
std.debug.print("\n", .{});
8381
std.debug.print("{s:>42} - ", .{"matrix mul benchmark (AOS)"});
8482

8583
var data0 = try std.ArrayList([16]f32).initCapacity(allocator, 64);
86-
defer data0.deinit();
84+
defer data0.deinit(allocator);
8785
var data1 = try std.ArrayList([16]f32).initCapacity(allocator, 64);
88-
defer data1.deinit();
86+
defer data1.deinit(allocator);
8987

9088
var i: usize = 0;
9189
while (i < 64) : (i += 1) {
@@ -118,8 +116,7 @@ noinline fn mat4MulBenchmark(allocator: std.mem.Allocator, comptime count: compt
118116

119117
{
120118
i = 0;
121-
var timer = try Timer.start();
122-
const start = timer.lap();
119+
const start = std.Io.Clock.awake.now(io);
123120
while (i < count) : (i += 1) {
124121
for (data1.items) |b| {
125122
for (data0.items) |a| {
@@ -145,16 +142,15 @@ noinline fn mat4MulBenchmark(allocator: std.mem.Allocator, comptime count: compt
145142
}
146143
}
147144
}
148-
const end = timer.read();
149-
const elapsed_s = @as(f64, @floatFromInt(end - start)) / time.ns_per_s;
145+
const elapsed = start.untilNow(io, .awake);
150146

151-
std.debug.print("scalar version: {d:.4}s, ", .{elapsed_s});
147+
std.debug.print("scalar version: {f}, ", .{elapsed});
152148
}
153149

154150
{
155151
i = 0;
156-
var timer = try Timer.start();
157-
const start = timer.lap();
152+
153+
const start = std.Io.Clock.awake.now(io);
158154
while (i < count) : (i += 1) {
159155
for (data1.items) |b| {
160156
for (data0.items) |a| {
@@ -165,20 +161,19 @@ noinline fn mat4MulBenchmark(allocator: std.mem.Allocator, comptime count: compt
165161
}
166162
}
167163
}
168-
const end = timer.read();
169-
const elapsed_s = @as(f64, @floatFromInt(end - start)) / time.ns_per_s;
170164

171-
std.debug.print("zmath version: {d:.4}s\n", .{elapsed_s});
165+
const elapsed = start.untilNow(io, .awake);
166+
std.debug.print("zmath version: {f}\n", .{elapsed});
172167
}
173168
}
174169

175-
noinline fn cross3ScaleBiasBenchmark(allocator: std.mem.Allocator, comptime count: comptime_int) !void {
170+
noinline fn cross3ScaleBiasBenchmark(io: std.Io, allocator: std.mem.Allocator, comptime count: comptime_int) !void {
176171
std.debug.print("{s:>42} - ", .{"cross3, scale, bias benchmark (AOS)"});
177172

178173
var data0 = try std.ArrayList([3]f32).initCapacity(allocator, 256);
179-
defer data0.deinit();
174+
defer data0.deinit(allocator);
180175
var data1 = try std.ArrayList([3]f32).initCapacity(allocator, 256);
181-
defer data1.deinit();
176+
defer data1.deinit(allocator);
182177

183178
var i: usize = 0;
184179
while (i < 256) : (i += 1) {
@@ -201,8 +196,8 @@ noinline fn cross3ScaleBiasBenchmark(allocator: std.mem.Allocator, comptime coun
201196

202197
{
203198
i = 0;
204-
var timer = try Timer.start();
205-
const start = timer.lap();
199+
200+
const start = std.Io.Clock.awake.now(io);
206201
while (i < count) : (i += 1) {
207202
for (data1.items) |b| {
208203
for (data0.items) |a| {
@@ -215,16 +210,15 @@ noinline fn cross3ScaleBiasBenchmark(allocator: std.mem.Allocator, comptime coun
215210
}
216211
}
217212
}
218-
const end = timer.read();
219-
const elapsed_s = @as(f64, @floatFromInt(end - start)) / time.ns_per_s;
213+
const elapsed = start.untilNow(io, .awake);
220214

221-
std.debug.print("scalar version: {d:.4}s, ", .{elapsed_s});
215+
std.debug.print("scalar version: {f}, ", .{elapsed});
222216
}
223217

224218
{
225219
i = 0;
226-
var timer = try Timer.start();
227-
const start = timer.lap();
220+
221+
const start = std.Io.Clock.awake.now(io);
228222
while (i < count) : (i += 1) {
229223
for (data1.items) |b| {
230224
for (data0.items) |a| {
@@ -235,14 +229,13 @@ noinline fn cross3ScaleBiasBenchmark(allocator: std.mem.Allocator, comptime coun
235229
}
236230
}
237231
}
238-
const end = timer.read();
239-
const elapsed_s = @as(f64, @floatFromInt(end - start)) / time.ns_per_s;
240232

241-
std.debug.print("zmath version: {d:.4}s\n", .{elapsed_s});
233+
const elapsed = start.untilNow(io, .awake);
234+
std.debug.print("zmath version: {f}\n", .{elapsed});
242235
}
243236
}
244237

245-
noinline fn cross3Dot3ScaleBiasBenchmark(allocator: std.mem.Allocator, comptime count: comptime_int) !void {
238+
noinline fn cross3Dot3ScaleBiasBenchmark(io: std.Io, allocator: std.mem.Allocator, comptime count: comptime_int) !void {
246239
std.debug.print("{s:>42} - ", .{"cross3, dot3, scale, bias benchmark (AOS)"});
247240

248241
var data0 = try std.ArrayList([3]f32).initCapacity(allocator, 256);
@@ -271,8 +264,8 @@ noinline fn cross3Dot3ScaleBiasBenchmark(allocator: std.mem.Allocator, comptime
271264

272265
{
273266
i = 0;
274-
var timer = try Timer.start();
275-
const start = timer.lap();
267+
268+
const start = std.Io.Clock.awake.now(io);
276269
while (i < count) : (i += 1) {
277270
for (data1.items) |b| {
278271
for (data0.items) |a| {
@@ -286,16 +279,15 @@ noinline fn cross3Dot3ScaleBiasBenchmark(allocator: std.mem.Allocator, comptime
286279
}
287280
}
288281
}
289-
const end = timer.read();
290-
const elapsed_s = @as(f64, @floatFromInt(end - start)) / time.ns_per_s;
282+
const elapsed = start.untilNow(io, .awake);
291283

292-
std.debug.print("scalar version: {d:.4}s, ", .{elapsed_s});
284+
std.debug.print("scalar version: {f}, ", .{elapsed});
293285
}
294286

295287
{
296288
i = 0;
297-
var timer = try Timer.start();
298-
const start = timer.lap();
289+
290+
const start = std.Io.Clock.awake.now(io);
299291
while (i < count) : (i += 1) {
300292
for (data1.items) |b| {
301293
for (data0.items) |a| {
@@ -306,14 +298,13 @@ noinline fn cross3Dot3ScaleBiasBenchmark(allocator: std.mem.Allocator, comptime
306298
}
307299
}
308300
}
309-
const end = timer.read();
310-
const elapsed_s = @as(f64, @floatFromInt(end - start)) / time.ns_per_s;
301+
const elapsed = start.untilNow(io, .awake);
311302

312-
std.debug.print("zmath version: {d:.4}s\n", .{elapsed_s});
303+
std.debug.print("zmath version: {f}\n", .{elapsed});
313304
}
314305
}
315306

316-
noinline fn quatBenchmark(allocator: std.mem.Allocator, comptime count: comptime_int) !void {
307+
noinline fn quatBenchmark(io: std.Io, allocator: std.mem.Allocator, comptime count: comptime_int) !void {
317308
std.debug.print("{s:>42} - ", .{"quaternion mul benchmark (AOS)"});
318309

319310
var data0 = try std.ArrayList([4]f32).initCapacity(allocator, 256);
@@ -342,8 +333,8 @@ noinline fn quatBenchmark(allocator: std.mem.Allocator, comptime count: comptime
342333

343334
{
344335
i = 0;
345-
var timer = try Timer.start();
346-
const start = timer.lap();
336+
337+
const start = std.Io.Clock.awake.now(io);
347338
while (i < count) : (i += 1) {
348339
for (data1.items) |b| {
349340
for (data0.items) |a| {
@@ -357,16 +348,14 @@ noinline fn quatBenchmark(allocator: std.mem.Allocator, comptime count: comptime
357348
}
358349
}
359350
}
360-
const end = timer.read();
361-
const elapsed_s = @as(f64, @floatFromInt(end - start)) / time.ns_per_s;
351+
const elapsed = start.untilNow(io, .awake);
362352

363-
std.debug.print("scalar version: {d:.4}s, ", .{elapsed_s});
353+
std.debug.print("scalar version: {f}, ", .{elapsed});
364354
}
365355

366356
{
367357
i = 0;
368-
var timer = try Timer.start();
369-
const start = timer.lap();
358+
const start = std.Io.Clock.awake.now(io);
370359
while (i < count) : (i += 1) {
371360
for (data1.items) |b| {
372361
for (data0.items) |a| {
@@ -377,14 +366,13 @@ noinline fn quatBenchmark(allocator: std.mem.Allocator, comptime count: comptime
377366
}
378367
}
379368
}
380-
const end = timer.read();
381-
const elapsed_s = @as(f64, @floatFromInt(end - start)) / time.ns_per_s;
369+
const elapsed = start.untilNow(io, .awake);
382370

383-
std.debug.print("zmath version: {d:.4}s\n", .{elapsed_s});
371+
std.debug.print("zmath version: {f}\n", .{elapsed});
384372
}
385373
}
386374

387-
noinline fn waveBenchmark(allocator: std.mem.Allocator, comptime count: comptime_int) !void {
375+
noinline fn waveBenchmark(io: std.Io, allocator: std.mem.Allocator, comptime count: comptime_int) !void {
388376
_ = allocator;
389377
std.debug.print("{s:>42} - ", .{"wave benchmark (SOA)"});
390378

@@ -394,8 +382,7 @@ noinline fn waveBenchmark(allocator: std.mem.Allocator, comptime count: comptime
394382

395383
const scale: f32 = 0.05;
396384

397-
var timer = try Timer.start();
398-
const start = timer.lap();
385+
const start = std.Io.Clock.awake.now(io);
399386

400387
var iter: usize = 0;
401388
while (iter < count) : (iter += 1) {
@@ -428,10 +415,9 @@ noinline fn waveBenchmark(allocator: std.mem.Allocator, comptime count: comptime
428415
}
429416
t += 0.001;
430417
}
431-
const end = timer.read();
432-
const elapsed_s = @as(f64, @floatFromInt(end - start)) / time.ns_per_s;
418+
const elapsed = start.untilNow(io, .awake);
433419

434-
std.debug.print("scalar version: {d:.4}s, ", .{elapsed_s});
420+
std.debug.print("scalar version: {f}, ", .{elapsed});
435421
}
436422

437423
{
@@ -445,8 +431,7 @@ noinline fn waveBenchmark(allocator: std.mem.Allocator, comptime count: comptime
445431

446432
const scale: f32 = 0.05;
447433

448-
var timer = try Timer.start();
449-
const start = timer.lap();
434+
const start = std.Io.Clock.awake.now(io);
450435

451436
var iter: usize = 0;
452437
while (iter < count) : (iter += 1) {
@@ -469,9 +454,8 @@ noinline fn waveBenchmark(allocator: std.mem.Allocator, comptime count: comptime
469454
}
470455
vt += zm.splat(T, 0.001);
471456
}
472-
const end = timer.read();
473-
const elapsed_s = @as(f64, @floatFromInt(end - start)) / time.ns_per_s;
457+
const elapsed = start.untilNow(io, .awake);
474458

475-
std.debug.print("zmath version: {d:.4}s\n", .{elapsed_s});
459+
std.debug.print("zmath version: {f}\n", .{elapsed});
476460
}
477461
}

src/root.zig

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4122,7 +4122,8 @@ test "zmath.fftN" {
41224122
-77.254834, 0.000000, -105.489863, 0.000000, -160.874864, 0.000000, -324.901452, 0.000000,
41234123
};
41244124
for (expected, 0..) |e, ie| {
4125-
try expect(std.math.approxEqAbs(f32, e, im[(ie / 4)][ie % 4], epsilon));
4125+
const v: [4]f32 = im[(ie / 4)];
4126+
try expect(std.math.approxEqAbs(f32, e, v[ie % 4], epsilon));
41264127
}
41274128
}
41284129

@@ -4185,7 +4186,8 @@ test "zmath.fftN" {
41854186
-321.749727, 0.000000, 0.000000, 0.000000, -649.802905, 0.000000, 0.000000, 0.000000,
41864187
};
41874188
for (expected, 0..) |e, ie| {
4188-
try expect(std.math.approxEqAbs(f32, e, im[(ie / 4)][ie % 4], epsilon));
4189+
const v: [4]f32 = im[(ie / 4)];
4190+
try expect(std.math.approxEqAbs(f32, e, v[ie % 4], epsilon));
41894191
}
41904192
}
41914193
}

0 commit comments

Comments
 (0)