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