1const builtin = @import("builtin");
2const std = @import("std");
3const assert = std.debug.assert;
4const expect = std.testing.expect;
5
6test "@abs integers" {
7 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
8 if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO
9 if (builtin.zig_backend == .stage2_riscv64) return error.SkipZigTest;
10
11 try comptime testAbsIntegers();
12 try testAbsIntegers();
13}
14
15fn testAbsIntegers() !void {
16 {
17 var x: i32 = -1000;
18 _ = &x;
19 try expect(@abs(x) == 1000);
20 }
21 {
22 var x: i32 = 0;
23 _ = &x;
24 try expect(@abs(x) == 0);
25 }
26 {
27 var x: i32 = 1000;
28 _ = &x;
29 try expect(@abs(x) == 1000);
30 }
31 {
32 var x: i64 = std.math.minInt(i64);
33 _ = &x;
34 try expect(@abs(x) == @as(u64, -std.math.minInt(i64)));
35 }
36 {
37 var x: i5 = -1;
38 _ = &x;
39 try expect(@abs(x) == 1);
40 }
41 {
42 var x: i5 = -5;
43 _ = &x;
44 try expect(@abs(x) == 5);
45 }
46 comptime {
47 try expect(@abs(@as(i2, -2)) == 2);
48 }
49}
50
51test "@abs signed C ABI integers" {
52 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO
53 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
54 if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO
55 if (builtin.zig_backend == .stage2_riscv64) return error.SkipZigTest; // TODO
56
57 const S = struct {
58 fn doTheTest() !void {
59 try testOne(isize, usize);
60 try testOne(c_short, c_ushort);
61 try testOne(c_int, c_uint);
62 try testOne(c_long, c_ulong);
63 if (!builtin.cpu.arch.isSpirV()) try testOne(c_longlong, c_ulonglong);
64 }
65 fn testOne(comptime Signed: type, comptime Unsigned: type) !void {
66 var negative_one: Signed = undefined;
67 negative_one = -1;
68 const one = @abs(negative_one);
69 comptime assert(@TypeOf(one) == Unsigned);
70 try expect(one == 1);
71 }
72 };
73
74 try S.doTheTest();
75 try comptime S.doTheTest();
76}
77
78test "@abs unsigned integers" {
79 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
80 if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO
81
82 try comptime testAbsUnsignedIntegers();
83 try testAbsUnsignedIntegers();
84}
85
86fn testAbsUnsignedIntegers() !void {
87 {
88 var x: u32 = 1000;
89 _ = &x;
90 try expect(@abs(x) == 1000);
91 }
92 {
93 var x: u32 = 0;
94 _ = &x;
95 try expect(@abs(x) == 0);
96 }
97 {
98 var x: u32 = 1000;
99 _ = &x;
100 try expect(@abs(x) == 1000);
101 }
102 {
103 var x: u5 = 1;
104 _ = &x;
105 try expect(@abs(x) == 1);
106 }
107 {
108 var x: u5 = 5;
109 _ = &x;
110 try expect(@abs(x) == 5);
111 }
112 comptime {
113 try expect(@abs(@as(u2, 2)) == 2);
114 }
115}
116
117test "@abs unsigned C ABI integers" {
118 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO
119 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
120 if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO
121
122 const S = struct {
123 fn doTheTest() !void {
124 try testOne(usize);
125 try testOne(c_ushort);
126 try testOne(c_uint);
127 try testOne(c_ulong);
128 if (!builtin.cpu.arch.isSpirV()) try testOne(c_ulonglong);
129 }
130 fn testOne(comptime Unsigned: type) !void {
131 var one: Unsigned = undefined;
132 one = 1;
133 const still_one = @abs(one);
134 comptime assert(@TypeOf(still_one) == Unsigned);
135 try expect(still_one == 1);
136 }
137 };
138
139 try S.doTheTest();
140 try comptime S.doTheTest();
141}
142
143test "@abs big int <= 128 bits" {
144 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
145 if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO
146 if (builtin.zig_backend == .stage2_riscv64) return error.SkipZigTest; // TODO
147
148 try comptime testAbsSignedBigInt();
149 try testAbsSignedBigInt();
150
151 try comptime testAbsUnsignedBigInt();
152 try testAbsUnsignedBigInt();
153}
154
155fn abs(comptime T: type, a: T) @Int(.unsigned, @typeInfo(T).int.bits) {
156 return @abs(a);
157}
158
159fn testAbsSignedBigInt() !void {
160 try expect(abs(i65, -18446744073709551616) == 18446744073709551616);
161 try expect(abs(i65, 18446744073709551615) == 18446744073709551615);
162 try expect(abs(i65, 1234) == 1234);
163 try expect(abs(i65, -1234) == 1234);
164
165 try expect(abs(i84, -9671406556917033397649408) == 9671406556917033397649408);
166 try expect(abs(i84, 9671406556917033397649407) == 9671406556917033397649407);
167 try expect(abs(i84, 1234) == 1234);
168 try expect(abs(i84, -1234) == 1234);
169
170 try expect(abs(i96, -39614081257132168796771975168) == 39614081257132168796771975168);
171 try expect(abs(i96, 39614081257132168796771975167) == 39614081257132168796771975167);
172 try expect(abs(i96, 1234) == 1234);
173 try expect(abs(i96, -1234) == 1234);
174
175 try expect(abs(i105, -20282409603651670423947251286016) == 20282409603651670423947251286016);
176 try expect(abs(i105, 20282409603651670423947251286015) == 20282409603651670423947251286015);
177 try expect(abs(i105, 1234) == 1234);
178 try expect(abs(i105, -1234) == 1234);
179
180 try expect(abs(i128, -170141183460469231731687303715884105728) == 170141183460469231731687303715884105728);
181 try expect(abs(i128, 170141183460469231731687303715884105727) == 170141183460469231731687303715884105727);
182 try expect(abs(i128, 1234) == 1234);
183 try expect(abs(i128, -1234) == 1234);
184}
185
186fn testAbsUnsignedBigInt() !void {
187 try expect(abs(u65, 36893488147419103231) == 36893488147419103231);
188 try expect(abs(u65, 1234) == 1234);
189
190 try expect(abs(u84, 19342813113834066795298815) == 19342813113834066795298815);
191 try expect(abs(u84, 1234) == 1234);
192
193 try expect(abs(u96, 79228162514264337593543950335) == 79228162514264337593543950335);
194 try expect(abs(u96, 1234) == 1234);
195
196 try expect(abs(u105, 40564819207303340847894502572031) == 40564819207303340847894502572031);
197 try expect(abs(u105, 1234) == 1234);
198
199 try expect(abs(u128, 340282366920938463463374607431768211455) == 340282366920938463463374607431768211455);
200 try expect(abs(u128, 1234) == 1234);
201}
202
203test "@abs floats" {
204 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
205 if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO
206
207 try comptime testAbsFloats(f16);
208 if (builtin.zig_backend != .stage2_riscv64) try testAbsFloats(f16);
209 try comptime testAbsFloats(f32);
210 try testAbsFloats(f32);
211 try comptime testAbsFloats(f64);
212 try testAbsFloats(f64);
213 try comptime testAbsFloats(f80);
214 if (builtin.zig_backend != .stage2_spirv and builtin.zig_backend != .stage2_riscv64) try testAbsFloats(f80);
215 try comptime testAbsFloats(f128);
216 if (builtin.zig_backend != .stage2_spirv and builtin.zig_backend != .stage2_riscv64) try testAbsFloats(f128);
217}
218
219fn testAbsFloats(comptime T: type) !void {
220 {
221 var x: T = -2.62;
222 _ = &x;
223 try expect(@abs(x) == 2.62);
224 }
225 {
226 var x: T = 2.62;
227 _ = &x;
228 try expect(@abs(x) == 2.62);
229 }
230 {
231 var x: T = 0.0;
232 _ = &x;
233 try expect(@abs(x) == 0.0);
234 }
235 {
236 var x: T = -std.math.pi;
237 _ = &x;
238 try expect(@abs(x) == std.math.pi);
239 }
240
241 {
242 var x: T = -std.math.inf(T);
243 _ = &x;
244 try expect(@abs(x) == std.math.inf(T));
245 }
246 {
247 var x: T = std.math.inf(T);
248 _ = &x;
249 try expect(@abs(x) == std.math.inf(T));
250 }
251 comptime {
252 try expect(@abs(@as(T, -std.math.e)) == std.math.e);
253 }
254}
255
256test "@abs int vectors" {
257 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest;
258 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
259 if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO
260 if (builtin.zig_backend == .stage2_riscv64) return error.SkipZigTest;
261 if (builtin.zig_backend == .stage2_spirv) return error.SkipZigTest;
262
263 try comptime testAbsIntVectors(1);
264 try testAbsIntVectors(1);
265 try comptime testAbsIntVectors(2);
266 try testAbsIntVectors(2);
267 try comptime testAbsIntVectors(3);
268 try testAbsIntVectors(3);
269 try comptime testAbsIntVectors(4);
270 try testAbsIntVectors(4);
271 try comptime testAbsIntVectors(8);
272 try testAbsIntVectors(8);
273 try comptime testAbsIntVectors(16);
274 try testAbsIntVectors(16);
275 try comptime testAbsIntVectors(17);
276 try testAbsIntVectors(17);
277}
278
279fn testAbsIntVectors(comptime len: comptime_int) !void {
280 const I32 = @Vector(len, i32);
281 const U32 = @Vector(len, u32);
282 const I64 = @Vector(len, i64);
283 const U64 = @Vector(len, u64);
284 {
285 var x: I32 = @splat(-10);
286 var y: U32 = @splat(10);
287 _ = .{ &x, &y };
288 try expect(std.mem.eql(u32, &@as([len]u32, y), &@as([len]u32, @abs(x))));
289 }
290 {
291 var x: I32 = @splat(10);
292 var y: U32 = @splat(10);
293 _ = .{ &x, &y };
294 try expect(std.mem.eql(u32, &@as([len]u32, y), &@as([len]u32, @abs(x))));
295 }
296 {
297 var x: I32 = @splat(0);
298 var y: U32 = @splat(0);
299 _ = .{ &x, &y };
300 try expect(std.mem.eql(u32, &@as([len]u32, y), &@as([len]u32, @abs(x))));
301 }
302 {
303 var x: I64 = @splat(-10);
304 var y: U64 = @splat(10);
305 _ = .{ &x, &y };
306 try expect(std.mem.eql(u64, &@as([len]u64, y), &@as([len]u64, @abs(x))));
307 }
308 {
309 var x: I64 = @splat(std.math.minInt(i64));
310 var y: U64 = @splat(-std.math.minInt(i64));
311 _ = .{ &x, &y };
312 try expect(std.mem.eql(u64, &@as([len]u64, y), &@as([len]u64, @abs(x))));
313 }
314 {
315 var x = comptime std.simd.repeat(len, @Vector(4, i32){ -2, 5, std.math.minInt(i32), -7 });
316 var y = comptime std.simd.repeat(len, @Vector(4, u32){ 2, 5, -std.math.minInt(i32), 7 });
317 _ = .{ &x, &y };
318 try expect(std.mem.eql(u32, &@as([len]u32, y), &@as([len]u32, @abs(x))));
319 }
320}
321
322test "@abs unsigned int vectors" {
323 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest;
324 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
325 if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO
326 if (builtin.zig_backend == .stage2_riscv64) return error.SkipZigTest;
327 if (builtin.zig_backend == .stage2_spirv) return error.SkipZigTest;
328
329 try comptime testAbsUnsignedIntVectors(1);
330 try testAbsUnsignedIntVectors(1);
331 try comptime testAbsUnsignedIntVectors(2);
332 try testAbsUnsignedIntVectors(2);
333 try comptime testAbsUnsignedIntVectors(3);
334 try testAbsUnsignedIntVectors(3);
335 try comptime testAbsUnsignedIntVectors(4);
336 try testAbsUnsignedIntVectors(4);
337 try comptime testAbsUnsignedIntVectors(8);
338 try testAbsUnsignedIntVectors(8);
339 try comptime testAbsUnsignedIntVectors(16);
340 try testAbsUnsignedIntVectors(16);
341 try comptime testAbsUnsignedIntVectors(17);
342 try testAbsUnsignedIntVectors(17);
343}
344
345fn testAbsUnsignedIntVectors(comptime len: comptime_int) !void {
346 const U32 = @Vector(len, u32);
347 const U64 = @Vector(len, u64);
348 {
349 var x: U32 = @splat(10);
350 var y: U32 = @splat(10);
351 _ = .{ &x, &y };
352 try expect(std.mem.eql(u32, &@as([len]u32, y), &@as([len]u32, @abs(x))));
353 }
354 {
355 var x: U32 = @splat(10);
356 var y: U32 = @splat(10);
357 _ = .{ &x, &y };
358 try expect(std.mem.eql(u32, &@as([len]u32, y), &@as([len]u32, @abs(x))));
359 }
360 {
361 var x: U32 = @splat(0);
362 var y: U32 = @splat(0);
363 _ = .{ &x, &y };
364 try expect(std.mem.eql(u32, &@as([len]u32, y), &@as([len]u32, @abs(x))));
365 }
366 {
367 var x: U64 = @splat(10);
368 var y: U64 = @splat(10);
369 _ = .{ &x, &y };
370 try expect(std.mem.eql(u64, &@as([len]u64, y), &@as([len]u64, @abs(x))));
371 }
372 {
373 var x = comptime std.simd.repeat(len, @Vector(3, u32){ 2, 5, 7 });
374 var y = comptime std.simd.repeat(len, @Vector(3, u32){ 2, 5, 7 });
375 _ = .{ &x, &y };
376 try expect(std.mem.eql(u32, &@as([len]u32, y), &@as([len]u32, @abs(x))));
377 }
378}
379
380test "@abs float vectors" {
381 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest;
382 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
383 if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO
384 if (builtin.zig_backend == .stage2_spirv) return error.SkipZigTest;
385 if (builtin.zig_backend == .stage2_riscv64) return error.SkipZigTest;
386
387 @setEvalBranchQuota(2000);
388 try comptime testAbsFloatVectors(f16, 1);
389 try testAbsFloatVectors(f16, 1);
390 try comptime testAbsFloatVectors(f16, 2);
391 try testAbsFloatVectors(f16, 2);
392 try comptime testAbsFloatVectors(f16, 3);
393 try testAbsFloatVectors(f16, 3);
394 try comptime testAbsFloatVectors(f16, 4);
395 try testAbsFloatVectors(f16, 4);
396 try comptime testAbsFloatVectors(f16, 8);
397 try testAbsFloatVectors(f16, 8);
398 try comptime testAbsFloatVectors(f16, 16);
399 try testAbsFloatVectors(f16, 16);
400 try comptime testAbsFloatVectors(f16, 17);
401
402 try testAbsFloatVectors(f32, 1);
403 try comptime testAbsFloatVectors(f32, 1);
404 try testAbsFloatVectors(f32, 1);
405 try comptime testAbsFloatVectors(f32, 2);
406 try testAbsFloatVectors(f32, 2);
407 try comptime testAbsFloatVectors(f32, 3);
408 try testAbsFloatVectors(f32, 3);
409 try comptime testAbsFloatVectors(f32, 4);
410 try testAbsFloatVectors(f32, 4);
411 try comptime testAbsFloatVectors(f32, 8);
412 try testAbsFloatVectors(f32, 8);
413 try comptime testAbsFloatVectors(f32, 16);
414 try testAbsFloatVectors(f32, 16);
415 try comptime testAbsFloatVectors(f32, 17);
416 try testAbsFloatVectors(f32, 17);
417
418 try comptime testAbsFloatVectors(f64, 1);
419 try testAbsFloatVectors(f64, 1);
420 try comptime testAbsFloatVectors(f64, 2);
421 try testAbsFloatVectors(f64, 2);
422 try comptime testAbsFloatVectors(f64, 3);
423 try testAbsFloatVectors(f64, 3);
424 try comptime testAbsFloatVectors(f64, 4);
425 try testAbsFloatVectors(f64, 4);
426 try comptime testAbsFloatVectors(f64, 8);
427 try testAbsFloatVectors(f64, 8);
428 try comptime testAbsFloatVectors(f64, 16);
429 try testAbsFloatVectors(f64, 16);
430 try comptime testAbsFloatVectors(f64, 17);
431 try testAbsFloatVectors(f64, 17);
432
433 try comptime testAbsFloatVectors(f80, 1);
434 try testAbsFloatVectors(f80, 1);
435 try comptime testAbsFloatVectors(f80, 2);
436 try testAbsFloatVectors(f80, 2);
437 try comptime testAbsFloatVectors(f80, 3);
438 try testAbsFloatVectors(f80, 3);
439 try comptime testAbsFloatVectors(f80, 4);
440 try testAbsFloatVectors(f80, 4);
441 try comptime testAbsFloatVectors(f80, 8);
442 try testAbsFloatVectors(f80, 8);
443 try comptime testAbsFloatVectors(f80, 16);
444 try testAbsFloatVectors(f80, 16);
445 try comptime testAbsFloatVectors(f80, 17);
446 try testAbsFloatVectors(f80, 17);
447
448 try comptime testAbsFloatVectors(f128, 1);
449 try testAbsFloatVectors(f128, 1);
450 try comptime testAbsFloatVectors(f128, 2);
451 try testAbsFloatVectors(f128, 2);
452 try comptime testAbsFloatVectors(f128, 3);
453 try testAbsFloatVectors(f128, 3);
454 try comptime testAbsFloatVectors(f128, 4);
455 try testAbsFloatVectors(f128, 4);
456 try comptime testAbsFloatVectors(f128, 8);
457 try testAbsFloatVectors(f128, 8);
458 try comptime testAbsFloatVectors(f128, 16);
459 try testAbsFloatVectors(f128, 16);
460 try comptime testAbsFloatVectors(f128, 17);
461 try testAbsFloatVectors(f128, 17);
462}
463
464fn testAbsFloatVectors(comptime T: type, comptime len: comptime_int) !void {
465 const V = @Vector(len, T);
466 {
467 var x: V = @splat(-7.5);
468 var y: V = @splat(7.5);
469 _ = .{ &x, &y };
470 try expect(std.mem.eql(T, &@as([len]T, y), &@as([len]T, @abs(x))));
471 }
472 {
473 var x: V = @splat(7.5);
474 var y: V = @splat(7.5);
475 _ = .{ &x, &y };
476 try expect(std.mem.eql(T, &@as([len]T, y), &@as([len]T, @abs(x))));
477 }
478 {
479 var x: V = @splat(0.0);
480 var y: V = @splat(0.0);
481 _ = .{ &x, &y };
482 try expect(std.mem.eql(T, &@as([len]T, y), &@as([len]T, @abs(x))));
483 }
484 {
485 var x: V = @splat(-std.math.pi);
486 var y: V = @splat(std.math.pi);
487 _ = .{ &x, &y };
488 try expect(std.mem.eql(T, &@as([len]T, y), &@as([len]T, @abs(x))));
489 }
490 {
491 var x: V = @splat(std.math.pi);
492 var y: V = @splat(std.math.pi);
493 _ = .{ &x, &y };
494 try expect(std.mem.eql(T, &@as([len]T, y), &@as([len]T, @abs(x))));
495 }
496}