╭ wrapping_arithmetic.zig ──────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭ find file ─ ─ ─ ─ ─ ─ ─ ─ ─ ─╮ ╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │ │ │http│ ssh │ │ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─╯ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
.. conflicting_externs/ hasdecl/ import/ namespace_depends_on_compile_var/ pub_enum/ x86_64/ zon/ abs.zig addrspace_and_linksection.zig align.zig alignof.zig array.zig asm.zig atomics.zig backing_int.zig basic.zig bit_shifting.zig bitcast.zig bitreverse.zig bool.zig builtin_functions_returning_void_ byteswap.zig byval_arg_var.zig call.zig cast.zig cast_int.zig comptime_memory.zig const_slice_child.zig decl_literals.zig decltest.zig defer.zig destructure.zig duplicated_test_names.zig enum.zig error.zig eval.zig export_builtin.zig export_c_keywords.zig export_keyword.zig export_self_referential_type_info extern.zig extern_struct_zero_size_fields.zi field_parent_ptr.zig file_to_embed.txt floatop.zig fn.zig fn_delegation.zig fn_in_struct_in_comptime.zig for.zig generics.zig globals.zig hasdecl.zig hasfield.zig if.zig import.zig import_c_keywords.zig incomplete_struct_param_tld.zig inline_switch.zig int128.zig int_comparison_elision.zig ir_block_deps.zig lower_strlit_to_vector.zig math.zig maximum_minimum.zig member_func.zig memcpy.zig memmove.zig memset.zig merge_error_sets.zig muladd.zig multiple_externs_with_conflicting namespace_depends_on_compile_var. nan.zig null.zig optional.zig overlapping_assign.zig packed-struct.zig packed-union.zig packed_struct_explicit_backing_in pointers.zig popcount.zig prefetch.zig ptrcast.zig ptrfromint.zig pub_enum.zig ref_var_in_if_after_if_2nd_switch reflection.zig return_address.zig saturating_arithmetic.zig select.zig shuffle.zig sizeof_and_typeof.zig slice.zig slice_sentinel_comptime.zig spirv.zig splat.zig src.zig string_literals.zig struct.zig struct_contains_null_ptr_itself.z struct_contains_slice_of_itself.z switch.zig switch_loop.zig switch_on_captured_error.zig switch_prong_err_enum.zig switch_prong_implicit_cast.zig this.zig threadlocal.zig truncate.zig try.zig tuple.zig tuple_declarations.zig type.zig type_info.zig typename.zig undefined.zig underscore.zig union.zig var_args.zig vector.zig void.zig wasm.zig while.zig widening.zig ╭─────────────────────────────────╮ │wrapping_arithmetic.zig │ ╰─────────────────────────────────╯ x86_64.zig zero.bin zon.zig
1 const std = @import("std"); 2 const builtin = @import("builtin"); 3 const minInt = std.math.minInt; 4 const maxInt = std.math.maxInt; 5 const expect = std.testing.expect; 6 7 test "wrapping add" { 8 if (builtin.zig_backend == .stage2_riscv64) return error.SkipZigTest; 9 10 const S = struct { 11 fn doTheTest() !void { 12 try testWrapAdd(i8, -3, 10, 7); 13 try testWrapAdd(i8, -128, -128, 0); 14 try testWrapAdd(i2, 1, 1, -2); 15 try testWrapAdd(i64, maxInt(i64), 1, minInt(i64)); 16 try testWrapAdd(i128, maxInt(i128), -maxInt(i128), 0); 17 try testWrapAdd(i128, minInt(i128), maxInt(i128), -1); 18 try testWrapAdd(i8, 127, 127, -2); 19 try testWrapAdd(u8, 3, 10, 13); 20 try testWrapAdd(u8, 255, 255, 254); 21 try testWrapAdd(u2, 3, 2, 1); 22 try testWrapAdd(u3, 7, 1, 0); 23 try testWrapAdd(u128, maxInt(u128), 1, minInt(u128)); 24 } 25 26 fn testWrapAdd(comptime T: type, lhs: T, rhs: T, expected: T) !void { 27 try expect((lhs +% rhs) == expected); 28 29 var x = lhs; 30 x +%= rhs; 31 try expect(x == expected); 32 } 33 }; 34 35 try S.doTheTest(); 36 try comptime S.doTheTest(); 37 38 try comptime S.testWrapAdd(comptime_int, 0, 0, 0); 39 try comptime S.testWrapAdd(comptime_int, 3, 2, 5); 40 try comptime S.testWrapAdd(comptime_int, 651075816498665588400716961808225370057, 468229432685078038144554201546849378455, 1119305249183743626545271163355074748512); 41 try comptime S.testWrapAdd(comptime_int, 7, -593423721213448152027139550640105366508, -593423721213448152027139550640105366501); 42 } 43 44 test "wrapping subtraction" { 45 if (builtin.zig_backend == .stage2_riscv64) return error.SkipZigTest; 46 47 const S = struct { 48 fn doTheTest() !void { 49 try testWrapSub(i8, -3, 10, -13); 50 try testWrapSub(i8, -128, -128, 0); 51 try testWrapSub(i8, -1, 127, -128); 52 try testWrapSub(i64, minInt(i64), 1, maxInt(i64)); 53 try testWrapSub(i128, maxInt(i128), -1, minInt(i128)); 54 try testWrapSub(i128, minInt(i128), -maxInt(i128), -1); 55 try testWrapSub(u8, 10, 3, 7); 56 try testWrapSub(u8, 0, 255, 1); 57 try testWrapSub(u5, 0, 31, 1); 58 try testWrapSub(u128, 0, maxInt(u128), 1); 59 } 60 61 fn testWrapSub(comptime T: type, lhs: T, rhs: T, expected: T) !void { 62 try expect((lhs -% rhs) == expected); 63 64 var x = lhs; 65 x -%= rhs; 66 try expect(x == expected); 67 } 68 }; 69 70 try S.doTheTest(); 71 try comptime S.doTheTest(); 72 73 try comptime S.testWrapSub(comptime_int, 0, 0, 0); 74 try comptime S.testWrapSub(comptime_int, 3, 2, 1); 75 try comptime S.testWrapSub(comptime_int, 651075816498665588400716961808225370057, 468229432685078038144554201546849378455, 182846383813587550256162760261375991602); 76 try comptime S.testWrapSub(comptime_int, 7, -593423721213448152027139550640105366508, 593423721213448152027139550640105366515); 77 } 78 79 test "wrapping multiplication" { 80 if (builtin.zig_backend == .stage2_riscv64) return error.SkipZigTest; 81 82 const S = struct { 83 fn doTheTest() !void { 84 try testWrapMul(i8, -3, 10, -30); 85 try testWrapMul(i4, 2, 4, -8); 86 try testWrapMul(i8, 2, 127, -2); 87 try testWrapMul(i8, -128, -128, 0); 88 try testWrapMul(i8, maxInt(i8), maxInt(i8), 1); 89 try testWrapMul(i16, maxInt(i16), -1, minInt(i16) + 1); 90 try testWrapMul(i128, maxInt(i128), -1, minInt(i128) + 1); 91 try testWrapMul(i128, minInt(i128), -1, minInt(i128)); 92 try testWrapMul(u8, 10, 3, 30); 93 try testWrapMul(u8, 2, 255, 254); 94 try testWrapMul(u128, maxInt(u128), maxInt(u128), 1); 95 } 96 97 fn testWrapMul(comptime T: type, lhs: T, rhs: T, expected: T) !void { 98 try expect((lhs *% rhs) == expected); 99 100 var x = lhs; 101 x *%= rhs; 102 try expect(x == expected); 103 } 104 }; 105 106 try S.doTheTest(); 107 try comptime S.doTheTest(); 108 109 try comptime S.testWrapMul(comptime_int, 0, 0, 0); 110 try comptime S.testWrapMul(comptime_int, 3, 2, 6); 111 try comptime S.testWrapMul(comptime_int, 651075816498665588400716961808225370057, 468229432685078038144554201546849378455, 304852860194144160265083087140337419215516305999637969803722975979232817921935); 112 try comptime S.testWrapMul(comptime_int, 7, -593423721213448152027139550640105366508, -4153966048494137064189976854480737565556); 113 } 114