all: migrate code to new cast builtin syntax

Most of this migration was performed automatically with `zig fmt`. There
were a few exceptions which I had to manually fix:

* `@alignCast` and `@addrSpaceCast` cannot be automatically rewritten
* `@truncate`'s fixup is incorrect for vectors
* Test cases are not formatted, and their error locations change
This commit is contained in:
mlugg
2023-06-22 18:46:56 +01:00
committed by Andrew Kelley
parent 447ca4e3ff
commit f26dda2117
651 changed files with 8967 additions and 9039 deletions

View File

@@ -32,7 +32,7 @@ pub fn BiasedFp(comptime T: type) type {
pub fn toFloat(self: Self, comptime FloatT: type, negative: bool) FloatT {
var word = self.f;
word |= @intCast(MantissaT, self.e) << std.math.floatMantissaBits(FloatT);
word |= @as(MantissaT, @intCast(self.e)) << std.math.floatMantissaBits(FloatT);
var f = floatFromUnsigned(FloatT, MantissaT, word);
if (negative) f = -f;
return f;
@@ -42,10 +42,10 @@ pub fn BiasedFp(comptime T: type) type {
pub fn floatFromUnsigned(comptime T: type, comptime MantissaT: type, v: MantissaT) T {
return switch (T) {
f16 => @bitCast(f16, @truncate(u16, v)),
f32 => @bitCast(f32, @truncate(u32, v)),
f64 => @bitCast(f64, @truncate(u64, v)),
f128 => @bitCast(f128, v),
f16 => @as(f16, @bitCast(@as(u16, @truncate(v)))),
f32 => @as(f32, @bitCast(@as(u32, @truncate(v)))),
f64 => @as(f64, @bitCast(@as(u64, @truncate(v)))),
f128 => @as(f128, @bitCast(v)),
else => unreachable,
};
}

View File

@@ -36,7 +36,7 @@ pub fn convertEiselLemire(comptime T: type, q: i64, w_: u64) ?BiasedFp(f64) {
}
// Normalize our significant digits, so the most-significant bit is set.
const lz = @clz(@bitCast(u64, w));
const lz = @clz(@as(u64, @bitCast(w)));
w = math.shl(u64, w, lz);
const r = computeProductApprox(q, w, float_info.mantissa_explicit_bits + 3);
@@ -62,9 +62,9 @@ pub fn convertEiselLemire(comptime T: type, q: i64, w_: u64) ?BiasedFp(f64) {
}
}
const upper_bit = @intCast(i32, r.hi >> 63);
var mantissa = math.shr(u64, r.hi, upper_bit + 64 - @intCast(i32, float_info.mantissa_explicit_bits) - 3);
var power2 = power(@intCast(i32, q)) + upper_bit - @intCast(i32, lz) - float_info.minimum_exponent;
const upper_bit = @as(i32, @intCast(r.hi >> 63));
var mantissa = math.shr(u64, r.hi, upper_bit + 64 - @as(i32, @intCast(float_info.mantissa_explicit_bits)) - 3);
var power2 = power(@as(i32, @intCast(q))) + upper_bit - @as(i32, @intCast(lz)) - float_info.minimum_exponent;
if (power2 <= 0) {
if (-power2 + 1 >= 64) {
// Have more than 64 bits below the minimum exponent, must be 0.
@@ -93,7 +93,7 @@ pub fn convertEiselLemire(comptime T: type, q: i64, w_: u64) ?BiasedFp(f64) {
q >= float_info.min_exponent_round_to_even and
q <= float_info.max_exponent_round_to_even and
mantissa & 3 == 1 and
math.shl(u64, mantissa, (upper_bit + 64 - @intCast(i32, float_info.mantissa_explicit_bits) - 3)) == r.hi)
math.shl(u64, mantissa, (upper_bit + 64 - @as(i32, @intCast(float_info.mantissa_explicit_bits)) - 3)) == r.hi)
{
// Zero the lowest bit, so we don't round up.
mantissa &= ~@as(u64, 1);
@@ -139,8 +139,8 @@ const U128 = struct {
pub fn mul(a: u64, b: u64) U128 {
const x = @as(u128, a) * b;
return .{
.hi = @truncate(u64, x >> 64),
.lo = @truncate(u64, x),
.hi = @as(u64, @truncate(x >> 64)),
.lo = @as(u64, @truncate(x)),
};
}
};
@@ -161,7 +161,7 @@ fn computeProductApprox(q: i64, w: u64, comptime precision: usize) U128 {
// 5^q < 2^64, then the multiplication always provides an exact value.
// That means whenever we need to round ties to even, we always have
// an exact value.
const index = @intCast(usize, q - @intCast(i64, eisel_lemire_smallest_power_of_five));
const index = @as(usize, @intCast(q - @as(i64, @intCast(eisel_lemire_smallest_power_of_five))));
const pow5 = eisel_lemire_table_powers_of_five_128[index];
// Only need one multiplication as long as there is 1 zero but

View File

@@ -108,19 +108,19 @@ pub fn convertFast(comptime T: type, n: Number(T)) ?T {
var value: T = 0;
if (n.exponent <= info.max_exponent_fast_path) {
// normal fast path
value = @floatFromInt(T, n.mantissa);
value = @as(T, @floatFromInt(n.mantissa));
value = if (n.exponent < 0)
value / fastPow10(T, @intCast(usize, -n.exponent))
value / fastPow10(T, @as(usize, @intCast(-n.exponent)))
else
value * fastPow10(T, @intCast(usize, n.exponent));
value * fastPow10(T, @as(usize, @intCast(n.exponent)));
} else {
// disguised fast path
const shift = n.exponent - info.max_exponent_fast_path;
const mantissa = math.mul(MantissaT, n.mantissa, fastIntPow10(MantissaT, @intCast(usize, shift))) catch return null;
const mantissa = math.mul(MantissaT, n.mantissa, fastIntPow10(MantissaT, @as(usize, @intCast(shift)))) catch return null;
if (mantissa > info.max_mantissa_fast_path) {
return null;
}
value = @floatFromInt(T, mantissa) * fastPow10(T, info.max_exponent_fast_path);
value = @as(T, @floatFromInt(mantissa)) * fastPow10(T, info.max_exponent_fast_path);
}
if (n.negative) {

View File

@@ -81,7 +81,7 @@ pub fn convertHex(comptime T: type, n_: Number(T)) T {
}
var bits = n.mantissa & ((1 << mantissa_bits) - 1);
bits |= @intCast(MantissaT, (n.exponent - exp_bias) & ((1 << exp_bits) - 1)) << mantissa_bits;
bits |= @as(MantissaT, @intCast((n.exponent - exp_bias) & ((1 << exp_bits) - 1))) << mantissa_bits;
if (n.negative) {
bits |= 1 << (mantissa_bits + exp_bits);
}

View File

@@ -48,13 +48,13 @@ pub fn convertSlow(comptime T: type, s: []const u8) BiasedFp(T) {
var exp2: i32 = 0;
// Shift right toward (1/2 .. 1]
while (d.decimal_point > 0) {
const n = @intCast(usize, d.decimal_point);
const n = @as(usize, @intCast(d.decimal_point));
const shift = getShift(n);
d.rightShift(shift);
if (d.decimal_point < -Decimal(T).decimal_point_range) {
return BiasedFp(T).zero();
}
exp2 += @intCast(i32, shift);
exp2 += @as(i32, @intCast(shift));
}
// Shift left toward (1/2 .. 1]
while (d.decimal_point <= 0) {
@@ -66,7 +66,7 @@ pub fn convertSlow(comptime T: type, s: []const u8) BiasedFp(T) {
else => 1,
};
} else {
const n = @intCast(usize, -d.decimal_point);
const n = @as(usize, @intCast(-d.decimal_point));
break :blk getShift(n);
}
};
@@ -74,17 +74,17 @@ pub fn convertSlow(comptime T: type, s: []const u8) BiasedFp(T) {
if (d.decimal_point > Decimal(T).decimal_point_range) {
return BiasedFp(T).inf(T);
}
exp2 -= @intCast(i32, shift);
exp2 -= @as(i32, @intCast(shift));
}
// We are now in the range [1/2 .. 1] but the binary format uses [1 .. 2]
exp2 -= 1;
while (min_exponent + 1 > exp2) {
var n = @intCast(usize, (min_exponent + 1) - exp2);
var n = @as(usize, @intCast((min_exponent + 1) - exp2));
if (n > max_shift) {
n = max_shift;
}
d.rightShift(n);
exp2 += @intCast(i32, n);
exp2 += @as(i32, @intCast(n));
}
if (exp2 - min_exponent >= infinite_power) {
return BiasedFp(T).inf(T);

View File

@@ -114,7 +114,7 @@ pub fn Decimal(comptime T: type) type {
return math.maxInt(MantissaT);
}
const dp = @intCast(usize, self.decimal_point);
const dp = @as(usize, @intCast(self.decimal_point));
var n: MantissaT = 0;
var i: usize = 0;
@@ -155,7 +155,7 @@ pub fn Decimal(comptime T: type) type {
const quotient = n / 10;
const remainder = n - (10 * quotient);
if (write_index < max_digits) {
self.digits[write_index] = @intCast(u8, remainder);
self.digits[write_index] = @as(u8, @intCast(remainder));
} else if (remainder > 0) {
self.truncated = true;
}
@@ -167,7 +167,7 @@ pub fn Decimal(comptime T: type) type {
const quotient = n / 10;
const remainder = n - (10 * quotient);
if (write_index < max_digits) {
self.digits[write_index] = @intCast(u8, remainder);
self.digits[write_index] = @as(u8, @intCast(remainder));
} else if (remainder > 0) {
self.truncated = true;
}
@@ -178,7 +178,7 @@ pub fn Decimal(comptime T: type) type {
if (self.num_digits > max_digits) {
self.num_digits = max_digits;
}
self.decimal_point += @intCast(i32, num_new_digits);
self.decimal_point += @as(i32, @intCast(num_new_digits));
self.trim();
}
@@ -202,7 +202,7 @@ pub fn Decimal(comptime T: type) type {
}
}
self.decimal_point -= @intCast(i32, read_index) - 1;
self.decimal_point -= @as(i32, @intCast(read_index)) - 1;
if (self.decimal_point < -decimal_point_range) {
self.num_digits = 0;
self.decimal_point = 0;
@@ -212,14 +212,14 @@ pub fn Decimal(comptime T: type) type {
const mask = math.shl(MantissaT, 1, shift) - 1;
while (read_index < self.num_digits) {
const new_digit = @intCast(u8, math.shr(MantissaT, n, shift));
const new_digit = @as(u8, @intCast(math.shr(MantissaT, n, shift)));
n = (10 * (n & mask)) + self.digits[read_index];
read_index += 1;
self.digits[write_index] = new_digit;
write_index += 1;
}
while (n > 0) {
const new_digit = @intCast(u8, math.shr(MantissaT, n, shift));
const new_digit = @as(u8, @intCast(math.shr(MantissaT, n, shift)));
n = 10 * (n & mask);
if (write_index < max_digits) {
self.digits[write_index] = new_digit;
@@ -268,7 +268,7 @@ pub fn Decimal(comptime T: type) type {
while (stream.scanDigit(10)) |digit| {
d.tryAddDigit(digit);
}
d.decimal_point = @intCast(i32, marker) - @intCast(i32, stream.offsetTrue());
d.decimal_point = @as(i32, @intCast(marker)) - @as(i32, @intCast(stream.offsetTrue()));
}
if (d.num_digits != 0) {
// Ignore trailing zeros if any
@@ -284,9 +284,9 @@ pub fn Decimal(comptime T: type) type {
i -= 1;
if (i == 0) break;
}
d.decimal_point += @intCast(i32, n_trailing_zeros);
d.decimal_point += @as(i32, @intCast(n_trailing_zeros));
d.num_digits -= n_trailing_zeros;
d.decimal_point += @intCast(i32, d.num_digits);
d.decimal_point += @as(i32, @intCast(d.num_digits));
if (d.num_digits > max_digits) {
d.truncated = true;
d.num_digits = max_digits;

View File

@@ -21,7 +21,7 @@ fn parse8Digits(v_: u64) u64 {
v = (v * 10) + (v >> 8); // will not overflow, fits in 63 bits
const v1 = (v & mask) *% mul1;
const v2 = ((v >> 16) & mask) *% mul2;
return @as(u64, @truncate(u32, (v1 +% v2) >> 32));
return @as(u64, @as(u32, @truncate((v1 +% v2) >> 32)));
}
/// Parse digits until a non-digit character is found.
@@ -106,7 +106,7 @@ fn parsePartialNumberBase(comptime T: type, stream: *FloatStream, negative: bool
var mantissa: MantissaT = 0;
tryParseDigits(MantissaT, stream, &mantissa, info.base);
var int_end = stream.offsetTrue();
var n_digits = @intCast(isize, stream.offsetTrue());
var n_digits = @as(isize, @intCast(stream.offsetTrue()));
// the base being 16 implies a 0x prefix, which shouldn't be included in the digit count
if (info.base == 16) n_digits -= 2;
@@ -117,8 +117,8 @@ fn parsePartialNumberBase(comptime T: type, stream: *FloatStream, negative: bool
const marker = stream.offsetTrue();
tryParseDigits(MantissaT, stream, &mantissa, info.base);
const n_after_dot = stream.offsetTrue() - marker;
exponent = -@intCast(i64, n_after_dot);
n_digits += @intCast(isize, n_after_dot);
exponent = -@as(i64, @intCast(n_after_dot));
n_digits += @as(isize, @intCast(n_after_dot));
}
// adjust required shift to offset mantissa for base-16 (2^4)
@@ -163,7 +163,7 @@ fn parsePartialNumberBase(comptime T: type, stream: *FloatStream, negative: bool
// '0' = '.' + 2
const next = stream.firstUnchecked();
if (next != '_') {
n_digits -= @intCast(isize, next -| ('0' - 1));
n_digits -= @as(isize, @intCast(next -| ('0' - 1)));
} else {
stream.underscore_count += 1;
}
@@ -179,7 +179,7 @@ fn parsePartialNumberBase(comptime T: type, stream: *FloatStream, negative: bool
exponent = blk: {
if (mantissa >= min_n_digit_int(MantissaT, info.max_mantissa_digits)) {
// big int
break :blk @intCast(i64, int_end) - @intCast(i64, stream.offsetTrue());
break :blk @as(i64, @intCast(int_end)) - @as(i64, @intCast(stream.offsetTrue()));
} else {
// the next byte must be present and be '.'
// We know this is true because we had more than 19
@@ -190,7 +190,7 @@ fn parsePartialNumberBase(comptime T: type, stream: *FloatStream, negative: bool
stream.advance(1);
var marker = stream.offsetTrue();
tryParseNDigits(MantissaT, stream, &mantissa, info.base, info.max_mantissa_digits);
break :blk @intCast(i64, marker) - @intCast(i64, stream.offsetTrue());
break :blk @as(i64, @intCast(marker)) - @as(i64, @intCast(stream.offsetTrue()));
}
};
// add back the explicit part