forked from ziglang/zig
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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 ziglang#1290
- Loading branch information
Showing
9 changed files
with
521 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
Oops, something went wrong.