-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8f27a43
commit 17f14e1
Showing
7 changed files
with
235 additions
and
4 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
const std = @import("std"); | ||
const builtin = @import("builtin"); | ||
const common = @import("common.zig"); | ||
|
||
pub const panic = common.panic; | ||
|
||
comptime { | ||
@export(__bitreversesi2, .{ .name = "__bitreversesi2", .linkage = common.linkage, .visibility = common.visibility }); | ||
@export(__bitreversedi2, .{ .name = "__bitreversedi2", .linkage = common.linkage, .visibility = common.visibility }); | ||
@export(__bitreverseti2, .{ .name = "__bitreverseti2", .linkage = common.linkage, .visibility = common.visibility }); | ||
} | ||
|
||
inline fn bitreverseXi2(comptime T: type, a: T) T { | ||
switch (@bitSizeOf(T)) { | ||
32 => { | ||
var t: T = a; | ||
t = ((t >> 1) & 0x55555555) | ((t & 0x55555555) << 1); | ||
t = ((t >> 2) & 0x33333333) | ((t & 0x33333333) << 2); | ||
t = ((t >> 4) & 0x0F0F0F0F) | ((t & 0x0F0F0F0F) << 4); | ||
t = ((t >> 8) & 0x00FF00FF) | ((t & 0x00FF00FF) << 8); | ||
t = (t >> 16) | (t << 16); | ||
return t; | ||
}, | ||
64 => { | ||
var t: T = a; | ||
t = ((t >> 1) & 0x5555555555555555) | ((t & 0x5555555555555555) << 1); | ||
t = ((t >> 2) & 0x3333333333333333) | ((t & 0x3333333333333333) << 2); | ||
t = ((t >> 4) & 0x0F0F0F0F0F0F0F0F) | ((t & 0x0F0F0F0F0F0F0F0F) << 4); | ||
t = ((t >> 8) & 0x00FF00FF00FF00FF) | ((t & 0x00FF00FF00FF00FF) << 8); | ||
t = ((t >> 16) & 0x0000FFFF0000FFFF) | ((t & 0x0000FFFF0000FFFF) << 16); | ||
t = (t >> 32) | (t << 32); | ||
return t; | ||
}, | ||
128 => { | ||
var t: T = a; | ||
t = ((t >> 1) & 0x55555555555555555555555555555555) | ((t & 0x55555555555555555555555555555555) << 1); | ||
t = ((t >> 2) & 0x33333333333333333333333333333333) | ((t & 0x33333333333333333333333333333333) << 2); | ||
t = ((t >> 4) & 0x0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F) | ((t & 0x0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F) << 4); | ||
t = ((t >> 8) & 0x00FF00FF00FF00FF00FF00FF00FF00FF) | ((t & 0x00FF00FF00FF00FF00FF00FF00FF00FF) << 8); | ||
t = ((t >> 16) & 0x0000FFFF0000FFFF0000FFFF0000FFFF) | ((t & 0x0000FFFF0000FFFF0000FFFF0000FFFF) << 16); | ||
t = ((t >> 32) & 0x00000000FFFFFFFF00000000FFFFFFFF) | ((t & 0x00000000FFFFFFFF00000000FFFFFFFF) << 32); | ||
t = (t >> 64) | (t << 64); | ||
return t; | ||
}, | ||
else => unreachable, | ||
} | ||
} | ||
|
||
pub fn __bitreversesi2(a: u32) callconv(.C) u32 { | ||
return bitreverseXi2(u32, a); | ||
} | ||
|
||
pub fn __bitreversedi2(a: u64) callconv(.C) u64 { | ||
return bitreverseXi2(u64, a); | ||
} | ||
|
||
pub fn __bitreverseti2(a: u128) callconv(.C) u128 { | ||
return bitreverseXi2(u128, a); | ||
} | ||
|
||
test { | ||
_ = @import("bitreversesi2_test.zig"); | ||
_ = @import("bitreversedi2_test.zig"); | ||
_ = @import("bitreverseti2_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,22 @@ | ||
const bitreverse = @import("bitreverse.zig"); | ||
const testing = @import("std").testing; | ||
|
||
fn test__bitreversedi2(input: u64, expected: u64) !void { | ||
const result = bitreverse.__bitreversedi2(input); | ||
try testing.expectEqual(expected, result); | ||
} | ||
|
||
test "bitreversedi2" { | ||
try test__bitreversedi2(0x0123456789abcdef, 0xf7b3d591e6a2c480); | ||
try test__bitreversedi2(0xf7b3d591e6a2c480, 0x0123456789abcdef); | ||
try test__bitreversedi2(0x89abcdef00000000, 0x00000000f7b3d591); | ||
try test__bitreversedi2(0x00000000f7b3d591, 0x89abcdef00000000); | ||
try test__bitreversedi2(0x0000c0da23000000, 0x000000c45b030000); | ||
try test__bitreversedi2(0x000000c45b030000, 0x0000c0da23000000); | ||
try test__bitreversedi2(0x000000000000032f, 0xf4c0000000000000); | ||
try test__bitreversedi2(0xf4c0000000000000, 0x000000000000032f); | ||
try test__bitreversedi2(0xaaaaaaaaaaaaaaaa, 0x5555555555555555); | ||
try test__bitreversedi2(0x5555555555555555, 0xaaaaaaaaaaaaaaaa); | ||
try test__bitreversedi2(0x0000000000000000, 0x0000000000000000); | ||
try test__bitreversedi2(0xffffffffffffffff, 0xffffffffffffffff); | ||
} |
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,22 @@ | ||
const bitreverse = @import("bitreverse.zig"); | ||
const testing = @import("std").testing; | ||
|
||
fn test__bitreversesi2(input: u32, expected: u32) !void { | ||
const result = bitreverse.__bitreversesi2(input); | ||
try testing.expectEqual(expected, result); | ||
} | ||
|
||
test "bitreversesi2" { | ||
try test__bitreversesi2(0x01234567, 0xe6a2c480); | ||
try test__bitreversesi2(0xe6a2c480, 0x01234567); | ||
try test__bitreversesi2(0x89abcdef, 0xf7b3d591); | ||
try test__bitreversesi2(0xf7b3d591, 0x89abcdef); | ||
try test__bitreversesi2(0xc0da2300, 0x00c45b03); | ||
try test__bitreversesi2(0x00c45b03, 0xc0da2300); | ||
try test__bitreversesi2(0x0000032f, 0xf4c00000); | ||
try test__bitreversesi2(0xf4c00000, 0x0000032f); | ||
try test__bitreversesi2(0xaaaaaaaa, 0x55555555); | ||
try test__bitreversesi2(0x55555555, 0xaaaaaaaa); | ||
try test__bitreversesi2(0x00000000, 0x00000000); | ||
try test__bitreversesi2(0xffffffff, 0xffffffff); | ||
} |
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,22 @@ | ||
const bitreverse = @import("bitreverse.zig"); | ||
const testing = @import("std").testing; | ||
|
||
fn test__bitreverseti2(input: u128, expected: u128) !void { | ||
const result = bitreverse.__bitreverseti2(input); | ||
try testing.expectEqual(expected, result); | ||
} | ||
|
||
test "bitreverseti2" { | ||
try test__bitreverseti2(0x0123456789abcdef0123456789abcdef, 0xf7b3d591e6a2c480f7b3d591e6a2c480); | ||
try test__bitreverseti2(0xf7b3d591e6a2c480f7b3d591e6a2c480, 0x0123456789abcdef0123456789abcdef); | ||
try test__bitreverseti2(0x89abcdef000000000000000000000000, 0x000000000000000000000000f7b3d591); | ||
try test__bitreverseti2(0x000000000000000000000000f7b3d591, 0x89abcdef000000000000000000000000); | ||
try test__bitreverseti2(0x000000000000c0da2300000000000000, 0x00000000000000c45b03000000000000); | ||
try test__bitreverseti2(0x00000000000000c45b03000000000000, 0x000000000000c0da2300000000000000); | ||
try test__bitreverseti2(0x0000000000000000000000000000032f, 0xf4c00000000000000000000000000000); | ||
try test__bitreverseti2(0xf4c00000000000000000000000000000, 0x0000000000000000000000000000032f); | ||
try test__bitreverseti2(0xaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, 0x55555555555555555555555555555555); | ||
try test__bitreverseti2(0x55555555555555555555555555555555, 0xaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa); | ||
try test__bitreverseti2(0x00000000000000000000000000000000, 0x00000000000000000000000000000000); | ||
try test__bitreverseti2(0xffffffffffffffffffffffffffffffff, 0xffffffffffffffffffffffffffffffff); | ||
} |
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