10Sduke/*	$OpenBSD: k_tanl.c,v 1.1 2008/12/09 20:00:35 martynas Exp $	*/
211884Sykantser/* From: @(#)k_tan.c 1.5 04/04/22 SMI */
30Sduke/*
40Sduke * ====================================================
50Sduke * Copyright 2004 Sun Microsystems, Inc.  All Rights Reserved.
60Sduke * Copyright (c) 2008 Steven G. Kargl, David Schultz, Bruce D. Evans.
70Sduke *
80Sduke * Permission to use, copy, modify, and distribute this
90Sduke * software is freely granted, provided that this notice
100Sduke * is preserved.
110Sduke * ====================================================
120Sduke */
130Sduke
140Sduke/*
150Sduke * ld128 version of k_tan.c.  See ../k_tan.c for most comments.
160Sduke */
170Sduke
180Sduke#include <math.h>
192362Sohair
202362Sohair#include "math_private.h"
212362Sohair
220Sduke/*
230Sduke * Domain [-0.67434, 0.67434], range ~[-3.37e-36, 1.982e-37]
240Sduke * |tan(x)/x - t(x)| < 2**-117.8 (XXX should be ~1e-37)
250Sduke *
260Sduke * See ../ld80/k_cosl.c for more details about the polynomial.
270Sduke */
280Sdukestatic const long double
2916958SiignatyevT3 = 0x1.5555555555555555555555555553p-2L,
3013200SakulyakhT5 = 0x1.1111111111111111111111111eb5p-3L,
3116958SiignatyevT7 = 0x1.ba1ba1ba1ba1ba1ba1ba1b694cd6p-5L,
320SdukeT9 = 0x1.664f4882c10f9f32d6bbe09d8bcdp-6L,
330SdukeT11 = 0x1.226e355e6c23c8f5b4f5762322eep-7L,
340SdukeT13 = 0x1.d6d3d0e157ddfb5fed8e84e27b37p-9L,
350SdukeT15 = 0x1.7da36452b75e2b5fce9ee7c2c92ep-10L,
360SdukeT17 = 0x1.355824803674477dfcf726649efep-11L,
370SdukeT19 = 0x1.f57d7734d1656e0aceb716f614c2p-13L,
3811884SykantserT21 = 0x1.967e18afcb180ed942dfdc518d6cp-14L,
390SdukeT23 = 0x1.497d8eea21e95bc7e2aa79b9f2cdp-15L,
400SdukeT25 = 0x1.0b132d39f055c81be49eff7afd50p-16L,
410SdukeT27 = 0x1.b0f72d33eff7bfa2fbc1059d90b6p-18L,
420SdukeT29 = 0x1.5ef2daf21d1113df38d0fbc00267p-19L,
4313200SakulyakhT31 = 0x1.1c77d6eac0234988cdaa04c96626p-20L,
440SdukeT33 = 0x1.cd2a5a292b180e0bdd701057dfe3p-22L,
450SdukeT35 = 0x1.75c7357d0298c01a31d0a6f7d518p-23L,
460SdukeT37 = 0x1.2f3190f4718a9a520f98f50081fcp-24L,
470Sdukepio4 = 0x1.921fb54442d18469898cc51701b8p-1L,
480Sdukepio4lo = 0x1.cd129024e088a67cc74020bbea60p-116L;
490Sduke
500Sdukestatic const double
510SdukeT39 =  0.000000028443389121318352,	/*  0x1e8a7592977938.0p-78 */
520SdukeT41 =  0.000000011981013102001973,	/*  0x19baa1b1223219.0p-79 */
530SdukeT43 =  0.0000000038303578044958070,	/*  0x107385dfb24529.0p-80 */
540SdukeT45 =  0.0000000034664378216909893,	/*  0x1dc6c702a05262.0p-81 */
550SdukeT47 = -0.0000000015090641701997785,	/* -0x19ecef3569ebb6.0p-82 */
560SdukeT49 =  0.0000000029449552300483952,	/*  0x194c0668da786a.0p-81 */
570SdukeT51 = -0.0000000022006995706097711,	/* -0x12e763b8845268.0p-81 */
580SdukeT53 =  0.0000000015468200913196612,	/*  0x1a92fc98c29554.0p-82 */
590SdukeT55 = -0.00000000061311613386849674,	/* -0x151106cbc779a9.0p-83 */
600SdukeT57 =  1.4912469681508012e-10;		/*  0x147edbdba6f43a.0p-85 */
610Sduke
620Sdukelong double
630Sduke__kernel_tanl(long double x, long double y, int iy) {
6411884Sykantser	long double z, r, v, w, s;
6511884Sykantser	long double osign;
660Sduke	int i;
6711884Sykantser
6811884Sykantser	iy = (iy == 1 ? -1 : 1);	/* XXX recover original interface */
6911884Sykantser	osign = (x >= 0 ? 1.0 : -1.0);	/* XXX slow, probably wrong for -0 */
7011884Sykantser	if (fabsl(x) >= 0.67434) {
7111884Sykantser		if (x < 0) {
7211884Sykantser			x = -x;
730Sduke			y = -y;
740Sduke		}
750Sduke		z = pio4 - x;
7611884Sykantser		w = pio4lo - y;
770Sduke		x = z + w;
780Sduke		y = 0.0;
790Sduke		i = 1;
8011884Sykantser	} else
810Sduke		i = 0;
820Sduke	z = x * x;
830Sduke	w = z * z;
840Sduke	r = T5 + w * (T9 + w * (T13 + w * (T17 + w * (T21 +
850Sduke	    w * (T25 + w * (T29 + w * (T33 +
860Sduke	    w * (T37 + w * (T41 + w * (T45 + w * (T49 + w * (T53 +
870Sduke	    w * T57))))))))))));
880Sduke	v = z * (T7 + w * (T11 + w * (T15 + w * (T19 + w * (T23 +
890Sduke	    w * (T27 + w * (T31 + w * (T35 +
900Sduke	    w * (T39 + w * (T43 + w * (T47 + w * (T51 + w * T55))))))))))));
910Sduke	s = z * x;
920Sduke	r = y + z * (s * (r + v) + y);
930Sduke	r += T3 * s;
940Sduke	w = x + r;
950Sduke	if (i == 1) {
960Sduke		v = (long double) iy;
970Sduke		return osign *
980Sduke			(v - 2.0 * (x - (w * w / (w + v) - r)));
990Sduke	}
1000Sduke	if (iy == 1)
1010Sduke		return w;
1020Sduke	else {
1030Sduke		/*
1040Sduke		 * if allow error up to 2 ulp, simply return
1050Sduke		 * -1.0 / (x+r) here
1060Sduke		 */
1070Sduke		/* compute -1.0 / (x+r) accurately */
1080Sduke		long double a, t;
1090Sduke		z = w;
1100Sduke		z = z + 0x1p32 - 0x1p32;
1110Sduke		v = r - (z - x);	/* z+v = r+x */
1120Sduke		t = a = -1.0 / w;	/* a = -1.0/w */
1130Sduke		t = t + 0x1p32 - 0x1p32;
1140Sduke		s = 1.0 + t * z;
1150Sduke		return t + a * (s + t * v);
1160Sduke	}
1170Sduke}
1180Sduke