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
|
|
|
|
2018-01-25 06:46:12 +00:00
|
|
|
// Avoid dragging in the runtime safety mechanisms into this .o file,
|
2017-09-30 23:58:41 +01:00
|
|
|
// unless we're trying to test this file.
|
2018-01-25 09:10:11 +00:00
|
|
|
pub fn panic(msg: []const u8, error_return_trace: ?&builtin.StackTrace) noreturn {
|
2017-09-30 23:58:41 +01:00
|
|
|
if (builtin.is_test) {
|
2018-01-23 03:24:07 +00:00
|
|
|
@setCold(true);
|
2017-09-30 23:58:41 +01:00
|
|
|
@import("std").debug.panic("{}", msg);
|
|
|
|
} else {
|
|
|
|
unreachable;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-11 03:24:01 +01:00
|
|
|
export fn memset(dest: ?&u8, c: u8, n: usize) ?&u8 {
|
2018-01-25 06:46:12 +00:00
|
|
|
@setRuntimeSafety(false);
|
2016-09-28 07:33:32 +01:00
|
|
|
|
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;
|
2018-04-11 03:24:01 +01:00
|
|
|
|
|
|
|
return dest;
|
2016-01-06 13:40:25 +00:00
|
|
|
}
|
|
|
|
|
2018-04-11 03:24:01 +01:00
|
|
|
export fn memcpy(noalias dest: ?&u8, noalias src: ?&const u8, n: usize) ?&u8 {
|
2018-01-25 06:46:12 +00:00
|
|
|
@setRuntimeSafety(false);
|
2016-09-28 07:33:32 +01:00
|
|
|
|
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];
|
2018-04-11 03:24:01 +01:00
|
|
|
|
|
|
|
return dest;
|
|
|
|
}
|
|
|
|
|
|
|
|
export fn memmove(dest: ?&u8, src: ?&const u8, n: usize) ?&u8 {
|
|
|
|
@setRuntimeSafety(false);
|
|
|
|
|
|
|
|
if (@ptrToInt(dest) < @ptrToInt(src)) {
|
|
|
|
var index: usize = 0;
|
|
|
|
while (index != n) : (index += 1) {
|
|
|
|
(??dest)[index] = (??src)[index];
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
var index = n;
|
|
|
|
while (index != 0) {
|
|
|
|
index -= 1;
|
|
|
|
(??dest)[index] = (??src)[index];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return dest;
|
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 {
|
2018-04-16 01:26:10 +01:00
|
|
|
if (builtin.mode != builtin.Mode.ReleaseFast and
|
|
|
|
builtin.mode != builtin.Mode.ReleaseSmall and
|
|
|
|
builtin.os != builtin.Os.windows) {
|
2017-12-19 07:39:43 +00:00
|
|
|
@export("__stack_chk_fail", __stack_chk_fail, builtin.GlobalLinkage.Strong);
|
|
|
|
}
|
2018-04-14 07:12:19 +01:00
|
|
|
if (builtin.os == builtin.Os.linux and builtin.arch == builtin.Arch.x86_64) {
|
|
|
|
@export("clone", clone, builtin.GlobalLinkage.Strong);
|
|
|
|
}
|
2017-12-19 07:39:43 +00:00
|
|
|
}
|
2018-01-25 09:10:11 +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
|
|
|
|
2018-04-14 07:12:19 +01:00
|
|
|
// TODO we should be able to put this directly in std/linux/x86_64.zig but
|
|
|
|
// it causes a segfault in release mode. this is a workaround of calling it
|
|
|
|
// across .o file boundaries. fix comptime @ptrCast of nakedcc functions.
|
|
|
|
nakedcc fn clone() void {
|
|
|
|
asm volatile (
|
|
|
|
\\ xor %%eax,%%eax
|
|
|
|
\\ mov $56,%%al
|
|
|
|
\\ mov %%rdi,%%r11
|
|
|
|
\\ mov %%rdx,%%rdi
|
|
|
|
\\ mov %%r8,%%rdx
|
|
|
|
\\ mov %%r9,%%r8
|
|
|
|
\\ mov 8(%%rsp),%%r10
|
|
|
|
\\ mov %%r11,%%r9
|
|
|
|
\\ and $-16,%%rsi
|
|
|
|
\\ sub $8,%%rsi
|
|
|
|
\\ mov %%rcx,(%%rsi)
|
|
|
|
\\ syscall
|
|
|
|
\\ test %%eax,%%eax
|
|
|
|
\\ jnz 1f
|
|
|
|
\\ xor %%ebp,%%ebp
|
|
|
|
\\ pop %%rdi
|
|
|
|
\\ call *%%r9
|
|
|
|
\\ mov %%eax,%%edi
|
|
|
|
\\ xor %%eax,%%eax
|
|
|
|
\\ mov $60,%%al
|
|
|
|
\\ syscall
|
|
|
|
\\ hlt
|
|
|
|
\\1: ret
|
|
|
|
\\
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2017-08-27 10:15:24 +01:00
|
|
|
const math = @import("../math/index.zig");
|
|
|
|
|
2018-01-25 09:10:11 +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
|
2018-01-25 09:10:11 +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
|
|
|
|
2018-01-25 09:10:11 +00:00
|
|
|
fn generic_fmod(comptime T: type, x: T, y: T) T {
|
2018-01-25 06:46:12 +00:00
|
|
|
@setRuntimeSafety(false);
|
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
|
|
|
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
|
|
|
}
|
|
|
|
|
2018-01-25 09:10:11 +00:00
|
|
|
fn isNan(comptime T: type, bits: T) bool {
|
2017-05-07 04:13:12 +01:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
2018-04-15 18:21:52 +01:00
|
|
|
|
|
|
|
// NOTE: The original code is full of implicit signed -> unsigned assumptions and u32 wraparound
|
|
|
|
// behaviour. Most intermediate i32 values are changed to u32 where appropriate but there are
|
|
|
|
// potentially some edge cases remaining that are not handled in the same way.
|
|
|
|
export fn sqrt(x: f64) f64 {
|
|
|
|
const tiny: f64 = 1.0e-300;
|
|
|
|
const sign: u32 = 0x80000000;
|
|
|
|
const u = @bitCast(u64, x);
|
|
|
|
|
|
|
|
var ix0 = u32(u >> 32);
|
|
|
|
var ix1 = u32(u & 0xFFFFFFFF);
|
|
|
|
|
|
|
|
// sqrt(nan) = nan, sqrt(+inf) = +inf, sqrt(-inf) = nan
|
|
|
|
if (ix0 & 0x7FF00000 == 0x7FF00000) {
|
|
|
|
return x * x + x;
|
|
|
|
}
|
|
|
|
|
|
|
|
// sqrt(+-0) = +-0
|
|
|
|
if (x == 0.0) {
|
|
|
|
return x;
|
|
|
|
}
|
|
|
|
// sqrt(-ve) = snan
|
|
|
|
if (ix0 & sign != 0) {
|
|
|
|
return math.snan(f64);
|
|
|
|
}
|
|
|
|
|
|
|
|
// normalize x
|
|
|
|
var m = i32(ix0 >> 20);
|
|
|
|
if (m == 0) {
|
|
|
|
// subnormal
|
|
|
|
while (ix0 == 0) {
|
|
|
|
m -= 21;
|
|
|
|
ix0 |= ix1 >> 11;
|
|
|
|
ix1 <<= 21;
|
|
|
|
}
|
|
|
|
|
|
|
|
// subnormal
|
|
|
|
var i: u32 = 0;
|
|
|
|
while (ix0 & 0x00100000 == 0) : (i += 1) {
|
|
|
|
ix0 <<= 1;
|
|
|
|
}
|
|
|
|
m -= i32(i) - 1;
|
|
|
|
ix0 |= ix1 >> u5(32 - i);
|
|
|
|
ix1 <<= u5(i);
|
|
|
|
}
|
|
|
|
|
|
|
|
// unbias exponent
|
|
|
|
m -= 1023;
|
|
|
|
ix0 = (ix0 & 0x000FFFFF) | 0x00100000;
|
|
|
|
if (m & 1 != 0) {
|
|
|
|
ix0 += ix0 + (ix1 >> 31);
|
|
|
|
ix1 = ix1 +% ix1;
|
|
|
|
}
|
|
|
|
m >>= 1;
|
|
|
|
|
|
|
|
// sqrt(x) bit by bit
|
|
|
|
ix0 += ix0 + (ix1 >> 31);
|
|
|
|
ix1 = ix1 +% ix1;
|
|
|
|
|
|
|
|
var q: u32 = 0;
|
|
|
|
var q1: u32 = 0;
|
|
|
|
var s0: u32 = 0;
|
|
|
|
var s1: u32 = 0;
|
|
|
|
var r: u32 = 0x00200000;
|
|
|
|
var t: u32 = undefined;
|
|
|
|
var t1: u32 = undefined;
|
|
|
|
|
|
|
|
while (r != 0) {
|
|
|
|
t = s0 +% r;
|
|
|
|
if (t <= ix0) {
|
|
|
|
s0 = t + r;
|
|
|
|
ix0 -= t;
|
|
|
|
q += r;
|
|
|
|
}
|
|
|
|
ix0 = ix0 +% ix0 +% (ix1 >> 31);
|
|
|
|
ix1 = ix1 +% ix1;
|
|
|
|
r >>= 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
r = sign;
|
|
|
|
while (r != 0) {
|
|
|
|
t = s1 +% r;
|
|
|
|
t = s0;
|
|
|
|
if (t < ix0 or (t == ix0 and t1 <= ix1)) {
|
|
|
|
s1 = t1 +% r;
|
|
|
|
if (t1 & sign == sign and s1 & sign == 0) {
|
|
|
|
s0 += 1;
|
|
|
|
}
|
|
|
|
ix0 -= t;
|
|
|
|
if (ix1 < t1) {
|
|
|
|
ix0 -= 1;
|
|
|
|
}
|
|
|
|
ix1 = ix1 -% t1;
|
|
|
|
q1 += r;
|
|
|
|
}
|
|
|
|
ix0 = ix0 +% ix0 +% (ix1 >> 31);
|
|
|
|
ix1 = ix1 +% ix1;
|
|
|
|
r >>= 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// rounding direction
|
|
|
|
if (ix0 | ix1 != 0) {
|
|
|
|
var z = 1.0 - tiny; // raise inexact
|
|
|
|
if (z >= 1.0) {
|
|
|
|
z = 1.0 + tiny;
|
|
|
|
if (q1 == 0xFFFFFFFF) {
|
|
|
|
q1 = 0;
|
|
|
|
q += 1;
|
|
|
|
} else if (z > 1.0) {
|
|
|
|
if (q1 == 0xFFFFFFFE) {
|
|
|
|
q += 1;
|
|
|
|
}
|
|
|
|
q1 += 2;
|
|
|
|
} else {
|
|
|
|
q1 += q1 & 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ix0 = (q >> 1) + 0x3FE00000;
|
|
|
|
ix1 = q1 >> 1;
|
|
|
|
if (q & 1 != 0) {
|
|
|
|
ix1 |= 0x80000000;
|
|
|
|
}
|
|
|
|
|
|
|
|
// NOTE: musl here appears to rely on signed twos-complement wraparound. +% has the same
|
|
|
|
// behaviour at least.
|
|
|
|
var iix0 = i32(ix0);
|
|
|
|
iix0 = iix0 +% (m << 20);
|
|
|
|
|
|
|
|
const uz = (u64(iix0) << 32) | ix1;
|
|
|
|
return @bitCast(f64, uz);
|
|
|
|
}
|
|
|
|
|
|
|
|
export fn sqrtf(x: f32) f32 {
|
|
|
|
const tiny: f32 = 1.0e-30;
|
|
|
|
const sign: i32 = @bitCast(i32, u32(0x80000000));
|
|
|
|
var ix: i32 = @bitCast(i32, x);
|
|
|
|
|
|
|
|
if ((ix & 0x7F800000) == 0x7F800000) {
|
|
|
|
return x * x + x; // sqrt(nan) = nan, sqrt(+inf) = +inf, sqrt(-inf) = snan
|
|
|
|
}
|
|
|
|
|
|
|
|
// zero
|
|
|
|
if (ix <= 0) {
|
|
|
|
if (ix & ~sign == 0) {
|
|
|
|
return x; // sqrt (+-0) = +-0
|
|
|
|
}
|
|
|
|
if (ix < 0) {
|
|
|
|
return math.snan(f32);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// normalize
|
|
|
|
var m = ix >> 23;
|
|
|
|
if (m == 0) {
|
|
|
|
// subnormal
|
|
|
|
var i: i32 = 0;
|
|
|
|
while (ix & 0x00800000 == 0) : (i += 1) {
|
|
|
|
ix <<= 1;
|
|
|
|
}
|
|
|
|
m -= i - 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
m -= 127; // unbias exponent
|
|
|
|
ix = (ix & 0x007FFFFF) | 0x00800000;
|
|
|
|
|
|
|
|
if (m & 1 != 0) { // odd m, double x to even
|
|
|
|
ix += ix;
|
|
|
|
}
|
|
|
|
|
|
|
|
m >>= 1; // m = [m / 2]
|
|
|
|
|
|
|
|
// sqrt(x) bit by bit
|
|
|
|
ix += ix;
|
|
|
|
var q: i32 = 0; // q = sqrt(x)
|
|
|
|
var s: i32 = 0;
|
|
|
|
var r: i32 = 0x01000000; // r = moving bit right -> left
|
|
|
|
|
|
|
|
while (r != 0) {
|
|
|
|
const t = s + r;
|
|
|
|
if (t <= ix) {
|
|
|
|
s = t + r;
|
|
|
|
ix -= t;
|
|
|
|
q += r;
|
|
|
|
}
|
|
|
|
ix += ix;
|
|
|
|
r >>= 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// floating add to find rounding direction
|
|
|
|
if (ix != 0) {
|
|
|
|
var z = 1.0 - tiny; // inexact
|
|
|
|
if (z >= 1.0) {
|
|
|
|
z = 1.0 + tiny;
|
|
|
|
if (z > 1.0) {
|
|
|
|
q += 2;
|
|
|
|
} else {
|
|
|
|
if (q & 1 != 0) {
|
|
|
|
q += 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ix = (q >> 1) + 0x3f000000;
|
|
|
|
ix += m << 23;
|
|
|
|
return @bitCast(f32, ix);
|
|
|
|
}
|