Home | History | Annotate | Line # | Download | only in ld80
s_tanpil.c revision 1.1
      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 /*
     28  * See ../src/s_tanpi.c for implementation details.
     29  */
     30 
     31 #ifdef __i386__
     32 #include <ieeefp.h>
     33 #endif
     34 #include <stdint.h>
     35 
     36 #include "math.h"
     37 #include "math_private.h"
     38 
     39 static const double
     40 pi_hi =  3.1415926814079285e+00,	/* 0x400921fb 0x58000000 */
     41 pi_lo = -2.7818135228334233e-08;	/* 0xbe5dde97 0x3dcb3b3a */
     42 
     43 static inline long double
     44 __kernel_tanpil(long double x)
     45 {
     46 	long double hi, lo, t;
     47 
     48 	if (x < 0.25) {
     49 		hi = (float)x;
     50 		lo = x - hi;
     51 		lo = lo * (pi_lo + pi_hi) + hi * pi_lo;
     52 		hi *= pi_hi;
     53 		_2sumF(hi, lo);
     54 		t = __kernel_tanl(hi, lo, -1);
     55 	} else if (x > 0.25) {
     56 		x = 0.5 - x;
     57 		hi = (float)x;
     58 		lo = x - hi;
     59 		lo = lo * (pi_lo + pi_hi) + hi * pi_lo;
     60 		hi *= pi_hi;
     61 		_2sumF(hi, lo);
     62 		t = - __kernel_tanl(hi, lo, 1);
     63 	} else
     64 		t = 1;
     65 
     66 	return (t);
     67 }
     68 
     69 static volatile const double vzero = 0;
     70 
     71 long double
     72 tanpil(long double x)
     73 {
     74 	long double ax, hi, lo, odd, t;
     75 	uint64_t lx;
     76 	uint32_t j0;
     77 	uint16_t hx, ix;
     78 
     79 	EXTRACT_LDBL80_WORDS(hx, lx, x);
     80 	ix = hx & 0x7fff;
     81 	INSERT_LDBL80_WORDS(ax, ix, lx);
     82 
     83 	ENTERI();
     84 
     85 	if (ix < 0x3fff) {			/* |x| < 1 */
     86 		if (ix < 0x3ffe) {		/* |x| < 0.5 */
     87 			if (ix < 0x3fdd) {	/* |x| < 0x1p-34 */
     88 				if (x == 0)
     89 					RETURNI(x);
     90 				INSERT_LDBL80_WORDS(hi, hx,
     91 				    lx & 0xffffffff00000000ull);
     92 				hi *= 0x1p63L;
     93 				lo = x * 0x1p63L - hi;
     94 				t = (pi_lo + pi_hi) * lo + pi_lo * hi +
     95 				    pi_hi * hi;
     96 				RETURNI(t * 0x1p-63L);
     97 			}
     98 			t = __kernel_tanpil(ax);
     99 		} else if (ax == 0.5)
    100 			t = 1 / vzero;
    101 		else
    102 			t = -__kernel_tanpil(1 - ax);
    103 		RETURNI((hx & 0x8000) ? -t : t);
    104 	}
    105 
    106 	if (ix < 0x403e) {			/* 1 <= |x| < 0x1p63 */
    107 		FFLOORL80(x, j0, ix, lx);	/* Integer part of ax. */
    108 		odd = (uint64_t)x & 1 ? -1 : 1;
    109 		ax -= x;
    110 		EXTRACT_LDBL80_WORDS(ix, lx, ax);
    111 
    112 		if (ix < 0x3ffe)		/* |x| < 0.5 */
    113 			t = ix == 0 ? copysignl(0, odd) : __kernel_tanpil(ax);
    114 		else if (ax == 0.5L)
    115 			t = odd / vzero;
    116 		else
    117 			t = -__kernel_tanpil(1 - ax);
    118 		RETURNI((hx & 0x8000) ? -t : t);
    119 	}
    120 
    121 	/* x = +-inf or nan. */
    122 	if (ix >= 0x7fff)
    123 		RETURNI(vzero / vzero);
    124 
    125 	/*
    126 	 * For 0x1p63 <= |x| < 0x1p64 need to determine if x is an even
    127 	 * or odd integer to set t = +0 or -0.
    128 	 * For |x| >= 0x1p64, it is always an even integer, so t = 0.
    129 	 */
    130 	t = ix >= 0x403f ? 0 : (copysignl(0, (lx & 1) ? -1 : 1));
    131 	RETURNI((hx & 0x8000) ? -t : t);
    132 }
    133