2016-01-06 13:40:25 +00:00
|
|
|
// These functions are provided when not linking against libc because LLVM
|
|
|
|
// sometimes generates code that calls them.
|
|
|
|
|
2017-05-02 00:16:48 +01:00
|
|
|
const builtin = @import("builtin");
|
2017-02-05 04:04:07 +00:00
|
|
|
|
2017-09-30 23:58:41 +01:00
|
|
|
// Avoid dragging in the debug safety mechanisms into this .o file,
|
|
|
|
// unless we're trying to test this file.
|
2018-01-12 07:12:11 +00:00
|
|
|
pub coldcc fn panic(msg: []const u8, error_return_trace: ?&builtin.StackTrace) -> noreturn {
|
2017-09-30 23:58:41 +01:00
|
|
|
if (builtin.is_test) {
|
|
|
|
@import("std").debug.panic("{}", msg);
|
|
|
|
} else {
|
|
|
|
unreachable;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Note that memset does not return `dest`, like the libc API.
|
|
|
|
// The semantics of memset is dictated by the corresponding
|
|
|
|
// LLVM intrinsics, not by the libc API.
|
2017-12-19 07:39:43 +00:00
|
|
|
export fn memset(dest: ?&u8, c: u8, n: usize) {
|
2016-09-28 07:33:32 +01:00
|
|
|
@setDebugSafety(this, false);
|
|
|
|
|
2016-07-27 04:40:11 +01:00
|
|
|
var index: usize = 0;
|
2017-05-03 23:12:07 +01:00
|
|
|
while (index != n) : (index += 1)
|
2017-05-03 00:20:23 +01:00
|
|
|
(??dest)[index] = c;
|
2016-01-06 13:40:25 +00:00
|
|
|
}
|
|
|
|
|
2017-09-30 23:58:41 +01:00
|
|
|
// Note that memcpy does not return `dest`, like the libc API.
|
|
|
|
// The semantics of memcpy is dictated by the corresponding
|
|
|
|
// LLVM intrinsics, not by the libc API.
|
2017-12-19 07:39:43 +00:00
|
|
|
export fn memcpy(noalias dest: ?&u8, noalias src: ?&const u8, n: usize) {
|
2016-09-28 07:33:32 +01:00
|
|
|
@setDebugSafety(this, false);
|
|
|
|
|
2016-07-27 04:40:11 +01:00
|
|
|
var index: usize = 0;
|
2017-05-03 23:12:07 +01:00
|
|
|
while (index != n) : (index += 1)
|
2017-05-03 00:20:23 +01:00
|
|
|
(??dest)[index] = (??src)[index];
|
2016-01-06 13:40:25 +00:00
|
|
|
}
|
2017-02-09 07:50:03 +00:00
|
|
|
|
2017-12-19 07:39:43 +00:00
|
|
|
comptime {
|
|
|
|
if (builtin.mode != builtin.Mode.ReleaseFast and builtin.os != builtin.Os.windows) {
|
|
|
|
@export("__stack_chk_fail", __stack_chk_fail, builtin.GlobalLinkage.Strong);
|
|
|
|
}
|
|
|
|
}
|
2017-12-18 14:59:57 +00:00
|
|
|
extern fn __stack_chk_fail() -> noreturn {
|
2017-03-27 02:07:07 +01:00
|
|
|
@panic("stack smashing detected");
|
2017-02-09 07:50:03 +00:00
|
|
|
}
|
2017-05-07 04:13:12 +01:00
|
|
|
|
2017-08-27 10:15:24 +01:00
|
|
|
const math = @import("../math/index.zig");
|
|
|
|
|
2017-12-22 05:50:30 +00:00
|
|
|
export fn fmodf(x: f32, y: f32) -> f32 { return generic_fmod(f32, x, y); }
|
|
|
|
export fn fmod(x: f64, y: f64) -> f64 { return generic_fmod(f64, x, y); }
|
2017-05-07 04:13:12 +01:00
|
|
|
|
2017-08-27 10:15:24 +01:00
|
|
|
// TODO add intrinsics for these (and probably the double version too)
|
|
|
|
// and have the math stuff use the intrinsic. same as @mod and @rem
|
2017-12-22 05:50:30 +00:00
|
|
|
export fn floorf(x: f32) -> f32 { return math.floor(x); }
|
|
|
|
export fn ceilf(x: f32) -> f32 { return math.ceil(x); }
|
|
|
|
export fn floor(x: f64) -> f64 { return math.floor(x); }
|
|
|
|
export fn ceil(x: f64) -> f64 { return math.ceil(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 06:32:15 +01:00
|
|
|
|
2017-05-07 04:13:12 +01:00
|
|
|
fn generic_fmod(comptime T: type, x: T, y: T) -> 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
|
|
|
@setDebugSafety(this, false);
|
|
|
|
|
2017-05-07 04:13:12 +01:00
|
|
|
const uint = @IntType(false, T.bit_count);
|
2017-08-27 10:15:24 +01:00
|
|
|
const log2uint = math.Log2Int(uint);
|
2017-05-07 04:13:12 +01:00
|
|
|
const digits = if (T == f32) 23 else 52;
|
|
|
|
const exp_bits = if (T == f32) 9 else 12;
|
|
|
|
const bits_minus_1 = T.bit_count - 1;
|
|
|
|
const mask = if (T == f32) 0xff else 0x7ff;
|
2017-08-29 21:52:31 +01:00
|
|
|
var ux = @bitCast(uint, x);
|
|
|
|
var uy = @bitCast(uint, y);
|
2017-05-07 04:13:12 +01:00
|
|
|
var ex = i32((ux >> digits) & mask);
|
|
|
|
var ey = i32((uy >> digits) & mask);
|
|
|
|
const sx = if (T == f32) u32(ux & 0x80000000) else i32(ux >> bits_minus_1);
|
|
|
|
var i: uint = undefined;
|
|
|
|
|
2017-08-09 15:09:38 +01:00
|
|
|
if (uy << 1 == 0 or isNan(uint, uy) or ex == mask)
|
2017-05-07 04:13:12 +01:00
|
|
|
return (x * y) / (x * y);
|
|
|
|
|
2017-08-09 15:09:38 +01:00
|
|
|
if (ux << 1 <= uy << 1) {
|
|
|
|
if (ux << 1 == uy << 1)
|
2017-05-07 04:13:12 +01:00
|
|
|
return 0 * x;
|
|
|
|
return x;
|
|
|
|
}
|
|
|
|
|
|
|
|
// normalize x and y
|
|
|
|
if (ex == 0) {
|
2017-08-09 15:09:38 +01:00
|
|
|
i = ux << exp_bits;
|
2017-12-22 05:50:30 +00:00
|
|
|
while (i >> bits_minus_1 == 0) : (b: {ex -= 1; break :b i <<= 1;}) {}
|
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
|
|
|
ux <<= log2uint(@bitCast(u32, -ex + 1));
|
2017-05-07 04:13:12 +01:00
|
|
|
} else {
|
|
|
|
ux &= @maxValue(uint) >> exp_bits;
|
2017-08-09 15:09:38 +01:00
|
|
|
ux |= 1 << digits;
|
2017-05-07 04:13:12 +01:00
|
|
|
}
|
|
|
|
if (ey == 0) {
|
2017-08-09 15:09:38 +01:00
|
|
|
i = uy << exp_bits;
|
2017-12-22 05:50:30 +00:00
|
|
|
while (i >> bits_minus_1 == 0) : (b: {ey -= 1; break :b i <<= 1;}) {}
|
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
|
|
|
uy <<= log2uint(@bitCast(u32, -ey + 1));
|
2017-05-07 04:13:12 +01:00
|
|
|
} else {
|
|
|
|
uy &= @maxValue(uint) >> exp_bits;
|
2017-08-09 15:09:38 +01:00
|
|
|
uy |= 1 << digits;
|
2017-05-07 04:13:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// x mod y
|
|
|
|
while (ex > ey) : (ex -= 1) {
|
|
|
|
i = ux -% uy;
|
|
|
|
if (i >> bits_minus_1 == 0) {
|
|
|
|
if (i == 0)
|
|
|
|
return 0 * x;
|
|
|
|
ux = i;
|
|
|
|
}
|
2017-08-09 15:09:38 +01:00
|
|
|
ux <<= 1;
|
2017-05-07 04:13:12 +01:00
|
|
|
}
|
|
|
|
i = ux -% uy;
|
|
|
|
if (i >> bits_minus_1 == 0) {
|
|
|
|
if (i == 0)
|
|
|
|
return 0 * x;
|
|
|
|
ux = i;
|
|
|
|
}
|
2017-12-22 05:50:30 +00:00
|
|
|
while (ux >> digits == 0) : (b: {ux <<= 1; break :b ex -= 1;}) {}
|
2017-05-07 04:13:12 +01:00
|
|
|
|
|
|
|
// scale result up
|
|
|
|
if (ex > 0) {
|
2017-08-09 15:09:38 +01:00
|
|
|
ux -%= 1 << digits;
|
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
|
|
|
ux |= uint(@bitCast(u32, ex)) << digits;
|
2017-05-07 04:13:12 +01:00
|
|
|
} else {
|
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
|
|
|
ux >>= log2uint(@bitCast(u32, -ex + 1));
|
2017-05-07 04:13:12 +01:00
|
|
|
}
|
|
|
|
if (T == f32) {
|
|
|
|
ux |= sx;
|
|
|
|
} else {
|
2017-08-09 15:09:38 +01:00
|
|
|
ux |= uint(sx) << bits_minus_1;
|
2017-05-07 04:13:12 +01:00
|
|
|
}
|
2017-08-29 21:52:31 +01:00
|
|
|
return @bitCast(T, ux);
|
2017-05-07 04:13:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
fn isNan(comptime T: type, bits: T) -> bool {
|
|
|
|
if (T == u32) {
|
|
|
|
return (bits & 0x7fffffff) > 0x7f800000;
|
|
|
|
} else if (T == u64) {
|
2017-08-09 15:09:38 +01:00
|
|
|
return (bits & (@maxValue(u64) >> 1)) > (u64(0x7ff) << 52);
|
2017-05-07 04:13:12 +01:00
|
|
|
} else {
|
|
|
|
unreachable;
|
|
|
|
}
|
|
|
|
}
|