xref: /freebsd/lib/msun/src/s_cospif.c (revision 884ba43116d4456d5900d3c8824153c604f132b8)
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_cospi.c for implementation details.
29  */
30 #define	INLINE_KERNEL_SINDF
31 #define	INLINE_KERNEL_COSDF
32 
33 #include "math.h"
34 #include "math_private.h"
35 #include "k_cosf.c"
36 #include "k_sinf.c"
37 
38 #define	__kernel_cospif(x)	(__kernel_cosdf(M_PI * (x)))
39 #define	__kernel_sinpif(x)	(__kernel_sindf(M_PI * (x)))
40 
41 volatile static const float vzero = 0;
42 
43 float
44 cospif(float x)
45 {
46 	float ax, c;
47 	uint32_t ix, j0;
48 
49 	GET_FLOAT_WORD(ix, x);
50 	ix = ix & 0x7fffffff;
51 	SET_FLOAT_WORD(ax, ix);
52 
53 	if (ix < 0x3f800000) {			/* |x| < 1 */
54 		if (ix < 0x3e800000) {		/* |x| < 0.25 */
55 			if (ix < 0x38800000) {	/* |x| < 0x1p-14 */
56 				/* Raise inexact iff != 0. */
57 				if ((int)ax == 0)
58 					return (1);
59 			}
60 			return (__kernel_cospif(ax));
61 		}
62 
63 		if (ix < 0x3f000000)		/* |x| < 0.5 */
64 			c = __kernel_sinpif(0.5F - ax);
65 		else if (ix < 0x3f400000) {	/* |x| < 0.75 */
66 			if (ix == 0x3f000000)
67 				return (0);
68 			c = -__kernel_sinpif(ax - 0.5F);
69 		} else
70 			c = -__kernel_cospif(1 - ax);
71 		return (c);
72 	}
73 
74 	if (ix < 0x4b000000) {			/* 1 <= |x| < 0x1p23 */
75 		/* Determine integer part of ax. */
76 		j0 = ((ix >> 23) & 0xff) - 0x7f;
77 		ix &= ~(0x007fffff >> j0);
78 		SET_FLOAT_WORD(x, ix);
79 
80 		ax -= x;
81 		GET_FLOAT_WORD(ix, ax);
82 
83 		if (ix < 0x3f000000) {		/* |x| < 0.5 */
84 			if (ix < 0x3e800000)	/* |x| < 0.25 */
85 				c = ix == 0 ? 1 : __kernel_cospif(ax);
86 			else
87 				c = __kernel_sinpif(0.5F - ax);
88 		} else {
89 			if (ix < 0x3f400000) {	/* |x| < 0.75 */
90 				if (ix == 0x3f000000)
91 					return (0);
92 				c = -__kernel_sinpif(ax - 0.5F);
93 			} else
94 				c = -__kernel_cospif(1 - ax);
95 		}
96 
97 		j0 = (uint32_t)x;
98 		return (j0 & 1 ? -c : c);
99 	}
100 
101 	/* x = +-inf or nan. */
102 	if (ix >= 0x7f800000)
103 		return (vzero / vzero);
104 
105 	/*
106 	 * |x| >= 0x1p23 is always an even integer, so return 1.
107 	 */
108 	return (1);
109 }
110