compiler_rt: add __cmpXi2 and __ucmpXi2

- adds __cmpsi2, __cmpdi2, __cmpti2
- adds __ucmpsi2, __ucmpdi2, __ucmpti2
- use 2 if statements with 2 temporaries and a constant
- tests: MIN, MIN+1, MIN/2, -1, 0, 1, MAX/2, MAX-1, MAX if applicable

See #1290
This commit is contained in:
Jan Philipp Hafer 2021-12-12 21:19:25 +01:00 committed by Andrew Kelley
parent 0550198c98
commit 20328e976f
9 changed files with 521 additions and 1 deletions

View File

@ -448,6 +448,7 @@ set(ZIG_STAGE2_SOURCES
"${CMAKE_SOURCE_DIR}/lib/std/special/compiler_rt/atomics.zig"
"${CMAKE_SOURCE_DIR}/lib/std/special/compiler_rt/bswap.zig"
"${CMAKE_SOURCE_DIR}/lib/std/special/compiler_rt/clear_cache.zig"
"${CMAKE_SOURCE_DIR}/lib/std/special/compiler_rt/cmp.zig"
"${CMAKE_SOURCE_DIR}/lib/std/special/compiler_rt/compareXf2.zig"
"${CMAKE_SOURCE_DIR}/lib/std/special/compiler_rt/count0bits.zig"
"${CMAKE_SOURCE_DIR}/lib/std/special/compiler_rt/divdf3.zig"

View File

@ -421,10 +421,22 @@ comptime {
// missing: Integral arithmetic which returns if overflow
// missing: Integral comparison
// Integral comparison
// (a < b) => 0
// (a == b) => 1
// (a > b) => 2
const __cmpsi2 = @import("compiler_rt/cmp.zig").__cmpsi2;
@export(__cmpsi2, .{ .name = "__cmpsi2", .linkage = linkage });
const __cmpdi2 = @import("compiler_rt/cmp.zig").__cmpdi2;
@export(__cmpdi2, .{ .name = "__cmpdi2", .linkage = linkage });
const __cmpti2 = @import("compiler_rt/cmp.zig").__cmpti2;
@export(__cmpti2, .{ .name = "__cmpti2", .linkage = linkage });
const __ucmpsi2 = @import("compiler_rt/cmp.zig").__ucmpsi2;
@export(__ucmpsi2, .{ .name = "__ucmpsi2", .linkage = linkage });
const __ucmpdi2 = @import("compiler_rt/cmp.zig").__ucmpdi2;
@export(__ucmpdi2, .{ .name = "__ucmpdi2", .linkage = linkage });
const __ucmpti2 = @import("compiler_rt/cmp.zig").__ucmpti2;
@export(__ucmpti2, .{ .name = "__ucmpti2", .linkage = linkage });
// missing: Floating point raised to integer power

View File

@ -0,0 +1,45 @@
const std = @import("std");
const builtin = @import("builtin");
// cmp - signed compare
// - cmpXi2_generic for unoptimized little and big endian
// ucmp - unsigned compare
// - ucmpXi2_generic for unoptimized little and big endian
// a < b => 0
// a == b => 1
// a > b => 2
fn XcmpXi2_generic(comptime T: type) fn (a: T, b: T) callconv(.C) i32 {
return struct {
fn f(a: T, b: T) callconv(.C) i32 {
@setRuntimeSafety(builtin.is_test);
var cmp1: i32 = 0;
var cmp2: i32 = 0;
if (a > b)
cmp1 = 1;
if (a < b)
cmp2 = 1;
return cmp1 - cmp2 + 1;
}
}.f;
}
pub const __cmpsi2 = XcmpXi2_generic(i32);
pub const __cmpdi2 = XcmpXi2_generic(i64);
pub const __cmpti2 = XcmpXi2_generic(i128);
pub const __ucmpsi2 = XcmpXi2_generic(u32);
pub const __ucmpdi2 = XcmpXi2_generic(u64);
pub const __ucmpti2 = XcmpXi2_generic(u128);
test {
_ = @import("cmpsi2_test.zig");
_ = @import("cmpdi2_test.zig");
_ = @import("cmpti2_test.zig");
_ = @import("ucmpsi2_test.zig");
_ = @import("ucmpdi2_test.zig");
_ = @import("ucmpti2_test.zig");
}

View File

@ -0,0 +1,105 @@
const cmp = @import("cmp.zig");
const testing = @import("std").testing;
fn test__cmpdi2(a: i64, b: i64, expected: i64) !void {
var result = cmp.__cmpdi2(a, b);
try testing.expectEqual(expected, result);
}
test "cmpdi2" {
// minInt == -9223372036854775808
// maxInt == 9223372036854775807
// minInt/2 == -4611686018427387904
// maxInt/2 == 4611686018427387903
// 1. equality minInt, minInt+1, minInt/2, 0, maxInt/2, maxInt-1, maxInt
try test__cmpdi2(-9223372036854775808, -9223372036854775808, 1);
try test__cmpdi2(-9223372036854775807, -9223372036854775807, 1);
try test__cmpdi2(-4611686018427387904, -4611686018427387904, 1);
try test__cmpdi2(-1, -1, 1);
try test__cmpdi2(0, 0, 1);
try test__cmpdi2(1, 1, 1);
try test__cmpdi2(4611686018427387903, 4611686018427387903, 1);
try test__cmpdi2(9223372036854775806, 9223372036854775806, 1);
try test__cmpdi2(9223372036854775807, 9223372036854775807, 1);
// 2. cmp minInt, { minInt + 1, minInt/2, -1, 0, 1, maxInt/2, maxInt-1, maxInt}
try test__cmpdi2(-9223372036854775808, -9223372036854775807, 0);
try test__cmpdi2(-9223372036854775808, -4611686018427387904, 0);
try test__cmpdi2(-9223372036854775808, -1, 0);
try test__cmpdi2(-9223372036854775808, 0, 0);
try test__cmpdi2(-9223372036854775808, 1, 0);
try test__cmpdi2(-9223372036854775808, 4611686018427387903, 0);
try test__cmpdi2(-9223372036854775808, 9223372036854775806, 0);
try test__cmpdi2(-9223372036854775808, 9223372036854775807, 0);
// 3. cmp minInt+1, {minInt, minInt/2, -1,0,1, maxInt/2, maxInt-1, maxInt}
try test__cmpdi2(-9223372036854775807, -9223372036854775808, 2);
try test__cmpdi2(-9223372036854775807, -4611686018427387904, 0);
try test__cmpdi2(-9223372036854775807, -1, 0);
try test__cmpdi2(-9223372036854775807, 0, 0);
try test__cmpdi2(-9223372036854775807, 1, 0);
try test__cmpdi2(-9223372036854775807, 4611686018427387903, 0);
try test__cmpdi2(-9223372036854775807, 9223372036854775806, 0);
try test__cmpdi2(-9223372036854775807, 9223372036854775807, 0);
// 4. cmp minInt/2, {minInt, minInt + 1, -1,0,1, maxInt/2, maxInt-1, maxInt}
try test__cmpdi2(-4611686018427387904, -9223372036854775808, 2);
try test__cmpdi2(-4611686018427387904, -9223372036854775807, 2);
try test__cmpdi2(-4611686018427387904, -1, 0);
try test__cmpdi2(-4611686018427387904, 0, 0);
try test__cmpdi2(-4611686018427387904, 1, 0);
try test__cmpdi2(-4611686018427387904, 4611686018427387903, 0);
try test__cmpdi2(-4611686018427387904, 9223372036854775806, 0);
try test__cmpdi2(-4611686018427387904, 9223372036854775807, 0);
// 5. cmp -1, {minInt, minInt + 1, minInt/2, 0,1, maxInt/2, maxInt-1, maxInt}
try test__cmpdi2(-1, -9223372036854775808, 2);
try test__cmpdi2(-1, -9223372036854775807, 2);
try test__cmpdi2(-1, -4611686018427387904, 2);
try test__cmpdi2(-1, 0, 0);
try test__cmpdi2(-1, 1, 0);
try test__cmpdi2(-1, 4611686018427387903, 0);
try test__cmpdi2(-1, 9223372036854775806, 0);
try test__cmpdi2(-1, 9223372036854775807, 0);
// 6. cmp 0, {minInt, minInt + 1, minInt/2, -1, 1, maxInt/2, maxInt-1, maxInt}
try test__cmpdi2(0, -9223372036854775808, 2);
try test__cmpdi2(0, -9223372036854775807, 2);
try test__cmpdi2(0, -4611686018427387904, 2);
try test__cmpdi2(0, -1, 2);
try test__cmpdi2(0, 1, 0);
try test__cmpdi2(0, 4611686018427387903, 0);
try test__cmpdi2(0, 9223372036854775806, 0);
try test__cmpdi2(0, 9223372036854775807, 0);
// 7. cmp 1, {minInt, minInt + 1, minInt/2, -1,0, maxInt/2, maxInt-1, maxInt}
try test__cmpdi2(1, -9223372036854775808, 2);
try test__cmpdi2(1, -9223372036854775807, 2);
try test__cmpdi2(1, -4611686018427387904, 2);
try test__cmpdi2(1, -1, 2);
try test__cmpdi2(1, 0, 2);
try test__cmpdi2(1, 4611686018427387903, 0);
try test__cmpdi2(1, 9223372036854775806, 0);
try test__cmpdi2(1, 9223372036854775807, 0);
// 8. cmp maxInt/2, {minInt, minInt + 1, minInt/2, -1,0,1, maxInt-1, maxInt}
try test__cmpdi2(4611686018427387903, -9223372036854775808, 2);
try test__cmpdi2(4611686018427387903, -9223372036854775807, 2);
try test__cmpdi2(4611686018427387903, -4611686018427387904, 2);
try test__cmpdi2(4611686018427387903, -1, 2);
try test__cmpdi2(4611686018427387903, 0, 2);
try test__cmpdi2(4611686018427387903, 1, 2);
try test__cmpdi2(4611686018427387903, 9223372036854775806, 0);
try test__cmpdi2(4611686018427387903, 9223372036854775807, 0);
// 9. cmp maxInt-1, {minInt, minInt + 1, minInt/2, -1,0,1, maxInt/2, maxInt}
try test__cmpdi2(9223372036854775806, -9223372036854775808, 2);
try test__cmpdi2(9223372036854775806, -9223372036854775807, 2);
try test__cmpdi2(9223372036854775806, -4611686018427387904, 2);
try test__cmpdi2(9223372036854775806, -1, 2);
try test__cmpdi2(9223372036854775806, 0, 2);
try test__cmpdi2(9223372036854775806, 1, 2);
try test__cmpdi2(9223372036854775806, 4611686018427387903, 2);
try test__cmpdi2(9223372036854775806, 9223372036854775807, 0);
// 10.cmp maxInt, {minInt, minInt + 1, minInt/2, -1,0,1, maxInt/2, maxInt-1, }
try test__cmpdi2(9223372036854775807, -9223372036854775808, 2);
try test__cmpdi2(9223372036854775807, -9223372036854775807, 2);
try test__cmpdi2(9223372036854775807, -4611686018427387904, 2);
try test__cmpdi2(9223372036854775807, -1, 2);
try test__cmpdi2(9223372036854775807, 0, 2);
try test__cmpdi2(9223372036854775807, 1, 2);
try test__cmpdi2(9223372036854775807, 4611686018427387903, 2);
try test__cmpdi2(9223372036854775807, 9223372036854775806, 2);
}

View File

@ -0,0 +1,105 @@
const cmp = @import("cmp.zig");
const testing = @import("std").testing;
fn test__cmpsi2(a: i32, b: i32, expected: i32) !void {
var result = cmp.__cmpsi2(a, b);
try testing.expectEqual(expected, result);
}
test "cmpsi2" {
// minInt == -2147483648
// maxInt == 2147483647
// minInt/2 == -1073741824
// maxInt/2 == 1073741823
// 1. equality minInt, minInt+1, minInt/2, -1, 0, 1, maxInt/2, maxInt-1, maxInt
try test__cmpsi2(-2147483648, -2147483648, 1);
try test__cmpsi2(-2147483647, -2147483647, 1);
try test__cmpsi2(-1073741824, -1073741824, 1);
try test__cmpsi2(-1, -1, 1);
try test__cmpsi2(0, 0, 1);
try test__cmpsi2(1, 1, 1);
try test__cmpsi2(1073741823, 1073741823, 1);
try test__cmpsi2(2147483646, 2147483646, 1);
try test__cmpsi2(2147483647, 2147483647, 1);
// 2. cmp minInt, { minInt + 1, minInt/2, -1,0,1, maxInt/2, maxInt-1, maxInt}
try test__cmpsi2(-2147483648, -2147483647, 0);
try test__cmpsi2(-2147483648, -1073741824, 0);
try test__cmpsi2(-2147483648, -1, 0);
try test__cmpsi2(-2147483648, 0, 0);
try test__cmpsi2(-2147483648, 1, 0);
try test__cmpsi2(-2147483648, 1073741823, 0);
try test__cmpsi2(-2147483648, 2147483646, 0);
try test__cmpsi2(-2147483648, 2147483647, 0);
// 3. cmp minInt+1, {minInt, minInt/2, -1,0,1, maxInt/2, maxInt-1, maxInt}
try test__cmpsi2(-2147483647, -2147483648, 2);
try test__cmpsi2(-2147483647, -1073741824, 0);
try test__cmpsi2(-2147483647, -1, 0);
try test__cmpsi2(-2147483647, 0, 0);
try test__cmpsi2(-2147483647, 1, 0);
try test__cmpsi2(-2147483647, 1073741823, 0);
try test__cmpsi2(-2147483647, 2147483646, 0);
try test__cmpsi2(-2147483647, 2147483647, 0);
// 4. cmp minInt/2, {minInt, minInt + 1, -1,0,1, maxInt/2, maxInt-1, maxInt}
try test__cmpsi2(-1073741824, -2147483648, 2);
try test__cmpsi2(-1073741824, -2147483647, 2);
try test__cmpsi2(-1073741824, -1, 0);
try test__cmpsi2(-1073741824, 0, 0);
try test__cmpsi2(-1073741824, 1, 0);
try test__cmpsi2(-1073741824, 1073741823, 0);
try test__cmpsi2(-1073741824, 2147483646, 0);
try test__cmpsi2(-1073741824, 2147483647, 0);
// 5. cmp -1, {minInt, minInt + 1, minInt/2, 0,1, maxInt/2, maxInt-1, maxInt}
try test__cmpsi2(-1, -2147483648, 2);
try test__cmpsi2(-1, -2147483647, 2);
try test__cmpsi2(-1, -1073741824, 2);
try test__cmpsi2(-1, 0, 0);
try test__cmpsi2(-1, 1, 0);
try test__cmpsi2(-1, 1073741823, 0);
try test__cmpsi2(-1, 2147483646, 0);
try test__cmpsi2(-1, 2147483647, 0);
// 6. cmp 0, {minInt, minInt + 1, minInt/2, -1, 1, maxInt/2, maxInt-1, maxInt}
try test__cmpsi2(0, -2147483648, 2);
try test__cmpsi2(0, -2147483647, 2);
try test__cmpsi2(0, -1073741824, 2);
try test__cmpsi2(0, -1, 2);
try test__cmpsi2(0, 1, 0);
try test__cmpsi2(0, 1073741823, 0);
try test__cmpsi2(0, 2147483646, 0);
try test__cmpsi2(0, 2147483647, 0);
// 7. cmp 1, {minInt, minInt + 1, minInt/2, -1,0, maxInt/2, maxInt-1, maxInt}
try test__cmpsi2(1, -2147483648, 2);
try test__cmpsi2(1, -2147483647, 2);
try test__cmpsi2(1, -1073741824, 2);
try test__cmpsi2(1, -1, 2);
try test__cmpsi2(1, 0, 2);
try test__cmpsi2(1, 1073741823, 0);
try test__cmpsi2(1, 2147483646, 0);
try test__cmpsi2(1, 2147483647, 0);
// 8. cmp maxInt/2, {minInt, minInt + 1, minInt/2, -1, 0, 1, maxInt-1, maxInt}
try test__cmpsi2(1073741823, -2147483648, 2);
try test__cmpsi2(1073741823, -2147483647, 2);
try test__cmpsi2(1073741823, -1073741824, 2);
try test__cmpsi2(1073741823, -1, 2);
try test__cmpsi2(1073741823, 0, 2);
try test__cmpsi2(1073741823, 1, 2);
try test__cmpsi2(1073741823, 2147483646, 0);
try test__cmpsi2(1073741823, 2147483647, 0);
// 9. cmp maxInt-1, {minInt, minInt + 1, minInt/2, -1, 0, 1, maxInt/2, maxInt}
try test__cmpsi2(2147483646, -2147483648, 2);
try test__cmpsi2(2147483646, -2147483647, 2);
try test__cmpsi2(2147483646, -1073741824, 2);
try test__cmpsi2(2147483646, -1, 2);
try test__cmpsi2(2147483646, 0, 2);
try test__cmpsi2(2147483646, 1, 2);
try test__cmpsi2(2147483646, 1073741823, 2);
try test__cmpsi2(2147483646, 2147483647, 0);
// 10.cmp maxInt, {minInt, minInt + 1, minInt/2, -1, 0, 1, maxInt/2, maxInt-1, }
try test__cmpsi2(2147483647, -2147483648, 2);
try test__cmpsi2(2147483647, -2147483647, 2);
try test__cmpsi2(2147483647, -1073741824, 2);
try test__cmpsi2(2147483647, -1, 2);
try test__cmpsi2(2147483647, 0, 2);
try test__cmpsi2(2147483647, 1, 2);
try test__cmpsi2(2147483647, 1073741823, 2);
try test__cmpsi2(2147483647, 2147483646, 2);
}

View File

@ -0,0 +1,105 @@
const cmp = @import("cmp.zig");
const testing = @import("std").testing;
fn test__cmpti2(a: i128, b: i128, expected: i128) !void {
var result = cmp.__cmpti2(a, b);
try testing.expectEqual(expected, result);
}
test "cmpti2" {
// minInt == -170141183460469231731687303715884105728
// maxInt == 170141183460469231731687303715884105727
// minInt/2 == -85070591730234615865843651857942052864
// maxInt/2 == 85070591730234615865843651857942052863
// 1. equality minInt, minInt+1, minInt/2, 0, maxInt/2, maxInt-1, maxInt
try test__cmpti2(-170141183460469231731687303715884105728, -170141183460469231731687303715884105728, 1);
try test__cmpti2(-170141183460469231731687303715884105727, -170141183460469231731687303715884105727, 1);
try test__cmpti2(-85070591730234615865843651857942052864, -85070591730234615865843651857942052864, 1);
try test__cmpti2(-1, -1, 1);
try test__cmpti2(0, 0, 1);
try test__cmpti2(1, 1, 1);
try test__cmpti2(85070591730234615865843651857942052863, 85070591730234615865843651857942052863, 1);
try test__cmpti2(170141183460469231731687303715884105726, 170141183460469231731687303715884105726, 1);
try test__cmpti2(170141183460469231731687303715884105727, 170141183460469231731687303715884105727, 1);
// 2. cmp minInt, { minInt + 1, minInt/2, -1,0,1, maxInt/2, maxInt-1, maxInt}
try test__cmpti2(-170141183460469231731687303715884105728, -170141183460469231731687303715884105727, 0);
try test__cmpti2(-170141183460469231731687303715884105728, -85070591730234615865843651857942052864, 0);
try test__cmpti2(-170141183460469231731687303715884105728, -1, 0);
try test__cmpti2(-170141183460469231731687303715884105728, 0, 0);
try test__cmpti2(-170141183460469231731687303715884105728, 1, 0);
try test__cmpti2(-170141183460469231731687303715884105728, 85070591730234615865843651857942052863, 0);
try test__cmpti2(-170141183460469231731687303715884105728, 170141183460469231731687303715884105726, 0);
try test__cmpti2(-170141183460469231731687303715884105728, 170141183460469231731687303715884105727, 0);
// 3. cmp minInt+1, {minInt, minInt/2, -1,0,1, maxInt/2, maxInt-1, maxInt}
try test__cmpti2(-170141183460469231731687303715884105727, -170141183460469231731687303715884105728, 2);
try test__cmpti2(-170141183460469231731687303715884105727, -85070591730234615865843651857942052864, 0);
try test__cmpti2(-170141183460469231731687303715884105727, -1, 0);
try test__cmpti2(-170141183460469231731687303715884105727, 0, 0);
try test__cmpti2(-170141183460469231731687303715884105727, 1, 0);
try test__cmpti2(-170141183460469231731687303715884105727, 85070591730234615865843651857942052863, 0);
try test__cmpti2(-170141183460469231731687303715884105727, 170141183460469231731687303715884105726, 0);
try test__cmpti2(-170141183460469231731687303715884105727, 170141183460469231731687303715884105727, 0);
// 4. cmp minInt/2, {minInt, minInt + 1, -1,0,1, maxInt/2, maxInt-1, maxInt}
try test__cmpti2(-85070591730234615865843651857942052864, -170141183460469231731687303715884105728, 2);
try test__cmpti2(-85070591730234615865843651857942052864, -170141183460469231731687303715884105727, 2);
try test__cmpti2(-85070591730234615865843651857942052864, -1, 0);
try test__cmpti2(-85070591730234615865843651857942052864, 0, 0);
try test__cmpti2(-85070591730234615865843651857942052864, 1, 0);
try test__cmpti2(-85070591730234615865843651857942052864, 85070591730234615865843651857942052863, 0);
try test__cmpti2(-85070591730234615865843651857942052864, 170141183460469231731687303715884105726, 0);
try test__cmpti2(-85070591730234615865843651857942052864, 170141183460469231731687303715884105727, 0);
// 5. cmp -1, {minInt, minInt + 1, minInt/2, 0,1, maxInt/2, maxInt-1, maxInt}
try test__cmpti2(-1, -170141183460469231731687303715884105728, 2);
try test__cmpti2(-1, -170141183460469231731687303715884105727, 2);
try test__cmpti2(-1, -85070591730234615865843651857942052864, 2);
try test__cmpti2(-1, 0, 0);
try test__cmpti2(-1, 1, 0);
try test__cmpti2(-1, 85070591730234615865843651857942052863, 0);
try test__cmpti2(-1, 170141183460469231731687303715884105726, 0);
try test__cmpti2(-1, 170141183460469231731687303715884105727, 0);
// 6. cmp 0, {minInt, minInt + 1, minInt/2, -1, 1, maxInt/2, maxInt-1, maxInt}
try test__cmpti2(0, -170141183460469231731687303715884105728, 2);
try test__cmpti2(0, -170141183460469231731687303715884105727, 2);
try test__cmpti2(0, -85070591730234615865843651857942052864, 2);
try test__cmpti2(0, -1, 2);
try test__cmpti2(0, 1, 0);
try test__cmpti2(0, 85070591730234615865843651857942052863, 0);
try test__cmpti2(0, 170141183460469231731687303715884105726, 0);
try test__cmpti2(0, 170141183460469231731687303715884105727, 0);
// 7. cmp 1, {minInt, minInt + 1, minInt/2, -1,0, maxInt/2, maxInt-1, maxInt}
try test__cmpti2(1, -170141183460469231731687303715884105728, 2);
try test__cmpti2(1, -170141183460469231731687303715884105727, 2);
try test__cmpti2(1, -85070591730234615865843651857942052864, 2);
try test__cmpti2(1, -1, 2);
try test__cmpti2(1, 0, 2);
try test__cmpti2(1, 85070591730234615865843651857942052863, 0);
try test__cmpti2(1, 170141183460469231731687303715884105726, 0);
try test__cmpti2(1, 170141183460469231731687303715884105727, 0);
// 8. cmp maxInt/2, {minInt, minInt + 1, minInt/2, -1,0,1, maxInt-1, maxInt}
try test__cmpti2(85070591730234615865843651857942052863, -170141183460469231731687303715884105728, 2);
try test__cmpti2(85070591730234615865843651857942052863, -170141183460469231731687303715884105727, 2);
try test__cmpti2(85070591730234615865843651857942052863, -85070591730234615865843651857942052864, 2);
try test__cmpti2(85070591730234615865843651857942052863, -1, 2);
try test__cmpti2(85070591730234615865843651857942052863, 0, 2);
try test__cmpti2(85070591730234615865843651857942052863, 1, 2);
try test__cmpti2(85070591730234615865843651857942052863, 170141183460469231731687303715884105726, 0);
try test__cmpti2(85070591730234615865843651857942052863, 170141183460469231731687303715884105727, 0);
// 9. cmp maxInt-1, {minInt, minInt + 1, minInt/2, -1,0,1, maxInt/2, maxInt}
try test__cmpti2(170141183460469231731687303715884105726, -170141183460469231731687303715884105728, 2);
try test__cmpti2(170141183460469231731687303715884105726, -170141183460469231731687303715884105727, 2);
try test__cmpti2(170141183460469231731687303715884105726, -85070591730234615865843651857942052864, 2);
try test__cmpti2(170141183460469231731687303715884105726, -1, 2);
try test__cmpti2(170141183460469231731687303715884105726, 0, 2);
try test__cmpti2(170141183460469231731687303715884105726, 1, 2);
try test__cmpti2(170141183460469231731687303715884105726, 85070591730234615865843651857942052863, 2);
try test__cmpti2(170141183460469231731687303715884105726, 170141183460469231731687303715884105727, 0);
// 10.cmp maxInt, {minInt, minInt + 1, minInt/2, -1,0,1, maxInt/2, maxInt-1, }
try test__cmpti2(170141183460469231731687303715884105727, -170141183460469231731687303715884105728, 2);
try test__cmpti2(170141183460469231731687303715884105727, -170141183460469231731687303715884105727, 2);
try test__cmpti2(170141183460469231731687303715884105727, -85070591730234615865843651857942052864, 2);
try test__cmpti2(170141183460469231731687303715884105727, -1, 2);
try test__cmpti2(170141183460469231731687303715884105727, 0, 2);
try test__cmpti2(170141183460469231731687303715884105727, 1, 2);
try test__cmpti2(170141183460469231731687303715884105727, 85070591730234615865843651857942052863, 2);
try test__cmpti2(170141183460469231731687303715884105727, 170141183460469231731687303715884105726, 2);
}

View File

@ -0,0 +1,49 @@
const cmp = @import("cmp.zig");
const testing = @import("std").testing;
fn test__ucmpdi2(a: u64, b: u64, expected: i32) !void {
var result = cmp.__ucmpdi2(a, b);
try testing.expectEqual(expected, result);
}
test "ucmpdi2" {
// minInt == 0
// maxInt == 18446744073709551615
// minInt/2 == 0
// maxInt/2 == 9223372036854775807
// 1. equality minInt, minInt/2, 0, maxInt/2, maxInt
try test__ucmpdi2(0, 0, 1);
try test__ucmpdi2(1, 1, 1);
try test__ucmpdi2(9223372036854775807, 9223372036854775807, 1);
try test__ucmpdi2(18446744073709551614, 18446744073709551614, 1);
try test__ucmpdi2(18446744073709551615, 18446744073709551615, 1);
// 2. cmp minInt, {minInt + 1, maxInt/2, maxInt-1, maxInt}
try test__ucmpdi2(0, 1, 0);
try test__ucmpdi2(0, 9223372036854775807, 0);
try test__ucmpdi2(0, 18446744073709551614, 0);
try test__ucmpdi2(0, 18446744073709551615, 0);
// 3. cmp minInt+1, {minInt, maxInt/2, maxInt-1, maxInt}
try test__ucmpdi2(1, 0, 2);
try test__ucmpdi2(1, 9223372036854775807, 0);
try test__ucmpdi2(1, 18446744073709551614, 0);
try test__ucmpdi2(1, 18446744073709551615, 0);
// 4. cmp minInt/2, {}
// 5. cmp -1, {}
// 6. cmp 0, {}
// 7. cmp 1, {}
// 8. cmp maxInt/2, {minInt, minInt+1, maxInt-1, maxInt}
try test__ucmpdi2(9223372036854775807, 0, 2);
try test__ucmpdi2(9223372036854775807, 1, 2);
try test__ucmpdi2(9223372036854775807, 18446744073709551614, 0);
try test__ucmpdi2(9223372036854775807, 18446744073709551615, 0);
// 9. cmp maxInt-1, {minInt, minInt + 1, maxInt/2, maxInt}
try test__ucmpdi2(18446744073709551614, 0, 2);
try test__ucmpdi2(18446744073709551614, 1, 2);
try test__ucmpdi2(18446744073709551614, 9223372036854775807, 2);
try test__ucmpdi2(18446744073709551614, 18446744073709551615, 0);
// 10.cmp maxInt, {minInt, 1, maxInt/2, maxInt-1}
try test__ucmpdi2(18446744073709551615, 0, 2);
try test__ucmpdi2(18446744073709551615, 1, 2);
try test__ucmpdi2(18446744073709551615, 9223372036854775807, 2);
try test__ucmpdi2(18446744073709551615, 18446744073709551614, 2);
}

View File

@ -0,0 +1,49 @@
const cmp = @import("cmp.zig");
const testing = @import("std").testing;
fn test__ucmpsi2(a: u32, b: u32, expected: i32) !void {
var result = cmp.__ucmpsi2(a, b);
try testing.expectEqual(expected, result);
}
test "ucmpsi2" {
// minInt == 0
// maxInt == 4294967295
// minInt/2 == 0
// maxInt/2 == 2147483647
// 1. equality 0, 1 maxInt/2, maxInt-1, maxInt
try test__ucmpsi2(0, 0, 1);
try test__ucmpsi2(1, 1, 1);
try test__ucmpsi2(2147483647, 2147483647, 1);
try test__ucmpsi2(4294967294, 4294967294, 1);
try test__ucmpsi2(4294967295, 4294967295, 1);
// 2. cmp minInt, {0, 1, maxInt/2, maxInt-1, maxInt}
try test__ucmpsi2(0, 1, 0);
try test__ucmpsi2(0, 2147483647, 0);
try test__ucmpsi2(0, 4294967294, 0);
try test__ucmpsi2(0, 4294967295, 0);
// 3. cmp minInt+1, {minInt, 0, maxInt/2, maxInt-1, maxInt}
try test__ucmpsi2(1, 0, 2);
try test__ucmpsi2(1, 2147483647, 0);
try test__ucmpsi2(1, 4294967294, 0);
try test__ucmpsi2(1, 4294967295, 0);
// 4. cmp minInt/2==minInt, {}
// 5. cmp -1 {}
// 6. cmp 0==minInt,{}
// 7. cmp 1==minInt+1, {}
// 8. cmp maxInt/2, {0, maxInt-1, maxInt}
try test__ucmpsi2(2147483647, 0, 2);
try test__ucmpsi2(2147483647, 1, 2);
try test__ucmpsi2(2147483647, 4294967294, 0);
try test__ucmpsi2(2147483647, 4294967295, 0);
// 9. cmp maxInt-1, {0,1,2, maxInt/2, maxInt}
try test__ucmpsi2(4294967294, 0, 2);
try test__ucmpsi2(4294967294, 1, 2);
try test__ucmpsi2(4294967294, 2147483647, 2);
try test__ucmpsi2(4294967294, 4294967295, 0);
// 10.cmp maxInt, {0,1,2, maxInt/2, maxInt-1}
try test__ucmpsi2(4294967295, 0, 2);
try test__ucmpsi2(4294967295, 1, 2);
try test__ucmpsi2(4294967295, 2147483647, 2);
try test__ucmpsi2(4294967295, 4294967294, 2);
}

View File

@ -0,0 +1,49 @@
const cmp = @import("cmp.zig");
const testing = @import("std").testing;
fn test__ucmpti2(a: u128, b: u128, expected: i32) !void {
var result = cmp.__ucmpti2(a, b);
try testing.expectEqual(expected, result);
}
test "ucmpti2" {
// minInt == 0
// maxInt == 340282366920938463463374607431768211455
// minInt/2 == 0
// maxInt/2 == 170141183460469231731687303715884105727
// 1. equality minInt, minInt+1, maxInt/2, maxInt-1, maxInt
try test__ucmpti2(0, 0, 1);
try test__ucmpti2(1, 1, 1);
try test__ucmpti2(170141183460469231731687303715884105727, 170141183460469231731687303715884105727, 1);
try test__ucmpti2(340282366920938463463374607431768211454, 340282366920938463463374607431768211454, 1);
try test__ucmpti2(340282366920938463463374607431768211455, 340282366920938463463374607431768211455, 1);
// 2. cmp minInt, {minInt + 1, maxInt/2, maxInt-1, maxInt}
try test__ucmpti2(0, 1, 0);
try test__ucmpti2(0, 170141183460469231731687303715884105727, 0);
try test__ucmpti2(0, 340282366920938463463374607431768211454, 0);
try test__ucmpti2(0, 340282366920938463463374607431768211455, 0);
// 3. cmp minInt+1, {minInt, maxInt/2, maxInt-1, maxInt}
try test__ucmpti2(1, 0, 2);
try test__ucmpti2(1, 170141183460469231731687303715884105727, 0);
try test__ucmpti2(1, 340282366920938463463374607431768211454, 0);
try test__ucmpti2(1, 340282366920938463463374607431768211455, 0);
// 4. cmp minInt/2, {}
// 5. cmp -1, {}
// 6. cmp 0, {}
// 7. cmp 1, {}
// 8. cmp maxInt/2, {}
try test__ucmpti2(170141183460469231731687303715884105727, 0, 2);
try test__ucmpti2(170141183460469231731687303715884105727, 1, 2);
try test__ucmpti2(170141183460469231731687303715884105727, 340282366920938463463374607431768211454, 0);
try test__ucmpti2(170141183460469231731687303715884105727, 340282366920938463463374607431768211455, 0);
// 9. cmp maxInt-1, {minInt, minInt + 1, maxInt/2, maxInt}
try test__ucmpti2(340282366920938463463374607431768211454, 0, 2);
try test__ucmpti2(340282366920938463463374607431768211454, 1, 2);
try test__ucmpti2(340282366920938463463374607431768211454, 170141183460469231731687303715884105727, 2);
try test__ucmpti2(340282366920938463463374607431768211454, 340282366920938463463374607431768211455, 0);
// 10.cmp maxInt, {minInt, minInt + 1, minInt/2, -2,-1,0,1,2, maxInt/2, maxInt-1, }
try test__ucmpti2(340282366920938463463374607431768211455, 0, 2);
try test__ucmpti2(340282366920938463463374607431768211455, 1, 2);
try test__ucmpti2(340282366920938463463374607431768211455, 170141183460469231731687303715884105727, 2);
try test__ucmpti2(340282366920938463463374607431768211455, 340282366920938463463374607431768211454, 2);
}