mirror of
https://github.com/ziglang/zig.git
synced 2025-12-06 06:13:07 +00:00
x86_64: implement shifts of big integers
This commit is contained in:
parent
9023ff04d0
commit
bcbd49b2a6
@ -90,7 +90,6 @@ pub fn fill(self: *Xoshiro256, buf: []u8) void {
|
|||||||
|
|
||||||
test "xoroshiro sequence" {
|
test "xoroshiro sequence" {
|
||||||
if (@import("builtin").zig_backend == .stage2_c) return error.SkipZigTest;
|
if (@import("builtin").zig_backend == .stage2_c) return error.SkipZigTest;
|
||||||
if (@import("builtin").zig_backend == .stage2_x86_64) return error.SkipZigTest;
|
|
||||||
|
|
||||||
var r = Xoshiro256.init(0);
|
var r = Xoshiro256.init(0);
|
||||||
|
|
||||||
|
|||||||
@ -262,7 +262,6 @@ const hexToBytes = std.fmt.hexToBytes;
|
|||||||
|
|
||||||
test "AesOcb test vector 1" {
|
test "AesOcb test vector 1" {
|
||||||
if (builtin.zig_backend == .stage2_c) return error.SkipZigTest;
|
if (builtin.zig_backend == .stage2_c) return error.SkipZigTest;
|
||||||
if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest;
|
|
||||||
|
|
||||||
var k: [Aes128Ocb.key_length]u8 = undefined;
|
var k: [Aes128Ocb.key_length]u8 = undefined;
|
||||||
var nonce: [Aes128Ocb.nonce_length]u8 = undefined;
|
var nonce: [Aes128Ocb.nonce_length]u8 = undefined;
|
||||||
@ -282,7 +281,6 @@ test "AesOcb test vector 1" {
|
|||||||
|
|
||||||
test "AesOcb test vector 2" {
|
test "AesOcb test vector 2" {
|
||||||
if (builtin.zig_backend == .stage2_c) return error.SkipZigTest;
|
if (builtin.zig_backend == .stage2_c) return error.SkipZigTest;
|
||||||
if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest;
|
|
||||||
|
|
||||||
var k: [Aes128Ocb.key_length]u8 = undefined;
|
var k: [Aes128Ocb.key_length]u8 = undefined;
|
||||||
var nonce: [Aes128Ocb.nonce_length]u8 = undefined;
|
var nonce: [Aes128Ocb.nonce_length]u8 = undefined;
|
||||||
@ -304,7 +302,6 @@ test "AesOcb test vector 2" {
|
|||||||
|
|
||||||
test "AesOcb test vector 3" {
|
test "AesOcb test vector 3" {
|
||||||
if (builtin.zig_backend == .stage2_c) return error.SkipZigTest;
|
if (builtin.zig_backend == .stage2_c) return error.SkipZigTest;
|
||||||
if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest;
|
|
||||||
|
|
||||||
var k: [Aes128Ocb.key_length]u8 = undefined;
|
var k: [Aes128Ocb.key_length]u8 = undefined;
|
||||||
var nonce: [Aes128Ocb.nonce_length]u8 = undefined;
|
var nonce: [Aes128Ocb.nonce_length]u8 = undefined;
|
||||||
@ -329,7 +326,6 @@ test "AesOcb test vector 3" {
|
|||||||
|
|
||||||
test "AesOcb test vector 4" {
|
test "AesOcb test vector 4" {
|
||||||
if (builtin.zig_backend == .stage2_c) return error.SkipZigTest;
|
if (builtin.zig_backend == .stage2_c) return error.SkipZigTest;
|
||||||
if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest;
|
|
||||||
|
|
||||||
var k: [Aes128Ocb.key_length]u8 = undefined;
|
var k: [Aes128Ocb.key_length]u8 = undefined;
|
||||||
var nonce: [Aes128Ocb.nonce_length]u8 = undefined;
|
var nonce: [Aes128Ocb.nonce_length]u8 = undefined;
|
||||||
|
|||||||
@ -907,9 +907,8 @@ const ct_unprotected = struct {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
test {
|
test "finite field arithmetic" {
|
||||||
if (builtin.zig_backend == .stage2_c) return error.SkipZigTest;
|
if (builtin.zig_backend == .stage2_c) return error.SkipZigTest;
|
||||||
if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest;
|
|
||||||
|
|
||||||
const M = Modulus(256);
|
const M = Modulus(256);
|
||||||
const m = try M.fromPrimitive(u256, 3429938563481314093726330772853735541133072814650493833233);
|
const m = try M.fromPrimitive(u256, 3429938563481314093726330772853735541133072814650493833233);
|
||||||
|
|||||||
@ -479,7 +479,6 @@ pub const AffineCoordinates = struct {
|
|||||||
|
|
||||||
test {
|
test {
|
||||||
if (@import("builtin").zig_backend == .stage2_c) return error.SkipZigTest;
|
if (@import("builtin").zig_backend == .stage2_c) return error.SkipZigTest;
|
||||||
if (@import("builtin").zig_backend == .stage2_x86_64) return error.SkipZigTest;
|
|
||||||
|
|
||||||
_ = @import("tests/p384.zig");
|
_ = @import("tests/p384.zig");
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1698,7 +1698,6 @@ test "big.int div multi-multi (2 branch)" {
|
|||||||
|
|
||||||
test "big.int div multi-multi (3.1/3.3 branch)" {
|
test "big.int div multi-multi (3.1/3.3 branch)" {
|
||||||
if (builtin.zig_backend == .stage2_c) return error.SkipZigTest;
|
if (builtin.zig_backend == .stage2_c) return error.SkipZigTest;
|
||||||
if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest;
|
|
||||||
|
|
||||||
var a = try Managed.initSet(testing.allocator, 0x11111111111111111111111111111111111111111111111111111111111111);
|
var a = try Managed.initSet(testing.allocator, 0x11111111111111111111111111111111111111111111111111111111111111);
|
||||||
defer a.deinit();
|
defer a.deinit();
|
||||||
@ -2187,7 +2186,6 @@ test "big.int bitNotWrap more than two limbs" {
|
|||||||
// This test requires int sizes greater than 128 bits.
|
// This test requires int sizes greater than 128 bits.
|
||||||
if (builtin.zig_backend == .stage2_wasm) return error.SkipZigTest; // TODO
|
if (builtin.zig_backend == .stage2_wasm) return error.SkipZigTest; // TODO
|
||||||
if (builtin.zig_backend == .stage2_c) return error.SkipZigTest; // TODO
|
if (builtin.zig_backend == .stage2_c) 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_aarch64) return error.SkipZigTest; // TODO
|
||||||
if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
|
if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
|
||||||
// LLVM: unexpected runtime library name: __umodei4
|
// LLVM: unexpected runtime library name: __umodei4
|
||||||
@ -2537,7 +2535,6 @@ test "big.int gcd non-one large" {
|
|||||||
|
|
||||||
test "big.int gcd large multi-limb result" {
|
test "big.int gcd large multi-limb result" {
|
||||||
if (builtin.zig_backend == .stage2_c) return error.SkipZigTest;
|
if (builtin.zig_backend == .stage2_c) return error.SkipZigTest;
|
||||||
if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest;
|
|
||||||
|
|
||||||
var a = try Managed.initSet(testing.allocator, 0x12345678123456781234567812345678123456781234567812345678);
|
var a = try Managed.initSet(testing.allocator, 0x12345678123456781234567812345678123456781234567812345678);
|
||||||
defer a.deinit();
|
defer a.deinit();
|
||||||
|
|||||||
@ -1,10 +1,6 @@
|
|||||||
const std = @import("../std.zig");
|
const std = @import("../std.zig");
|
||||||
const builtin = @import("builtin");
|
const builtin = @import("builtin");
|
||||||
const math = std.math;
|
|
||||||
const testing = std.testing;
|
const testing = std.testing;
|
||||||
const maxInt = std.math.maxInt;
|
|
||||||
const assert = std.debug.assert;
|
|
||||||
const Log2Int = std.math.Log2Int;
|
|
||||||
|
|
||||||
/// Returns the base-10 logarithm of x.
|
/// Returns the base-10 logarithm of x.
|
||||||
///
|
///
|
||||||
@ -38,13 +34,13 @@ pub fn log10(x: anytype) @TypeOf(x) {
|
|||||||
|
|
||||||
/// Return the log base 10 of integer value x, rounding down to the
|
/// Return the log base 10 of integer value x, rounding down to the
|
||||||
/// nearest integer.
|
/// nearest integer.
|
||||||
pub fn log10_int(x: anytype) Log2Int(@TypeOf(x)) {
|
pub fn log10_int(x: anytype) std.math.Log2Int(@TypeOf(x)) {
|
||||||
const T = @TypeOf(x);
|
const T = @TypeOf(x);
|
||||||
const OutT = Log2Int(T);
|
const OutT = std.math.Log2Int(T);
|
||||||
if (@typeInfo(T) != .Int or @typeInfo(T).Int.signedness != .unsigned)
|
if (@typeInfo(T) != .Int or @typeInfo(T).Int.signedness != .unsigned)
|
||||||
@compileError("log10_int requires an unsigned integer, found " ++ @typeName(T));
|
@compileError("log10_int requires an unsigned integer, found " ++ @typeName(T));
|
||||||
|
|
||||||
assert(x != 0);
|
std.debug.assert(x != 0);
|
||||||
|
|
||||||
const bit_size = @typeInfo(T).Int.bits;
|
const bit_size = @typeInfo(T).Int.bits;
|
||||||
|
|
||||||
@ -133,18 +129,7 @@ inline fn less_than_5(x: u32) u32 {
|
|||||||
return (((x + C1) & (x + C2)) ^ ((x + C3) & (x + C4))) >> 17;
|
return (((x + C1) & (x + C2)) ^ ((x + C3) & (x + C4))) >> 17;
|
||||||
}
|
}
|
||||||
|
|
||||||
fn oldlog10(x: anytype) u8 {
|
test log10_int {
|
||||||
return @as(u8, @intFromFloat(@log10(@as(f64, @floatFromInt(x)))));
|
|
||||||
}
|
|
||||||
|
|
||||||
test "oldlog10 doesn't work" {
|
|
||||||
try testing.expect(14 != oldlog10(pow10(15) - 1));
|
|
||||||
|
|
||||||
// log10(10**15 -1) should indeed be 14
|
|
||||||
try testing.expect(14 == log10_int(@as(u64, pow10(15) - 1)));
|
|
||||||
}
|
|
||||||
|
|
||||||
test "log10_int vs old implementation" {
|
|
||||||
if (builtin.zig_backend == .stage2_wasm) return error.SkipZigTest; // TODO
|
if (builtin.zig_backend == .stage2_wasm) return error.SkipZigTest; // TODO
|
||||||
if (builtin.zig_backend == .stage2_c) return error.SkipZigTest; // TODO
|
if (builtin.zig_backend == .stage2_c) return error.SkipZigTest; // TODO
|
||||||
if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO
|
if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO
|
||||||
@ -152,53 +137,22 @@ test "log10_int vs old implementation" {
|
|||||||
if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
|
if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
|
||||||
if (builtin.zig_backend == .stage2_llvm and comptime builtin.target.isWasm()) return error.SkipZigTest; // TODO
|
if (builtin.zig_backend == .stage2_llvm and comptime builtin.target.isWasm()) return error.SkipZigTest; // TODO
|
||||||
|
|
||||||
const int_types = .{ u8, u16, u32, u64, u128 };
|
inline for (
|
||||||
|
.{ u8, u16, u32, u64, u128, u256, u512 },
|
||||||
|
.{ 2, 4, 9, 19, 38, 77, 154 },
|
||||||
|
) |T, max_exponent| {
|
||||||
|
for (0..max_exponent + 1) |exponent_usize| {
|
||||||
|
const exponent: std.math.Log2Int(T) = @intCast(exponent_usize);
|
||||||
|
const power_of_ten = try std.math.powi(T, 10, exponent);
|
||||||
|
|
||||||
inline for (int_types) |T| {
|
if (exponent > 0) {
|
||||||
const last = @min(maxInt(T), 100_000);
|
try testing.expectEqual(exponent - 1, log10_int(power_of_ten - 9));
|
||||||
for (1..last) |i| {
|
try testing.expectEqual(exponent - 1, log10_int(power_of_ten - 1));
|
||||||
const x = @as(T, @intCast(i));
|
}
|
||||||
try testing.expectEqual(oldlog10(x), log10_int(x));
|
try testing.expectEqual(exponent, log10_int(power_of_ten));
|
||||||
}
|
try testing.expectEqual(exponent, log10_int(power_of_ten + 1));
|
||||||
|
try testing.expectEqual(exponent, log10_int(power_of_ten + 8));
|
||||||
const max_int: T = maxInt(T);
|
|
||||||
try testing.expectEqual(oldlog10(max_int), log10_int(max_int));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
test "log10_int close to powers of 10" {
|
|
||||||
if (builtin.zig_backend == .stage2_wasm) return error.SkipZigTest; // TODO
|
|
||||||
if (builtin.zig_backend == .stage2_c) return error.SkipZigTest; // TODO
|
|
||||||
if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO
|
|
||||||
if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO
|
|
||||||
if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
|
|
||||||
if (builtin.zig_backend == .stage2_llvm and comptime builtin.target.isWasm()) return error.SkipZigTest; // TODO
|
|
||||||
if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest;
|
|
||||||
|
|
||||||
const int_types = .{ u8, u16, u32, u64, u128, u256, u512 };
|
|
||||||
const max_log_values: [7]usize = .{ 2, 4, 9, 19, 38, 77, 154 };
|
|
||||||
|
|
||||||
inline for (int_types, max_log_values) |T, expected_max_ilog| {
|
|
||||||
const max_val: T = maxInt(T);
|
|
||||||
|
|
||||||
try testing.expectEqual(expected_max_ilog, log10_int(max_val));
|
|
||||||
|
|
||||||
for (0..(expected_max_ilog + 1)) |idx| {
|
|
||||||
const i = @as(T, @intCast(idx));
|
|
||||||
const p: T = try math.powi(T, 10, i);
|
|
||||||
|
|
||||||
const b = @as(Log2Int(T), @intCast(i));
|
|
||||||
|
|
||||||
if (p >= 10) {
|
|
||||||
try testing.expectEqual(b - 1, log10_int(p - 9));
|
|
||||||
try testing.expectEqual(b - 1, log10_int(p - 1));
|
|
||||||
}
|
|
||||||
|
|
||||||
try testing.expectEqual(b, log10_int(p));
|
|
||||||
try testing.expectEqual(b, log10_int(p + 1));
|
|
||||||
if (p >= 10) {
|
|
||||||
try testing.expectEqual(b, log10_int(p + 9));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
try testing.expectEqual(max_exponent, log10_int(@as(T, std.math.maxInt(T))));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
File diff suppressed because it is too large
Load Diff
Loading…
x
Reference in New Issue
Block a user