xref: /freebsd/lib/msun/src/s_tanpif.c (revision 924226fba12cc9a228c73b956e1b7fa24c60b055)
1 /*-
2  * Copyright (c) 2017 Steven G. Kargl
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice unmodified, this list of conditions, and the following
10  *    disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  */
26 
27 /*
28  * See ../src/s_tanpi.c for implementation details.
29  */
30 
31 #define INLINE_KERNEL_TANDF
32 
33 #include "math.h"
34 #include "math_private.h"
35 #include "k_tanf.c"
36 
37 static const float
38 pi_hi =  3.14160156e+00F,	/* 0x40491000 */
39 pi_lo = -8.90890988e-06F;	/* 0xb715777a */
40 
41 static inline float
42 __kernel_tanpif(float x)
43 {
44 	float t;
45 
46 	if (x < 0.25F)
47 		t = __kernel_tandf(M_PI * x, 1);
48 	else if (x > 0.25F)
49 		t = -__kernel_tandf(M_PI * (0.5 - x), -1);
50 	else
51 		t = 1;
52 
53 	return (t);
54 }
55 
56 volatile static const float vzero = 0;
57 
58 float
59 tanpif(float x)
60 {
61 	float ax, hi, lo, t;
62 	uint32_t hx, ix, j0;
63 
64 	GET_FLOAT_WORD(hx, x);
65 	ix = hx & 0x7fffffff;
66 	SET_FLOAT_WORD(ax, ix);
67 
68 	if (ix < 0x3f800000) {			/* |x| < 1 */
69 		if (ix < 0x3f000000) {		/* |x| < 0.5 */
70 			if (ix < 0x38800000) {	/* |x| < 0x1p-14 */
71 				if (ix == 0)
72 					return (x);
73 				SET_FLOAT_WORD(hi, hx & 0xffff0000);
74 				hi *= 0x1p23F;
75 				lo = x * 0x1p23F - hi;
76 				t = (pi_lo + pi_hi) * lo + pi_lo * hi +
77 				    pi_hi * hi;
78 				return (t * 0x1p-23F);
79 			}
80 			t = __kernel_tanpif(ax);
81 		} else if (ix == 0x3f000000)
82 			return ((ax - ax) / (ax - ax));
83 		else
84 			t = - __kernel_tanpif(1 - ax);
85 		return ((hx & 0x80000000) ? -t : t);
86 	}
87 
88 	if (ix < 0x4b000000) {		/* 1 <= |x| < 0x1p23 */
89 		/* Determine integer part of ax. */
90 		j0 = ((ix >> 23) & 0xff) - 0x7f;
91 		ix &= ~(0x007fffff >> j0);
92 		SET_FLOAT_WORD(x, ix);
93 
94 		ax -= x;
95 		GET_FLOAT_WORD(ix, ax);
96 
97 		if (ix < 0x3f000000)		/* |x| < 0.5 */
98 			t = ix == 0 ? 0 : __kernel_tanpif(ax);
99 		else if (ix == 0x3f000000)
100 			return ((ax - ax) / (ax - ax));
101 		else
102 			t = - __kernel_tanpif(1 - ax);
103 		return ((hx & 0x80000000) ? -t : t);
104 	}
105 
106 	/* x = +-inf or nan. */
107 	if (ix >= 0x7f800000)
108 		return (vzero / vzero);
109 
110 	/*
111 	 * |x| >= 0x1p23 is always an integer, so return +-0.
112 	 */
113 	return (copysignf(0, x));
114 }
115