Home | History | Annotate | Line # | Download | only in src
      1 /*-
      2  * Copyright (c) 2017,2023 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 #include <sys/cdefs.h>
     28 /*
     29  * See ../src/s_cospi.c for implementation details.
     30  */
     31 #define	INLINE_KERNEL_SINDF
     32 #define	INLINE_KERNEL_COSDF
     33 
     34 #include "namespace.h"
     35 __weak_alias(cospif, _cospif)
     36 
     37 #include "math.h"
     38 #include "math_private.h"
     39 #include "k_cosdf.c"
     40 #include "k_sindf.c"
     41 
     42 #define	__kernel_cospif(x)	(__kernel_cosdf(M_PI * (x)))
     43 #define	__kernel_sinpif(x)	(__kernel_sindf(M_PI * (x)))
     44 
     45 static volatile const float vzero = 0;
     46 
     47 float
     48 cospif(float x)
     49 {
     50 	float ax, c;
     51 	uint32_t ix, jj0;
     52 
     53 	GET_FLOAT_WORD(ix, x);
     54 	ix = ix & 0x7fffffff;
     55 	SET_FLOAT_WORD(ax, ix);
     56 
     57 	if (ix < 0x3f800000) {			/* |x| < 1 */
     58 		if (ix < 0x3e800000) {		/* |x| < 0.25 */
     59 			if (ix < 0x38800000) {	/* |x| < 0x1p-14 */
     60 				/* Raise inexact iff != 0. */
     61 				if ((int)ax == 0)
     62 					return (1);
     63 			}
     64 			return (__kernel_cospif(ax));
     65 		}
     66 
     67 		if (ix < 0x3f000000)		/* |x| < 0.5 */
     68 			c = __kernel_sinpif(0.5F - ax);
     69 		else if (ix < 0x3f400000) {	/* |x| < 0.75 */
     70 			if (ix == 0x3f000000)
     71 				return (0);
     72 			c = -__kernel_sinpif(ax - 0.5F);
     73 		} else
     74 			c = -__kernel_cospif(1 - ax);
     75 		return (c);
     76 	}
     77 
     78 	if (ix < 0x4b000000) {		/* 1 <= |x| < 0x1p23 */
     79 		FFLOORF(x, jj0, ix);	/* Integer part of ax. */
     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 		jj0 = (uint32_t)x;
     98 		return (jj0 & 1 ? -c : c);
     99 	}
    100 
    101 	/* x = +-inf or nan. */
    102 	if (ix >= 0x7f800000)
    103 		return (vzero / vzero);
    104 
    105 	/*
    106 	 * For 0x1p23 <= |x| < 0x1p24 need to determine if x is an even
    107 	 * or odd integer to return +1 or -1.
    108 	 * For |x| >= 0x1p24, it is always an even integer, so return 1.
    109 	 */
    110 	return (ix < 0x4b800000 ? ((ix & 1) ? -1 : 1) : 1);
    111 }
    112