Home | History | Annotate | Line # | Download | only in src
      1 /*-
      2  * ====================================================
      3  * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
      4  *
      5  * Developed at SunPro, a Sun Microsystems, Inc. business.
      6  * Permission to use, copy, modify, and distribute this
      7  * software is freely granted, provided that this notice
      8  * is preserved.
      9  * ====================================================
     10  */
     11 
     12 /* s_sincosf.c -- float version of s_sincos.c.
     13  * Conversion to float by Ian Lance Taylor, Cygnus Support, ian (at) cygnus.com.
     14  * Optimized by Bruce D. Evans.
     15  * Merged s_sinf.c and s_cosf.c by Steven G. Kargl.
     16  */
     17 
     18 #include <sys/cdefs.h>
     19 #if 0
     20 __FBSDID("$FreeBSD: head/lib/msun/src/s_sincosf.c 319047 2017-05-28 06:13:38Z mmel $");
     21 #endif
     22 #if defined(LIBM_SCCS) && !defined(lint)
     23 __RCSID("$NetBSD: s_sincosf.c,v 1.1 2022/08/27 08:31:59 christos Exp $");
     24 #endif
     25 
     26 #include "namespace.h"
     27 #include <float.h>
     28 
     29 #include "math.h"
     30 #define INLINE_REM_PIO2F
     31 #include "math_private.h"
     32 #include "e_rem_pio2f.h"
     33 #include "k_sincosf.h"
     34 
     35 /* Small multiples of pi/2 rounded to double precision. */
     36 static const double
     37 p1pio2 = 1*M_PI_2,			/* 0x3FF921FB, 0x54442D18 */
     38 p2pio2 = 2*M_PI_2,			/* 0x400921FB, 0x54442D18 */
     39 p3pio2 = 3*M_PI_2,			/* 0x4012D97C, 0x7F3321D2 */
     40 p4pio2 = 4*M_PI_2;			/* 0x401921FB, 0x54442D18 */
     41 
     42 #ifdef __weak_alias
     43 __weak_alias(sincosf,_sincosf)
     44 #endif
     45 
     46 void
     47 sincosf(float x, float *sn, float *cs)
     48 {
     49 	float c, s;
     50 	double y;
     51 	int32_t n, hx, ix;
     52 
     53 	GET_FLOAT_WORD(hx, x);
     54 	ix = hx & 0x7fffffff;
     55 
     56 	if (ix <= 0x3f490fda) {		/* |x| ~<= pi/4 */
     57 		if (ix < 0x39800000) {	/* |x| < 2**-12 */
     58 			if ((int)x == 0) {
     59 				*sn = x;	/* x with inexact if x != 0 */
     60 				*cs = 1;
     61 				return;
     62 			}
     63 		}
     64 		__kernel_sincosdf(x, sn, cs);
     65 		return;
     66 	}
     67 
     68 	if (ix <= 0x407b53d1) {		/* |x| ~<= 5*pi/4 */
     69 		if (ix <= 0x4016cbe3) {	/* |x| ~<= 3pi/4 */
     70 			if (hx > 0) {
     71 				__kernel_sincosdf(x - p1pio2, cs, sn);
     72 				*cs = -*cs;
     73 			} else {
     74 				__kernel_sincosdf(x + p1pio2, cs, sn);
     75 				*sn = -*sn;
     76 			}
     77 		} else {
     78 			if (hx > 0)
     79 				__kernel_sincosdf(x - p2pio2, sn, cs);
     80 			else
     81 				__kernel_sincosdf(x + p2pio2, sn, cs);
     82 			*sn = -*sn;
     83 			*cs = -*cs;
     84 		}
     85 		return;
     86 	}
     87 
     88 	if (ix <= 0x40e231d5) {		/* |x| ~<= 9*pi/4 */
     89 		if (ix <= 0x40afeddf) {	/* |x| ~<= 7*pi/4 */
     90 			if (hx > 0) {
     91 				__kernel_sincosdf(x - p3pio2, cs, sn);
     92 				*sn = -*sn;
     93 			} else {
     94 				__kernel_sincosdf(x + p3pio2, cs, sn);
     95 				*cs = -*cs;
     96 			}
     97 		} else {
     98 			if (hx > 0)
     99 				__kernel_sincosdf(x - p4pio2, sn, cs);
    100 			else
    101 				__kernel_sincosdf(x + p4pio2, sn, cs);
    102 		}
    103 		return;
    104 	}
    105 
    106 	/* If x = Inf or NaN, then sin(x) = NaN and cos(x) = NaN. */
    107 	if (ix >= 0x7f800000) {
    108 		*sn = x - x;
    109 		*cs = x - x;
    110 		return;
    111 	}
    112 
    113 	/* Argument reduction. */
    114 	n = __ieee754_rem_pio2fd(x, &y);
    115 	__kernel_sincosdf(y, &s, &c);
    116 
    117 	switch(n & 3) {
    118 	case 0:
    119 		*sn = s;
    120 		*cs = c;
    121 		break;
    122 	case 1:
    123 		*sn = c;
    124 		*cs = -s;
    125 		break;
    126 	case 2:
    127 		*sn = -s;
    128 		*cs = -c;
    129 		break;
    130 	default:
    131 		*sn = -c;
    132 		*cs = s;
    133 	}
    134 }
    135 
    136 
    137