libm/math/
asinf.rs

1/* origin: FreeBSD /usr/src/lib/msun/src/e_asinf.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::fabsf::fabsf;
17use super::sqrt::sqrt;
18
19const PIO2: f64 = 1.570796326794896558e+00;
20
21/* coefficients for R(x^2) */
22const P_S0: f32 = 1.6666586697e-01;
23const P_S1: f32 = -4.2743422091e-02;
24const P_S2: f32 = -8.6563630030e-03;
25const Q_S1: f32 = -7.0662963390e-01;
26
27fn r(z: f32) -> f32 {
28    let p = z * (P_S0 + z * (P_S1 + z * P_S2));
29    let q = 1. + z * Q_S1;
30    p / q
31}
32
33/// Arcsine (f32)
34///
35/// Computes the inverse sine (arc sine) of the argument `x`.
36/// Arguments to asin must be in the range -1 to 1.
37/// Returns values in radians, in the range of -pi/2 to pi/2.
38#[cfg_attr(all(test, assert_no_panic), no_panic::no_panic)]
39pub fn asinf(mut x: f32) -> f32 {
40    let x1p_120 = f64::from_bits(0x3870000000000000); // 0x1p-120 === 2 ^ (-120)
41
42    let hx = x.to_bits();
43    let ix = hx & 0x7fffffff;
44
45    if ix >= 0x3f800000 {
46        /* |x| >= 1 */
47        if ix == 0x3f800000 {
48            /* |x| == 1 */
49            return ((x as f64) * PIO2 + x1p_120) as f32; /* asin(+-1) = +-pi/2 with inexact */
50        }
51        return 0. / (x - x); /* asin(|x|>1) is NaN */
52    }
53
54    if ix < 0x3f000000 {
55        /* |x| < 0.5 */
56        /* if 0x1p-126 <= |x| < 0x1p-12, avoid raising underflow */
57        if (ix < 0x39800000) && (ix >= 0x00800000) {
58            return x;
59        }
60        return x + x * r(x * x);
61    }
62
63    /* 1 > |x| >= 0.5 */
64    let z = (1. - fabsf(x)) * 0.5;
65    let s = sqrt(z as f64);
66    x = (PIO2 - 2. * (s + s * (r(z) as f64))) as f32;
67    if (hx >> 31) != 0 { -x } else { x }
68}