aboutsummaryrefslogtreecommitdiff
path: root/test/behavior/int128.zig
blob: 2413ab65ede6ac4e81a831f7da0ee9c241fbee45 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
const std = @import("std");
const expect = std.testing.expect;
const maxInt = std.math.maxInt;
const minInt = std.math.minInt;
const builtin = @import("builtin");

test "uint128" {
    if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO
    if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
    if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO
    if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest;

    var buff: u128 = maxInt(u128);
    try expect(buff == maxInt(u128));

    const magic_const = 0x12341234123412341234123412341234;
    buff = magic_const;

    try expect(buff == magic_const);
    try expect(magic_const == 0x12341234123412341234123412341234);

    buff = 0;
    try expect(buff == @as(u128, 0));
}

test "undefined 128 bit int" {
    if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO
    if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
    if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO
    if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest;
    if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest;

    @setRuntimeSafety(true);

    // TODO implement @setRuntimeSafety
    if (builtin.mode != .Debug and builtin.mode != .ReleaseSafe) {
        return error.SkipZigTest;
    }

    var undef: u128 = undefined;
    var undef_signed: i128 = undefined;
    try expect(undef == 0xaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa and @as(u128, @bitCast(undef_signed)) == undef);
}

test "int128" {
    if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO
    if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
    if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO
    if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest;

    var buff: i128 = -1;
    try expect(buff < 0 and (buff + 1) == 0);
    try expect(@as(i8, @intCast(buff)) == @as(i8, -1));

    buff = minInt(i128);
    try expect(buff < 0);

    buff = -0x12341234123412341234123412341234;
    try expect(-buff == 0x12341234123412341234123412341234);

    const a: i128 = -170141183460469231731687303715884105728;
    const b: i128 = -0x8000_0000_0000_0000_0000_0000_0000_0000;
    try expect(@divFloor(b, 1_000_000) == -170141183460469231731687303715885);
    try expect(a == b);
}

test "truncate int128" {
    if (builtin.zig_backend == .stage2_wasm) return error.SkipZigTest; // TODO
    if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest; // TODO
    if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO
    if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
    if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO
    if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest;

    {
        var buff: u128 = maxInt(u128);
        try expect(@as(u64, @truncate(buff)) == maxInt(u64));
        try expect(@as(u90, @truncate(buff)) == maxInt(u90));
        try expect(@as(u128, @truncate(buff)) == maxInt(u128));
    }

    {
        var buff: i128 = maxInt(i128);
        try expect(@as(i64, @truncate(buff)) == -1);
        try expect(@as(i90, @truncate(buff)) == -1);
        try expect(@as(i128, @truncate(buff)) == maxInt(i128));
    }
}

test "shift int128" {
    if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO
    if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
    if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO
    if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest;

    const types = .{ u128, i128 };
    inline for (types) |t| {
        try testShlTrunc(t, 0x8, 123);
        try comptime testShlTrunc(t, 0x8, 123);

        try testShlTrunc(t, 0x40000000_00000000, 64);
        try comptime testShlTrunc(t, 0x40000000_00000000, 64);

        try testShlTrunc(t, 0x01000000_00000000_00000000, 38);
        try comptime testShlTrunc(t, 0x01000000_00000000_00000000, 38);

        try testShlTrunc(t, 0x00000008_00000000_00000000_00000000, 27);
        try comptime testShlTrunc(t, 0x00000008_00000000_00000000_00000000, 27);
    }
}

fn testShlTrunc(comptime Type: type, x: Type, rhs: u7) !void {
    const shifted = x << rhs;
    try expect(shifted == @as(Type, 0x40000000_00000000_00000000_00000000));
}