1/* origin: FreeBSD /usr/src/lib/msun/src/e_acosf.c */
2/*
3 * Conversion to float by Ian Lance Taylor, Cygnus Support, ian@cygnus.com.
4 */
5/*
6 * ====================================================
7 * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
8 *
9 * Developed at SunPro, a Sun Microsystems, Inc. business.
10 * Permission to use, copy, modify, and distribute this
11 * software is freely granted, provided that this notice
12 * is preserved.
13 * ====================================================
14 */
15
16use super::sqrtf::sqrtf;
17
18const PIO2_HI: f32 = 1.5707962513e+00; /* 0x3fc90fda */
19const PIO2_LO: f32 = 7.5497894159e-08; /* 0x33a22168 */
20const P_S0: f32 = 1.6666586697e-01;
21const P_S1: f32 = -4.2743422091e-02;
22const P_S2: f32 = -8.6563630030e-03;
23const Q_S1: f32 = -7.0662963390e-01;
24
25fn r(z: f32) -> f32 {
26 let p: f32 = z * (P_S0 + z * (P_S1 + z * P_S2));
27 let q: f32 = 1. + z * Q_S1;
28 p / q
29}
30
31/// Arccosine (f32)
32///
33/// Computes the inverse cosine (arc cosine) of the input value.
34/// Arguments must be in the range -1 to 1.
35/// Returns values in radians, in the range of 0 to pi.
36#[cfg_attr(all(test, assert_no_panic), no_panic::no_panic)]
37pub fn acosf(x: f32) -> f32 {
38 let x1p_120 = f32::from_bits(0x03800000); // 0x1p-120 === 2 ^ (-120)
39
40 let z: f32;
41 let w: f32;
42 let s: f32;
43
44 let mut hx = x.to_bits();
45 let ix = hx & 0x7fffffff;
46 /* |x| >= 1 or nan */
47 if ix >= 0x3f800000 {
48 if ix == 0x3f800000 {
49 if (hx >> 31) != 0 {
50 return 2. * PIO2_HI + x1p_120;
51 }
52 return 0.;
53 }
54 return 0. / (x - x);
55 }
56 /* |x| < 0.5 */
57 if ix < 0x3f000000 {
58 if ix <= 0x32800000 {
59 /* |x| < 2**-26 */
60 return PIO2_HI + x1p_120;
61 }
62 return PIO2_HI - (x - (PIO2_LO - x * r(x * x)));
63 }
64 /* x < -0.5 */
65 if (hx >> 31) != 0 {
66 z = (1. + x) * 0.5;
67 s = sqrtf(z);
68 w = r(z) * s - PIO2_LO;
69 return 2. * (PIO2_HI - (s + w));
70 }
71 /* x > 0.5 */
72 z = (1. - x) * 0.5;
73 s = sqrtf(z);
74 hx = s.to_bits();
75 let df = f32::from_bits(hx & 0xfffff000);
76 let c = (z - df * df) / (s + df);
77 w = r(z) * s + c;
78 2. * (df + w)
79}
80