2017-05-17 17:26:35 +01:00
|
|
|
const builtin = @import("builtin");
|
2017-12-24 03:08:53 +00:00
|
|
|
const std = @import("../index.zig");
|
2017-06-16 09:26:10 +01:00
|
|
|
const TypeId = builtin.TypeId;
|
2017-12-24 03:08:53 +00:00
|
|
|
const assert = std.debug.assert;
|
2017-06-16 09:26:10 +01:00
|
|
|
|
2017-08-20 10:43:17 +01:00
|
|
|
pub const e = 2.71828182845904523536028747135266249775724709369995;
|
|
|
|
pub const pi = 3.14159265358979323846264338327950288419716939937510;
|
2017-06-16 09:26:10 +01: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;
|
|
|
|
|
|
|
|
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 12:01:04 +01:00
|
|
|
pub const snan = @import("nan.zig").snan;
|
2017-06-16 09:26:10 +01: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 09:26:10 +01:00
|
|
|
assert(@typeId(T) == TypeId.Float);
|
2017-12-22 05:50:30 +00:00
|
|
|
return fabs(x - y) < epsilon;
|
2017-06-16 09:26:10 +01:00
|
|
|
}
|
2017-02-09 07:50:03 +00:00
|
|
|
|
2017-06-16 09:26:10 +01: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 09:26:10 +01:00
|
|
|
const T = @typeOf(value);
|
|
|
|
switch (T) {
|
|
|
|
f32 => {
|
|
|
|
var x: f32 = undefined;
|
|
|
|
const p = @ptrCast(&volatile f32, &x);
|
|
|
|
*p = x;
|
|
|
|
},
|
|
|
|
f64 => {
|
|
|
|
var x: f64 = undefined;
|
|
|
|
const p = @ptrCast(&volatile f64, &x);
|
|
|
|
*p = x;
|
|
|
|
},
|
|
|
|
else => {
|
|
|
|
@compileError("forceEval not implemented for " ++ @typeName(T));
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-25 09:10:11 +00:00
|
|
|
pub fn raiseInvalid() void {
|
2017-06-16 09:26:10 +01:00
|
|
|
// Raise INVALID fpu exception
|
|
|
|
}
|
|
|
|
|
2018-01-25 09:10:11 +00:00
|
|
|
pub fn raiseUnderflow() void {
|
2017-06-16 09:26:10 +01:00
|
|
|
// Raise UNDERFLOW fpu exception
|
|
|
|
}
|
|
|
|
|
2018-01-25 09:10:11 +00:00
|
|
|
pub fn raiseOverflow() void {
|
2017-06-16 09:26:10 +01:00
|
|
|
// Raise OVERFLOW fpu exception
|
|
|
|
}
|
|
|
|
|
2018-01-25 09:10:11 +00:00
|
|
|
pub fn raiseInexact() void {
|
2017-06-16 09:26:10 +01:00
|
|
|
// Raise INEXACT fpu exception
|
|
|
|
}
|
|
|
|
|
2018-01-25 09:10:11 +00:00
|
|
|
pub fn raiseDivByZero() void {
|
2017-06-16 09:26:10 +01:00
|
|
|
// Raise INEXACT fpu exception
|
|
|
|
}
|
|
|
|
|
|
|
|
pub const isNan = @import("isnan.zig").isNan;
|
2017-06-20 12:01:04 +01:00
|
|
|
pub const isSignalNan = @import("isnan.zig").isSignalNan;
|
2017-06-16 09:26:10 +01: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 20:57:41 +01:00
|
|
|
pub const trunc = @import("trunc.zig").trunc;
|
2017-06-16 09:26:10 +01:00
|
|
|
pub const round = @import("round.zig").round;
|
2017-06-08 03:56:57 +01:00
|
|
|
pub const frexp = @import("frexp.zig").frexp;
|
2017-06-16 09:26:10 +01: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;
|
|
|
|
|
|
|
|
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");
|
|
|
|
}
|
|
|
|
|
2017-06-08 03:56:57 +01:00
|
|
|
|
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 04:13:12 +01: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-09 00:05:41 +01:00
|
|
|
}
|
2016-07-25 02:35:50 +01:00
|
|
|
|
2017-05-07 04:13:12 +01: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 02:35:50 +01: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 02:35:50 +01:00
|
|
|
}
|
2017-05-07 04:13:12 +01: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 02:35:50 +01: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 02:35:50 +01:00
|
|
|
}
|
2017-05-07 04:13:12 +01: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 02:35:50 +01: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 02:35:50 +01:00
|
|
|
}
|
2017-05-07 04:13:12 +01:00
|
|
|
|
2018-02-01 03:48:40 +00:00
|
|
|
pub fn negate(x: var) !@typeOf(x) {
|
2017-05-31 23:23:56 +01: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 13:58:39 +01: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 06:32:15 +01:00
|
|
|
}
|
|
|
|
|
2017-10-01 02:09:58 +01: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 02:09:58 +01:00
|
|
|
const abs_shift_amt = absCast(shift_amt);
|
|
|
|
const casted_shift_amt = if (abs_shift_amt >= T.bit_count) return 0 else Log2Int(T)(abs_shift_amt);
|
|
|
|
|
|
|
|
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 02:09:58 +01:00
|
|
|
const abs_shift_amt = absCast(shift_amt);
|
|
|
|
const casted_shift_amt = if (abs_shift_amt >= T.bit_count) return 0 else Log2Int(T)(abs_shift_amt);
|
|
|
|
|
|
|
|
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" {
|
|
|
|
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);
|
|
|
|
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" {
|
|
|
|
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);
|
|
|
|
assert(rotl(u8, 0b00000001, isize(-1)) == 0b10000000);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-01-25 09:10:11 +00:00
|
|
|
pub fn Log2Int(comptime T: type) type {
|
2017-12-22 05:50:30 +00:00
|
|
|
return @IntType(false, log2(T.bit_count));
|
2016-09-09 13:58:39 +01:00
|
|
|
}
|
2017-02-07 22:19:51 +00:00
|
|
|
|
2017-05-07 04:13:12 +01: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 04:13:12 +01: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 17:26:35 +01:00
|
|
|
comptime assert(@typeId(T) == builtin.TypeId.Int); // must pass an integer to absInt
|
2017-05-07 04:13:12 +01:00
|
|
|
comptime assert(T.is_signed); // must pass a signed integer to absInt
|
|
|
|
if (x == @minValue(@typeOf(x)))
|
|
|
|
return error.Overflow;
|
|
|
|
{
|
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 04:13:12 +01: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 04:13:12 +01:00
|
|
|
}
|
|
|
|
|
2017-06-08 03:56:57 +01:00
|
|
|
pub const absFloat = @import("fabs.zig").fabs;
|
2017-05-07 04:13:12 +01: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);
|
2017-05-07 04:13:12 +01:00
|
|
|
if (denominator == 0)
|
|
|
|
return error.DivisionByZero;
|
2017-05-17 17:26:35 +01:00
|
|
|
if (@typeId(T) == builtin.TypeId.Int and T.is_signed and numerator == @minValue(T) and denominator == -1)
|
2017-05-07 04:13:12 +01:00
|
|
|
return error.Overflow;
|
|
|
|
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 04:13:12 +01: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 04:13:12 +01: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);
|
2017-05-07 04:13:12 +01:00
|
|
|
if (denominator == 0)
|
|
|
|
return error.DivisionByZero;
|
2017-05-17 17:26:35 +01:00
|
|
|
if (@typeId(T) == builtin.TypeId.Int and T.is_signed and numerator == @minValue(T) and denominator == -1)
|
2017-05-07 04:13:12 +01:00
|
|
|
return error.Overflow;
|
|
|
|
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 04:13:12 +01: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 04:13:12 +01: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);
|
2017-05-07 04:13:12 +01:00
|
|
|
if (denominator == 0)
|
|
|
|
return error.DivisionByZero;
|
2017-05-17 17:26:35 +01:00
|
|
|
if (@typeId(T) == builtin.TypeId.Int and T.is_signed and numerator == @minValue(T) and denominator == -1)
|
2017-05-07 04:13:12 +01:00
|
|
|
return error.Overflow;
|
|
|
|
const result = @divTrunc(numerator, denominator);
|
|
|
|
if (result * denominator != numerator)
|
|
|
|
return error.UnexpectedRemainder;
|
|
|
|
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 04:13:12 +01: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 04:13:12 +01: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);
|
2017-05-07 04:13:12 +01:00
|
|
|
if (denominator == 0)
|
|
|
|
return error.DivisionByZero;
|
|
|
|
if (denominator < 0)
|
|
|
|
return error.NegativeDenominator;
|
|
|
|
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 04:13:12 +01: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 04:13:12 +01: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);
|
2017-05-07 04:13:12 +01:00
|
|
|
if (denominator == 0)
|
|
|
|
return error.DivisionByZero;
|
|
|
|
if (denominator < 0)
|
|
|
|
return error.NegativeDenominator;
|
|
|
|
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 04:13:12 +01: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 04:13:12 +01: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 23:23:56 +01: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 23:23:56 +01:00
|
|
|
const uint = @IntType(false, @typeOf(x).bit_count);
|
|
|
|
if (x >= 0)
|
|
|
|
return uint(x);
|
|
|
|
|
|
|
|
return uint(-(x + 1)) + 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
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) {
|
2017-05-31 23:23:56 +01:00
|
|
|
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 == -@minValue(int))
|
|
|
|
return @minValue(int);
|
|
|
|
|
|
|
|
return -int(x);
|
|
|
|
}
|
|
|
|
|
|
|
|
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 23:23:56 +01: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 23:23:56 +01: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 06:32:15 +01:00
|
|
|
|
|
|
|
/// Cast an integer to a different integer type. If the value doesn't fit,
|
|
|
|
/// return an error.
|
2018-02-01 03:48:40 +00:00
|
|
|
pub fn cast(comptime T: type, x: var) !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 06:32:15 +01:00
|
|
|
comptime assert(@typeId(T) == builtin.TypeId.Int); // must pass an integer
|
|
|
|
if (x > @maxValue(T)) {
|
|
|
|
return error.Overflow;
|
|
|
|
} else {
|
|
|
|
return T(x);
|
|
|
|
}
|
|
|
|
}
|
2017-12-15 00:41:35 +00:00
|
|
|
|
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;
|
|
|
|
inline while(T.bit_count > i) : (i *= 2) {
|
|
|
|
x |= (x >> i);
|
|
|
|
}
|
|
|
|
|
|
|
|
return x - (x >> 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
test "math.floorPowerOfTwo" {
|
|
|
|
testFloorPowerOfTwo();
|
|
|
|
comptime testFloorPowerOfTwo();
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
}
|