aboutsummaryrefslogtreecommitdiff
path: root/lib/std/math/scalbn.zig
blob: cf8ff9003dd5ec480ba4550c2f1902df9af8c06c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
// SPDX-License-Identifier: MIT
// Copyright (c) 2015-2021 Zig Contributors
// This file is part of [zig](https://ziglang.org/), which is MIT licensed.
// The MIT license requires this copyright notice to be included in all copies
// and substantial portions of the software.
// Ported from musl, which is licensed under the MIT license:
// https://git.musl-libc.org/cgit/musl/tree/COPYRIGHT
//
// https://git.musl-libc.org/cgit/musl/tree/src/math/scalbnf.c
// https://git.musl-libc.org/cgit/musl/tree/src/math/scalbn.c

const std = @import("../std.zig");
const math = std.math;
const expect = std.testing.expect;

/// Returns x * 2^n.
pub fn scalbn(x: anytype, 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 = @intCast(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 = @intCast(u64, n +% 0x3FF) << 52;
    return y * @bitCast(f64, u);
}

test "math.scalbn" {
    expect(scalbn(@as(f32, 1.5), 4) == scalbn32(1.5, 4));
    expect(scalbn(@as(f64, 1.5), 4) == scalbn64(1.5, 4));
}

test "math.scalbn32" {
    expect(scalbn32(1.5, 4) == 24.0);
}

test "math.scalbn64" {
    expect(scalbn64(1.5, 4) == 24.0);
}