diff options
| author | Andrew Kelley <andrew@ziglang.org> | 2023-06-24 16:58:19 -0700 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-06-24 16:58:19 -0700 |
| commit | 146b79af153bbd5dafda0ba12a040385c7fc58f8 (patch) | |
| tree | 67e3db8b444d65c667e314770fc983a7fc8ba293 /lib/std/math | |
| parent | 13853bef0df3c90633021850cc6d6abaeea03282 (diff) | |
| parent | 21ac0beb436f49fe49c6982a872f2dc48e4bea5e (diff) | |
| download | zig-146b79af153bbd5dafda0ba12a040385c7fc58f8.tar.gz zig-146b79af153bbd5dafda0ba12a040385c7fc58f8.zip | |
Merge pull request #16163 from mlugg/feat/builtins-infer-dest-ty
Infer destination type of cast builtins using result type
Diffstat (limited to 'lib/std/math')
39 files changed, 288 insertions, 288 deletions
diff --git a/lib/std/math/acos.zig b/lib/std/math/acos.zig index e88bed7227..1a29ca7b54 100644 --- a/lib/std/math/acos.zig +++ b/lib/std/math/acos.zig @@ -36,7 +36,7 @@ fn acos32(x: f32) f32 { const pio2_hi = 1.5707962513e+00; const pio2_lo = 7.5497894159e-08; - const hx: u32 = @bitCast(u32, x); + const hx: u32 = @as(u32, @bitCast(x)); const ix: u32 = hx & 0x7FFFFFFF; // |x| >= 1 or nan @@ -72,8 +72,8 @@ fn acos32(x: f32) f32 { // x > 0.5 const z = (1.0 - x) * 0.5; const s = @sqrt(z); - const jx = @bitCast(u32, s); - const df = @bitCast(f32, jx & 0xFFFFF000); + const jx = @as(u32, @bitCast(s)); + const df = @as(f32, @bitCast(jx & 0xFFFFF000)); const c = (z - df * df) / (s + df); const w = r32(z) * s + c; return 2 * (df + w); @@ -100,13 +100,13 @@ fn acos64(x: f64) f64 { const pio2_hi: f64 = 1.57079632679489655800e+00; const pio2_lo: f64 = 6.12323399573676603587e-17; - const ux = @bitCast(u64, x); - const hx = @intCast(u32, ux >> 32); + const ux = @as(u64, @bitCast(x)); + const hx = @as(u32, @intCast(ux >> 32)); const ix = hx & 0x7FFFFFFF; // |x| >= 1 or nan if (ix >= 0x3FF00000) { - const lx = @intCast(u32, ux & 0xFFFFFFFF); + const lx = @as(u32, @intCast(ux & 0xFFFFFFFF)); // acos(1) = 0, acos(-1) = pi if ((ix - 0x3FF00000) | lx == 0) { @@ -141,8 +141,8 @@ fn acos64(x: f64) f64 { // x > 0.5 const z = (1.0 - x) * 0.5; const s = @sqrt(z); - const jx = @bitCast(u64, s); - const df = @bitCast(f64, jx & 0xFFFFFFFF00000000); + const jx = @as(u64, @bitCast(s)); + const df = @as(f64, @bitCast(jx & 0xFFFFFFFF00000000)); const c = (z - df * df) / (s + df); const w = r64(z) * s + c; return 2 * (df + w); diff --git a/lib/std/math/acosh.zig b/lib/std/math/acosh.zig index a78130d2ef..0c6de9933e 100644 --- a/lib/std/math/acosh.zig +++ b/lib/std/math/acosh.zig @@ -24,7 +24,7 @@ pub fn acosh(x: anytype) @TypeOf(x) { // acosh(x) = log(x + sqrt(x * x - 1)) fn acosh32(x: f32) f32 { - const u = @bitCast(u32, x); + const u = @as(u32, @bitCast(x)); const i = u & 0x7FFFFFFF; // |x| < 2, invalid if x < 1 or nan @@ -42,7 +42,7 @@ fn acosh32(x: f32) f32 { } fn acosh64(x: f64) f64 { - const u = @bitCast(u64, x); + const u = @as(u64, @bitCast(x)); const e = (u >> 52) & 0x7FF; // |x| < 2, invalid if x < 1 or nan diff --git a/lib/std/math/asin.zig b/lib/std/math/asin.zig index 48ad04c579..ac1d01ff55 100644 --- a/lib/std/math/asin.zig +++ b/lib/std/math/asin.zig @@ -36,7 +36,7 @@ fn r32(z: f32) f32 { fn asin32(x: f32) f32 { const pio2 = 1.570796326794896558e+00; - const hx: u32 = @bitCast(u32, x); + const hx: u32 = @as(u32, @bitCast(x)); const ix: u32 = hx & 0x7FFFFFFF; // |x| >= 1 @@ -92,13 +92,13 @@ fn asin64(x: f64) f64 { const pio2_hi: f64 = 1.57079632679489655800e+00; const pio2_lo: f64 = 6.12323399573676603587e-17; - const ux = @bitCast(u64, x); - const hx = @intCast(u32, ux >> 32); + const ux = @as(u64, @bitCast(x)); + const hx = @as(u32, @intCast(ux >> 32)); const ix = hx & 0x7FFFFFFF; // |x| >= 1 or nan if (ix >= 0x3FF00000) { - const lx = @intCast(u32, ux & 0xFFFFFFFF); + const lx = @as(u32, @intCast(ux & 0xFFFFFFFF)); // asin(1) = +-pi/2 with inexact if ((ix - 0x3FF00000) | lx == 0) { @@ -128,8 +128,8 @@ fn asin64(x: f64) f64 { if (ix >= 0x3FEF3333) { fx = pio2_hi - 2 * (s + s * r); } else { - const jx = @bitCast(u64, s); - const df = @bitCast(f64, jx & 0xFFFFFFFF00000000); + const jx = @as(u64, @bitCast(s)); + const df = @as(f64, @bitCast(jx & 0xFFFFFFFF00000000)); const c = (z - df * df) / (s + df); fx = 0.5 * pio2_hi - (2 * s * r - (pio2_lo - 2 * c) - (0.5 * pio2_hi - 2 * df)); } diff --git a/lib/std/math/asinh.zig b/lib/std/math/asinh.zig index 65028ef5d9..13b1045bf6 100644 --- a/lib/std/math/asinh.zig +++ b/lib/std/math/asinh.zig @@ -26,11 +26,11 @@ pub fn asinh(x: anytype) @TypeOf(x) { // asinh(x) = sign(x) * log(|x| + sqrt(x * x + 1)) ~= x - x^3/6 + o(x^5) fn asinh32(x: f32) f32 { - const u = @bitCast(u32, x); + const u = @as(u32, @bitCast(x)); const i = u & 0x7FFFFFFF; const s = i >> 31; - var rx = @bitCast(f32, i); // |x| + var rx = @as(f32, @bitCast(i)); // |x| // TODO: Shouldn't need this explicit check. if (math.isNegativeInf(x)) { @@ -58,11 +58,11 @@ fn asinh32(x: f32) f32 { } fn asinh64(x: f64) f64 { - const u = @bitCast(u64, x); + const u = @as(u64, @bitCast(x)); const e = (u >> 52) & 0x7FF; const s = e >> 63; - var rx = @bitCast(f64, u & (maxInt(u64) >> 1)); // |x| + var rx = @as(f64, @bitCast(u & (maxInt(u64) >> 1))); // |x| if (math.isNegativeInf(x)) { return x; diff --git a/lib/std/math/atan.zig b/lib/std/math/atan.zig index 41caae11a6..75be6ea746 100644 --- a/lib/std/math/atan.zig +++ b/lib/std/math/atan.zig @@ -46,7 +46,7 @@ fn atan32(x_: f32) f32 { }; var x = x_; - var ix: u32 = @bitCast(u32, x); + var ix: u32 = @as(u32, @bitCast(x)); const sign = ix >> 31; ix &= 0x7FFFFFFF; @@ -143,8 +143,8 @@ fn atan64(x_: f64) f64 { }; var x = x_; - var ux = @bitCast(u64, x); - var ix = @intCast(u32, ux >> 32); + var ux = @as(u64, @bitCast(x)); + var ix = @as(u32, @intCast(ux >> 32)); const sign = ix >> 31; ix &= 0x7FFFFFFF; @@ -165,7 +165,7 @@ fn atan64(x_: f64) f64 { // |x| < 2^(-27) if (ix < 0x3E400000) { if (ix < 0x00100000) { - math.doNotOptimizeAway(@floatCast(f32, x)); + math.doNotOptimizeAway(@as(f32, @floatCast(x))); } return x; } @@ -212,7 +212,7 @@ fn atan64(x_: f64) f64 { } test "math.atan" { - try expect(@bitCast(u32, atan(@as(f32, 0.2))) == @bitCast(u32, atan32(0.2))); + try expect(@as(u32, @bitCast(atan(@as(f32, 0.2)))) == @as(u32, @bitCast(atan32(0.2)))); try expect(atan(@as(f64, 0.2)) == atan64(0.2)); } diff --git a/lib/std/math/atan2.zig b/lib/std/math/atan2.zig index b9b37e7da4..026c76b5b2 100644 --- a/lib/std/math/atan2.zig +++ b/lib/std/math/atan2.zig @@ -44,8 +44,8 @@ fn atan2_32(y: f32, x: f32) f32 { return x + y; } - var ix = @bitCast(u32, x); - var iy = @bitCast(u32, y); + var ix = @as(u32, @bitCast(x)); + var iy = @as(u32, @bitCast(y)); // x = 1.0 if (ix == 0x3F800000) { @@ -129,13 +129,13 @@ fn atan2_64(y: f64, x: f64) f64 { return x + y; } - var ux = @bitCast(u64, x); - var ix = @intCast(u32, ux >> 32); - var lx = @intCast(u32, ux & 0xFFFFFFFF); + var ux = @as(u64, @bitCast(x)); + var ix = @as(u32, @intCast(ux >> 32)); + var lx = @as(u32, @intCast(ux & 0xFFFFFFFF)); - var uy = @bitCast(u64, y); - var iy = @intCast(u32, uy >> 32); - var ly = @intCast(u32, uy & 0xFFFFFFFF); + var uy = @as(u64, @bitCast(y)); + var iy = @as(u32, @intCast(uy >> 32)); + var ly = @as(u32, @intCast(uy & 0xFFFFFFFF)); // x = 1.0 if ((ix -% 0x3FF00000) | lx == 0) { diff --git a/lib/std/math/atanh.zig b/lib/std/math/atanh.zig index aed5d8bca8..58b56ac8fa 100644 --- a/lib/std/math/atanh.zig +++ b/lib/std/math/atanh.zig @@ -26,11 +26,11 @@ pub fn atanh(x: anytype) @TypeOf(x) { // atanh(x) = log((1 + x) / (1 - x)) / 2 = log1p(2x / (1 - x)) / 2 ~= x + x^3 / 3 + o(x^5) fn atanh_32(x: f32) f32 { - const u = @bitCast(u32, x); + const u = @as(u32, @bitCast(x)); const i = u & 0x7FFFFFFF; const s = u >> 31; - var y = @bitCast(f32, i); // |x| + var y = @as(f32, @bitCast(i)); // |x| if (y == 1.0) { return math.copysign(math.inf(f32), x); @@ -55,11 +55,11 @@ fn atanh_32(x: f32) f32 { } fn atanh_64(x: f64) f64 { - const u = @bitCast(u64, x); + const u = @as(u64, @bitCast(x)); const e = (u >> 52) & 0x7FF; const s = u >> 63; - var y = @bitCast(f64, u & (maxInt(u64) >> 1)); // |x| + var y = @as(f64, @bitCast(u & (maxInt(u64) >> 1))); // |x| if (y == 1.0) { return math.copysign(math.inf(f64), x); @@ -69,7 +69,7 @@ fn atanh_64(x: f64) f64 { if (e < 0x3FF - 32) { // underflow if (e == 0) { - math.doNotOptimizeAway(@floatCast(f32, y)); + math.doNotOptimizeAway(@as(f32, @floatCast(y))); } } // |x| < 0.5 diff --git a/lib/std/math/big/int.zig b/lib/std/math/big/int.zig index 846a809e05..213876ccad 100644 --- a/lib/std/math/big/int.zig +++ b/lib/std/math/big/int.zig @@ -30,7 +30,7 @@ pub fn calcLimbLen(scalar: anytype) usize { } const w_value = std.math.absCast(scalar); - return @intCast(usize, @divFloor(@intCast(Limb, math.log2(w_value)), limb_bits) + 1); + return @as(usize, @intCast(@divFloor(@as(Limb, @intCast(math.log2(w_value))), limb_bits) + 1)); } pub fn calcToStringLimbsBufferLen(a_len: usize, base: u8) usize { @@ -87,8 +87,8 @@ pub fn addMulLimbWithCarry(a: Limb, b: Limb, c: Limb, carry: *Limb) Limb { // r2 = b * c const bc = @as(DoubleLimb, math.mulWide(Limb, b, c)); - const r2 = @truncate(Limb, bc); - const c2 = @truncate(Limb, bc >> limb_bits); + const r2 = @as(Limb, @truncate(bc)); + const c2 = @as(Limb, @truncate(bc >> limb_bits)); // ov2[0] = ov1[0] + r2 const ov2 = @addWithOverflow(ov1[0], r2); @@ -107,8 +107,8 @@ fn subMulLimbWithBorrow(a: Limb, b: Limb, c: Limb, carry: *Limb) Limb { // r2 = b * c const bc = @as(DoubleLimb, std.math.mulWide(Limb, b, c)); - const r2 = @truncate(Limb, bc); - const c2 = @truncate(Limb, bc >> limb_bits); + const r2 = @as(Limb, @truncate(bc)); + const c2 = @as(Limb, @truncate(bc >> limb_bits)); // ov2[0] = ov1[0] - r2 const ov2 = @subWithOverflow(ov1[0], r2); @@ -244,7 +244,7 @@ pub const Mutable = struct { } else { var i: usize = 0; while (true) : (i += 1) { - self.limbs[i] = @truncate(Limb, w_value); + self.limbs[i] = @as(Limb, @truncate(w_value)); w_value >>= limb_bits; if (w_value == 0) break; @@ -340,7 +340,7 @@ pub const Mutable = struct { } const req_limbs = calcTwosCompLimbCount(bit_count); - const bit = @truncate(Log2Limb, bit_count - 1); + const bit = @as(Log2Limb, @truncate(bit_count - 1)); const signmask = @as(Limb, 1) << bit; // 0b0..010..0 where 1 is the sign bit. const mask = (signmask << 1) -% 1; // 0b0..011..1 where the leftmost 1 is the sign bit. @@ -365,7 +365,7 @@ pub const Mutable = struct { r.set(0); } else { const new_req_limbs = calcTwosCompLimbCount(bit_count - 1); - const msb = @truncate(Log2Limb, bit_count - 2); + const msb = @as(Log2Limb, @truncate(bit_count - 2)); const new_signmask = @as(Limb, 1) << msb; // 0b0..010..0 where 1 is the sign bit. const new_mask = (new_signmask << 1) -% 1; // 0b0..001..1 where the rightmost 0 is the sign bit. @@ -1153,7 +1153,7 @@ pub const Mutable = struct { // const msb = @truncate(Log2Limb, checkbit); // const checkmask = (@as(Limb, 1) << msb) -% 1; - if (a.limbs[a.limbs.len - 1] >> @truncate(Log2Limb, checkbit) != 0) { + if (a.limbs[a.limbs.len - 1] >> @as(Log2Limb, @truncate(checkbit)) != 0) { // Need to saturate. r.setTwosCompIntLimit(if (a.positive) .max else .min, signedness, bit_count); return; @@ -1554,7 +1554,7 @@ pub const Mutable = struct { // Optimization for small divisor. By using a half limb we can avoid requiring DoubleLimb // divisions in the hot code path. This may often require compiler_rt software-emulation. if (divisor < maxInt(HalfLimb)) { - lldiv0p5(q.limbs, &r.limbs[0], x.limbs[xy_trailing..x.len], @intCast(HalfLimb, divisor)); + lldiv0p5(q.limbs, &r.limbs[0], x.limbs[xy_trailing..x.len], @as(HalfLimb, @intCast(divisor))); } else { lldiv1(q.limbs, &r.limbs[0], x.limbs[xy_trailing..x.len], divisor); } @@ -1671,7 +1671,7 @@ pub const Mutable = struct { } else { const q0 = (@as(DoubleLimb, x.limbs[i]) << limb_bits) | @as(DoubleLimb, x.limbs[i - 1]); const n0 = @as(DoubleLimb, y.limbs[t]); - q.limbs[k] = @intCast(Limb, q0 / n0); + q.limbs[k] = @as(Limb, @intCast(q0 / n0)); } // 3.2 @@ -1750,7 +1750,7 @@ pub const Mutable = struct { return; } - const bit = @truncate(Log2Limb, bit_count - 1); + const bit = @as(Log2Limb, @truncate(bit_count - 1)); const signmask = @as(Limb, 1) << bit; const mask = (signmask << 1) -% 1; @@ -1781,7 +1781,7 @@ pub const Mutable = struct { return; } - const bit = @truncate(Log2Limb, bit_count - 1); + const bit = @as(Log2Limb, @truncate(bit_count - 1)); const signmask = @as(Limb, 1) << bit; // 0b0..010...0 where 1 is the sign bit. const mask = (signmask << 1) -% 1; // 0b0..01..1 where the leftmost 1 is the sign bit. @@ -1912,7 +1912,7 @@ pub const Mutable = struct { .Big => buffer.len - ((total_bits + 7) / 8), }; - const sign_bit = @as(u8, 1) << @intCast(u3, (total_bits - 1) % 8); + const sign_bit = @as(u8, 1) << @as(u3, @intCast((total_bits - 1) % 8)); positive = ((buffer[last_byte] & sign_bit) == 0); } @@ -1942,7 +1942,7 @@ pub const Mutable = struct { .signed => b: { const SLimb = std.meta.Int(.signed, @bitSizeOf(Limb)); const limb = mem.readVarPackedInt(SLimb, buffer, bit_index + bit_offset, bit_count - bit_index, endian, .signed); - break :b @bitCast(Limb, limb); + break :b @as(Limb, @bitCast(limb)); }, }; @@ -2170,7 +2170,7 @@ pub const Const = struct { var r: UT = 0; if (@sizeOf(UT) <= @sizeOf(Limb)) { - r = @intCast(UT, self.limbs[0]); + r = @as(UT, @intCast(self.limbs[0])); } else { for (self.limbs[0..self.limbs.len], 0..) |_, ri| { const limb = self.limbs[self.limbs.len - ri - 1]; @@ -2180,10 +2180,10 @@ pub const Const = struct { } if (info.signedness == .unsigned) { - return if (self.positive) @intCast(T, r) else error.NegativeIntoUnsigned; + return if (self.positive) @as(T, @intCast(r)) else error.NegativeIntoUnsigned; } else { if (self.positive) { - return @intCast(T, r); + return @as(T, @intCast(r)); } else { if (math.cast(T, r)) |ok| { return -ok; @@ -2292,7 +2292,7 @@ pub const Const = struct { outer: for (self.limbs[0..self.limbs.len]) |limb| { var shift: usize = 0; while (shift < limb_bits) : (shift += base_shift) { - const r = @intCast(u8, (limb >> @intCast(Log2Limb, shift)) & @as(Limb, base - 1)); + const r = @as(u8, @intCast((limb >> @as(Log2Limb, @intCast(shift))) & @as(Limb, base - 1))); const ch = std.fmt.digitToChar(r, case); string[digits_len] = ch; digits_len += 1; @@ -2340,7 +2340,7 @@ pub const Const = struct { var r_word = r.limbs[0]; var i: usize = 0; while (i < digits_per_limb) : (i += 1) { - const ch = std.fmt.digitToChar(@intCast(u8, r_word % base), case); + const ch = std.fmt.digitToChar(@as(u8, @intCast(r_word % base)), case); r_word /= base; string[digits_len] = ch; digits_len += 1; @@ -2352,7 +2352,7 @@ pub const Const = struct { var r_word = q.limbs[0]; while (r_word != 0) { - const ch = std.fmt.digitToChar(@intCast(u8, r_word % base), case); + const ch = std.fmt.digitToChar(@as(u8, @intCast(r_word % base)), case); r_word /= base; string[digits_len] = ch; digits_len += 1; @@ -3680,13 +3680,13 @@ fn lldiv1(quo: []Limb, rem: *Limb, a: []const Limb, b: Limb) void { rem.* = 0; } else if (pdiv < b) { quo[i] = 0; - rem.* = @truncate(Limb, pdiv); + rem.* = @as(Limb, @truncate(pdiv)); } else if (pdiv == b) { quo[i] = 1; rem.* = 0; } else { - quo[i] = @truncate(Limb, @divTrunc(pdiv, b)); - rem.* = @truncate(Limb, pdiv - (quo[i] *% b)); + quo[i] = @as(Limb, @truncate(@divTrunc(pdiv, b))); + rem.* = @as(Limb, @truncate(pdiv - (quo[i] *% b))); } } } @@ -3719,7 +3719,7 @@ fn llshl(r: []Limb, a: []const Limb, shift: usize) void { @setRuntimeSafety(debug_safety); assert(a.len >= 1); - const interior_limb_shift = @truncate(Log2Limb, shift); + const interior_limb_shift = @as(Log2Limb, @truncate(shift)); // We only need the extra limb if the shift of the last element overflows. // This is useful for the implementation of `shiftLeftSat`. @@ -3741,7 +3741,7 @@ fn llshl(r: []Limb, a: []const Limb, shift: usize) void { r[dst_i] = carry | @call(.always_inline, math.shr, .{ Limb, src_digit, - limb_bits - @intCast(Limb, interior_limb_shift), + limb_bits - @as(Limb, @intCast(interior_limb_shift)), }); carry = (src_digit << interior_limb_shift); } @@ -3756,7 +3756,7 @@ fn llshr(r: []Limb, a: []const Limb, shift: usize) void { assert(r.len >= a.len - (shift / limb_bits)); const limb_shift = shift / limb_bits; - const interior_limb_shift = @truncate(Log2Limb, shift); + const interior_limb_shift = @as(Log2Limb, @truncate(shift)); var carry: Limb = 0; var i: usize = 0; @@ -3769,7 +3769,7 @@ fn llshr(r: []Limb, a: []const Limb, shift: usize) void { carry = @call(.always_inline, math.shl, .{ Limb, src_digit, - limb_bits - @intCast(Limb, interior_limb_shift), + limb_bits - @as(Limb, @intCast(interior_limb_shift)), }); } } @@ -4150,7 +4150,7 @@ fn llpow(r: []Limb, a: []const Limb, b: u32, tmp_limbs: []Limb) void { // Square the result if the current bit is zero, square and multiply by a if // it is one. var exp_bits = 32 - 1 - b_leading_zeros; - var exp = b << @intCast(u5, 1 + b_leading_zeros); + var exp = b << @as(u5, @intCast(1 + b_leading_zeros)); var i: usize = 0; while (i < exp_bits) : (i += 1) { @@ -4174,9 +4174,9 @@ fn fixedIntFromSignedDoubleLimb(A: SignedDoubleLimb, storage: []Limb) Mutable { assert(storage.len >= 2); const A_is_positive = A >= 0; - const Au = @intCast(DoubleLimb, if (A < 0) -A else A); - storage[0] = @truncate(Limb, Au); - storage[1] = @truncate(Limb, Au >> limb_bits); + const Au = @as(DoubleLimb, @intCast(if (A < 0) -A else A)); + storage[0] = @as(Limb, @truncate(Au)); + storage[1] = @as(Limb, @truncate(Au >> limb_bits)); return .{ .limbs = storage[0..2], .positive = A_is_positive, diff --git a/lib/std/math/big/int_test.zig b/lib/std/math/big/int_test.zig index 9c3c1b6881..3eaa46d7c1 100644 --- a/lib/std/math/big/int_test.zig +++ b/lib/std/math/big/int_test.zig @@ -2898,19 +2898,19 @@ test "big int conversion write twos complement with padding" { buffer = &[_]u8{ 0xd, 0xc, 0xb, 0xa, 0x9, 0x8, 0x7, 0x6, 0x5, 0x4, 0x3, 0x2, 0xaa }; m.readTwosComplement(buffer[0..13], bit_count, .Little, .unsigned); - try testing.expect(m.toConst().orderAgainstScalar(@truncate(Limb, 0xaa_02030405_06070809_0a0b0c0d)) == .eq); + try testing.expect(m.toConst().orderAgainstScalar(@as(Limb, @truncate(0xaa_02030405_06070809_0a0b0c0d))) == .eq); buffer = &[_]u8{ 0xaa, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8, 0x9, 0xa, 0xb, 0xc, 0xd }; m.readTwosComplement(buffer[0..13], bit_count, .Big, .unsigned); - try testing.expect(m.toConst().orderAgainstScalar(@truncate(Limb, 0xaa_02030405_06070809_0a0b0c0d)) == .eq); + try testing.expect(m.toConst().orderAgainstScalar(@as(Limb, @truncate(0xaa_02030405_06070809_0a0b0c0d))) == .eq); buffer = &[_]u8{ 0xd, 0xc, 0xb, 0xa, 0x9, 0x8, 0x7, 0x6, 0x5, 0x4, 0x3, 0x2, 0xaa, 0xaa, 0xaa, 0xaa }; m.readTwosComplement(buffer[0..16], bit_count, .Little, .unsigned); - try testing.expect(m.toConst().orderAgainstScalar(@truncate(Limb, 0xaaaaaaaa_02030405_06070809_0a0b0c0d)) == .eq); + try testing.expect(m.toConst().orderAgainstScalar(@as(Limb, @truncate(0xaaaaaaaa_02030405_06070809_0a0b0c0d))) == .eq); buffer = &[_]u8{ 0xaa, 0xaa, 0xaa, 0xaa, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8, 0x9, 0xa, 0xb, 0xc, 0xd }; m.readTwosComplement(buffer[0..16], bit_count, .Big, .unsigned); - try testing.expect(m.toConst().orderAgainstScalar(@truncate(Limb, 0xaaaaaaaa_02030405_06070809_0a0b0c0d)) == .eq); + try testing.expect(m.toConst().orderAgainstScalar(@as(Limb, @truncate(0xaaaaaaaa_02030405_06070809_0a0b0c0d))) == .eq); bit_count = 12 * 8 + 2; @@ -3014,20 +3014,20 @@ test "big int bit reverse" { try bitReverseTest(u96, 0x123456789abcdef111213141, 0x828c84888f7b3d591e6a2c48); try bitReverseTest(u128, 0x123456789abcdef11121314151617181, 0x818e868a828c84888f7b3d591e6a2c48); - try bitReverseTest(i8, @bitCast(i8, @as(u8, 0x92)), @bitCast(i8, @as(u8, 0x49))); - try bitReverseTest(i16, @bitCast(i16, @as(u16, 0x1234)), @bitCast(i16, @as(u16, 0x2c48))); - try bitReverseTest(i24, @bitCast(i24, @as(u24, 0x123456)), @bitCast(i24, @as(u24, 0x6a2c48))); - try bitReverseTest(i24, @bitCast(i24, @as(u24, 0x12345f)), @bitCast(i24, @as(u24, 0xfa2c48))); - try bitReverseTest(i24, @bitCast(i24, @as(u24, 0xf23456)), @bitCast(i24, @as(u24, 0x6a2c4f))); - try bitReverseTest(i32, @bitCast(i32, @as(u32, 0x12345678)), @bitCast(i32, @as(u32, 0x1e6a2c48))); - try bitReverseTest(i32, @bitCast(i32, @as(u32, 0xf2345678)), @bitCast(i32, @as(u32, 0x1e6a2c4f))); - try bitReverseTest(i32, @bitCast(i32, @as(u32, 0x1234567f)), @bitCast(i32, @as(u32, 0xfe6a2c48))); - try bitReverseTest(i40, @bitCast(i40, @as(u40, 0x123456789a)), @bitCast(i40, @as(u40, 0x591e6a2c48))); - try bitReverseTest(i48, @bitCast(i48, @as(u48, 0x123456789abc)), @bitCast(i48, @as(u48, 0x3d591e6a2c48))); - try bitReverseTest(i56, @bitCast(i56, @as(u56, 0x123456789abcde)), @bitCast(i56, @as(u56, 0x7b3d591e6a2c48))); - try bitReverseTest(i64, @bitCast(i64, @as(u64, 0x123456789abcdef1)), @bitCast(i64, @as(u64, 0x8f7b3d591e6a2c48))); - try bitReverseTest(i96, @bitCast(i96, @as(u96, 0x123456789abcdef111213141)), @bitCast(i96, @as(u96, 0x828c84888f7b3d591e6a2c48))); - try bitReverseTest(i128, @bitCast(i128, @as(u128, 0x123456789abcdef11121314151617181)), @bitCast(i128, @as(u128, 0x818e868a828c84888f7b3d591e6a2c48))); + try bitReverseTest(i8, @as(i8, @bitCast(@as(u8, 0x92))), @as(i8, @bitCast(@as(u8, 0x49)))); + try bitReverseTest(i16, @as(i16, @bitCast(@as(u16, 0x1234))), @as(i16, @bitCast(@as(u16, 0x2c48)))); + try bitReverseTest(i24, @as(i24, @bitCast(@as(u24, 0x123456))), @as(i24, @bitCast(@as(u24, 0x6a2c48)))); + try bitReverseTest(i24, @as(i24, @bitCast(@as(u24, 0x12345f))), @as(i24, @bitCast(@as(u24, 0xfa2c48)))); + try bitReverseTest(i24, @as(i24, @bitCast(@as(u24, 0xf23456))), @as(i24, @bitCast(@as(u24, 0x6a2c4f)))); + try bitReverseTest(i32, @as(i32, @bitCast(@as(u32, 0x12345678))), @as(i32, @bitCast(@as(u32, 0x1e6a2c48)))); + try bitReverseTest(i32, @as(i32, @bitCast(@as(u32, 0xf2345678))), @as(i32, @bitCast(@as(u32, 0x1e6a2c4f)))); + try bitReverseTest(i32, @as(i32, @bitCast(@as(u32, 0x1234567f))), @as(i32, @bitCast(@as(u32, 0xfe6a2c48)))); + try bitReverseTest(i40, @as(i40, @bitCast(@as(u40, 0x123456789a))), @as(i40, @bitCast(@as(u40, 0x591e6a2c48)))); + try bitReverseTest(i48, @as(i48, @bitCast(@as(u48, 0x123456789abc))), @as(i48, @bitCast(@as(u48, 0x3d591e6a2c48)))); + try bitReverseTest(i56, @as(i56, @bitCast(@as(u56, 0x123456789abcde))), @as(i56, @bitCast(@as(u56, 0x7b3d591e6a2c48)))); + try bitReverseTest(i64, @as(i64, @bitCast(@as(u64, 0x123456789abcdef1))), @as(i64, @bitCast(@as(u64, 0x8f7b3d591e6a2c48)))); + try bitReverseTest(i96, @as(i96, @bitCast(@as(u96, 0x123456789abcdef111213141))), @as(i96, @bitCast(@as(u96, 0x828c84888f7b3d591e6a2c48)))); + try bitReverseTest(i128, @as(i128, @bitCast(@as(u128, 0x123456789abcdef11121314151617181))), @as(i128, @bitCast(@as(u128, 0x818e868a828c84888f7b3d591e6a2c48)))); } fn byteSwapTest(comptime T: type, comptime input: comptime_int, comptime expected_output: comptime_int) !void { @@ -3063,16 +3063,16 @@ test "big int byte swap" { try byteSwapTest(u128, 0x123456789abcdef11121314151617181, 0x8171615141312111f1debc9a78563412); try byteSwapTest(i8, -50, -50); - try byteSwapTest(i16, @bitCast(i16, @as(u16, 0x1234)), @bitCast(i16, @as(u16, 0x3412))); - try byteSwapTest(i24, @bitCast(i24, @as(u24, 0x123456)), @bitCast(i24, @as(u24, 0x563412))); - try byteSwapTest(i32, @bitCast(i32, @as(u32, 0x12345678)), @bitCast(i32, @as(u32, 0x78563412))); - try byteSwapTest(i40, @bitCast(i40, @as(u40, 0x123456789a)), @bitCast(i40, @as(u40, 0x9a78563412))); - try byteSwapTest(i48, @bitCast(i48, @as(u48, 0x123456789abc)), @bitCast(i48, @as(u48, 0xbc9a78563412))); - try byteSwapTest(i56, @bitCast(i56, @as(u56, 0x123456789abcde)), @bitCast(i56, @as(u56, 0xdebc9a78563412))); - try byteSwapTest(i64, @bitCast(i64, @as(u64, 0x123456789abcdef1)), @bitCast(i64, @as(u64, 0xf1debc9a78563412))); - try byteSwapTest(i88, @bitCast(i88, @as(u88, 0x123456789abcdef1112131)), @bitCast(i88, @as(u88, 0x312111f1debc9a78563412))); - try byteSwapTest(i96, @bitCast(i96, @as(u96, 0x123456789abcdef111213141)), @bitCast(i96, @as(u96, 0x41312111f1debc9a78563412))); - try byteSwapTest(i128, @bitCast(i128, @as(u128, 0x123456789abcdef11121314151617181)), @bitCast(i128, @as(u128, 0x8171615141312111f1debc9a78563412))); + try byteSwapTest(i16, @as(i16, @bitCast(@as(u16, 0x1234))), @as(i16, @bitCast(@as(u16, 0x3412)))); + try byteSwapTest(i24, @as(i24, @bitCast(@as(u24, 0x123456))), @as(i24, @bitCast(@as(u24, 0x563412)))); + try byteSwapTest(i32, @as(i32, @bitCast(@as(u32, 0x12345678))), @as(i32, @bitCast(@as(u32, 0x78563412)))); + try byteSwapTest(i40, @as(i40, @bitCast(@as(u40, 0x123456789a))), @as(i40, @bitCast(@as(u40, 0x9a78563412)))); + try byteSwapTest(i48, @as(i48, @bitCast(@as(u48, 0x123456789abc))), @as(i48, @bitCast(@as(u48, 0xbc9a78563412)))); + try byteSwapTest(i56, @as(i56, @bitCast(@as(u56, 0x123456789abcde))), @as(i56, @bitCast(@as(u56, 0xdebc9a78563412)))); + try byteSwapTest(i64, @as(i64, @bitCast(@as(u64, 0x123456789abcdef1))), @as(i64, @bitCast(@as(u64, 0xf1debc9a78563412)))); + try byteSwapTest(i88, @as(i88, @bitCast(@as(u88, 0x123456789abcdef1112131))), @as(i88, @bitCast(@as(u88, 0x312111f1debc9a78563412)))); + try byteSwapTest(i96, @as(i96, @bitCast(@as(u96, 0x123456789abcdef111213141))), @as(i96, @bitCast(@as(u96, 0x41312111f1debc9a78563412)))); + try byteSwapTest(i128, @as(i128, @bitCast(@as(u128, 0x123456789abcdef11121314151617181))), @as(i128, @bitCast(@as(u128, 0x8171615141312111f1debc9a78563412)))); try byteSwapTest(u512, 0x80, 1 << 511); try byteSwapTest(i512, 0x80, minInt(i512)); @@ -3080,11 +3080,11 @@ test "big int byte swap" { try byteSwapTest(i512, -0x100, (1 << 504) - 1); try byteSwapTest(i400, -0x100, (1 << 392) - 1); try byteSwapTest(i400, -0x2, -(1 << 392) - 1); - try byteSwapTest(i24, @bitCast(i24, @as(u24, 0xf23456)), 0x5634f2); - try byteSwapTest(i24, 0x1234f6, @bitCast(i24, @as(u24, 0xf63412))); - try byteSwapTest(i32, @bitCast(i32, @as(u32, 0xf2345678)), 0x785634f2); - try byteSwapTest(i32, 0x123456f8, @bitCast(i32, @as(u32, 0xf8563412))); - try byteSwapTest(i48, 0x123456789abc, @bitCast(i48, @as(u48, 0xbc9a78563412))); + try byteSwapTest(i24, @as(i24, @bitCast(@as(u24, 0xf23456))), 0x5634f2); + try byteSwapTest(i24, 0x1234f6, @as(i24, @bitCast(@as(u24, 0xf63412)))); + try byteSwapTest(i32, @as(i32, @bitCast(@as(u32, 0xf2345678))), 0x785634f2); + try byteSwapTest(i32, 0x123456f8, @as(i32, @bitCast(@as(u32, 0xf8563412)))); + try byteSwapTest(i48, 0x123456789abc, @as(i48, @bitCast(@as(u48, 0xbc9a78563412)))); } test "big.int mul multi-multi alias r with a and b" { diff --git a/lib/std/math/big/rational.zig b/lib/std/math/big/rational.zig index 22f7ba183f..5313380c27 100644 --- a/lib/std/math/big/rational.zig +++ b/lib/std/math/big/rational.zig @@ -137,7 +137,7 @@ pub const Rational = struct { debug.assert(@typeInfo(T) == .Float); const UnsignedInt = std.meta.Int(.unsigned, @typeInfo(T).Float.bits); - const f_bits = @bitCast(UnsignedInt, f); + const f_bits = @as(UnsignedInt, @bitCast(f)); const exponent_bits = math.floatExponentBits(T); const exponent_bias = (1 << (exponent_bits - 1)) - 1; @@ -146,7 +146,7 @@ pub const Rational = struct { const exponent_mask = (1 << exponent_bits) - 1; const mantissa_mask = (1 << mantissa_bits) - 1; - var exponent = @intCast(i16, (f_bits >> mantissa_bits) & exponent_mask); + var exponent = @as(i16, @intCast((f_bits >> mantissa_bits) & exponent_mask)); var mantissa = f_bits & mantissa_mask; switch (exponent) { @@ -177,9 +177,9 @@ pub const Rational = struct { try self.q.set(1); if (shift >= 0) { - try self.q.shiftLeft(&self.q, @intCast(usize, shift)); + try self.q.shiftLeft(&self.q, @as(usize, @intCast(shift))); } else { - try self.p.shiftLeft(&self.p, @intCast(usize, -shift)); + try self.p.shiftLeft(&self.p, @as(usize, @intCast(-shift))); } try self.reduce(); @@ -210,7 +210,7 @@ pub const Rational = struct { } // 1. left-shift a or sub so that a/b is in [1 << msize1, 1 << (msize2 + 1)] - var exp = @intCast(isize, self.p.bitCountTwosComp()) - @intCast(isize, self.q.bitCountTwosComp()); + var exp = @as(isize, @intCast(self.p.bitCountTwosComp())) - @as(isize, @intCast(self.q.bitCountTwosComp())); var a2 = try self.p.clone(); defer a2.deinit(); @@ -220,9 +220,9 @@ pub const Rational = struct { const shift = msize2 - exp; if (shift >= 0) { - try a2.shiftLeft(&a2, @intCast(usize, shift)); + try a2.shiftLeft(&a2, @as(usize, @intCast(shift))); } else { - try b2.shiftLeft(&b2, @intCast(usize, -shift)); + try b2.shiftLeft(&b2, @as(usize, @intCast(-shift))); } // 2. compute quotient and remainder @@ -254,8 +254,8 @@ pub const Rational = struct { // 4. Rounding if (emin - msize <= exp and exp <= emin) { // denormal - const shift1 = @intCast(math.Log2Int(BitReprType), emin - (exp - 1)); - const lost_bits = mantissa & ((@intCast(BitReprType, 1) << shift1) - 1); + const shift1 = @as(math.Log2Int(BitReprType), @intCast(emin - (exp - 1))); + const lost_bits = mantissa & ((@as(BitReprType, @intCast(1)) << shift1) - 1); have_rem = have_rem or lost_bits != 0; mantissa >>= shift1; exp = 2 - ebias; @@ -276,7 +276,7 @@ pub const Rational = struct { } mantissa >>= 1; - const f = math.scalbn(@floatFromInt(T, mantissa), @intCast(i32, exp - msize1)); + const f = math.scalbn(@as(T, @floatFromInt(mantissa)), @as(i32, @intCast(exp - msize1))); if (math.isInf(f)) { exact = false; } @@ -477,7 +477,7 @@ fn extractLowBits(a: Int, comptime T: type) T { const t_bits = @typeInfo(T).Int.bits; const limb_bits = @typeInfo(Limb).Int.bits; if (t_bits <= limb_bits) { - return @truncate(T, a.limbs[0]); + return @as(T, @truncate(a.limbs[0])); } else { var r: T = 0; comptime var i: usize = 0; diff --git a/lib/std/math/cbrt.zig b/lib/std/math/cbrt.zig index 1ff1818e8d..737757b817 100644 --- a/lib/std/math/cbrt.zig +++ b/lib/std/math/cbrt.zig @@ -27,7 +27,7 @@ fn cbrt32(x: f32) f32 { const B1: u32 = 709958130; // (127 - 127.0 / 3 - 0.03306235651) * 2^23 const B2: u32 = 642849266; // (127 - 127.0 / 3 - 24 / 3 - 0.03306235651) * 2^23 - var u = @bitCast(u32, x); + var u = @as(u32, @bitCast(x)); var hx = u & 0x7FFFFFFF; // cbrt(nan, inf) = itself @@ -41,7 +41,7 @@ fn cbrt32(x: f32) f32 { if (hx == 0) { return x; } - u = @bitCast(u32, x * 0x1.0p24); + u = @as(u32, @bitCast(x * 0x1.0p24)); hx = u & 0x7FFFFFFF; hx = hx / 3 + B2; } else { @@ -52,7 +52,7 @@ fn cbrt32(x: f32) f32 { u |= hx; // first step newton to 16 bits - var t: f64 = @bitCast(f32, u); + var t: f64 = @as(f32, @bitCast(u)); var r: f64 = t * t * t; t = t * (@as(f64, x) + x + r) / (x + r + r); @@ -60,7 +60,7 @@ fn cbrt32(x: f32) f32 { r = t * t * t; t = t * (@as(f64, x) + x + r) / (x + r + r); - return @floatCast(f32, t); + return @as(f32, @floatCast(t)); } fn cbrt64(x: f64) f64 { @@ -74,8 +74,8 @@ fn cbrt64(x: f64) f64 { const P3: f64 = -0.758397934778766047437; const P4: f64 = 0.145996192886612446982; - var u = @bitCast(u64, x); - var hx = @intCast(u32, u >> 32) & 0x7FFFFFFF; + var u = @as(u64, @bitCast(x)); + var hx = @as(u32, @intCast(u >> 32)) & 0x7FFFFFFF; // cbrt(nan, inf) = itself if (hx >= 0x7FF00000) { @@ -84,8 +84,8 @@ fn cbrt64(x: f64) f64 { // cbrt to ~5bits if (hx < 0x00100000) { - u = @bitCast(u64, x * 0x1.0p54); - hx = @intCast(u32, u >> 32) & 0x7FFFFFFF; + u = @as(u64, @bitCast(x * 0x1.0p54)); + hx = @as(u32, @intCast(u >> 32)) & 0x7FFFFFFF; // cbrt(0) is itself if (hx == 0) { @@ -98,7 +98,7 @@ fn cbrt64(x: f64) f64 { u &= 1 << 63; u |= @as(u64, hx) << 32; - var t = @bitCast(f64, u); + var t = @as(f64, @bitCast(u)); // cbrt to 23 bits // cbrt(x) = t * cbrt(x / t^3) ~= t * P(t^3 / x) @@ -106,9 +106,9 @@ fn cbrt64(x: f64) f64 { t = t * ((P0 + r * (P1 + r * P2)) + ((r * r) * r) * (P3 + r * P4)); // Round t away from 0 to 23 bits - u = @bitCast(u64, t); + u = @as(u64, @bitCast(t)); u = (u + 0x80000000) & 0xFFFFFFFFC0000000; - t = @bitCast(f64, u); + t = @as(f64, @bitCast(u)); // one step newton to 53 bits const s = t * t; diff --git a/lib/std/math/complex/atan.zig b/lib/std/math/complex/atan.zig index 56c199016d..381fc43f7d 100644 --- a/lib/std/math/complex/atan.zig +++ b/lib/std/math/complex/atan.zig @@ -32,7 +32,7 @@ fn redupif32(x: f32) f32 { t -= 0.5; } - const u = @floatFromInt(f32, @intFromFloat(i32, t)); + const u = @as(f32, @floatFromInt(@as(i32, @intFromFloat(t)))); return ((x - u * DP1) - u * DP2) - t * DP3; } @@ -81,7 +81,7 @@ fn redupif64(x: f64) f64 { t -= 0.5; } - const u = @floatFromInt(f64, @intFromFloat(i64, t)); + const u = @as(f64, @floatFromInt(@as(i64, @intFromFloat(t)))); return ((x - u * DP1) - u * DP2) - t * DP3; } diff --git a/lib/std/math/complex/cosh.zig b/lib/std/math/complex/cosh.zig index b3ffab5175..413279db2d 100644 --- a/lib/std/math/complex/cosh.zig +++ b/lib/std/math/complex/cosh.zig @@ -26,10 +26,10 @@ fn cosh32(z: Complex(f32)) Complex(f32) { const x = z.re; const y = z.im; - const hx = @bitCast(u32, x); + const hx = @as(u32, @bitCast(x)); const ix = hx & 0x7fffffff; - const hy = @bitCast(u32, y); + const hy = @as(u32, @bitCast(y)); const iy = hy & 0x7fffffff; if (ix < 0x7f800000 and iy < 0x7f800000) { @@ -89,14 +89,14 @@ fn cosh64(z: Complex(f64)) Complex(f64) { const x = z.re; const y = z.im; - const fx = @bitCast(u64, x); - const hx = @intCast(u32, fx >> 32); - const lx = @truncate(u32, fx); + const fx = @as(u64, @bitCast(x)); + const hx = @as(u32, @intCast(fx >> 32)); + const lx = @as(u32, @truncate(fx)); const ix = hx & 0x7fffffff; - const fy = @bitCast(u64, y); - const hy = @intCast(u32, fy >> 32); - const ly = @truncate(u32, fy); + const fy = @as(u64, @bitCast(y)); + const hy = @as(u32, @intCast(fy >> 32)); + const ly = @as(u32, @truncate(fy)); const iy = hy & 0x7fffffff; // nearly non-exceptional case where x, y are finite diff --git a/lib/std/math/complex/exp.zig b/lib/std/math/complex/exp.zig index 84ee251d0e..4644ea4be7 100644 --- a/lib/std/math/complex/exp.zig +++ b/lib/std/math/complex/exp.zig @@ -30,13 +30,13 @@ fn exp32(z: Complex(f32)) Complex(f32) { const x = z.re; const y = z.im; - const hy = @bitCast(u32, y) & 0x7fffffff; + const hy = @as(u32, @bitCast(y)) & 0x7fffffff; // cexp(x + i0) = exp(x) + i0 if (hy == 0) { return Complex(f32).init(@exp(x), y); } - const hx = @bitCast(u32, x); + const hx = @as(u32, @bitCast(x)); // cexp(0 + iy) = cos(y) + isin(y) if ((hx & 0x7fffffff) == 0) { return Complex(f32).init(@cos(y), @sin(y)); @@ -75,18 +75,18 @@ fn exp64(z: Complex(f64)) Complex(f64) { const x = z.re; const y = z.im; - const fy = @bitCast(u64, y); - const hy = @intCast(u32, (fy >> 32) & 0x7fffffff); - const ly = @truncate(u32, fy); + const fy = @as(u64, @bitCast(y)); + const hy = @as(u32, @intCast((fy >> 32) & 0x7fffffff)); + const ly = @as(u32, @truncate(fy)); // cexp(x + i0) = exp(x) + i0 if (hy | ly == 0) { return Complex(f64).init(@exp(x), y); } - const fx = @bitCast(u64, x); - const hx = @intCast(u32, fx >> 32); - const lx = @truncate(u32, fx); + const fx = @as(u64, @bitCast(x)); + const hx = @as(u32, @intCast(fx >> 32)); + const lx = @as(u32, @truncate(fx)); // cexp(0 + iy) = cos(y) + isin(y) if ((hx & 0x7fffffff) | lx == 0) { diff --git a/lib/std/math/complex/ldexp.zig b/lib/std/math/complex/ldexp.zig index c196d4afe6..201b6305af 100644 --- a/lib/std/math/complex/ldexp.zig +++ b/lib/std/math/complex/ldexp.zig @@ -27,10 +27,10 @@ fn frexp_exp32(x: f32, expt: *i32) f32 { const kln2 = 162.88958740; // k * ln2 const exp_x = @exp(x - kln2); - const hx = @bitCast(u32, exp_x); + const hx = @as(u32, @bitCast(exp_x)); // TODO zig should allow this cast implicitly because it should know the value is in range - expt.* = @intCast(i32, hx >> 23) - (0x7f + 127) + k; - return @bitCast(f32, (hx & 0x7fffff) | ((0x7f + 127) << 23)); + expt.* = @as(i32, @intCast(hx >> 23)) - (0x7f + 127) + k; + return @as(f32, @bitCast((hx & 0x7fffff) | ((0x7f + 127) << 23))); } fn ldexp_cexp32(z: Complex(f32), expt: i32) Complex(f32) { @@ -39,10 +39,10 @@ fn ldexp_cexp32(z: Complex(f32), expt: i32) Complex(f32) { const exptf = expt + ex_expt; const half_expt1 = @divTrunc(exptf, 2); - const scale1 = @bitCast(f32, (0x7f + half_expt1) << 23); + const scale1 = @as(f32, @bitCast((0x7f + half_expt1) << 23)); const half_expt2 = exptf - half_expt1; - const scale2 = @bitCast(f32, (0x7f + half_expt2) << 23); + const scale2 = @as(f32, @bitCast((0x7f + half_expt2) << 23)); return Complex(f32).init( @cos(z.im) * exp_x * scale1 * scale2, @@ -56,14 +56,14 @@ fn frexp_exp64(x: f64, expt: *i32) f64 { const exp_x = @exp(x - kln2); - const fx = @bitCast(u64, exp_x); - const hx = @intCast(u32, fx >> 32); - const lx = @truncate(u32, fx); + const fx = @as(u64, @bitCast(exp_x)); + const hx = @as(u32, @intCast(fx >> 32)); + const lx = @as(u32, @truncate(fx)); - expt.* = @intCast(i32, hx >> 20) - (0x3ff + 1023) + k; + expt.* = @as(i32, @intCast(hx >> 20)) - (0x3ff + 1023) + k; const high_word = (hx & 0xfffff) | ((0x3ff + 1023) << 20); - return @bitCast(f64, (@as(u64, high_word) << 32) | lx); + return @as(f64, @bitCast((@as(u64, high_word) << 32) | lx)); } fn ldexp_cexp64(z: Complex(f64), expt: i32) Complex(f64) { @@ -72,10 +72,10 @@ fn ldexp_cexp64(z: Complex(f64), expt: i32) Complex(f64) { const exptf = @as(i64, expt + ex_expt); const half_expt1 = @divTrunc(exptf, 2); - const scale1 = @bitCast(f64, (0x3ff + half_expt1) << (20 + 32)); + const scale1 = @as(f64, @bitCast((0x3ff + half_expt1) << (20 + 32))); const half_expt2 = exptf - half_expt1; - const scale2 = @bitCast(f64, (0x3ff + half_expt2) << (20 + 32)); + const scale2 = @as(f64, @bitCast((0x3ff + half_expt2) << (20 + 32))); return Complex(f64).init( @cos(z.im) * exp_x * scale1 * scale2, diff --git a/lib/std/math/complex/sinh.zig b/lib/std/math/complex/sinh.zig index 9afb7faf30..c9ea0d04fc 100644 --- a/lib/std/math/complex/sinh.zig +++ b/lib/std/math/complex/sinh.zig @@ -26,10 +26,10 @@ fn sinh32(z: Complex(f32)) Complex(f32) { const x = z.re; const y = z.im; - const hx = @bitCast(u32, x); + const hx = @as(u32, @bitCast(x)); const ix = hx & 0x7fffffff; - const hy = @bitCast(u32, y); + const hy = @as(u32, @bitCast(y)); const iy = hy & 0x7fffffff; if (ix < 0x7f800000 and iy < 0x7f800000) { @@ -89,14 +89,14 @@ fn sinh64(z: Complex(f64)) Complex(f64) { const x = z.re; const y = z.im; - const fx = @bitCast(u64, x); - const hx = @intCast(u32, fx >> 32); - const lx = @truncate(u32, fx); + const fx = @as(u64, @bitCast(x)); + const hx = @as(u32, @intCast(fx >> 32)); + const lx = @as(u32, @truncate(fx)); const ix = hx & 0x7fffffff; - const fy = @bitCast(u64, y); - const hy = @intCast(u32, fy >> 32); - const ly = @truncate(u32, fy); + const fy = @as(u64, @bitCast(y)); + const hy = @as(u32, @intCast(fy >> 32)); + const ly = @as(u32, @truncate(fy)); const iy = hy & 0x7fffffff; if (ix < 0x7ff00000 and iy < 0x7ff00000) { diff --git a/lib/std/math/complex/sqrt.zig b/lib/std/math/complex/sqrt.zig index 456d10aa85..fe2e8e6531 100644 --- a/lib/std/math/complex/sqrt.zig +++ b/lib/std/math/complex/sqrt.zig @@ -58,14 +58,14 @@ fn sqrt32(z: Complex(f32)) Complex(f32) { if (dx >= 0) { const t = @sqrt((dx + math.hypot(f64, dx, dy)) * 0.5); return Complex(f32).init( - @floatCast(f32, t), - @floatCast(f32, dy / (2.0 * t)), + @as(f32, @floatCast(t)), + @as(f32, @floatCast(dy / (2.0 * t))), ); } else { const t = @sqrt((-dx + math.hypot(f64, dx, dy)) * 0.5); return Complex(f32).init( - @floatCast(f32, @fabs(y) / (2.0 * t)), - @floatCast(f32, math.copysign(t, y)), + @as(f32, @floatCast(@fabs(y) / (2.0 * t))), + @as(f32, @floatCast(math.copysign(t, y))), ); } } diff --git a/lib/std/math/complex/tanh.zig b/lib/std/math/complex/tanh.zig index 92e197e308..a90f141741 100644 --- a/lib/std/math/complex/tanh.zig +++ b/lib/std/math/complex/tanh.zig @@ -24,7 +24,7 @@ fn tanh32(z: Complex(f32)) Complex(f32) { const x = z.re; const y = z.im; - const hx = @bitCast(u32, x); + const hx = @as(u32, @bitCast(x)); const ix = hx & 0x7fffffff; if (ix >= 0x7f800000) { @@ -32,7 +32,7 @@ fn tanh32(z: Complex(f32)) Complex(f32) { const r = if (y == 0) y else x * y; return Complex(f32).init(x, r); } - const xx = @bitCast(f32, hx - 0x40000000); + const xx = @as(f32, @bitCast(hx - 0x40000000)); const r = if (math.isInf(y)) y else @sin(y) * @cos(y); return Complex(f32).init(xx, math.copysign(@as(f32, 0.0), r)); } @@ -62,11 +62,11 @@ fn tanh64(z: Complex(f64)) Complex(f64) { const x = z.re; const y = z.im; - const fx = @bitCast(u64, x); + const fx = @as(u64, @bitCast(x)); // TODO: zig should allow this conversion implicitly because it can notice that the value necessarily // fits in range. - const hx = @intCast(u32, fx >> 32); - const lx = @truncate(u32, fx); + const hx = @as(u32, @intCast(fx >> 32)); + const lx = @as(u32, @truncate(fx)); const ix = hx & 0x7fffffff; if (ix >= 0x7ff00000) { @@ -75,7 +75,7 @@ fn tanh64(z: Complex(f64)) Complex(f64) { return Complex(f64).init(x, r); } - const xx = @bitCast(f64, (@as(u64, hx - 0x40000000) << 32) | lx); + const xx = @as(f64, @bitCast((@as(u64, hx - 0x40000000) << 32) | lx)); const r = if (math.isInf(y)) y else @sin(y) * @cos(y); return Complex(f64).init(xx, math.copysign(@as(f64, 0.0), r)); } diff --git a/lib/std/math/copysign.zig b/lib/std/math/copysign.zig index b5fd6d4d9a..3cefc0471f 100644 --- a/lib/std/math/copysign.zig +++ b/lib/std/math/copysign.zig @@ -7,9 +7,9 @@ pub fn copysign(magnitude: anytype, sign: @TypeOf(magnitude)) @TypeOf(magnitude) const T = @TypeOf(magnitude); const TBits = std.meta.Int(.unsigned, @typeInfo(T).Float.bits); const sign_bit_mask = @as(TBits, 1) << (@bitSizeOf(T) - 1); - const mag = @bitCast(TBits, magnitude) & ~sign_bit_mask; - const sgn = @bitCast(TBits, sign) & sign_bit_mask; - return @bitCast(T, mag | sgn); + const mag = @as(TBits, @bitCast(magnitude)) & ~sign_bit_mask; + const sgn = @as(TBits, @bitCast(sign)) & sign_bit_mask; + return @as(T, @bitCast(mag | sgn)); } test "math.copysign" { diff --git a/lib/std/math/cosh.zig b/lib/std/math/cosh.zig index d633f2fa0c..085d6fd2f9 100644 --- a/lib/std/math/cosh.zig +++ b/lib/std/math/cosh.zig @@ -29,9 +29,9 @@ pub fn cosh(x: anytype) @TypeOf(x) { // = 1 + 0.5 * (exp(x) - 1) * (exp(x) - 1) / exp(x) // = 1 + (x * x) / 2 + o(x^4) fn cosh32(x: f32) f32 { - const u = @bitCast(u32, x); + const u = @as(u32, @bitCast(x)); const ux = u & 0x7FFFFFFF; - const ax = @bitCast(f32, ux); + const ax = @as(f32, @bitCast(ux)); // |x| < log(2) if (ux < 0x3F317217) { @@ -54,9 +54,9 @@ fn cosh32(x: f32) f32 { } fn cosh64(x: f64) f64 { - const u = @bitCast(u64, x); - const w = @intCast(u32, u >> 32) & (maxInt(u32) >> 1); - const ax = @bitCast(f64, u & (maxInt(u64) >> 1)); + const u = @as(u64, @bitCast(x)); + const w = @as(u32, @intCast(u >> 32)) & (maxInt(u32) >> 1); + const ax = @as(f64, @bitCast(u & (maxInt(u64) >> 1))); // TODO: Shouldn't need this explicit check. if (x == 0.0) { diff --git a/lib/std/math/expm1.zig b/lib/std/math/expm1.zig index 5c4052db56..8192573a88 100644 --- a/lib/std/math/expm1.zig +++ b/lib/std/math/expm1.zig @@ -38,7 +38,7 @@ fn expm1_32(x_: f32) f32 { const Q2: f32 = 1.5807170421e-3; var x = x_; - const ux = @bitCast(u32, x); + const ux = @as(u32, @bitCast(x)); const hx = ux & 0x7FFFFFFF; const sign = hx >> 31; @@ -88,8 +88,8 @@ fn expm1_32(x_: f32) f32 { kf += 0.5; } - k = @intFromFloat(i32, kf); - const t = @floatFromInt(f32, k); + k = @as(i32, @intFromFloat(kf)); + const t = @as(f32, @floatFromInt(k)); hi = x - t * ln2_hi; lo = t * ln2_lo; } @@ -133,7 +133,7 @@ fn expm1_32(x_: f32) f32 { } } - const twopk = @bitCast(f32, @intCast(u32, (0x7F +% k) << 23)); + const twopk = @as(f32, @bitCast(@as(u32, @intCast((0x7F +% k) << 23)))); if (k < 0 or k > 56) { var y = x - e + 1.0; @@ -146,7 +146,7 @@ fn expm1_32(x_: f32) f32 { return y - 1.0; } - const uf = @bitCast(f32, @intCast(u32, 0x7F -% k) << 23); + const uf = @as(f32, @bitCast(@as(u32, @intCast(0x7F -% k)) << 23)); if (k < 23) { return (x - e + (1 - uf)) * twopk; } else { @@ -169,8 +169,8 @@ fn expm1_64(x_: f64) f64 { const Q5: f64 = -2.01099218183624371326e-07; var x = x_; - const ux = @bitCast(u64, x); - const hx = @intCast(u32, ux >> 32) & 0x7FFFFFFF; + const ux = @as(u64, @bitCast(x)); + const hx = @as(u32, @intCast(ux >> 32)) & 0x7FFFFFFF; const sign = ux >> 63; if (math.isNegativeInf(x)) { @@ -219,8 +219,8 @@ fn expm1_64(x_: f64) f64 { kf += 0.5; } - k = @intFromFloat(i32, kf); - const t = @floatFromInt(f64, k); + k = @as(i32, @intFromFloat(kf)); + const t = @as(f64, @floatFromInt(k)); hi = x - t * ln2_hi; lo = t * ln2_lo; } @@ -231,7 +231,7 @@ fn expm1_64(x_: f64) f64 { // |x| < 2^(-54) else if (hx < 0x3C900000) { if (hx < 0x00100000) { - math.doNotOptimizeAway(@floatCast(f32, x)); + math.doNotOptimizeAway(@as(f32, @floatCast(x))); } return x; } else { @@ -264,7 +264,7 @@ fn expm1_64(x_: f64) f64 { } } - const twopk = @bitCast(f64, @intCast(u64, 0x3FF +% k) << 52); + const twopk = @as(f64, @bitCast(@as(u64, @intCast(0x3FF +% k)) << 52)); if (k < 0 or k > 56) { var y = x - e + 1.0; @@ -277,7 +277,7 @@ fn expm1_64(x_: f64) f64 { return y - 1.0; } - const uf = @bitCast(f64, @intCast(u64, 0x3FF -% k) << 52); + const uf = @as(f64, @bitCast(@as(u64, @intCast(0x3FF -% k)) << 52)); if (k < 20) { return (x - e + (1 - uf)) * twopk; } else { diff --git a/lib/std/math/expo2.zig b/lib/std/math/expo2.zig index 4345233173..b451e46865 100644 --- a/lib/std/math/expo2.zig +++ b/lib/std/math/expo2.zig @@ -21,7 +21,7 @@ fn expo2f(x: f32) f32 { const kln2 = 0x1.45C778p+7; const u = (0x7F + k / 2) << 23; - const scale = @bitCast(f32, u); + const scale = @as(f32, @bitCast(u)); return @exp(x - kln2) * scale * scale; } @@ -30,6 +30,6 @@ fn expo2d(x: f64) f64 { const kln2 = 0x1.62066151ADD8BP+10; const u = (0x3FF + k / 2) << 20; - const scale = @bitCast(f64, @as(u64, u) << 32); + const scale = @as(f64, @bitCast(@as(u64, u) << 32)); return @exp(x - kln2) * scale * scale; } diff --git a/lib/std/math/float.zig b/lib/std/math/float.zig index 768cc03285..5552ec5c9c 100644 --- a/lib/std/math/float.zig +++ b/lib/std/math/float.zig @@ -11,7 +11,7 @@ inline fn mantissaOne(comptime T: type) comptime_int { inline fn reconstructFloat(comptime T: type, comptime exponent: comptime_int, comptime mantissa: comptime_int) T { const TBits = @Type(.{ .Int = .{ .signedness = .unsigned, .bits = @bitSizeOf(T) } }); const biased_exponent = @as(TBits, exponent + floatExponentMax(T)); - return @bitCast(T, (biased_exponent << floatMantissaBits(T)) | @as(TBits, mantissa)); + return @as(T, @bitCast((biased_exponent << floatMantissaBits(T)) | @as(TBits, mantissa))); } /// Returns the number of bits in the exponent of floating point type T. diff --git a/lib/std/math/frexp.zig b/lib/std/math/frexp.zig index 31168d28d4..f295b959cb 100644 --- a/lib/std/math/frexp.zig +++ b/lib/std/math/frexp.zig @@ -38,8 +38,8 @@ pub fn frexp(x: anytype) Frexp(@TypeOf(x)) { fn frexp32(x: f32) Frexp(f32) { var result: Frexp(f32) = undefined; - var y = @bitCast(u32, x); - const e = @intCast(i32, y >> 23) & 0xFF; + var y = @as(u32, @bitCast(x)); + const e = @as(i32, @intCast(y >> 23)) & 0xFF; if (e == 0) { if (x != 0) { @@ -68,15 +68,15 @@ fn frexp32(x: f32) Frexp(f32) { result.exponent = e - 0x7E; y &= 0x807FFFFF; y |= 0x3F000000; - result.significand = @bitCast(f32, y); + result.significand = @as(f32, @bitCast(y)); return result; } fn frexp64(x: f64) Frexp(f64) { var result: Frexp(f64) = undefined; - var y = @bitCast(u64, x); - const e = @intCast(i32, y >> 52) & 0x7FF; + var y = @as(u64, @bitCast(x)); + const e = @as(i32, @intCast(y >> 52)) & 0x7FF; if (e == 0) { if (x != 0) { @@ -105,15 +105,15 @@ fn frexp64(x: f64) Frexp(f64) { result.exponent = e - 0x3FE; y &= 0x800FFFFFFFFFFFFF; y |= 0x3FE0000000000000; - result.significand = @bitCast(f64, y); + result.significand = @as(f64, @bitCast(y)); return result; } fn frexp128(x: f128) Frexp(f128) { var result: Frexp(f128) = undefined; - var y = @bitCast(u128, x); - const e = @intCast(i32, y >> 112) & 0x7FFF; + var y = @as(u128, @bitCast(x)); + const e = @as(i32, @intCast(y >> 112)) & 0x7FFF; if (e == 0) { if (x != 0) { @@ -142,7 +142,7 @@ fn frexp128(x: f128) Frexp(f128) { result.exponent = e - 0x3FFE; y &= 0x8000FFFFFFFFFFFFFFFFFFFFFFFFFFFF; y |= 0x3FFE0000000000000000000000000000; - result.significand = @bitCast(f128, y); + result.significand = @as(f128, @bitCast(y)); return result; } diff --git a/lib/std/math/hypot.zig b/lib/std/math/hypot.zig index 981f6143fe..9fb569667b 100644 --- a/lib/std/math/hypot.zig +++ b/lib/std/math/hypot.zig @@ -25,8 +25,8 @@ pub fn hypot(comptime T: type, x: T, y: T) T { } fn hypot32(x: f32, y: f32) f32 { - var ux = @bitCast(u32, x); - var uy = @bitCast(u32, y); + var ux = @as(u32, @bitCast(x)); + var uy = @as(u32, @bitCast(y)); ux &= maxInt(u32) >> 1; uy &= maxInt(u32) >> 1; @@ -36,8 +36,8 @@ fn hypot32(x: f32, y: f32) f32 { uy = tmp; } - var xx = @bitCast(f32, ux); - var yy = @bitCast(f32, uy); + var xx = @as(f32, @bitCast(ux)); + var yy = @as(f32, @bitCast(uy)); if (uy == 0xFF << 23) { return yy; } @@ -56,7 +56,7 @@ fn hypot32(x: f32, y: f32) f32 { yy *= 0x1.0p-90; } - return z * @sqrt(@floatCast(f32, @as(f64, x) * x + @as(f64, y) * y)); + return z * @sqrt(@as(f32, @floatCast(@as(f64, x) * x + @as(f64, y) * y))); } fn sq(hi: *f64, lo: *f64, x: f64) void { @@ -69,8 +69,8 @@ fn sq(hi: *f64, lo: *f64, x: f64) void { } fn hypot64(x: f64, y: f64) f64 { - var ux = @bitCast(u64, x); - var uy = @bitCast(u64, y); + var ux = @as(u64, @bitCast(x)); + var uy = @as(u64, @bitCast(y)); ux &= maxInt(u64) >> 1; uy &= maxInt(u64) >> 1; @@ -82,8 +82,8 @@ fn hypot64(x: f64, y: f64) f64 { const ex = ux >> 52; const ey = uy >> 52; - var xx = @bitCast(f64, ux); - var yy = @bitCast(f64, uy); + var xx = @as(f64, @bitCast(ux)); + var yy = @as(f64, @bitCast(uy)); // hypot(inf, nan) == inf if (ey == 0x7FF) { diff --git a/lib/std/math/ilogb.zig b/lib/std/math/ilogb.zig index 7c58be2ec5..735a2250c9 100644 --- a/lib/std/math/ilogb.zig +++ b/lib/std/math/ilogb.zig @@ -38,8 +38,8 @@ fn ilogbX(comptime T: type, x: T) i32 { const absMask = signBit - 1; - var u = @bitCast(Z, x) & absMask; - var e = @intCast(i32, u >> significandBits); + var u = @as(Z, @bitCast(x)) & absMask; + var e = @as(i32, @intCast(u >> significandBits)); if (e == 0) { if (u == 0) { @@ -49,12 +49,12 @@ fn ilogbX(comptime T: type, x: T) i32 { // offset sign bit, exponent bits, and integer bit (if present) + bias const offset = 1 + exponentBits + @as(comptime_int, @intFromBool(T == f80)) - exponentBias; - return offset - @intCast(i32, @clz(u)); + return offset - @as(i32, @intCast(@clz(u))); } if (e == maxExponent) { math.raiseInvalid(); - if (u > @bitCast(Z, math.inf(T))) { + if (u > @as(Z, @bitCast(math.inf(T)))) { return fp_ilogbnan; // u is a NaN } else return maxInt(i32); } diff --git a/lib/std/math/isfinite.zig b/lib/std/math/isfinite.zig index 556f8a2378..36c6cdd062 100644 --- a/lib/std/math/isfinite.zig +++ b/lib/std/math/isfinite.zig @@ -7,7 +7,7 @@ pub fn isFinite(x: anytype) bool { const T = @TypeOf(x); const TBits = std.meta.Int(.unsigned, @typeInfo(T).Float.bits); const remove_sign = ~@as(TBits, 0) >> 1; - return @bitCast(TBits, x) & remove_sign < @bitCast(TBits, math.inf(T)); + return @as(TBits, @bitCast(x)) & remove_sign < @as(TBits, @bitCast(math.inf(T))); } test "math.isFinite" { diff --git a/lib/std/math/isinf.zig b/lib/std/math/isinf.zig index ac30470f31..9b3a0a8f4a 100644 --- a/lib/std/math/isinf.zig +++ b/lib/std/math/isinf.zig @@ -7,7 +7,7 @@ pub inline fn isInf(x: anytype) bool { const T = @TypeOf(x); const TBits = std.meta.Int(.unsigned, @typeInfo(T).Float.bits); const remove_sign = ~@as(TBits, 0) >> 1; - return @bitCast(TBits, x) & remove_sign == @bitCast(TBits, math.inf(T)); + return @as(TBits, @bitCast(x)) & remove_sign == @as(TBits, @bitCast(math.inf(T))); } /// Returns whether x is an infinity with a positive sign. diff --git a/lib/std/math/isnormal.zig b/lib/std/math/isnormal.zig index 08f848f5df..38b459b54e 100644 --- a/lib/std/math/isnormal.zig +++ b/lib/std/math/isnormal.zig @@ -15,7 +15,7 @@ pub fn isNormal(x: anytype) bool { // The sign bit is removed because all ones would overflow into it. // For f80, even though it has an explicit integer part stored, // the exponent effectively takes priority if mismatching. - const value = @bitCast(TBits, x) +% increment_exp; + const value = @as(TBits, @bitCast(x)) +% increment_exp; return value & remove_sign >= (increment_exp << 1); } @@ -35,7 +35,7 @@ test "math.isNormal" { try expect(!isNormal(@as(T, math.floatTrueMin(T)))); // largest subnormal - try expect(!isNormal(@bitCast(T, ~(~@as(TBits, 0) << math.floatFractionalBits(T))))); + try expect(!isNormal(@as(T, @bitCast(~(~@as(TBits, 0) << math.floatFractionalBits(T)))))); // non-finite numbers try expect(!isNormal(-math.inf(T))); @@ -43,6 +43,6 @@ test "math.isNormal" { try expect(!isNormal(math.nan(T))); // overflow edge-case (described in implementation, also see #10133) - try expect(!isNormal(@bitCast(T, ~@as(TBits, 0)))); + try expect(!isNormal(@as(T, @bitCast(~@as(TBits, 0))))); } } diff --git a/lib/std/math/ldexp.zig b/lib/std/math/ldexp.zig index 448e94f8e5..d32a8189b6 100644 --- a/lib/std/math/ldexp.zig +++ b/lib/std/math/ldexp.zig @@ -16,53 +16,53 @@ pub fn ldexp(x: anytype, n: i32) @TypeOf(x) { const max_biased_exponent = 2 * math.floatExponentMax(T); const mantissa_mask = @as(TBits, (1 << mantissa_bits) - 1); - const repr = @bitCast(TBits, x); + const repr = @as(TBits, @bitCast(x)); const sign_bit = repr & (1 << (exponent_bits + mantissa_bits)); if (math.isNan(x) or !math.isFinite(x)) return x; - var exponent: i32 = @intCast(i32, (repr << 1) >> (mantissa_bits + 1)); + var exponent: i32 = @as(i32, @intCast((repr << 1) >> (mantissa_bits + 1))); if (exponent == 0) exponent += (@as(i32, exponent_bits) + @intFromBool(T == f80)) - @clz(repr << 1); if (n >= 0) { if (n > max_biased_exponent - exponent) { // Overflow. Return +/- inf - return @bitCast(T, @bitCast(TBits, math.inf(T)) | sign_bit); + return @as(T, @bitCast(@as(TBits, @bitCast(math.inf(T))) | sign_bit)); } else if (exponent + n <= 0) { // Result is subnormal - return @bitCast(T, (repr << @intCast(Log2Int(TBits), n)) | sign_bit); + return @as(T, @bitCast((repr << @as(Log2Int(TBits), @intCast(n))) | sign_bit)); } else if (exponent <= 0) { // Result is normal, but needs shifting - var result = @intCast(TBits, n + exponent) << mantissa_bits; - result |= (repr << @intCast(Log2Int(TBits), 1 - exponent)) & mantissa_mask; - return @bitCast(T, result | sign_bit); + var result = @as(TBits, @intCast(n + exponent)) << mantissa_bits; + result |= (repr << @as(Log2Int(TBits), @intCast(1 - exponent))) & mantissa_mask; + return @as(T, @bitCast(result | sign_bit)); } // Result needs no shifting - return @bitCast(T, repr + (@intCast(TBits, n) << mantissa_bits)); + return @as(T, @bitCast(repr + (@as(TBits, @intCast(n)) << mantissa_bits))); } else { if (n <= -exponent) { if (n < -(mantissa_bits + exponent)) - return @bitCast(T, sign_bit); // Severe underflow. Return +/- 0 + return @as(T, @bitCast(sign_bit)); // Severe underflow. Return +/- 0 // Result underflowed, we need to shift and round - const shift = @intCast(Log2Int(TBits), @min(-n, -(exponent + n) + 1)); + const shift = @as(Log2Int(TBits), @intCast(@min(-n, -(exponent + n) + 1))); const exact_tie: bool = @ctz(repr) == shift - 1; var result = repr & mantissa_mask; if (T != f80) // Include integer bit result |= @as(TBits, @intFromBool(exponent > 0)) << fractional_bits; - result = @intCast(TBits, (result >> (shift - 1))); + result = @as(TBits, @intCast((result >> (shift - 1)))); // Round result, including round-to-even for exact ties result = ((result + 1) >> 1) & ~@as(TBits, @intFromBool(exact_tie)); - return @bitCast(T, result | sign_bit); + return @as(T, @bitCast(result | sign_bit)); } // Result is exact, and needs no shifting - return @bitCast(T, repr - (@intCast(TBits, -n) << mantissa_bits)); + return @as(T, @bitCast(repr - (@as(TBits, @intCast(-n)) << mantissa_bits))); } } @@ -105,8 +105,8 @@ test "math.ldexp" { // Multiplications might flush the denormals to zero, esp. at // runtime, so we manually construct the constants here instead. const Z = std.meta.Int(.unsigned, @bitSizeOf(T)); - const EightTimesTrueMin = @bitCast(T, @as(Z, 8)); - const TwoTimesTrueMin = @bitCast(T, @as(Z, 2)); + const EightTimesTrueMin = @as(T, @bitCast(@as(Z, 8))); + const TwoTimesTrueMin = @as(T, @bitCast(@as(Z, 2))); // subnormals -> subnormals try expect(ldexp(math.floatTrueMin(T), 3) == EightTimesTrueMin); diff --git a/lib/std/math/log.zig b/lib/std/math/log.zig index c1a0f5c8e4..9f27130ce1 100644 --- a/lib/std/math/log.zig +++ b/lib/std/math/log.zig @@ -30,12 +30,12 @@ pub fn log(comptime T: type, base: T, x: T) T { // TODO implement integer log without using float math .Int => |IntType| switch (IntType.signedness) { .signed => @compileError("log not implemented for signed integers"), - .unsigned => return @intFromFloat(T, @floor(@log(@floatFromInt(f64, x)) / @log(float_base))), + .unsigned => return @as(T, @intFromFloat(@floor(@log(@as(f64, @floatFromInt(x))) / @log(float_base)))), }, .Float => { switch (T) { - f32 => return @floatCast(f32, @log(@as(f64, x)) / @log(float_base)), + f32 => return @as(f32, @floatCast(@log(@as(f64, x)) / @log(float_base))), f64 => return @log(x) / @log(float_base), else => @compileError("log not implemented for " ++ @typeName(T)), } diff --git a/lib/std/math/log10.zig b/lib/std/math/log10.zig index 44e5a88445..785f11771c 100644 --- a/lib/std/math/log10.zig +++ b/lib/std/math/log10.zig @@ -49,9 +49,9 @@ pub fn log10_int(x: anytype) Log2Int(@TypeOf(x)) { const bit_size = @typeInfo(T).Int.bits; if (bit_size <= 8) { - return @intCast(OutT, log10_int_u8(x)); + return @as(OutT, @intCast(log10_int_u8(x))); } else if (bit_size <= 16) { - return @intCast(OutT, less_than_5(x)); + return @as(OutT, @intCast(less_than_5(x))); } var val = x; @@ -71,7 +71,7 @@ pub fn log10_int(x: anytype) Log2Int(@TypeOf(x)) { log += 5; } - return @intCast(OutT, log + less_than_5(@intCast(u32, val))); + return @as(OutT, @intCast(log + less_than_5(@as(u32, @intCast(val))))); } fn pow10(comptime y: comptime_int) comptime_int { @@ -134,7 +134,7 @@ inline fn less_than_5(x: u32) u32 { } fn oldlog10(x: anytype) u8 { - return @intFromFloat(u8, @log10(@floatFromInt(f64, x))); + return @as(u8, @intFromFloat(@log10(@as(f64, @floatFromInt(x))))); } test "oldlog10 doesn't work" { @@ -158,7 +158,7 @@ test "log10_int vs old implementation" { inline for (int_types) |T| { const last = @min(maxInt(T), 100_000); for (1..last) |i| { - const x = @intCast(T, i); + const x = @as(T, @intCast(i)); try testing.expectEqual(oldlog10(x), log10_int(x)); } @@ -185,10 +185,10 @@ test "log10_int close to powers of 10" { try testing.expectEqual(expected_max_ilog, log10_int(max_val)); for (0..(expected_max_ilog + 1)) |idx| { - const i = @intCast(T, idx); + const i = @as(T, @intCast(idx)); const p: T = try math.powi(T, 10, i); - const b = @intCast(Log2Int(T), i); + const b = @as(Log2Int(T), @intCast(i)); if (p >= 10) { try testing.expectEqual(b - 1, log10_int(p - 9)); diff --git a/lib/std/math/log1p.zig b/lib/std/math/log1p.zig index ad67955a8d..1f986a20c8 100644 --- a/lib/std/math/log1p.zig +++ b/lib/std/math/log1p.zig @@ -33,7 +33,7 @@ fn log1p_32(x: f32) f32 { const Lg3: f32 = 0x91e9ee.0p-25; const Lg4: f32 = 0xf89e26.0p-26; - const u = @bitCast(u32, x); + const u = @as(u32, @bitCast(x)); var ix = u; var k: i32 = 1; var f: f32 = undefined; @@ -72,9 +72,9 @@ fn log1p_32(x: f32) f32 { if (k != 0) { const uf = 1 + x; - var iu = @bitCast(u32, uf); + var iu = @as(u32, @bitCast(uf)); iu += 0x3F800000 - 0x3F3504F3; - k = @intCast(i32, iu >> 23) - 0x7F; + k = @as(i32, @intCast(iu >> 23)) - 0x7F; // correction to avoid underflow in c / u if (k < 25) { @@ -86,7 +86,7 @@ fn log1p_32(x: f32) f32 { // u into [sqrt(2)/2, sqrt(2)] iu = (iu & 0x007FFFFF) + 0x3F3504F3; - f = @bitCast(f32, iu) - 1; + f = @as(f32, @bitCast(iu)) - 1; } const s = f / (2.0 + f); @@ -96,7 +96,7 @@ fn log1p_32(x: f32) f32 { const t2 = z * (Lg1 + w * Lg3); const R = t2 + t1; const hfsq = 0.5 * f * f; - const dk = @floatFromInt(f32, k); + const dk = @as(f32, @floatFromInt(k)); return s * (hfsq + R) + (dk * ln2_lo + c) - hfsq + f + dk * ln2_hi; } @@ -112,8 +112,8 @@ fn log1p_64(x: f64) f64 { const Lg6: f64 = 1.531383769920937332e-01; const Lg7: f64 = 1.479819860511658591e-01; - var ix = @bitCast(u64, x); - var hx = @intCast(u32, ix >> 32); + var ix = @as(u64, @bitCast(x)); + var hx = @as(u32, @intCast(ix >> 32)); var k: i32 = 1; var c: f64 = undefined; var f: f64 = undefined; @@ -150,10 +150,10 @@ fn log1p_64(x: f64) f64 { if (k != 0) { const uf = 1 + x; - const hu = @bitCast(u64, uf); - var iu = @intCast(u32, hu >> 32); + const hu = @as(u64, @bitCast(uf)); + var iu = @as(u32, @intCast(hu >> 32)); iu += 0x3FF00000 - 0x3FE6A09E; - k = @intCast(i32, iu >> 20) - 0x3FF; + k = @as(i32, @intCast(iu >> 20)) - 0x3FF; // correction to avoid underflow in c / u if (k < 54) { @@ -166,7 +166,7 @@ fn log1p_64(x: f64) f64 { // u into [sqrt(2)/2, sqrt(2)] iu = (iu & 0x000FFFFF) + 0x3FE6A09E; const iq = (@as(u64, iu) << 32) | (hu & 0xFFFFFFFF); - f = @bitCast(f64, iq) - 1; + f = @as(f64, @bitCast(iq)) - 1; } const hfsq = 0.5 * f * f; @@ -176,7 +176,7 @@ fn log1p_64(x: f64) f64 { const t1 = w * (Lg2 + w * (Lg4 + w * Lg6)); const t2 = z * (Lg1 + w * (Lg3 + w * (Lg5 + w * Lg7))); const R = t2 + t1; - const dk = @floatFromInt(f64, k); + const dk = @as(f64, @floatFromInt(k)); return s * (hfsq + R) + (dk * ln2_lo + c) - hfsq + f + dk * ln2_hi; } diff --git a/lib/std/math/modf.zig b/lib/std/math/modf.zig index d12c497729..b9d0083e3c 100644 --- a/lib/std/math/modf.zig +++ b/lib/std/math/modf.zig @@ -37,8 +37,8 @@ pub fn modf(x: anytype) modf_result(@TypeOf(x)) { fn modf32(x: f32) modf32_result { var result: modf32_result = undefined; - const u = @bitCast(u32, x); - const e = @intCast(i32, (u >> 23) & 0xFF) - 0x7F; + const u = @as(u32, @bitCast(x)); + const e = @as(i32, @intCast((u >> 23) & 0xFF)) - 0x7F; const us = u & 0x80000000; // TODO: Shouldn't need this. @@ -54,26 +54,26 @@ fn modf32(x: f32) modf32_result { if (e == 0x80 and u << 9 != 0) { // nan result.fpart = x; } else { - result.fpart = @bitCast(f32, us); + result.fpart = @as(f32, @bitCast(us)); } return result; } // no integral part if (e < 0) { - result.ipart = @bitCast(f32, us); + result.ipart = @as(f32, @bitCast(us)); result.fpart = x; return result; } - const mask = @as(u32, 0x007FFFFF) >> @intCast(u5, e); + const mask = @as(u32, 0x007FFFFF) >> @as(u5, @intCast(e)); if (u & mask == 0) { result.ipart = x; - result.fpart = @bitCast(f32, us); + result.fpart = @as(f32, @bitCast(us)); return result; } - const uf = @bitCast(f32, u & ~mask); + const uf = @as(f32, @bitCast(u & ~mask)); result.ipart = uf; result.fpart = x - uf; return result; @@ -82,8 +82,8 @@ fn modf32(x: f32) modf32_result { fn modf64(x: f64) modf64_result { var result: modf64_result = undefined; - const u = @bitCast(u64, x); - const e = @intCast(i32, (u >> 52) & 0x7FF) - 0x3FF; + const u = @as(u64, @bitCast(x)); + const e = @as(i32, @intCast((u >> 52) & 0x7FF)) - 0x3FF; const us = u & (1 << 63); if (math.isInf(x)) { @@ -98,26 +98,26 @@ fn modf64(x: f64) modf64_result { if (e == 0x400 and u << 12 != 0) { // nan result.fpart = x; } else { - result.fpart = @bitCast(f64, us); + result.fpart = @as(f64, @bitCast(us)); } return result; } // no integral part if (e < 0) { - result.ipart = @bitCast(f64, us); + result.ipart = @as(f64, @bitCast(us)); result.fpart = x; return result; } - const mask = @as(u64, maxInt(u64) >> 12) >> @intCast(u6, e); + const mask = @as(u64, maxInt(u64) >> 12) >> @as(u6, @intCast(e)); if (u & mask == 0) { result.ipart = x; - result.fpart = @bitCast(f64, us); + result.fpart = @as(f64, @bitCast(us)); return result; } - const uf = @bitCast(f64, u & ~mask); + const uf = @as(f64, @bitCast(u & ~mask)); result.ipart = uf; result.fpart = x - uf; return result; diff --git a/lib/std/math/pow.zig b/lib/std/math/pow.zig index 7643e143e3..36aef966cf 100644 --- a/lib/std/math/pow.zig +++ b/lib/std/math/pow.zig @@ -144,7 +144,7 @@ pub fn pow(comptime T: type, x: T, y: T) T { var xe = r2.exponent; var x1 = r2.significand; - var i = @intFromFloat(std.meta.Int(.signed, @typeInfo(T).Float.bits), yi); + var i = @as(std.meta.Int(.signed, @typeInfo(T).Float.bits), @intFromFloat(yi)); while (i != 0) : (i >>= 1) { const overflow_shift = math.floatExponentBits(T) + 1; if (xe < -(1 << overflow_shift) or (1 << overflow_shift) < xe) { @@ -179,7 +179,7 @@ pub fn pow(comptime T: type, x: T, y: T) T { fn isOddInteger(x: f64) bool { const r = math.modf(x); - return r.fpart == 0.0 and @intFromFloat(i64, r.ipart) & 1 == 1; + return r.fpart == 0.0 and @as(i64, @intFromFloat(r.ipart)) & 1 == 1; } test "math.pow" { diff --git a/lib/std/math/signbit.zig b/lib/std/math/signbit.zig index 9aab487d37..df061568b1 100644 --- a/lib/std/math/signbit.zig +++ b/lib/std/math/signbit.zig @@ -6,7 +6,7 @@ const expect = std.testing.expect; pub fn signbit(x: anytype) bool { const T = @TypeOf(x); const TBits = std.meta.Int(.unsigned, @typeInfo(T).Float.bits); - return @bitCast(TBits, x) >> (@bitSizeOf(T) - 1) != 0; + return @as(TBits, @bitCast(x)) >> (@bitSizeOf(T) - 1) != 0; } test "math.signbit" { diff --git a/lib/std/math/sinh.zig b/lib/std/math/sinh.zig index 5ec47fa3b5..0082f61d3f 100644 --- a/lib/std/math/sinh.zig +++ b/lib/std/math/sinh.zig @@ -29,9 +29,9 @@ pub fn sinh(x: anytype) @TypeOf(x) { // = (exp(x) - 1 + (exp(x) - 1) / exp(x)) / 2 // = x + x^3 / 6 + o(x^5) fn sinh32(x: f32) f32 { - const u = @bitCast(u32, x); + const u = @as(u32, @bitCast(x)); const ux = u & 0x7FFFFFFF; - const ax = @bitCast(f32, ux); + const ax = @as(f32, @bitCast(ux)); if (x == 0.0 or math.isNan(x)) { return x; @@ -60,9 +60,9 @@ fn sinh32(x: f32) f32 { } fn sinh64(x: f64) f64 { - const u = @bitCast(u64, x); - const w = @intCast(u32, u >> 32) & (maxInt(u32) >> 1); - const ax = @bitCast(f64, u & (maxInt(u64) >> 1)); + const u = @as(u64, @bitCast(x)); + const w = @as(u32, @intCast(u >> 32)) & (maxInt(u32) >> 1); + const ax = @as(f64, @bitCast(u & (maxInt(u64) >> 1))); if (x == 0.0 or math.isNan(x)) { return x; diff --git a/lib/std/math/sqrt.zig b/lib/std/math/sqrt.zig index 926582034e..0dd5381cd9 100644 --- a/lib/std/math/sqrt.zig +++ b/lib/std/math/sqrt.zig @@ -57,7 +57,7 @@ fn sqrt_int(comptime T: type, value: T) Sqrt(T) { one >>= 2; } - return @intCast(Sqrt(T), res); + return @as(Sqrt(T), @intCast(res)); } } diff --git a/lib/std/math/tanh.zig b/lib/std/math/tanh.zig index dcde79a925..9c9a3e6801 100644 --- a/lib/std/math/tanh.zig +++ b/lib/std/math/tanh.zig @@ -29,9 +29,9 @@ pub fn tanh(x: anytype) @TypeOf(x) { // = (exp(2x) - 1) / (exp(2x) - 1 + 2) // = (1 - exp(-2x)) / (exp(-2x) - 1 + 2) fn tanh32(x: f32) f32 { - const u = @bitCast(u32, x); + const u = @as(u32, @bitCast(x)); const ux = u & 0x7FFFFFFF; - const ax = @bitCast(f32, ux); + const ax = @as(f32, @bitCast(ux)); const sign = (u >> 31) != 0; var t: f32 = undefined; @@ -66,10 +66,10 @@ fn tanh32(x: f32) f32 { } fn tanh64(x: f64) f64 { - const u = @bitCast(u64, x); + const u = @as(u64, @bitCast(x)); const ux = u & 0x7FFFFFFFFFFFFFFF; - const w = @intCast(u32, ux >> 32); - const ax = @bitCast(f64, ux); + const w = @as(u32, @intCast(ux >> 32)); + const ax = @as(f64, @bitCast(ux)); const sign = (u >> 63) != 0; var t: f64 = undefined; @@ -96,7 +96,7 @@ fn tanh64(x: f64) f64 { } // |x| is subnormal else { - math.doNotOptimizeAway(@floatCast(f32, ax)); + math.doNotOptimizeAway(@as(f32, @floatCast(ax))); t = ax; } |
