zig/std/math.zig
Andrew Kelley fc100d7b3b lots of miscellaneous things all in one big commit
* add `@compileLog(...)` builtin function
   - Helps debug code running at compile time
   - See #240
 * fix crash when there is an error on the start value of a slice
 * add implicit cast from int and float types to int and float
   literals if the value is known at compile time
 * make array concatenation work with slices in addition to
   arrays and c string literals
 * fix compile error message for something not having field access
 * fix crash when `@setDebugSafety()` was called from a
   function being evaluated at compile-time
 * fix compile-time evaluation of overflow math builtins.
 * avoid debug safety panic handler in builtin.o and compiler_rt.o
   since we use no debug safety in these modules anyway
 * add compiler_rt functions for division on ARM
   - Closes #254
 * move default panic handler to std.debug so users can
   call it manually
 * std.io.printf supports a width in the format specifier
2017-02-09 03:09:25 -05:00

86 lines
2.4 KiB
Zig

const assert = @import("debug.zig").assert;
pub const Cmp = enum {
Equal,
Greater,
Less,
};
pub fn min(x: var, y: var) -> @typeOf(x + y) {
if (x < y) x else y
}
pub fn max(x: var, y: var) -> @typeOf(x + y) {
if (x > y) x else y
}
error Overflow;
pub fn mulOverflow(comptime T: type, a: T, b: T) -> %T {
var answer: T = undefined;
if (@mulWithOverflow(T, a, b, &answer)) error.Overflow else answer
}
pub fn addOverflow(comptime T: type, a: T, b: T) -> %T {
var answer: T = undefined;
if (@addWithOverflow(T, a, b, &answer)) error.Overflow else answer
}
pub fn subOverflow(comptime T: type, a: T, b: T) -> %T {
var answer: T = undefined;
if (@subWithOverflow(T, a, b, &answer)) error.Overflow else answer
}
pub fn shlOverflow(comptime T: type, a: T, b: T) -> %T {
var answer: T = undefined;
if (@shlWithOverflow(T, a, b, &answer)) error.Overflow else answer
}
pub fn log(comptime base: usize, value: var) -> @typeOf(value) {
const T = @typeOf(value);
if (@isInteger(T)) {
if (base == 2) {
return T.bit_count - 1 - @clz(value);
} else {
@compileError("TODO implement log for non base 2 integers");
}
} else if (@isFloat(T)) {
@compileError("TODO implement log for floats");
} else {
@compileError("log expects integer or float, found '" ++ @typeName(T) ++ "'");
}
}
/// x must be an integer or a float
/// Note that this causes undefined behavior if
/// @typeOf(x).is_signed && x == @minValue(@typeOf(x)).
pub fn abs(x: var) -> @typeOf(x) {
const T = @typeOf(x);
if (@isInteger(T)) {
return if (x < 0) -x else x;
} else if (@isFloat(T)) {
@compileError("TODO implement abs for floats");
} else {
@unreachable();
}
}
fn getReturnTypeForAbs(comptime T: type) -> type {
if (@isInteger(T)) {
return @intType(false, T.bit_count);
} else {
return T;
}
}
fn testMath() {
@setFnTest(this);
assert(%%mulOverflow(i32, 3, 4) == 12);
assert(%%addOverflow(i32, 3, 4) == 7);
assert(%%subOverflow(i32, 3, 4) == -1);
assert(%%shlOverflow(i32, 0b11, 4) == 0b110000);
comptime {
assert(%%mulOverflow(i32, 3, 4) == 12);
assert(%%addOverflow(i32, 3, 4) == 7);
assert(%%subOverflow(i32, 3, 4) == -1);
assert(%%shlOverflow(i32, 0b11, 4) == 0b110000);
}
}