master
  1// Ported from musl, which is licensed under the MIT license:
  2// https://git.musl-libc.org/cgit/musl/tree/COPYRIGHT
  3//
  4// https://git.musl-libc.org/cgit/musl/tree/src/math/acosf.c
  5// https://git.musl-libc.org/cgit/musl/tree/src/math/acos.c
  6
  7const std = @import("../std.zig");
  8const math = std.math;
  9const expect = std.testing.expect;
 10
 11/// Returns the arc-cosine of x.
 12///
 13/// Special cases:
 14///  - acos(x)   = nan if x < -1 or x > 1
 15pub fn acos(x: anytype) @TypeOf(x) {
 16    const T = @TypeOf(x);
 17    return switch (T) {
 18        f32 => acos32(x),
 19        f64 => acos64(x),
 20        else => @compileError("acos not implemented for " ++ @typeName(T)),
 21    };
 22}
 23
 24fn r32(z: f32) f32 {
 25    const pS0 = 1.6666586697e-01;
 26    const pS1 = -4.2743422091e-02;
 27    const pS2 = -8.6563630030e-03;
 28    const qS1 = -7.0662963390e-01;
 29
 30    const p = z * (pS0 + z * (pS1 + z * pS2));
 31    const q = 1.0 + z * qS1;
 32    return p / q;
 33}
 34
 35fn acos32(x: f32) f32 {
 36    const pio2_hi = 1.5707962513e+00;
 37    const pio2_lo = 7.5497894159e-08;
 38
 39    const hx: u32 = @as(u32, @bitCast(x));
 40    const ix: u32 = hx & 0x7FFFFFFF;
 41
 42    // |x| >= 1 or nan
 43    if (ix >= 0x3F800000) {
 44        if (ix == 0x3F800000) {
 45            if (hx >> 31 != 0) {
 46                return 2.0 * pio2_hi + 0x1.0p-120;
 47            } else {
 48                return 0.0;
 49            }
 50        } else {
 51            return math.nan(f32);
 52        }
 53    }
 54
 55    // |x| < 0.5
 56    if (ix < 0x3F000000) {
 57        if (ix <= 0x32800000) { // |x| < 2^(-26)
 58            return pio2_hi + 0x1.0p-120;
 59        } else {
 60            return pio2_hi - (x - (pio2_lo - x * r32(x * x)));
 61        }
 62    }
 63
 64    // x < -0.5
 65    if (hx >> 31 != 0) {
 66        const z = (1 + x) * 0.5;
 67        const s = @sqrt(z);
 68        const w = r32(z) * s - pio2_lo;
 69        return 2 * (pio2_hi - (s + w));
 70    }
 71
 72    // x > 0.5
 73    const z = (1.0 - x) * 0.5;
 74    const s = @sqrt(z);
 75    const jx = @as(u32, @bitCast(s));
 76    const df = @as(f32, @bitCast(jx & 0xFFFFF000));
 77    const c = (z - df * df) / (s + df);
 78    const w = r32(z) * s + c;
 79    return 2 * (df + w);
 80}
 81
 82fn r64(z: f64) f64 {
 83    const pS0: f64 = 1.66666666666666657415e-01;
 84    const pS1: f64 = -3.25565818622400915405e-01;
 85    const pS2: f64 = 2.01212532134862925881e-01;
 86    const pS3: f64 = -4.00555345006794114027e-02;
 87    const pS4: f64 = 7.91534994289814532176e-04;
 88    const pS5: f64 = 3.47933107596021167570e-05;
 89    const qS1: f64 = -2.40339491173441421878e+00;
 90    const qS2: f64 = 2.02094576023350569471e+00;
 91    const qS3: f64 = -6.88283971605453293030e-01;
 92    const qS4: f64 = 7.70381505559019352791e-02;
 93
 94    const p = z * (pS0 + z * (pS1 + z * (pS2 + z * (pS3 + z * (pS4 + z * pS5)))));
 95    const q = 1.0 + z * (qS1 + z * (qS2 + z * (qS3 + z * qS4)));
 96    return p / q;
 97}
 98
 99fn acos64(x: f64) f64 {
100    const pio2_hi: f64 = 1.57079632679489655800e+00;
101    const pio2_lo: f64 = 6.12323399573676603587e-17;
102
103    const ux = @as(u64, @bitCast(x));
104    const hx = @as(u32, @intCast(ux >> 32));
105    const ix = hx & 0x7FFFFFFF;
106
107    // |x| >= 1 or nan
108    if (ix >= 0x3FF00000) {
109        const lx = @as(u32, @intCast(ux & 0xFFFFFFFF));
110
111        // acos(1) = 0, acos(-1) = pi
112        if ((ix - 0x3FF00000) | lx == 0) {
113            if (hx >> 31 != 0) {
114                return 2 * pio2_hi + 0x1.0p-120;
115            } else {
116                return 0;
117            }
118        }
119
120        return math.nan(f64);
121    }
122
123    // |x| < 0.5
124    if (ix < 0x3FE00000) {
125        // |x| < 2^(-57)
126        if (ix <= 0x3C600000) {
127            return pio2_hi + 0x1.0p-120;
128        } else {
129            return pio2_hi - (x - (pio2_lo - x * r64(x * x)));
130        }
131    }
132
133    // x < -0.5
134    if (hx >> 31 != 0) {
135        const z = (1.0 + x) * 0.5;
136        const s = @sqrt(z);
137        const w = r64(z) * s - pio2_lo;
138        return 2 * (pio2_hi - (s + w));
139    }
140
141    // x > 0.5
142    const z = (1.0 - x) * 0.5;
143    const s = @sqrt(z);
144    const jx = @as(u64, @bitCast(s));
145    const df = @as(f64, @bitCast(jx & 0xFFFFFFFF00000000));
146    const c = (z - df * df) / (s + df);
147    const w = r64(z) * s + c;
148    return 2 * (df + w);
149}
150
151test acos {
152    try expect(acos(@as(f32, 0.0)) == acos32(0.0));
153    try expect(acos(@as(f64, 0.0)) == acos64(0.0));
154}
155
156test acos32 {
157    const epsilon = 0.000001;
158
159    try expect(math.approxEqAbs(f32, acos32(0.0), 1.570796, epsilon));
160    try expect(math.approxEqAbs(f32, acos32(0.2), 1.369438, epsilon));
161    try expect(math.approxEqAbs(f32, acos32(0.3434), 1.220262, epsilon));
162    try expect(math.approxEqAbs(f32, acos32(0.5), 1.047198, epsilon));
163    try expect(math.approxEqAbs(f32, acos32(0.8923), 0.468382, epsilon));
164    try expect(math.approxEqAbs(f32, acos32(-0.2), 1.772154, epsilon));
165}
166
167test acos64 {
168    const epsilon = 0.000001;
169
170    try expect(math.approxEqAbs(f64, acos64(0.0), 1.570796, epsilon));
171    try expect(math.approxEqAbs(f64, acos64(0.2), 1.369438, epsilon));
172    try expect(math.approxEqAbs(f64, acos64(0.3434), 1.220262, epsilon));
173    try expect(math.approxEqAbs(f64, acos64(0.5), 1.047198, epsilon));
174    try expect(math.approxEqAbs(f64, acos64(0.8923), 0.468382, epsilon));
175    try expect(math.approxEqAbs(f64, acos64(-0.2), 1.772154, epsilon));
176}
177
178test "acos32.special" {
179    try expect(math.isNan(acos32(-2)));
180    try expect(math.isNan(acos32(1.5)));
181}
182
183test "acos64.special" {
184    try expect(math.isNan(acos64(-2)));
185    try expect(math.isNan(acos64(1.5)));
186}