diff options
Diffstat (limited to 'std/math')
| -rw-r--r-- | std/math/asinh.zig | 3 | ||||
| -rw-r--r-- | std/math/atanh.zig | 3 | ||||
| -rw-r--r-- | std/math/big/int.zig | 60 | ||||
| -rw-r--r-- | std/math/copysign.zig | 7 | ||||
| -rw-r--r-- | std/math/cosh.zig | 3 | ||||
| -rw-r--r-- | std/math/fabs.zig | 3 | ||||
| -rw-r--r-- | std/math/hypot.zig | 9 | ||||
| -rw-r--r-- | std/math/ilogb.zig | 34 | ||||
| -rw-r--r-- | std/math/index.zig | 82 | ||||
| -rw-r--r-- | std/math/isfinite.zig | 3 | ||||
| -rw-r--r-- | std/math/isinf.zig | 3 | ||||
| -rw-r--r-- | std/math/isnan.zig | 3 | ||||
| -rw-r--r-- | std/math/isnormal.zig | 3 | ||||
| -rw-r--r-- | std/math/log10.zig | 3 | ||||
| -rw-r--r-- | std/math/log2.zig | 3 | ||||
| -rw-r--r-- | std/math/modf.zig | 3 | ||||
| -rw-r--r-- | std/math/sinh.zig | 3 | ||||
| -rw-r--r-- | std/math/sqrt.zig | 3 | ||||
| -rw-r--r-- | std/math/tanh.zig | 3 | ||||
| -rw-r--r-- | std/math/trunc.zig | 5 |
20 files changed, 158 insertions, 81 deletions
diff --git a/std/math/asinh.zig b/std/math/asinh.zig index af8c2d8f54..98892bcbcb 100644 --- a/std/math/asinh.zig +++ b/std/math/asinh.zig @@ -7,6 +7,7 @@ const std = @import("../index.zig"); const math = std.math; const assert = std.debug.assert; +const maxInt = std.math.maxInt; pub fn asinh(x: var) @typeOf(x) { const T = @typeOf(x); @@ -55,7 +56,7 @@ fn asinh64(x: f64) f64 { const e = (u >> 52) & 0x7FF; const s = u >> 63; - var rx = @bitCast(f64, u & (@maxValue(u64) >> 1)); // |x| + var rx = @bitCast(f64, u & (maxInt(u64) >> 1)); // |x| if (math.isNegativeInf(x)) { return x; diff --git a/std/math/atanh.zig b/std/math/atanh.zig index 4ae8a66bc0..c97855c234 100644 --- a/std/math/atanh.zig +++ b/std/math/atanh.zig @@ -7,6 +7,7 @@ const std = @import("../index.zig"); const math = std.math; const assert = std.debug.assert; +const maxInt = std.math.maxInt; pub fn atanh(x: var) @typeOf(x) { const T = @typeOf(x); @@ -52,7 +53,7 @@ fn atanh_64(x: f64) f64 { const e = (u >> 52) & 0x7FF; const s = u >> 63; - var y = @bitCast(f64, u & (@maxValue(u64) >> 1)); // |x| + var y = @bitCast(f64, u & (maxInt(u64) >> 1)); // |x| if (y == 1.0) { return math.copysign(f64, math.inf(f64), x); diff --git a/std/math/big/int.zig b/std/math/big/int.zig index a9fb078afa..24ca890d7b 100644 --- a/std/math/big/int.zig +++ b/std/math/big/int.zig @@ -5,6 +5,8 @@ const math = std.math; const mem = std.mem; const Allocator = mem.Allocator; const ArrayList = std.ArrayList; +const maxInt = std.math.maxInt; +const minInt = std.math.minInt; const TypeId = builtin.TypeId; @@ -212,7 +214,7 @@ pub const Int = struct.{ self.positive = value >= 0; self.len = req_limbs; - if (w_value <= @maxValue(Limb)) { + if (w_value <= maxInt(Limb)) { self.limbs[0] = w_value; } else { const mask = (1 << Limb.bit_count) - 1; @@ -267,7 +269,7 @@ pub const Int = struct.{ if (math.cast(T, r)) |ok| { return -ok; } else |_| { - return @minValue(T); + return minInt(T); } } } @@ -371,7 +373,7 @@ pub const Int = struct.{ } } // Non power-of-two: batch divisions per word size. else { - const digits_per_limb = math.log(Limb, base, @maxValue(Limb)); + const digits_per_limb = math.log(Limb, base, maxInt(Limb)); var limb_base: Limb = 1; var j: usize = 0; while (j < digits_per_limb) : (j += 1) { @@ -717,7 +719,7 @@ pub const Int = struct.{ const c3: Limb = @boolToInt(@addWithOverflow(Limb, r1, r2, &r1)); // This never overflows, c1, c3 are either 0 or 1 and if both are 1 then - // c2 is at least <= @maxValue(Limb) - 2. + // c2 is at least <= maxInt(Limb) - 2. carry.* = c1 + c2 + c3; return r1; @@ -873,11 +875,11 @@ pub const Int = struct.{ while (i > t) : (i -= 1) { // 3.1 if (x.limbs[i] == y.limbs[t]) { - q.limbs[i - t - 1] = @maxValue(Limb); + q.limbs[i - t - 1] = maxInt(Limb); } else { const num = (DoubleLimb(x.limbs[i]) << Limb.bit_count) | DoubleLimb(x.limbs[i - 1]); const z = @intCast(Limb, num / DoubleLimb(y.limbs[t])); - q.limbs[i - t - 1] = if (z > @maxValue(Limb)) @maxValue(Limb) else Limb(z); + q.limbs[i - t - 1] = if (z > maxInt(Limb)) maxInt(Limb) else Limb(z); } // 3.2 @@ -1081,7 +1083,7 @@ test "big.int comptime_int set" { comptime var i: usize = 0; inline while (i < s_limb_count) : (i += 1) { - const result = Limb(s & @maxValue(Limb)); + const result = Limb(s & maxInt(Limb)); s >>= Limb.bit_count / 2; s >>= Limb.bit_count / 2; debug.assert(a.limbs[i] == result); @@ -1403,7 +1405,7 @@ test "big.int compare similar" { } test "big.int compare different limb size" { - var a = try Int.initSet(al, @maxValue(Limb) + 1); + var a = try Int.initSet(al, maxInt(Limb) + 1); var b = try Int.initSet(al, 1); debug.assert(a.cmpAbs(b) == 1); @@ -1457,16 +1459,16 @@ test "big.int add single-single" { } test "big.int add multi-single" { - var a = try Int.initSet(al, @maxValue(Limb) + 1); + var a = try Int.initSet(al, maxInt(Limb) + 1); var b = try Int.initSet(al, 1); var c = try Int.init(al); try c.add(a, b); - debug.assert((try c.to(DoubleLimb)) == @maxValue(Limb) + 2); + debug.assert((try c.to(DoubleLimb)) == maxInt(Limb) + 2); try c.add(b, a); - debug.assert((try c.to(DoubleLimb)) == @maxValue(Limb) + 2); + debug.assert((try c.to(DoubleLimb)) == maxInt(Limb) + 2); } test "big.int add multi-multi" { @@ -1533,13 +1535,13 @@ test "big.int sub single-single" { } test "big.int sub multi-single" { - var a = try Int.initSet(al, @maxValue(Limb) + 1); + var a = try Int.initSet(al, maxInt(Limb) + 1); var b = try Int.initSet(al, 1); var c = try Int.init(al); try c.sub(a, b); - debug.assert((try c.to(Limb)) == @maxValue(Limb)); + debug.assert((try c.to(Limb)) == maxInt(Limb)); } test "big.int sub multi-multi" { @@ -1600,13 +1602,13 @@ test "big.int mul single-single" { } test "big.int mul multi-single" { - var a = try Int.initSet(al, @maxValue(Limb)); + var a = try Int.initSet(al, maxInt(Limb)); var b = try Int.initSet(al, 2); var c = try Int.init(al); try c.mul(a, b); - debug.assert((try c.to(DoubleLimb)) == 2 * @maxValue(Limb)); + debug.assert((try c.to(DoubleLimb)) == 2 * maxInt(Limb)); } test "big.int mul multi-multi" { @@ -1622,29 +1624,29 @@ test "big.int mul multi-multi" { } test "big.int mul alias r with a" { - var a = try Int.initSet(al, @maxValue(Limb)); + var a = try Int.initSet(al, maxInt(Limb)); var b = try Int.initSet(al, 2); try a.mul(a, b); - debug.assert((try a.to(DoubleLimb)) == 2 * @maxValue(Limb)); + debug.assert((try a.to(DoubleLimb)) == 2 * maxInt(Limb)); } test "big.int mul alias r with b" { - var a = try Int.initSet(al, @maxValue(Limb)); + var a = try Int.initSet(al, maxInt(Limb)); var b = try Int.initSet(al, 2); try a.mul(b, a); - debug.assert((try a.to(DoubleLimb)) == 2 * @maxValue(Limb)); + debug.assert((try a.to(DoubleLimb)) == 2 * maxInt(Limb)); } test "big.int mul alias r with a and b" { - var a = try Int.initSet(al, @maxValue(Limb)); + var a = try Int.initSet(al, maxInt(Limb)); try a.mul(a, a); - debug.assert((try a.to(DoubleLimb)) == @maxValue(Limb) * @maxValue(Limb)); + debug.assert((try a.to(DoubleLimb)) == maxInt(Limb) * maxInt(Limb)); } test "big.int mul a*0" { @@ -2047,8 +2049,8 @@ test "big.int bitwise and simple" { } test "big.int bitwise and multi-limb" { - var a = try Int.initSet(al, @maxValue(Limb) + 1); - var b = try Int.initSet(al, @maxValue(Limb)); + var a = try Int.initSet(al, maxInt(Limb) + 1); + var b = try Int.initSet(al, maxInt(Limb)); try a.bitAnd(a, b); @@ -2065,12 +2067,12 @@ test "big.int bitwise xor simple" { } test "big.int bitwise xor multi-limb" { - var a = try Int.initSet(al, @maxValue(Limb) + 1); - var b = try Int.initSet(al, @maxValue(Limb)); + var a = try Int.initSet(al, maxInt(Limb) + 1); + var b = try Int.initSet(al, maxInt(Limb)); try a.bitXor(a, b); - debug.assert((try a.to(DoubleLimb)) == (@maxValue(Limb) + 1) ^ @maxValue(Limb)); + debug.assert((try a.to(DoubleLimb)) == (maxInt(Limb) + 1) ^ maxInt(Limb)); } test "big.int bitwise or simple" { @@ -2083,13 +2085,13 @@ test "big.int bitwise or simple" { } test "big.int bitwise or multi-limb" { - var a = try Int.initSet(al, @maxValue(Limb) + 1); - var b = try Int.initSet(al, @maxValue(Limb)); + var a = try Int.initSet(al, maxInt(Limb) + 1); + var b = try Int.initSet(al, maxInt(Limb)); try a.bitOr(a, b); // TODO: big.int.cpp or is wrong on multi-limb. - debug.assert((try a.to(DoubleLimb)) == (@maxValue(Limb) + 1) + @maxValue(Limb)); + debug.assert((try a.to(DoubleLimb)) == (maxInt(Limb) + 1) + maxInt(Limb)); } test "big.int var args" { diff --git a/std/math/copysign.zig b/std/math/copysign.zig index 8c71dcb0bc..4c6e333d6c 100644 --- a/std/math/copysign.zig +++ b/std/math/copysign.zig @@ -1,6 +1,7 @@ const std = @import("../index.zig"); const math = std.math; const assert = std.debug.assert; +const maxInt = std.math.maxInt; pub fn copysign(comptime T: type, x: T, y: T) T { return switch (T) { @@ -15,7 +16,7 @@ fn copysign16(x: f16, y: f16) f16 { const ux = @bitCast(u16, x); const uy = @bitCast(u16, y); - const h1 = ux & (@maxValue(u16) / 2); + const h1 = ux & (maxInt(u16) / 2); const h2 = uy & (u16(1) << 15); return @bitCast(f16, h1 | h2); } @@ -24,7 +25,7 @@ fn copysign32(x: f32, y: f32) f32 { const ux = @bitCast(u32, x); const uy = @bitCast(u32, y); - const h1 = ux & (@maxValue(u32) / 2); + const h1 = ux & (maxInt(u32) / 2); const h2 = uy & (u32(1) << 31); return @bitCast(f32, h1 | h2); } @@ -33,7 +34,7 @@ fn copysign64(x: f64, y: f64) f64 { const ux = @bitCast(u64, x); const uy = @bitCast(u64, y); - const h1 = ux & (@maxValue(u64) / 2); + const h1 = ux & (maxInt(u64) / 2); const h2 = uy & (u64(1) << 63); return @bitCast(f64, h1 | h2); } diff --git a/std/math/cosh.zig b/std/math/cosh.zig index 52beafb642..77e02855fd 100644 --- a/std/math/cosh.zig +++ b/std/math/cosh.zig @@ -9,6 +9,7 @@ const std = @import("../index.zig"); const math = std.math; const expo2 = @import("expo2.zig").expo2; const assert = std.debug.assert; +const maxInt = std.math.maxInt; pub fn cosh(x: var) @typeOf(x) { const T = @typeOf(x); @@ -50,7 +51,7 @@ fn cosh32(x: f32) f32 { fn cosh64(x: f64) f64 { const u = @bitCast(u64, x); const w = @intCast(u32, u >> 32); - const ax = @bitCast(f64, u & (@maxValue(u64) >> 1)); + const ax = @bitCast(f64, u & (maxInt(u64) >> 1)); // TODO: Shouldn't need this explicit check. if (x == 0.0) { diff --git a/std/math/fabs.zig b/std/math/fabs.zig index ae8f9616a8..443010ac7f 100644 --- a/std/math/fabs.zig +++ b/std/math/fabs.zig @@ -6,6 +6,7 @@ const std = @import("../index.zig"); const math = std.math; const assert = std.debug.assert; +const maxInt = std.math.maxInt; pub fn fabs(x: var) @typeOf(x) { const T = @typeOf(x); @@ -31,7 +32,7 @@ fn fabs32(x: f32) f32 { fn fabs64(x: f64) f64 { var u = @bitCast(u64, x); - u &= @maxValue(u64) >> 1; + u &= maxInt(u64) >> 1; return @bitCast(f64, u); } diff --git a/std/math/hypot.zig b/std/math/hypot.zig index f834f422e6..a63657a621 100644 --- a/std/math/hypot.zig +++ b/std/math/hypot.zig @@ -8,6 +8,7 @@ const std = @import("../index.zig"); const math = std.math; const assert = std.debug.assert; +const maxInt = std.math.maxInt; pub fn hypot(comptime T: type, x: T, y: T) T { return switch (T) { @@ -21,8 +22,8 @@ fn hypot32(x: f32, y: f32) f32 { var ux = @bitCast(u32, x); var uy = @bitCast(u32, y); - ux &= @maxValue(u32) >> 1; - uy &= @maxValue(u32) >> 1; + ux &= maxInt(u32) >> 1; + uy &= maxInt(u32) >> 1; if (ux < uy) { const tmp = ux; ux = uy; @@ -65,8 +66,8 @@ fn hypot64(x: f64, y: f64) f64 { var ux = @bitCast(u64, x); var uy = @bitCast(u64, y); - ux &= @maxValue(u64) >> 1; - uy &= @maxValue(u64) >> 1; + ux &= maxInt(u64) >> 1; + uy &= maxInt(u64) >> 1; if (ux < uy) { const tmp = ux; ux = uy; diff --git a/std/math/ilogb.zig b/std/math/ilogb.zig index a24f580a32..c787ab68f9 100644 --- a/std/math/ilogb.zig +++ b/std/math/ilogb.zig @@ -1,12 +1,14 @@ // Special Cases: // -// - ilogb(+-inf) = @maxValue(i32) -// - ilogb(0) = @maxValue(i32) -// - ilogb(nan) = @maxValue(i32) +// - ilogb(+-inf) = maxInt(i32) +// - ilogb(0) = maxInt(i32) +// - ilogb(nan) = maxInt(i32) const std = @import("../index.zig"); const math = std.math; const assert = std.debug.assert; +const maxInt = std.math.maxInt; +const minInt = std.math.minInt; pub fn ilogb(x: var) i32 { const T = @typeOf(x); @@ -18,7 +20,7 @@ pub fn ilogb(x: var) i32 { } // NOTE: Should these be exposed publically? -const fp_ilogbnan = -1 - i32(@maxValue(u32) >> 1); +const fp_ilogbnan = -1 - i32(maxInt(u32) >> 1); const fp_ilogb0 = fp_ilogbnan; fn ilogb32(x: f32) i32 { @@ -27,7 +29,7 @@ fn ilogb32(x: f32) i32 { // TODO: We should be able to merge this with the lower check. if (math.isNan(x)) { - return @maxValue(i32); + return maxInt(i32); } if (e == 0) { @@ -50,7 +52,7 @@ fn ilogb32(x: f32) i32 { if (u << 9 != 0) { return fp_ilogbnan; } else { - return @maxValue(i32); + return maxInt(i32); } } @@ -62,7 +64,7 @@ fn ilogb64(x: f64) i32 { var e = @intCast(i32, (u >> 52) & 0x7FF); if (math.isNan(x)) { - return @maxValue(i32); + return maxInt(i32); } if (e == 0) { @@ -85,7 +87,7 @@ fn ilogb64(x: f64) i32 { if (u << 12 != 0) { return fp_ilogbnan; } else { - return @maxValue(i32); + return maxInt(i32); } } @@ -116,15 +118,15 @@ test "math.ilogb64" { } test "math.ilogb32.special" { - assert(ilogb32(math.inf(f32)) == @maxValue(i32)); - assert(ilogb32(-math.inf(f32)) == @maxValue(i32)); - assert(ilogb32(0.0) == @minValue(i32)); - assert(ilogb32(math.nan(f32)) == @maxValue(i32)); + assert(ilogb32(math.inf(f32)) == maxInt(i32)); + assert(ilogb32(-math.inf(f32)) == maxInt(i32)); + assert(ilogb32(0.0) == minInt(i32)); + assert(ilogb32(math.nan(f32)) == maxInt(i32)); } test "math.ilogb64.special" { - assert(ilogb64(math.inf(f64)) == @maxValue(i32)); - assert(ilogb64(-math.inf(f64)) == @maxValue(i32)); - assert(ilogb64(0.0) == @minValue(i32)); - assert(ilogb64(math.nan(f64)) == @maxValue(i32)); + assert(ilogb64(math.inf(f64)) == maxInt(i32)); + assert(ilogb64(-math.inf(f64)) == maxInt(i32)); + assert(ilogb64(0.0) == minInt(i32)); + assert(ilogb64(math.nan(f64)) == maxInt(i32)); } diff --git a/std/math/index.zig b/std/math/index.zig index 8db2105f30..526d68addc 100644 --- a/std/math/index.zig +++ b/std/math/index.zig @@ -382,7 +382,7 @@ pub fn absInt(x: var) !@typeOf(x) { comptime assert(@typeId(T) == builtin.TypeId.Int); // must pass an integer to absInt comptime assert(T.is_signed); // must pass a signed integer to absInt - if (x == @minValue(@typeOf(x))) { + if (x == minInt(@typeOf(x))) { return error.Overflow; } else { @setRuntimeSafety(false); @@ -404,7 +404,7 @@ pub const absFloat = @import("fabs.zig").fabs; pub fn divTrunc(comptime T: type, numerator: T, denominator: T) !T { @setRuntimeSafety(false); if (denominator == 0) return error.DivisionByZero; - if (@typeId(T) == builtin.TypeId.Int and T.is_signed and numerator == @minValue(T) and denominator == -1) return error.Overflow; + if (@typeId(T) == builtin.TypeId.Int and T.is_signed and numerator == minInt(T) and denominator == -1) return error.Overflow; return @divTrunc(numerator, denominator); } @@ -425,7 +425,7 @@ fn testDivTrunc() void { pub fn divFloor(comptime T: type, numerator: T, denominator: T) !T { @setRuntimeSafety(false); if (denominator == 0) return error.DivisionByZero; - if (@typeId(T) == builtin.TypeId.Int and T.is_signed and numerator == @minValue(T) and denominator == -1) return error.Overflow; + if (@typeId(T) == builtin.TypeId.Int and T.is_signed and numerator == minInt(T) and denominator == -1) return error.Overflow; return @divFloor(numerator, denominator); } @@ -446,7 +446,7 @@ fn testDivFloor() void { pub fn divExact(comptime T: type, numerator: T, denominator: T) !T { @setRuntimeSafety(false); if (denominator == 0) return error.DivisionByZero; - if (@typeId(T) == builtin.TypeId.Int and T.is_signed and numerator == @minValue(T) and denominator == -1) return error.Overflow; + if (@typeId(T) == builtin.TypeId.Int and T.is_signed and numerator == minInt(T) and denominator == -1) return error.Overflow; const result = @divTrunc(numerator, denominator); if (result * denominator != numerator) return error.UnexpectedRemainder; return result; @@ -530,8 +530,8 @@ test "math.absCast" { assert(absCast(i32(999)) == 999); assert(@typeOf(absCast(i32(999))) == u32); - assert(absCast(i32(@minValue(i32))) == -@minValue(i32)); - assert(@typeOf(absCast(i32(@minValue(i32)))) == u32); + assert(absCast(i32(minInt(i32))) == -minInt(i32)); + assert(@typeOf(absCast(i32(minInt(i32)))) == u32); } /// Returns the negation of the integer parameter. @@ -540,9 +540,9 @@ pub fn negateCast(x: var) !@IntType(true, @typeOf(x).bit_count) { if (@typeOf(x).is_signed) return negate(x); const int = @IntType(true, @typeOf(x).bit_count); - if (x > -@minValue(int)) return error.Overflow; + if (x > -minInt(int)) return error.Overflow; - if (x == -@minValue(int)) return @minValue(int); + if (x == -minInt(int)) return minInt(int); return -@intCast(int, x); } @@ -551,10 +551,10 @@ test "math.negateCast" { assert((negateCast(u32(999)) catch unreachable) == -999); assert(@typeOf(negateCast(u32(999)) catch unreachable) == i32); - assert((negateCast(u32(-@minValue(i32))) catch unreachable) == @minValue(i32)); - assert(@typeOf(negateCast(u32(-@minValue(i32))) catch unreachable) == i32); + assert((negateCast(u32(-minInt(i32))) catch unreachable) == minInt(i32)); + assert(@typeOf(negateCast(u32(-minInt(i32))) catch unreachable) == i32); - if (negateCast(u32(@maxValue(i32) + 10))) |_| unreachable else |err| assert(err == error.Overflow); + if (negateCast(u32(maxInt(i32) + 10))) |_| unreachable else |err| assert(err == error.Overflow); } /// Cast an integer to a different integer type. If the value doesn't fit, @@ -562,9 +562,9 @@ test "math.negateCast" { pub fn cast(comptime T: type, x: var) (error.{Overflow}!T) { comptime assert(@typeId(T) == builtin.TypeId.Int); // must pass an integer comptime assert(@typeId(@typeOf(x)) == builtin.TypeId.Int); // must pass an integer - if (@maxValue(@typeOf(x)) > @maxValue(T) and x > @maxValue(T)) { + if (maxInt(@typeOf(x)) > maxInt(T) and x > maxInt(T)) { return error.Overflow; - } else if (@minValue(@typeOf(x)) < @minValue(T) and x < @minValue(T)) { + } else if (minInt(@typeOf(x)) < minInt(T) and x < minInt(T)) { return error.Overflow; } else { return @intCast(T, x); @@ -658,3 +658,59 @@ test "math.f64_min" { const fmin: f64 = f64_min; assert(@bitCast(u64, fmin) == f64_min_u64); } + +pub fn maxInt(comptime T: type) comptime_int { + const info = @typeInfo(T); + const bit_count = comptime_int(info.Int.bits); // TODO #1683 + if (bit_count == 0) return 0; + return (1 << (bit_count - @boolToInt(info.Int.is_signed))) - 1; +} + +pub fn minInt(comptime T: type) comptime_int { + const info = @typeInfo(T); + const bit_count = comptime_int(info.Int.bits); // TODO #1683 + if (!info.Int.is_signed) return 0; + if (bit_count == 0) return 0; + return -(1 << (bit_count - 1)); +} + +test "minInt and maxInt" { + assert(maxInt(u0) == 0); + assert(maxInt(u1) == 1); + assert(maxInt(u8) == 255); + assert(maxInt(u16) == 65535); + assert(maxInt(u32) == 4294967295); + assert(maxInt(u64) == 18446744073709551615); + + assert(maxInt(i0) == 0); + assert(maxInt(i1) == 0); + assert(maxInt(i8) == 127); + assert(maxInt(i16) == 32767); + assert(maxInt(i32) == 2147483647); + assert(maxInt(i63) == 4611686018427387903); + assert(maxInt(i64) == 9223372036854775807); + + assert(minInt(u0) == 0); + assert(minInt(u1) == 0); + assert(minInt(u8) == 0); + assert(minInt(u16) == 0); + assert(minInt(u32) == 0); + assert(minInt(u63) == 0); + assert(minInt(u64) == 0); + + assert(minInt(i0) == 0); + assert(minInt(i1) == -1); + assert(minInt(i8) == -128); + assert(minInt(i16) == -32768); + assert(minInt(i32) == -2147483648); + assert(minInt(i63) == -4611686018427387904); + assert(minInt(i64) == -9223372036854775808); +} + +test "max value type" { + // If the type of maxInt(i32) was i32 then this implicit cast to + // u32 would not work. But since the value is a number literal, + // it works fine. + const x: u32 = maxInt(i32); + assert(x == 2147483647); +} diff --git a/std/math/isfinite.zig b/std/math/isfinite.zig index 3a5d4f01bb..bdfdff8f9f 100644 --- a/std/math/isfinite.zig +++ b/std/math/isfinite.zig @@ -1,6 +1,7 @@ const std = @import("../index.zig"); const math = std.math; const assert = std.debug.assert; +const maxInt = std.math.maxInt; pub fn isFinite(x: var) bool { const T = @typeOf(x); @@ -15,7 +16,7 @@ pub fn isFinite(x: var) bool { }, f64 => { const bits = @bitCast(u64, x); - return bits & (@maxValue(u64) >> 1) < (0x7FF << 52); + return bits & (maxInt(u64) >> 1) < (0x7FF << 52); }, else => { @compileError("isFinite not implemented for " ++ @typeName(T)); diff --git a/std/math/isinf.zig b/std/math/isinf.zig index cf68b5769c..93f66cc870 100644 --- a/std/math/isinf.zig +++ b/std/math/isinf.zig @@ -1,6 +1,7 @@ const std = @import("../index.zig"); const math = std.math; const assert = std.debug.assert; +const maxInt = std.math.maxInt; pub fn isInf(x: var) bool { const T = @typeOf(x); @@ -15,7 +16,7 @@ pub fn isInf(x: var) bool { }, f64 => { const bits = @bitCast(u64, x); - return bits & (@maxValue(u64) >> 1) == (0x7FF << 52); + return bits & (maxInt(u64) >> 1) == (0x7FF << 52); }, else => { @compileError("isInf not implemented for " ++ @typeName(T)); diff --git a/std/math/isnan.zig b/std/math/isnan.zig index e05c1428b0..a2cb85b4f7 100644 --- a/std/math/isnan.zig +++ b/std/math/isnan.zig @@ -1,6 +1,7 @@ const std = @import("../index.zig"); const math = std.math; const assert = std.debug.assert; +const maxInt = std.math.maxInt; pub fn isNan(x: var) bool { const T = @typeOf(x); @@ -15,7 +16,7 @@ pub fn isNan(x: var) bool { }, f64 => { const bits = @bitCast(u64, x); - return (bits & (@maxValue(u64) >> 1)) > (u64(0x7FF) << 52); + return (bits & (maxInt(u64) >> 1)) > (u64(0x7FF) << 52); }, else => { @compileError("isNan not implemented for " ++ @typeName(T)); diff --git a/std/math/isnormal.zig b/std/math/isnormal.zig index 22109936c4..cc088e46a0 100644 --- a/std/math/isnormal.zig +++ b/std/math/isnormal.zig @@ -1,6 +1,7 @@ const std = @import("../index.zig"); const math = std.math; const assert = std.debug.assert; +const maxInt = std.math.maxInt; pub fn isNormal(x: var) bool { const T = @typeOf(x); @@ -15,7 +16,7 @@ pub fn isNormal(x: var) bool { }, f64 => { const bits = @bitCast(u64, x); - return (bits + (1 << 52)) & (@maxValue(u64) >> 1) >= (1 << 53); + return (bits + (1 << 52)) & (maxInt(u64) >> 1) >= (1 << 53); }, else => { @compileError("isNormal not implemented for " ++ @typeName(T)); diff --git a/std/math/log10.zig b/std/math/log10.zig index a93ce48fb7..2b53d8a6ae 100644 --- a/std/math/log10.zig +++ b/std/math/log10.zig @@ -10,6 +10,7 @@ const math = std.math; const assert = std.debug.assert; const builtin = @import("builtin"); const TypeId = builtin.TypeId; +const maxInt = std.math.maxInt; pub fn log10(x: var) @typeOf(x) { const T = @typeOf(x); @@ -151,7 +152,7 @@ pub fn log10_64(x_: f64) f64 { // hi + lo = f - hfsq + s * (hfsq + R) ~ log(1 + f) var hi = f - hfsq; var hii = @bitCast(u64, hi); - hii &= u64(@maxValue(u64)) << 32; + hii &= u64(maxInt(u64)) << 32; hi = @bitCast(f64, hii); const lo = f - hi - hfsq + s * (hfsq + R); diff --git a/std/math/log2.zig b/std/math/log2.zig index 858f6ffa02..555c0bdf18 100644 --- a/std/math/log2.zig +++ b/std/math/log2.zig @@ -10,6 +10,7 @@ const math = std.math; const assert = std.debug.assert; const builtin = @import("builtin"); const TypeId = builtin.TypeId; +const maxInt = std.math.maxInt; pub fn log2(x: var) @typeOf(x) { const T = @typeOf(x); @@ -151,7 +152,7 @@ pub fn log2_64(x_: f64) f64 { // hi + lo = f - hfsq + s * (hfsq + R) ~ log(1 + f) var hi = f - hfsq; var hii = @bitCast(u64, hi); - hii &= u64(@maxValue(u64)) << 32; + hii &= u64(maxInt(u64)) << 32; hi = @bitCast(f64, hii); const lo = f - hi - hfsq + s * (hfsq + R); diff --git a/std/math/modf.zig b/std/math/modf.zig index e06874dd88..cf4987e644 100644 --- a/std/math/modf.zig +++ b/std/math/modf.zig @@ -6,6 +6,7 @@ const std = @import("../index.zig"); const math = std.math; const assert = std.debug.assert; +const maxInt = std.math.maxInt; fn modf_result(comptime T: type) type { return struct.{ @@ -101,7 +102,7 @@ fn modf64(x: f64) modf64_result { return result; } - const mask = u64(@maxValue(u64) >> 12) >> @intCast(u6, e); + const mask = u64(maxInt(u64) >> 12) >> @intCast(u6, e); if (u & mask == 0) { result.ipart = x; result.fpart = @bitCast(f64, us); diff --git a/std/math/sinh.zig b/std/math/sinh.zig index 3105b9a26e..733b89754a 100644 --- a/std/math/sinh.zig +++ b/std/math/sinh.zig @@ -9,6 +9,7 @@ const std = @import("../index.zig"); const math = std.math; const assert = std.debug.assert; const expo2 = @import("expo2.zig").expo2; +const maxInt = std.math.maxInt; pub fn sinh(x: var) @typeOf(x) { const T = @typeOf(x); @@ -56,7 +57,7 @@ fn sinh32(x: f32) f32 { fn sinh64(x: f64) f64 { const u = @bitCast(u64, x); const w = @intCast(u32, u >> 32); - const ax = @bitCast(f64, u & (@maxValue(u64) >> 1)); + const ax = @bitCast(f64, u & (maxInt(u64) >> 1)); if (x == 0.0 or math.isNan(x)) { return x; diff --git a/std/math/sqrt.zig b/std/math/sqrt.zig index e12ecf9683..4300f20f5a 100644 --- a/std/math/sqrt.zig +++ b/std/math/sqrt.zig @@ -10,6 +10,7 @@ const math = std.math; const assert = std.debug.assert; const builtin = @import("builtin"); const TypeId = builtin.TypeId; +const maxInt = std.math.maxInt; pub fn sqrt(x: var) (if (@typeId(@typeOf(x)) == TypeId.Int) @IntType(false, @typeOf(x).bit_count / 2) else @typeOf(x)) { const T = @typeOf(x); @@ -17,7 +18,7 @@ pub fn sqrt(x: var) (if (@typeId(@typeOf(x)) == TypeId.Int) @IntType(false, @typ TypeId.ComptimeFloat => return T(@sqrt(f64, x)), // TODO upgrade to f128 TypeId.Float => return @sqrt(T, x), TypeId.ComptimeInt => comptime { - if (x > @maxValue(u128)) { + if (x > maxInt(u128)) { @compileError("sqrt not implemented for comptime_int greater than 128 bits"); } if (x < 0) { diff --git a/std/math/tanh.zig b/std/math/tanh.zig index 6204b2a374..faeb2641cc 100644 --- a/std/math/tanh.zig +++ b/std/math/tanh.zig @@ -9,6 +9,7 @@ const std = @import("../index.zig"); const math = std.math; const assert = std.debug.assert; const expo2 = @import("expo2.zig").expo2; +const maxInt = std.math.maxInt; pub fn tanh(x: var) @typeOf(x) { const T = @typeOf(x); @@ -69,7 +70,7 @@ fn tanh32(x: f32) f32 { fn tanh64(x: f64) f64 { const u = @bitCast(u64, x); const w = @intCast(u32, u >> 32); - const ax = @bitCast(f64, u & (@maxValue(u64) >> 1)); + const ax = @bitCast(f64, u & (maxInt(u64) >> 1)); var t: f64 = undefined; diff --git a/std/math/trunc.zig b/std/math/trunc.zig index 92d5bfebc5..bb309a1e24 100644 --- a/std/math/trunc.zig +++ b/std/math/trunc.zig @@ -7,6 +7,7 @@ const std = @import("../index.zig"); const math = std.math; const assert = std.debug.assert; +const maxInt = std.math.maxInt; pub fn trunc(x: var) @typeOf(x) { const T = @typeOf(x); @@ -29,7 +30,7 @@ fn trunc32(x: f32) f32 { e = 1; } - m = u32(@maxValue(u32)) >> @intCast(u5, e); + m = u32(maxInt(u32)) >> @intCast(u5, e); if (u & m == 0) { return x; } else { @@ -50,7 +51,7 @@ fn trunc64(x: f64) f64 { e = 1; } - m = u64(@maxValue(u64)) >> @intCast(u6, e); + m = u64(maxInt(u64)) >> @intCast(u6, e); if (u & m == 0) { return x; } else { |
