/rust/registry/src/index.crates.io-1949cf8c6b5b557f/libm-0.2.15/src/math/acosf.rs
Line | Count | Source |
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 | | |
16 | | use super::sqrt::sqrtf; |
17 | | |
18 | | const PIO2_HI: f32 = 1.5707962513e+00; /* 0x3fc90fda */ |
19 | | const PIO2_LO: f32 = 7.5497894159e-08; /* 0x33a22168 */ |
20 | | const P_S0: f32 = 1.6666586697e-01; |
21 | | const P_S1: f32 = -4.2743422091e-02; |
22 | | const P_S2: f32 = -8.6563630030e-03; |
23 | | const Q_S1: f32 = -7.0662963390e-01; |
24 | | |
25 | 0 | fn r(z: f32) -> f32 { |
26 | 0 | let p = z * (P_S0 + z * (P_S1 + z * P_S2)); |
27 | 0 | let q = 1. + z * Q_S1; |
28 | 0 | p / q |
29 | 0 | } |
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)] |
37 | 0 | pub fn acosf(x: f32) -> f32 { |
38 | 0 | 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 | 0 | let mut hx = x.to_bits(); |
45 | 0 | let ix = hx & 0x7fffffff; |
46 | | /* |x| >= 1 or nan */ |
47 | 0 | if ix >= 0x3f800000 { |
48 | 0 | if ix == 0x3f800000 { |
49 | 0 | if (hx >> 31) != 0 { |
50 | 0 | return 2. * PIO2_HI + x1p_120; |
51 | 0 | } |
52 | 0 | return 0.; |
53 | 0 | } |
54 | 0 | return 0. / (x - x); |
55 | 0 | } |
56 | | /* |x| < 0.5 */ |
57 | 0 | if ix < 0x3f000000 { |
58 | 0 | if ix <= 0x32800000 { |
59 | | /* |x| < 2**-26 */ |
60 | 0 | return PIO2_HI + x1p_120; |
61 | 0 | } |
62 | 0 | return PIO2_HI - (x - (PIO2_LO - x * r(x * x))); |
63 | 0 | } |
64 | | /* x < -0.5 */ |
65 | 0 | if (hx >> 31) != 0 { |
66 | 0 | z = (1. + x) * 0.5; |
67 | 0 | s = sqrtf(z); |
68 | 0 | w = r(z) * s - PIO2_LO; |
69 | 0 | return 2. * (PIO2_HI - (s + w)); |
70 | 0 | } |
71 | | /* x > 0.5 */ |
72 | 0 | z = (1. - x) * 0.5; |
73 | 0 | s = sqrtf(z); |
74 | 0 | hx = s.to_bits(); |
75 | 0 | let df = f32::from_bits(hx & 0xfffff000); |
76 | 0 | let c = (z - df * df) / (s + df); |
77 | 0 | w = r(z) * s + c; |
78 | 0 | 2. * (df + w) |
79 | 0 | } |