Skip to content

Commit

Permalink
compiler_rt: add __cmpXi2 and __ucmpXi2
Browse files Browse the repository at this point in the history
- 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 ziglang#1290
  • Loading branch information
matu3ba committed Dec 13, 2021
1 parent a76910b commit 0c58afd
Show file tree
Hide file tree
Showing 9 changed files with 521 additions and 1 deletion.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
14 changes: 13 additions & 1 deletion lib/std/special/compiler_rt.zig
Original file line number Diff line number Diff line change
Expand Up @@ -413,10 +413,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

Expand Down
45 changes: 45 additions & 0 deletions lib/std/special/compiler_rt/cmp.zig
Original file line number Diff line number Diff line change
@@ -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");
}
105 changes: 105 additions & 0 deletions lib/std/special/compiler_rt/cmpdi2_test.zig
Original file line number Diff line number Diff line change
@@ -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);
}
105 changes: 105 additions & 0 deletions lib/std/special/compiler_rt/cmpsi2_test.zig
Original file line number Diff line number Diff line change
@@ -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);
}
Loading

0 comments on commit 0c58afd

Please sign in to comment.