2017-05-17 16:26:35 +00:00
|
|
|
const builtin = @import("builtin");
|
2017-12-24 03:08:53 +00:00
|
|
|
const std = @import("../index.zig");
|
2017-06-16 08:26:10 +00:00
|
|
|
const TypeId = builtin.TypeId;
|
2017-12-24 03:08:53 +00:00
|
|
|
const assert = std.debug.assert;
|
2017-06-16 08:26:10 +00:00
|
|
|
|
2017-08-20 09:43:17 +00:00
|
|
|
pub const e = 2.71828182845904523536028747135266249775724709369995;
|
|
|
|
pub const pi = 3.14159265358979323846264338327950288419716939937510;
|
2017-06-16 08:26:10 +00:00
|
|
|
|
|
|
|
// float.h details
|
|
|
|
pub const f64_true_min = 4.94065645841246544177e-324;
|
|
|
|
pub const f64_min = 2.22507385850720138309e-308;
|
|
|
|
pub const f64_max = 1.79769313486231570815e+308;
|
|
|
|
pub const f64_epsilon = 2.22044604925031308085e-16;
|
|
|
|
pub const f64_toint = 1.0 / f64_epsilon;
|
|
|
|
|
|
|
|
pub const f32_true_min = 1.40129846432481707092e-45;
|
|
|
|
pub const f32_min = 1.17549435082228750797e-38;
|
|
|
|
pub const f32_max = 3.40282346638528859812e+38;
|
|
|
|
pub const f32_epsilon = 1.1920928955078125e-07;
|
|
|
|
pub const f32_toint = 1.0 / f32_epsilon;
|
|
|
|
|
2018-06-29 23:44:54 +00:00
|
|
|
pub const f16_true_min = 0.000000059604644775390625; // 2**-24
|
|
|
|
pub const f16_min = 0.00006103515625; // 2**-14
|
|
|
|
pub const f16_max = 65504;
|
|
|
|
pub const f16_epsilon = 0.0009765625; // 2**-10
|
|
|
|
pub const f16_toint = 1.0 / f16_epsilon;
|
|
|
|
|
2017-06-16 08:26:10 +00:00
|
|
|
pub const nan_u32 = u32(0x7F800001);
|
|
|
|
pub const nan_f32 = @bitCast(f32, nan_u32);
|
|
|
|
|
|
|
|
pub const inf_u32 = u32(0x7F800000);
|
|
|
|
pub const inf_f32 = @bitCast(f32, inf_u32);
|
|
|
|
|
|
|
|
pub const nan_u64 = u64(0x7FF << 52) | 1;
|
|
|
|
pub const nan_f64 = @bitCast(f64, nan_u64);
|
|
|
|
|
|
|
|
pub const inf_u64 = u64(0x7FF << 52);
|
|
|
|
pub const inf_f64 = @bitCast(f64, inf_u64);
|
|
|
|
|
|
|
|
pub const nan = @import("nan.zig").nan;
|
2017-06-20 11:01:04 +00:00
|
|
|
pub const snan = @import("nan.zig").snan;
|
2017-06-16 08:26:10 +00:00
|
|
|
pub const inf = @import("inf.zig").inf;
|
|
|
|
|
2018-01-25 09:10:11 +00:00
|
|
|
pub fn approxEq(comptime T: type, x: T, y: T, epsilon: T) bool {
|
2017-06-16 08:26:10 +00:00
|
|
|
assert(@typeId(T) == TypeId.Float);
|
2017-12-22 05:50:30 +00:00
|
|
|
return fabs(x - y) < epsilon;
|
2017-06-16 08:26:10 +00:00
|
|
|
}
|
2017-02-09 07:50:03 +00:00
|
|
|
|
2017-06-16 08:26:10 +00:00
|
|
|
// TODO: Hide the following in an internal module.
|
2018-01-25 09:10:11 +00:00
|
|
|
pub fn forceEval(value: var) void {
|
2017-06-16 08:26:10 +00:00
|
|
|
const T = @typeOf(value);
|
|
|
|
switch (T) {
|
|
|
|
f32 => {
|
|
|
|
var x: f32 = undefined;
|
2018-05-31 14:56:59 +00:00
|
|
|
const p = @ptrCast(*volatile f32, &x);
|
2018-05-01 00:35:54 +00:00
|
|
|
p.* = x;
|
2017-06-16 08:26:10 +00:00
|
|
|
},
|
|
|
|
f64 => {
|
|
|
|
var x: f64 = undefined;
|
2018-05-31 14:56:59 +00:00
|
|
|
const p = @ptrCast(*volatile f64, &x);
|
2018-05-01 00:35:54 +00:00
|
|
|
p.* = x;
|
2017-06-16 08:26:10 +00:00
|
|
|
},
|
|
|
|
else => {
|
|
|
|
@compileError("forceEval not implemented for " ++ @typeName(T));
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-25 09:10:11 +00:00
|
|
|
pub fn raiseInvalid() void {
|
2017-06-16 08:26:10 +00:00
|
|
|
// Raise INVALID fpu exception
|
|
|
|
}
|
|
|
|
|
2018-01-25 09:10:11 +00:00
|
|
|
pub fn raiseUnderflow() void {
|
2017-06-16 08:26:10 +00:00
|
|
|
// Raise UNDERFLOW fpu exception
|
|
|
|
}
|
|
|
|
|
2018-01-25 09:10:11 +00:00
|
|
|
pub fn raiseOverflow() void {
|
2017-06-16 08:26:10 +00:00
|
|
|
// Raise OVERFLOW fpu exception
|
|
|
|
}
|
|
|
|
|
2018-01-25 09:10:11 +00:00
|
|
|
pub fn raiseInexact() void {
|
2017-06-16 08:26:10 +00:00
|
|
|
// Raise INEXACT fpu exception
|
|
|
|
}
|
|
|
|
|
2018-01-25 09:10:11 +00:00
|
|
|
pub fn raiseDivByZero() void {
|
2017-06-16 08:26:10 +00:00
|
|
|
// Raise INEXACT fpu exception
|
|
|
|
}
|
|
|
|
|
|
|
|
pub const isNan = @import("isnan.zig").isNan;
|
2017-06-20 11:01:04 +00:00
|
|
|
pub const isSignalNan = @import("isnan.zig").isSignalNan;
|
2017-06-16 08:26:10 +00:00
|
|
|
pub const fabs = @import("fabs.zig").fabs;
|
|
|
|
pub const ceil = @import("ceil.zig").ceil;
|
|
|
|
pub const floor = @import("floor.zig").floor;
|
2017-08-07 19:57:41 +00:00
|
|
|
pub const trunc = @import("trunc.zig").trunc;
|
2017-06-16 08:26:10 +00:00
|
|
|
pub const round = @import("round.zig").round;
|
2017-06-08 02:56:57 +00:00
|
|
|
pub const frexp = @import("frexp.zig").frexp;
|
2017-06-16 08:26:10 +00:00
|
|
|
pub const frexp32_result = @import("frexp.zig").frexp32_result;
|
|
|
|
pub const frexp64_result = @import("frexp.zig").frexp64_result;
|
|
|
|
pub const modf = @import("modf.zig").modf;
|
|
|
|
pub const modf32_result = @import("modf.zig").modf32_result;
|
|
|
|
pub const modf64_result = @import("modf.zig").modf64_result;
|
|
|
|
pub const copysign = @import("copysign.zig").copysign;
|
|
|
|
pub const isFinite = @import("isfinite.zig").isFinite;
|
|
|
|
pub const isInf = @import("isinf.zig").isInf;
|
|
|
|
pub const isPositiveInf = @import("isinf.zig").isPositiveInf;
|
|
|
|
pub const isNegativeInf = @import("isinf.zig").isNegativeInf;
|
|
|
|
pub const isNormal = @import("isnormal.zig").isNormal;
|
|
|
|
pub const signbit = @import("signbit.zig").signbit;
|
|
|
|
pub const scalbn = @import("scalbn.zig").scalbn;
|
|
|
|
pub const pow = @import("pow.zig").pow;
|
|
|
|
pub const sqrt = @import("sqrt.zig").sqrt;
|
|
|
|
pub const cbrt = @import("cbrt.zig").cbrt;
|
|
|
|
pub const acos = @import("acos.zig").acos;
|
|
|
|
pub const asin = @import("asin.zig").asin;
|
|
|
|
pub const atan = @import("atan.zig").atan;
|
|
|
|
pub const atan2 = @import("atan2.zig").atan2;
|
|
|
|
pub const hypot = @import("hypot.zig").hypot;
|
|
|
|
pub const exp = @import("exp.zig").exp;
|
|
|
|
pub const exp2 = @import("exp2.zig").exp2;
|
|
|
|
pub const expm1 = @import("expm1.zig").expm1;
|
|
|
|
pub const ilogb = @import("ilogb.zig").ilogb;
|
|
|
|
pub const ln = @import("ln.zig").ln;
|
|
|
|
pub const log = @import("log.zig").log;
|
|
|
|
pub const log2 = @import("log2.zig").log2;
|
|
|
|
pub const log10 = @import("log10.zig").log10;
|
|
|
|
pub const log1p = @import("log1p.zig").log1p;
|
|
|
|
pub const fma = @import("fma.zig").fma;
|
|
|
|
pub const asinh = @import("asinh.zig").asinh;
|
|
|
|
pub const acosh = @import("acosh.zig").acosh;
|
|
|
|
pub const atanh = @import("atanh.zig").atanh;
|
|
|
|
pub const sinh = @import("sinh.zig").sinh;
|
|
|
|
pub const cosh = @import("cosh.zig").cosh;
|
|
|
|
pub const tanh = @import("tanh.zig").tanh;
|
|
|
|
pub const cos = @import("cos.zig").cos;
|
|
|
|
pub const sin = @import("sin.zig").sin;
|
|
|
|
pub const tan = @import("tan.zig").tan;
|
|
|
|
|
2018-04-24 07:18:31 +00:00
|
|
|
pub const complex = @import("complex/index.zig");
|
|
|
|
pub const Complex = complex.Complex;
|
|
|
|
|
2018-06-08 22:24:20 +00:00
|
|
|
pub const big = @import("big/index.zig");
|
|
|
|
|
2017-06-16 08:26:10 +00:00
|
|
|
test "math" {
|
|
|
|
_ = @import("nan.zig");
|
|
|
|
_ = @import("isnan.zig");
|
|
|
|
_ = @import("fabs.zig");
|
|
|
|
_ = @import("ceil.zig");
|
|
|
|
_ = @import("floor.zig");
|
|
|
|
_ = @import("trunc.zig");
|
|
|
|
_ = @import("round.zig");
|
|
|
|
_ = @import("frexp.zig");
|
|
|
|
_ = @import("modf.zig");
|
|
|
|
_ = @import("copysign.zig");
|
|
|
|
_ = @import("isfinite.zig");
|
|
|
|
_ = @import("isinf.zig");
|
|
|
|
_ = @import("isnormal.zig");
|
|
|
|
_ = @import("signbit.zig");
|
|
|
|
_ = @import("scalbn.zig");
|
|
|
|
_ = @import("pow.zig");
|
|
|
|
_ = @import("sqrt.zig");
|
|
|
|
_ = @import("cbrt.zig");
|
|
|
|
_ = @import("acos.zig");
|
|
|
|
_ = @import("asin.zig");
|
|
|
|
_ = @import("atan.zig");
|
|
|
|
_ = @import("atan2.zig");
|
|
|
|
_ = @import("hypot.zig");
|
|
|
|
_ = @import("exp.zig");
|
|
|
|
_ = @import("exp2.zig");
|
|
|
|
_ = @import("expm1.zig");
|
|
|
|
_ = @import("ilogb.zig");
|
|
|
|
_ = @import("ln.zig");
|
|
|
|
_ = @import("log.zig");
|
|
|
|
_ = @import("log2.zig");
|
|
|
|
_ = @import("log10.zig");
|
|
|
|
_ = @import("log1p.zig");
|
|
|
|
_ = @import("fma.zig");
|
|
|
|
_ = @import("asinh.zig");
|
|
|
|
_ = @import("acosh.zig");
|
|
|
|
_ = @import("atanh.zig");
|
|
|
|
_ = @import("sinh.zig");
|
|
|
|
_ = @import("cosh.zig");
|
|
|
|
_ = @import("tanh.zig");
|
|
|
|
_ = @import("sin.zig");
|
|
|
|
_ = @import("cos.zig");
|
|
|
|
_ = @import("tan.zig");
|
2018-04-24 07:18:31 +00:00
|
|
|
|
|
|
|
_ = @import("complex/index.zig");
|
2018-06-08 22:24:20 +00:00
|
|
|
|
|
|
|
_ = @import("big/index.zig");
|
2017-06-16 08:26:10 +00:00
|
|
|
}
|
|
|
|
|
2018-06-21 05:13:53 +00:00
|
|
|
pub fn floatMantissaBits(comptime T: type) comptime_int {
|
|
|
|
assert(@typeId(T) == builtin.TypeId.Float);
|
|
|
|
|
|
|
|
return switch (T.bit_count) {
|
|
|
|
16 => 10,
|
|
|
|
32 => 23,
|
|
|
|
64 => 52,
|
|
|
|
80 => 64,
|
|
|
|
128 => 112,
|
|
|
|
else => @compileError("unknown floating point type " ++ @typeName(T)),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn floatExponentBits(comptime T: type) comptime_int {
|
|
|
|
assert(@typeId(T) == builtin.TypeId.Float);
|
|
|
|
|
|
|
|
return switch (T.bit_count) {
|
|
|
|
16 => 5,
|
|
|
|
32 => 8,
|
|
|
|
64 => 11,
|
|
|
|
80 => 15,
|
|
|
|
128 => 15,
|
|
|
|
else => @compileError("unknown floating point type " ++ @typeName(T)),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2018-01-25 09:10:11 +00:00
|
|
|
pub fn min(x: var, y: var) @typeOf(x + y) {
|
2017-12-22 05:50:30 +00:00
|
|
|
return if (x < y) x else y;
|
2016-02-02 09:43:33 +00:00
|
|
|
}
|
|
|
|
|
2017-05-07 03:13:12 +00:00
|
|
|
test "math.min" {
|
|
|
|
assert(min(i32(-1), i32(2)) == -1);
|
|
|
|
}
|
|
|
|
|
2018-01-25 09:10:11 +00:00
|
|
|
pub fn max(x: var, y: var) @typeOf(x + y) {
|
2017-12-22 05:50:30 +00:00
|
|
|
return if (x > y) x else y;
|
2016-05-08 23:05:41 +00:00
|
|
|
}
|
2016-07-25 01:35:50 +00:00
|
|
|
|
2017-05-07 03:13:12 +00:00
|
|
|
test "math.max" {
|
|
|
|
assert(max(i32(-1), i32(2)) == 2);
|
|
|
|
}
|
|
|
|
|
2018-02-02 19:26:14 +00:00
|
|
|
pub fn mul(comptime T: type, a: T, b: T) (error{Overflow}!T) {
|
2016-07-25 01:35:50 +00:00
|
|
|
var answer: T = undefined;
|
2017-12-22 05:50:30 +00:00
|
|
|
return if (@mulWithOverflow(T, a, b, &answer)) error.Overflow else answer;
|
2016-07-25 01:35:50 +00:00
|
|
|
}
|
2017-05-07 03:13:12 +00:00
|
|
|
|
2018-02-02 19:26:14 +00:00
|
|
|
pub fn add(comptime T: type, a: T, b: T) (error{Overflow}!T) {
|
2016-07-25 01:35:50 +00:00
|
|
|
var answer: T = undefined;
|
2017-12-22 05:50:30 +00:00
|
|
|
return if (@addWithOverflow(T, a, b, &answer)) error.Overflow else answer;
|
2016-07-25 01:35:50 +00:00
|
|
|
}
|
2017-05-07 03:13:12 +00:00
|
|
|
|
2018-02-02 19:26:14 +00:00
|
|
|
pub fn sub(comptime T: type, a: T, b: T) (error{Overflow}!T) {
|
2016-07-25 01:35:50 +00:00
|
|
|
var answer: T = undefined;
|
2017-12-22 05:50:30 +00:00
|
|
|
return if (@subWithOverflow(T, a, b, &answer)) error.Overflow else answer;
|
2016-07-25 01:35:50 +00:00
|
|
|
}
|
2017-05-07 03:13:12 +00:00
|
|
|
|
2018-02-01 03:48:40 +00:00
|
|
|
pub fn negate(x: var) !@typeOf(x) {
|
2017-05-31 22:23:56 +00:00
|
|
|
return sub(@typeOf(x), 0, x);
|
|
|
|
}
|
|
|
|
|
2018-02-01 03:48:40 +00:00
|
|
|
pub fn shlExact(comptime T: type, a: T, shift_amt: Log2Int(T)) !T {
|
2016-09-09 12:58:39 +00:00
|
|
|
var answer: T = undefined;
|
2017-12-22 05:50:30 +00:00
|
|
|
return if (@shlWithOverflow(T, a, shift_amt, &answer)) error.Overflow else answer;
|
bit shifting safety
* add u3, u4, u5, u6, u7 and i3, i4, i5, i6, i7
* shift operations shift amount parameter type is
integer with log2 bit width of other param
- This enforces not violating undefined behavior on
shift amount >= bit width with the type system
* clean up math.log, math.ln, math.log2, math.log10
closes #403
2017-08-19 05:32:15 +00:00
|
|
|
}
|
|
|
|
|
2017-10-01 01:09:58 +00:00
|
|
|
/// Shifts left. Overflowed bits are truncated.
|
|
|
|
/// A negative shift amount results in a right shift.
|
2018-01-25 09:10:11 +00:00
|
|
|
pub fn shl(comptime T: type, a: T, shift_amt: var) T {
|
2017-10-01 01:09:58 +00:00
|
|
|
const abs_shift_amt = absCast(shift_amt);
|
2018-06-17 06:57:07 +00:00
|
|
|
const casted_shift_amt = if (abs_shift_amt >= T.bit_count) return 0 else @intCast(Log2Int(T), abs_shift_amt);
|
2017-10-01 01:09:58 +00:00
|
|
|
|
|
|
|
if (@typeOf(shift_amt).is_signed) {
|
|
|
|
if (shift_amt >= 0) {
|
|
|
|
return a << casted_shift_amt;
|
|
|
|
} else {
|
|
|
|
return a >> casted_shift_amt;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return a << casted_shift_amt;
|
|
|
|
}
|
|
|
|
|
|
|
|
test "math.shl" {
|
|
|
|
assert(shl(u8, 0b11111111, usize(3)) == 0b11111000);
|
|
|
|
assert(shl(u8, 0b11111111, usize(8)) == 0);
|
|
|
|
assert(shl(u8, 0b11111111, usize(9)) == 0);
|
|
|
|
assert(shl(u8, 0b11111111, isize(-2)) == 0b00111111);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Shifts right. Overflowed bits are truncated.
|
|
|
|
/// A negative shift amount results in a lefft shift.
|
2018-01-25 09:10:11 +00:00
|
|
|
pub fn shr(comptime T: type, a: T, shift_amt: var) T {
|
2017-10-01 01:09:58 +00:00
|
|
|
const abs_shift_amt = absCast(shift_amt);
|
2018-06-17 06:57:07 +00:00
|
|
|
const casted_shift_amt = if (abs_shift_amt >= T.bit_count) return 0 else @intCast(Log2Int(T), abs_shift_amt);
|
2017-10-01 01:09:58 +00:00
|
|
|
|
|
|
|
if (@typeOf(shift_amt).is_signed) {
|
|
|
|
if (shift_amt >= 0) {
|
|
|
|
return a >> casted_shift_amt;
|
|
|
|
} else {
|
|
|
|
return a << casted_shift_amt;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return a >> casted_shift_amt;
|
|
|
|
}
|
|
|
|
|
|
|
|
test "math.shr" {
|
|
|
|
assert(shr(u8, 0b11111111, usize(3)) == 0b00011111);
|
|
|
|
assert(shr(u8, 0b11111111, usize(8)) == 0);
|
|
|
|
assert(shr(u8, 0b11111111, usize(9)) == 0);
|
|
|
|
assert(shr(u8, 0b11111111, isize(-2)) == 0b11111100);
|
|
|
|
}
|
|
|
|
|
2018-01-13 00:23:12 +00:00
|
|
|
/// Rotates right. Only unsigned values can be rotated.
|
|
|
|
/// Negative shift values results in shift modulo the bit count.
|
2018-01-25 09:10:11 +00:00
|
|
|
pub fn rotr(comptime T: type, x: T, r: var) T {
|
2018-01-13 00:23:12 +00:00
|
|
|
if (T.is_signed) {
|
|
|
|
@compileError("cannot rotate signed integer");
|
|
|
|
} else {
|
|
|
|
const ar = @mod(r, T.bit_count);
|
|
|
|
return shr(T, x, ar) | shl(T, x, T.bit_count - ar);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
test "math.rotr" {
|
2018-05-01 00:35:54 +00:00
|
|
|
assert(rotr(u8, 0b00000001, usize(0)) == 0b00000001);
|
|
|
|
assert(rotr(u8, 0b00000001, usize(9)) == 0b10000000);
|
|
|
|
assert(rotr(u8, 0b00000001, usize(8)) == 0b00000001);
|
|
|
|
assert(rotr(u8, 0b00000001, usize(4)) == 0b00010000);
|
2018-01-13 00:23:12 +00:00
|
|
|
assert(rotr(u8, 0b00000001, isize(-1)) == 0b00000010);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Rotates left. Only unsigned values can be rotated.
|
|
|
|
/// Negative shift values results in shift modulo the bit count.
|
2018-01-25 09:10:11 +00:00
|
|
|
pub fn rotl(comptime T: type, x: T, r: var) T {
|
2018-01-13 00:23:12 +00:00
|
|
|
if (T.is_signed) {
|
|
|
|
@compileError("cannot rotate signed integer");
|
|
|
|
} else {
|
|
|
|
const ar = @mod(r, T.bit_count);
|
|
|
|
return shl(T, x, ar) | shr(T, x, T.bit_count - ar);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
test "math.rotl" {
|
2018-05-01 00:35:54 +00:00
|
|
|
assert(rotl(u8, 0b00000001, usize(0)) == 0b00000001);
|
|
|
|
assert(rotl(u8, 0b00000001, usize(9)) == 0b00000010);
|
|
|
|
assert(rotl(u8, 0b00000001, usize(8)) == 0b00000001);
|
|
|
|
assert(rotl(u8, 0b00000001, usize(4)) == 0b00010000);
|
2018-01-13 00:23:12 +00:00
|
|
|
assert(rotl(u8, 0b00000001, isize(-1)) == 0b10000000);
|
|
|
|
}
|
|
|
|
|
2018-01-25 09:10:11 +00:00
|
|
|
pub fn Log2Int(comptime T: type) type {
|
2018-06-06 10:41:55 +00:00
|
|
|
// comptime ceil log2
|
|
|
|
comptime var count: usize = 0;
|
|
|
|
comptime var s = T.bit_count - 1;
|
|
|
|
inline while (s != 0) : (s >>= 1) {
|
|
|
|
count += 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return @IntType(false, count);
|
2016-09-09 12:58:39 +00:00
|
|
|
}
|
2017-02-07 22:19:51 +00:00
|
|
|
|
2017-05-07 03:13:12 +00:00
|
|
|
test "math overflow functions" {
|
|
|
|
testOverflow();
|
|
|
|
comptime testOverflow();
|
|
|
|
}
|
|
|
|
|
2018-01-25 09:10:11 +00:00
|
|
|
fn testOverflow() void {
|
2018-01-09 05:07:01 +00:00
|
|
|
assert((mul(i32, 3, 4) catch unreachable) == 12);
|
|
|
|
assert((add(i32, 3, 4) catch unreachable) == 7);
|
|
|
|
assert((sub(i32, 3, 4) catch unreachable) == -1);
|
|
|
|
assert((shlExact(i32, 0b11, 4) catch unreachable) == 0b110000);
|
2017-05-07 03:13:12 +00:00
|
|
|
}
|
|
|
|
|
2018-02-01 03:48:40 +00:00
|
|
|
pub fn absInt(x: var) !@typeOf(x) {
|
2017-02-07 22:19:51 +00:00
|
|
|
const T = @typeOf(x);
|
2017-05-17 16:26:35 +00:00
|
|
|
comptime assert(@typeId(T) == builtin.TypeId.Int); // must pass an integer to absInt
|
2017-05-07 03:13:12 +00:00
|
|
|
comptime assert(T.is_signed); // must pass a signed integer to absInt
|
2018-05-01 00:35:54 +00:00
|
|
|
|
|
|
|
if (x == @minValue(@typeOf(x))) {
|
2017-05-07 03:13:12 +00:00
|
|
|
return error.Overflow;
|
2018-05-01 00:35:54 +00:00
|
|
|
} else {
|
2018-01-25 06:46:12 +00:00
|
|
|
@setRuntimeSafety(false);
|
2017-02-07 22:19:51 +00:00
|
|
|
return if (x < 0) -x else x;
|
|
|
|
}
|
|
|
|
}
|
2017-05-07 03:13:12 +00:00
|
|
|
|
|
|
|
test "math.absInt" {
|
|
|
|
testAbsInt();
|
|
|
|
comptime testAbsInt();
|
|
|
|
}
|
2018-01-25 09:10:11 +00:00
|
|
|
fn testAbsInt() void {
|
2018-01-09 05:07:01 +00:00
|
|
|
assert((absInt(i32(-10)) catch unreachable) == 10);
|
|
|
|
assert((absInt(i32(10)) catch unreachable) == 10);
|
2017-05-07 03:13:12 +00:00
|
|
|
}
|
|
|
|
|
2017-06-08 02:56:57 +00:00
|
|
|
pub const absFloat = @import("fabs.zig").fabs;
|
2017-05-07 03:13:12 +00:00
|
|
|
|
2018-02-01 03:48:40 +00:00
|
|
|
pub fn divTrunc(comptime T: type, numerator: T, denominator: T) !T {
|
2018-01-25 06:46:12 +00:00
|
|
|
@setRuntimeSafety(false);
|
2018-05-01 00:35:54 +00:00
|
|
|
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;
|
2017-05-07 03:13:12 +00:00
|
|
|
return @divTrunc(numerator, denominator);
|
|
|
|
}
|
|
|
|
|
|
|
|
test "math.divTrunc" {
|
|
|
|
testDivTrunc();
|
|
|
|
comptime testDivTrunc();
|
|
|
|
}
|
2018-01-25 09:10:11 +00:00
|
|
|
fn testDivTrunc() void {
|
2018-01-09 05:07:01 +00:00
|
|
|
assert((divTrunc(i32, 5, 3) catch unreachable) == 1);
|
|
|
|
assert((divTrunc(i32, -5, 3) catch unreachable) == -1);
|
2017-05-07 03:13:12 +00:00
|
|
|
if (divTrunc(i8, -5, 0)) |_| unreachable else |err| assert(err == error.DivisionByZero);
|
|
|
|
if (divTrunc(i8, -128, -1)) |_| unreachable else |err| assert(err == error.Overflow);
|
|
|
|
|
2018-01-09 05:07:01 +00:00
|
|
|
assert((divTrunc(f32, 5.0, 3.0) catch unreachable) == 1.0);
|
|
|
|
assert((divTrunc(f32, -5.0, 3.0) catch unreachable) == -1.0);
|
2017-05-07 03:13:12 +00:00
|
|
|
}
|
|
|
|
|
2018-02-01 03:48:40 +00:00
|
|
|
pub fn divFloor(comptime T: type, numerator: T, denominator: T) !T {
|
2018-01-25 06:46:12 +00:00
|
|
|
@setRuntimeSafety(false);
|
2018-05-01 00:35:54 +00:00
|
|
|
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;
|
2017-05-07 03:13:12 +00:00
|
|
|
return @divFloor(numerator, denominator);
|
|
|
|
}
|
|
|
|
|
|
|
|
test "math.divFloor" {
|
|
|
|
testDivFloor();
|
|
|
|
comptime testDivFloor();
|
|
|
|
}
|
2018-01-25 09:10:11 +00:00
|
|
|
fn testDivFloor() void {
|
2018-01-09 05:07:01 +00:00
|
|
|
assert((divFloor(i32, 5, 3) catch unreachable) == 1);
|
|
|
|
assert((divFloor(i32, -5, 3) catch unreachable) == -2);
|
2017-05-07 03:13:12 +00:00
|
|
|
if (divFloor(i8, -5, 0)) |_| unreachable else |err| assert(err == error.DivisionByZero);
|
|
|
|
if (divFloor(i8, -128, -1)) |_| unreachable else |err| assert(err == error.Overflow);
|
|
|
|
|
2018-01-09 05:07:01 +00:00
|
|
|
assert((divFloor(f32, 5.0, 3.0) catch unreachable) == 1.0);
|
|
|
|
assert((divFloor(f32, -5.0, 3.0) catch unreachable) == -2.0);
|
2017-05-07 03:13:12 +00:00
|
|
|
}
|
|
|
|
|
2018-02-01 03:48:40 +00:00
|
|
|
pub fn divExact(comptime T: type, numerator: T, denominator: T) !T {
|
2018-01-25 06:46:12 +00:00
|
|
|
@setRuntimeSafety(false);
|
2018-05-01 00:35:54 +00:00
|
|
|
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;
|
2017-05-07 03:13:12 +00:00
|
|
|
const result = @divTrunc(numerator, denominator);
|
2018-05-01 00:35:54 +00:00
|
|
|
if (result * denominator != numerator) return error.UnexpectedRemainder;
|
2017-05-07 03:13:12 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
test "math.divExact" {
|
|
|
|
testDivExact();
|
|
|
|
comptime testDivExact();
|
2017-02-07 22:19:51 +00:00
|
|
|
}
|
2018-01-25 09:10:11 +00:00
|
|
|
fn testDivExact() void {
|
2018-01-09 05:07:01 +00:00
|
|
|
assert((divExact(i32, 10, 5) catch unreachable) == 2);
|
|
|
|
assert((divExact(i32, -10, 5) catch unreachable) == -2);
|
2017-05-07 03:13:12 +00:00
|
|
|
if (divExact(i8, -5, 0)) |_| unreachable else |err| assert(err == error.DivisionByZero);
|
|
|
|
if (divExact(i8, -128, -1)) |_| unreachable else |err| assert(err == error.Overflow);
|
|
|
|
if (divExact(i32, 5, 2)) |_| unreachable else |err| assert(err == error.UnexpectedRemainder);
|
2017-02-07 22:19:51 +00:00
|
|
|
|
2018-01-09 05:07:01 +00:00
|
|
|
assert((divExact(f32, 10.0, 5.0) catch unreachable) == 2.0);
|
|
|
|
assert((divExact(f32, -10.0, 5.0) catch unreachable) == -2.0);
|
2017-05-07 03:13:12 +00:00
|
|
|
if (divExact(f32, 5.0, 2.0)) |_| unreachable else |err| assert(err == error.UnexpectedRemainder);
|
|
|
|
}
|
|
|
|
|
2018-02-01 03:48:40 +00:00
|
|
|
pub fn mod(comptime T: type, numerator: T, denominator: T) !T {
|
2018-01-25 06:46:12 +00:00
|
|
|
@setRuntimeSafety(false);
|
2018-05-01 00:35:54 +00:00
|
|
|
if (denominator == 0) return error.DivisionByZero;
|
|
|
|
if (denominator < 0) return error.NegativeDenominator;
|
2017-05-07 03:13:12 +00:00
|
|
|
return @mod(numerator, denominator);
|
|
|
|
}
|
|
|
|
|
|
|
|
test "math.mod" {
|
|
|
|
testMod();
|
|
|
|
comptime testMod();
|
|
|
|
}
|
2018-01-25 09:10:11 +00:00
|
|
|
fn testMod() void {
|
2018-01-09 05:07:01 +00:00
|
|
|
assert((mod(i32, -5, 3) catch unreachable) == 1);
|
|
|
|
assert((mod(i32, 5, 3) catch unreachable) == 2);
|
2017-05-07 03:13:12 +00:00
|
|
|
if (mod(i32, 10, -1)) |_| unreachable else |err| assert(err == error.NegativeDenominator);
|
|
|
|
if (mod(i32, 10, 0)) |_| unreachable else |err| assert(err == error.DivisionByZero);
|
|
|
|
|
2018-01-09 05:07:01 +00:00
|
|
|
assert((mod(f32, -5, 3) catch unreachable) == 1);
|
|
|
|
assert((mod(f32, 5, 3) catch unreachable) == 2);
|
2017-05-07 03:13:12 +00:00
|
|
|
if (mod(f32, 10, -1)) |_| unreachable else |err| assert(err == error.NegativeDenominator);
|
|
|
|
if (mod(f32, 10, 0)) |_| unreachable else |err| assert(err == error.DivisionByZero);
|
|
|
|
}
|
|
|
|
|
2018-02-01 03:48:40 +00:00
|
|
|
pub fn rem(comptime T: type, numerator: T, denominator: T) !T {
|
2018-01-25 06:46:12 +00:00
|
|
|
@setRuntimeSafety(false);
|
2018-05-01 00:35:54 +00:00
|
|
|
if (denominator == 0) return error.DivisionByZero;
|
|
|
|
if (denominator < 0) return error.NegativeDenominator;
|
2017-05-07 03:13:12 +00:00
|
|
|
return @rem(numerator, denominator);
|
|
|
|
}
|
|
|
|
|
|
|
|
test "math.rem" {
|
|
|
|
testRem();
|
|
|
|
comptime testRem();
|
|
|
|
}
|
2018-01-25 09:10:11 +00:00
|
|
|
fn testRem() void {
|
2018-01-09 05:07:01 +00:00
|
|
|
assert((rem(i32, -5, 3) catch unreachable) == -2);
|
|
|
|
assert((rem(i32, 5, 3) catch unreachable) == 2);
|
2017-05-07 03:13:12 +00:00
|
|
|
if (rem(i32, 10, -1)) |_| unreachable else |err| assert(err == error.NegativeDenominator);
|
|
|
|
if (rem(i32, 10, 0)) |_| unreachable else |err| assert(err == error.DivisionByZero);
|
|
|
|
|
2018-01-09 05:07:01 +00:00
|
|
|
assert((rem(f32, -5, 3) catch unreachable) == -2);
|
|
|
|
assert((rem(f32, 5, 3) catch unreachable) == 2);
|
2017-05-07 03:13:12 +00:00
|
|
|
if (rem(f32, 10, -1)) |_| unreachable else |err| assert(err == error.NegativeDenominator);
|
|
|
|
if (rem(f32, 10, 0)) |_| unreachable else |err| assert(err == error.DivisionByZero);
|
|
|
|
}
|
|
|
|
|
2017-05-31 22:23:56 +00:00
|
|
|
/// Returns the absolute value of the integer parameter.
|
|
|
|
/// Result is an unsigned integer.
|
2018-01-25 09:10:11 +00:00
|
|
|
pub fn absCast(x: var) @IntType(false, @typeOf(x).bit_count) {
|
2017-05-31 22:23:56 +00:00
|
|
|
const uint = @IntType(false, @typeOf(x).bit_count);
|
2018-06-17 06:57:07 +00:00
|
|
|
if (x >= 0) return @intCast(uint, x);
|
2017-05-31 22:23:56 +00:00
|
|
|
|
2018-06-17 06:57:07 +00:00
|
|
|
return @intCast(uint, -(x + 1)) + 1;
|
2017-05-31 22:23:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
test "math.absCast" {
|
|
|
|
assert(absCast(i32(-999)) == 999);
|
|
|
|
assert(@typeOf(absCast(i32(-999))) == u32);
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns the negation of the integer parameter.
|
|
|
|
/// Result is a signed integer.
|
2018-02-01 03:48:40 +00:00
|
|
|
pub fn negateCast(x: var) !@IntType(true, @typeOf(x).bit_count) {
|
2018-05-01 00:35:54 +00:00
|
|
|
if (@typeOf(x).is_signed) return negate(x);
|
2017-05-31 22:23:56 +00:00
|
|
|
|
|
|
|
const int = @IntType(true, @typeOf(x).bit_count);
|
2018-05-01 00:35:54 +00:00
|
|
|
if (x > -@minValue(int)) return error.Overflow;
|
2017-05-31 22:23:56 +00:00
|
|
|
|
2018-05-01 00:35:54 +00:00
|
|
|
if (x == -@minValue(int)) return @minValue(int);
|
2017-05-31 22:23:56 +00:00
|
|
|
|
2018-06-17 06:57:07 +00:00
|
|
|
return -@intCast(int, x);
|
2017-05-31 22:23:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
test "math.negateCast" {
|
2018-01-09 05:07:01 +00:00
|
|
|
assert((negateCast(u32(999)) catch unreachable) == -999);
|
|
|
|
assert(@typeOf(negateCast(u32(999)) catch unreachable) == i32);
|
2017-05-31 22:23:56 +00:00
|
|
|
|
2018-01-09 05:07:01 +00:00
|
|
|
assert((negateCast(u32(-@minValue(i32))) catch unreachable) == @minValue(i32));
|
|
|
|
assert(@typeOf(negateCast(u32(-@minValue(i32))) catch unreachable) == i32);
|
2017-05-31 22:23:56 +00:00
|
|
|
|
|
|
|
if (negateCast(u32(@maxValue(i32) + 10))) |_| unreachable else |err| assert(err == error.Overflow);
|
|
|
|
}
|
bit shifting safety
* add u3, u4, u5, u6, u7 and i3, i4, i5, i6, i7
* shift operations shift amount parameter type is
integer with log2 bit width of other param
- This enforces not violating undefined behavior on
shift amount >= bit width with the type system
* clean up math.log, math.ln, math.log2, math.log10
closes #403
2017-08-19 05:32:15 +00:00
|
|
|
|
2018-05-29 00:23:55 +00:00
|
|
|
/// Cast an integer to a different integer type. If the value doesn't fit,
|
bit shifting safety
* add u3, u4, u5, u6, u7 and i3, i4, i5, i6, i7
* shift operations shift amount parameter type is
integer with log2 bit width of other param
- This enforces not violating undefined behavior on
shift amount >= bit width with the type system
* clean up math.log, math.ln, math.log2, math.log10
closes #403
2017-08-19 05:32:15 +00:00
|
|
|
/// return an error.
|
2018-03-27 19:20:07 +00:00
|
|
|
pub fn cast(comptime T: type, x: var) (error{Overflow}!T) {
|
bit shifting safety
* add u3, u4, u5, u6, u7 and i3, i4, i5, i6, i7
* shift operations shift amount parameter type is
integer with log2 bit width of other param
- This enforces not violating undefined behavior on
shift amount >= bit width with the type system
* clean up math.log, math.ln, math.log2, math.log10
closes #403
2017-08-19 05:32:15 +00:00
|
|
|
comptime assert(@typeId(T) == builtin.TypeId.Int); // must pass an integer
|
2018-03-27 19:20:07 +00:00
|
|
|
comptime assert(@typeId(@typeOf(x)) == builtin.TypeId.Int); // must pass an integer
|
|
|
|
if (@maxValue(@typeOf(x)) > @maxValue(T) and x > @maxValue(T)) {
|
|
|
|
return error.Overflow;
|
|
|
|
} else if (@minValue(@typeOf(x)) < @minValue(T) and x < @minValue(T)) {
|
bit shifting safety
* add u3, u4, u5, u6, u7 and i3, i4, i5, i6, i7
* shift operations shift amount parameter type is
integer with log2 bit width of other param
- This enforces not violating undefined behavior on
shift amount >= bit width with the type system
* clean up math.log, math.ln, math.log2, math.log10
closes #403
2017-08-19 05:32:15 +00:00
|
|
|
return error.Overflow;
|
|
|
|
} else {
|
2018-06-17 06:57:07 +00:00
|
|
|
return @intCast(T, x);
|
bit shifting safety
* add u3, u4, u5, u6, u7 and i3, i4, i5, i6, i7
* shift operations shift amount parameter type is
integer with log2 bit width of other param
- This enforces not violating undefined behavior on
shift amount >= bit width with the type system
* clean up math.log, math.ln, math.log2, math.log10
closes #403
2017-08-19 05:32:15 +00:00
|
|
|
}
|
|
|
|
}
|
2017-12-15 00:41:35 +00:00
|
|
|
|
2018-03-27 19:20:07 +00:00
|
|
|
test "math.cast" {
|
|
|
|
if (cast(u8, u32(300))) |_| @panic("fail") else |err| assert(err == error.Overflow);
|
|
|
|
if (cast(i8, i32(-200))) |_| @panic("fail") else |err| assert(err == error.Overflow);
|
|
|
|
if (cast(u8, i8(-1))) |_| @panic("fail") else |err| assert(err == error.Overflow);
|
|
|
|
if (cast(u64, i8(-1))) |_| @panic("fail") else |err| assert(err == error.Overflow);
|
|
|
|
|
|
|
|
assert((try cast(u8, u32(255))) == u8(255));
|
|
|
|
assert(@typeOf(try cast(u8, u32(255))) == u8);
|
|
|
|
}
|
|
|
|
|
2018-06-16 21:01:23 +00:00
|
|
|
pub const AlignCastError = error{UnalignedMemory};
|
|
|
|
|
2018-06-16 21:27:45 +00:00
|
|
|
/// Align cast a pointer but return an error if it's the wrong alignment
|
2018-06-16 21:01:23 +00:00
|
|
|
pub fn alignCast(comptime alignment: u29, ptr: var) AlignCastError!@typeOf(@alignCast(alignment, ptr)) {
|
|
|
|
const addr = @ptrToInt(ptr);
|
|
|
|
if (addr % alignment != 0) {
|
|
|
|
return error.UnalignedMemory;
|
|
|
|
}
|
|
|
|
return @alignCast(alignment, ptr);
|
|
|
|
}
|
|
|
|
|
2018-01-25 09:10:11 +00:00
|
|
|
pub fn floorPowerOfTwo(comptime T: type, value: T) T {
|
2017-12-15 00:41:35 +00:00
|
|
|
var x = value;
|
|
|
|
|
|
|
|
comptime var i = 1;
|
2018-05-01 00:35:54 +00:00
|
|
|
inline while (T.bit_count > i) : (i *= 2) {
|
2017-12-15 00:41:35 +00:00
|
|
|
x |= (x >> i);
|
|
|
|
}
|
|
|
|
|
|
|
|
return x - (x >> 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
test "math.floorPowerOfTwo" {
|
|
|
|
testFloorPowerOfTwo();
|
|
|
|
comptime testFloorPowerOfTwo();
|
|
|
|
}
|
|
|
|
|
2018-05-07 05:04:43 +00:00
|
|
|
pub fn log2_int(comptime T: type, x: T) Log2Int(T) {
|
|
|
|
assert(x != 0);
|
2018-06-17 06:57:07 +00:00
|
|
|
return @intCast(Log2Int(T), T.bit_count - 1 - @clz(x));
|
2018-05-07 05:04:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn log2_int_ceil(comptime T: type, x: T) Log2Int(T) {
|
|
|
|
assert(x != 0);
|
|
|
|
const log2_val = log2_int(T, x);
|
|
|
|
if (T(1) << log2_val == x)
|
|
|
|
return log2_val;
|
|
|
|
return log2_val + 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
test "std.math.log2_int_ceil" {
|
|
|
|
assert(log2_int_ceil(u32, 1) == 0);
|
|
|
|
assert(log2_int_ceil(u32, 2) == 1);
|
|
|
|
assert(log2_int_ceil(u32, 3) == 2);
|
|
|
|
assert(log2_int_ceil(u32, 4) == 2);
|
|
|
|
assert(log2_int_ceil(u32, 5) == 3);
|
|
|
|
assert(log2_int_ceil(u32, 6) == 3);
|
|
|
|
assert(log2_int_ceil(u32, 7) == 3);
|
|
|
|
assert(log2_int_ceil(u32, 8) == 3);
|
|
|
|
assert(log2_int_ceil(u32, 9) == 4);
|
|
|
|
assert(log2_int_ceil(u32, 10) == 4);
|
|
|
|
}
|
|
|
|
|
2018-01-25 09:10:11 +00:00
|
|
|
fn testFloorPowerOfTwo() void {
|
2017-12-15 00:41:35 +00:00
|
|
|
assert(floorPowerOfTwo(u32, 63) == 32);
|
|
|
|
assert(floorPowerOfTwo(u32, 64) == 64);
|
|
|
|
assert(floorPowerOfTwo(u32, 65) == 64);
|
|
|
|
assert(floorPowerOfTwo(u4, 7) == 4);
|
|
|
|
assert(floorPowerOfTwo(u4, 8) == 8);
|
|
|
|
assert(floorPowerOfTwo(u4, 9) == 8);
|
|
|
|
}
|
2018-06-17 06:57:07 +00:00
|
|
|
|
|
|
|
pub fn lossyCast(comptime T: type, value: var) T {
|
|
|
|
switch (@typeInfo(@typeOf(value))) {
|
|
|
|
builtin.TypeId.Int => return @intToFloat(T, value),
|
|
|
|
builtin.TypeId.Float => return @floatCast(T, value),
|
|
|
|
builtin.TypeId.ComptimeInt => return T(value),
|
|
|
|
builtin.TypeId.ComptimeFloat => return T(value),
|
|
|
|
else => @compileError("bad type"),
|
|
|
|
}
|
|
|
|
}
|