mirror of
https://github.com/ziglang/zig.git
synced 2024-12-01 17:42:31 +00:00
3671582c15
The purpose of this is: * Only one way to do things * Changing a function with void return type to return a possible error becomes a 1 character change, subtly encouraging people to use errors. See #632 Here are some imperfect sed commands for performing this update: remove arrow: ``` sed -i 's/\(\bfn\b.*\)-> /\1/g' $(find . -name "*.zig") ``` add void: ``` sed -i 's/\(\bfn\b.*\))\s*{/\1) void {/g' $(find ../ -name "*.zig") ``` Some cleanup may be necessary, but this should do the bulk of the work.
86 lines
1.8 KiB
Zig
86 lines
1.8 KiB
Zig
const std = @import("../index.zig");
|
|
const math = std.math;
|
|
const assert = std.debug.assert;
|
|
|
|
pub fn scalbn(x: var, n: i32) @typeOf(x) {
|
|
const T = @typeOf(x);
|
|
return switch (T) {
|
|
f32 => scalbn32(x, n),
|
|
f64 => scalbn64(x, n),
|
|
else => @compileError("scalbn not implemented for " ++ @typeName(T)),
|
|
};
|
|
}
|
|
|
|
fn scalbn32(x: f32, n_: i32) f32 {
|
|
var y = x;
|
|
var n = n_;
|
|
|
|
if (n > 127) {
|
|
y *= 0x1.0p127;
|
|
n -= 127;
|
|
if (n > 1023) {
|
|
y *= 0x1.0p127;
|
|
n -= 127;
|
|
if (n > 127) {
|
|
n = 127;
|
|
}
|
|
}
|
|
} else if (n < -126) {
|
|
y *= 0x1.0p-126 * 0x1.0p24;
|
|
n += 126 - 24;
|
|
if (n < -126) {
|
|
y *= 0x1.0p-126 * 0x1.0p24;
|
|
n += 126 - 24;
|
|
if (n < -126) {
|
|
n = -126;
|
|
}
|
|
}
|
|
}
|
|
|
|
const u = u32(n +% 0x7F) << 23;
|
|
return y * @bitCast(f32, u);
|
|
}
|
|
|
|
fn scalbn64(x: f64, n_: i32) f64 {
|
|
var y = x;
|
|
var n = n_;
|
|
|
|
if (n > 1023) {
|
|
y *= 0x1.0p1023;
|
|
n -= 1023;
|
|
if (n > 1023) {
|
|
y *= 0x1.0p1023;
|
|
n -= 1023;
|
|
if (n > 1023) {
|
|
n = 1023;
|
|
}
|
|
}
|
|
} else if (n < -1022) {
|
|
y *= 0x1.0p-1022 * 0x1.0p53;
|
|
n += 1022 - 53;
|
|
if (n < -1022) {
|
|
y *= 0x1.0p-1022 * 0x1.0p53;
|
|
n += 1022 - 53;
|
|
if (n < -1022) {
|
|
n = -1022;
|
|
}
|
|
}
|
|
}
|
|
|
|
const u = u64(n +% 0x3FF) << 52;
|
|
return y * @bitCast(f64, u);
|
|
}
|
|
|
|
test "math.scalbn" {
|
|
assert(scalbn(f32(1.5), 4) == scalbn32(1.5, 4));
|
|
assert(scalbn(f64(1.5), 4) == scalbn64(1.5, 4));
|
|
}
|
|
|
|
test "math.scalbn32" {
|
|
assert(scalbn32(1.5, 4) == 24.0);
|
|
}
|
|
|
|
test "math.scalbn64" {
|
|
assert(scalbn64(1.5, 4) == 24.0);
|
|
}
|