master
  1const std = @import("std");
  2const expect = std.testing.expect;
  3const maxInt = std.math.maxInt;
  4const minInt = std.math.minInt;
  5const builtin = @import("builtin");
  6
  7test "uint128" {
  8    if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
  9    if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO
 10    if (builtin.zig_backend == .stage2_spirv) return error.SkipZigTest;
 11    if (builtin.zig_backend == .stage2_riscv64) return error.SkipZigTest;
 12
 13    var buff: u128 = maxInt(u128);
 14    try expect(buff == maxInt(u128));
 15
 16    const magic_const = 0x12341234123412341234123412341234;
 17    buff = magic_const;
 18
 19    try expect(buff == magic_const);
 20    try expect(magic_const == 0x12341234123412341234123412341234);
 21
 22    buff = 0;
 23    try expect(buff == @as(u128, 0));
 24}
 25
 26test "undefined 128 bit int" {
 27    if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
 28    if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO
 29    if (builtin.zig_backend == .stage2_spirv) return error.SkipZigTest;
 30    if (builtin.zig_backend == .stage2_riscv64) return error.SkipZigTest;
 31
 32    @setRuntimeSafety(true);
 33
 34    // TODO implement @setRuntimeSafety
 35    if (builtin.mode != .Debug and builtin.mode != .ReleaseSafe) {
 36        return error.SkipZigTest;
 37    }
 38
 39    var undef: u128 = undefined;
 40    var undef_signed: i128 = undefined;
 41    _ = .{ &undef, &undef_signed };
 42    try expect(undef == 0xaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa and @as(u128, @bitCast(undef_signed)) == undef);
 43}
 44
 45test "int128" {
 46    if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
 47    if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO
 48    if (builtin.zig_backend == .stage2_spirv) return error.SkipZigTest;
 49    if (builtin.zig_backend == .stage2_riscv64) return error.SkipZigTest;
 50
 51    var buff: i128 = -1;
 52    try expect(buff < 0 and (buff + 1) == 0);
 53    try expect(@as(i8, @intCast(buff)) == @as(i8, -1));
 54
 55    buff = minInt(i128);
 56    try expect(buff < 0);
 57
 58    buff = -0x12341234123412341234123412341234;
 59    try expect(-buff == 0x12341234123412341234123412341234);
 60
 61    const a: i128 = -170141183460469231731687303715884105728;
 62    const b: i128 = -0x8000_0000_0000_0000_0000_0000_0000_0000;
 63    try expect(@divFloor(b, 1_000_000) == -170141183460469231731687303715885);
 64    try expect(a == b);
 65}
 66
 67test "truncate int128" {
 68    if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
 69    if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO
 70    if (builtin.zig_backend == .stage2_spirv) return error.SkipZigTest;
 71    if (builtin.zig_backend == .stage2_riscv64) return error.SkipZigTest;
 72
 73    {
 74        var buff: u128 = maxInt(u128);
 75        _ = &buff;
 76        try expect(@as(u64, @truncate(buff)) == maxInt(u64));
 77        try expect(@as(u90, @truncate(buff)) == maxInt(u90));
 78        try expect(@as(u128, @truncate(buff)) == maxInt(u128));
 79    }
 80
 81    {
 82        var buff: i128 = maxInt(i128);
 83        _ = &buff;
 84        try expect(@as(i64, @truncate(buff)) == -1);
 85        try expect(@as(i90, @truncate(buff)) == -1);
 86        try expect(@as(i128, @truncate(buff)) == maxInt(i128));
 87    }
 88}
 89
 90test "shift int128" {
 91    if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
 92    if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO
 93    if (builtin.zig_backend == .stage2_spirv) return error.SkipZigTest;
 94    if (builtin.zig_backend == .stage2_riscv64) return error.SkipZigTest;
 95
 96    const types = .{ u128, i128 };
 97    inline for (types) |t| {
 98        try testShlTrunc(t, 0x8, 123);
 99        try comptime testShlTrunc(t, 0x8, 123);
100
101        try testShlTrunc(t, 0x40000000_00000000, 64);
102        try comptime testShlTrunc(t, 0x40000000_00000000, 64);
103
104        try testShlTrunc(t, 0x01000000_00000000_00000000, 38);
105        try comptime testShlTrunc(t, 0x01000000_00000000_00000000, 38);
106
107        try testShlTrunc(t, 0x00000008_00000000_00000000_00000000, 27);
108        try comptime testShlTrunc(t, 0x00000008_00000000_00000000_00000000, 27);
109    }
110}
111
112fn testShlTrunc(comptime Type: type, x: Type, rhs: u7) !void {
113    const shifted = x << rhs;
114    try expect(shifted == @as(Type, 0x40000000_00000000_00000000_00000000));
115}