diff --git a/lib/msun/Makefile b/lib/msun/Makefile index 7107aad56aa7..dcee5572f949 100644 --- a/lib/msun/Makefile +++ b/lib/msun/Makefile @@ -1,264 +1,274 @@ # @(#)Makefile 5.1beta 93/09/24 # $FreeBSD$ # # ==================================================== # Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. # # Developed at SunPro, a Sun Microsystems, Inc. business. # Permission to use, copy, modify, and distribute this # software is freely granted, provided that this notice # is preserved. # ==================================================== # # PACKAGE= clibs ARCH_SUBDIR= ${MACHINE_CPUARCH:S/i386/i387/} .include "${ARCH_SUBDIR}/Makefile.inc" .PATH: ${.CURDIR}/${ARCH_SUBDIR} .if ${MACHINE_CPUARCH} == "i386" || ${MACHINE_CPUARCH} == "amd64" .PATH: ${.CURDIR}/x86 CFLAGS+= -I${.CURDIR}/x86 .endif # long double format .if ${LDBL_PREC} == 64 .PATH: ${.CURDIR}/ld80 CFLAGS+= -I${.CURDIR}/ld80 .elif ${LDBL_PREC} == 113 .PATH: ${.CURDIR}/ld128 CFLAGS+= -I${.CURDIR}/ld128 .endif CFLAGS+= -I${.CURDIR}/${ARCH_SUBDIR} .include .if ${COMPILER_TYPE} == "clang" && ${COMPILER_VERSION} >= 100000 && \ (${MACHINE_CPUARCH} == "amd64" || ${MACHINE_CPUARCH} == "i386") # When using clang with x86_64 CPUs that support AVX, some floating point # transformations may raise exceptions that would not have been raised by the # original code. To avoid this, use the -fp-exception-behavior=maytrap flag, # introduced in clang 10.0.0. # See also: https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=254911 CFLAGS+= -ffp-exception-behavior=maytrap .endif .PATH: ${.CURDIR}/bsdsrc .PATH: ${.CURDIR}/src .PATH: ${.CURDIR}/man LIB= m SHLIBDIR?= /lib SHLIB_MAJOR= 5 WARNS?= 1 IGNORE_PRAGMA= COMMON_SRCS= b_exp.c b_log.c b_tgamma.c \ e_acos.c e_acosf.c e_acosh.c e_acoshf.c e_asin.c e_asinf.c \ e_atan2.c e_atan2f.c e_atanh.c e_atanhf.c e_cosh.c e_coshf.c e_exp.c \ e_expf.c e_fmod.c e_fmodf.c e_gamma.c e_gamma_r.c e_gammaf.c \ e_gammaf_r.c e_hypot.c e_hypotf.c e_j0.c e_j0f.c e_j1.c e_j1f.c \ e_jn.c e_jnf.c e_lgamma.c e_lgamma_r.c e_lgammaf.c e_lgammaf_r.c \ e_log.c e_log10.c e_log10f.c e_log2.c e_log2f.c e_logf.c \ e_pow.c e_powf.c e_rem_pio2.c \ e_rem_pio2f.c e_remainder.c e_remainderf.c e_scalb.c e_scalbf.c \ e_sinh.c e_sinhf.c e_sqrt.c e_sqrtf.c fenv.c \ imprecise.c \ k_cos.c k_cosf.c k_exp.c k_expf.c k_rem_pio2.c k_sin.c k_sinf.c \ k_tan.c k_tanf.c \ s_asinh.c s_asinhf.c s_atan.c s_atanf.c s_carg.c s_cargf.c s_cargl.c \ s_cbrt.c s_cbrtf.c s_ceil.c s_ceilf.c s_clog.c s_clogf.c \ s_copysign.c s_copysignf.c s_cos.c s_cosf.c \ s_csqrt.c s_csqrtf.c s_erf.c s_erff.c \ s_exp2.c s_exp2f.c s_expm1.c s_expm1f.c s_fabsf.c s_fdim.c \ s_finite.c s_finitef.c \ s_floor.c s_floorf.c s_fma.c s_fmaf.c \ s_fmax.c s_fmaxf.c s_fmin.c \ s_fminf.c s_frexp.c s_frexpf.c s_ilogb.c s_ilogbf.c \ s_ilogbl.c s_isfinite.c s_isnan.c s_isnormal.c \ s_llrint.c s_llrintf.c s_llround.c s_llroundf.c s_llroundl.c \ s_log1p.c s_log1pf.c s_logb.c s_logbf.c s_lrint.c s_lrintf.c \ s_lround.c s_lroundf.c s_lroundl.c s_modff.c \ s_nan.c s_nearbyint.c s_nextafter.c s_nextafterf.c \ s_nexttowardf.c s_remquo.c s_remquof.c \ s_rint.c s_rintf.c s_round.c s_roundf.c \ s_scalbln.c s_scalbn.c s_scalbnf.c s_signbit.c \ s_signgam.c s_significand.c s_significandf.c s_sin.c \ s_sincos.c s_sincosf.c s_sinf.c \ s_tan.c s_tanf.c s_tanh.c s_tanhf.c s_tgammaf.c s_trunc.c s_truncf.c \ w_cabs.c w_cabsf.c w_drem.c w_dremf.c # Location of fpmath.h and _fpmath.h .if exists(${LIBCSRCDIR}/${MACHINE_ARCH}) LIBC_ARCH=${MACHINE_ARCH} .else LIBC_ARCH=${MACHINE_CPUARCH} .endif CFLAGS+= -I${.CURDIR}/src -I${LIBCSRCDIR}/include \ -I${LIBCSRCDIR}/${LIBC_ARCH} SYM_MAPS+= ${.CURDIR}/Symbol.map VERSION_DEF= ${LIBCSRCDIR}/Versions.def SYMBOL_MAPS= ${SYM_MAPS} # C99 long double functions COMMON_SRCS+= s_copysignl.c s_fabsl.c s_llrintl.c s_lrintl.c s_modfl.c .if ${LDBL_PREC} != 53 # If long double != double use these; otherwise, we alias the double versions. COMMON_SRCS+= catrigl.c \ e_acoshl.c e_acosl.c e_asinl.c e_atan2l.c e_atanhl.c \ e_coshl.c e_fmodl.c e_hypotl.c \ e_lgammal.c e_lgammal_r.c e_powl.c \ e_remainderl.c e_sinhl.c e_sqrtl.c \ invtrig.c k_cosl.c k_sinl.c k_tanl.c \ s_asinhl.c s_atanl.c s_cbrtl.c s_ceill.c \ s_clogl.c s_cosl.c s_cprojl.c \ s_csqrtl.c s_erfl.c s_exp2l.c s_expl.c s_floorl.c s_fmal.c \ s_fmaxl.c s_fminl.c s_frexpl.c s_logbl.c s_logl.c s_nanl.c \ s_nextafterl.c s_nexttoward.c s_remquol.c s_rintl.c s_roundl.c \ s_scalbnl.c s_sinl.c s_sincosl.c \ s_tanhl.c s_tanl.c s_truncl.c w_cabsl.c # Work around this warning from gcc: # lib/msun/ld80/e_powl.c:275:1: error: floating constant exceeds range of # 'long double' [-Werror=overflow] # if( y >= LDBL_MAX ) # See also: https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=130067 .if ${COMPILER_TYPE} == "gcc" CFLAGS.e_powl.c+= -Wno-error=overflow + +# IEEE-754 2008 and ISO/IEC TS 18661-4 half-cycle trignometric functions +COMMON_SRCS+= s_cospi.c s_cospif.c s_cospil.c \ + s_sinpi.c s_sinpif.c s_sinpil.c \ + s_tanpi.c s_tanpif.c s_tanpil.c + .endif .endif # C99 complex functions COMMON_SRCS+= catrig.c catrigf.c \ s_ccosh.c s_ccoshf.c s_cexp.c s_cexpf.c \ s_cimag.c s_cimagf.c s_cimagl.c \ s_conj.c s_conjf.c s_conjl.c \ s_cpow.c s_cpowf.c s_cpowl.c \ s_cproj.c s_cprojf.c s_creal.c s_crealf.c s_creall.c \ s_csinh.c s_csinhf.c s_ctanh.c s_ctanhf.c # FreeBSD's C library supplies these functions: #COMMON_SRCS+= s_fabs.c s_frexp.c s_isnan.c s_ldexp.c s_modf.c # Exclude the generic versions of what we provide in the MD area. .if defined(ARCH_SRCS) .for i in ${ARCH_SRCS} COMMON_SRCS:= ${COMMON_SRCS:N${i:R}.c} .endfor .endif SRCS= ${COMMON_SRCS} ${ARCH_SRCS} INCS+= fenv.h math.h MAN= acos.3 acosh.3 asin.3 asinh.3 atan.3 atan2.3 atanh.3 \ ceil.3 cacos.3 ccos.3 ccosh.3 cexp.3 \ - cimag.3 clog.3 copysign.3 cos.3 cosh.3 cpow.3 csqrt.3 erf.3 \ + cimag.3 clog.3 copysign.3 cos.3 cosh.3 cospi.3 \ + cpow.3 csqrt.3 erf.3 \ exp.3 fabs.3 fdim.3 \ feclearexcept.3 feenableexcept.3 fegetenv.3 \ fegetround.3 fenv.3 floor.3 \ fma.3 fmax.3 fmod.3 hypot.3 ieee.3 ieee_test.3 ilogb.3 j0.3 \ lgamma.3 log.3 lrint.3 lround.3 math.3 nan.3 \ nextafter.3 remainder.3 rint.3 \ round.3 scalbn.3 signbit.3 sin.3 sincos.3 \ - sinh.3 sqrt.3 tan.3 tanh.3 trunc.3 \ + sinh.3 sinpi.3 sqrt.3 tan.3 tanh.3 tanpi.3 trunc.3 \ complex.3 MLINKS+=acos.3 acosf.3 acos.3 acosl.3 MLINKS+=acosh.3 acoshf.3 acosh.3 acoshl.3 MLINKS+=asin.3 asinf.3 asin.3 asinl.3 MLINKS+=asinh.3 asinhf.3 asinh.3 asinhl.3 MLINKS+=atan.3 atanf.3 atan.3 atanl.3 MLINKS+=atanh.3 atanhf.3 atanh.3 atanhl.3 MLINKS+=atan2.3 atan2f.3 atan2.3 atan2l.3 \ atan2.3 carg.3 atan2.3 cargf.3 atan2.3 cargl.3 MLINKS+=cacos.3 cacosf.3 cacos.3 cacosl.3 \ cacos.3 cacosh.3 cacos.3 cacoshf.3 cacos.3 cacoshl.3 \ cacos.3 casin.3 cacos.3 casinf.3 cacos.3 casinl.3 \ cacos.3 casinh.3 cacos.3 casinhf.3 cacos.3 casinhl.3 \ cacos.3 catan.3 cacos.3 catanf.3 cacos.3 catanl.3 \ cacos.3 catanh.3 cacos.3 catanhf.3 cacos.3 catanhl.3 MLINKS+=ccos.3 ccosf.3 ccos.3 csin.3 ccos.3 csinf.3 ccos.3 ctan.3 ccos.3 ctanf.3 MLINKS+=ccosh.3 ccoshf.3 ccosh.3 csinh.3 ccosh.3 csinhf.3 \ ccosh.3 ctanh.3 ccosh.3 ctanhf.3 MLINKS+=ceil.3 ceilf.3 ceil.3 ceill.3 MLINKS+=cexp.3 cexpf.3 MLINKS+=cimag.3 cimagf.3 cimag.3 cimagl.3 \ cimag.3 conj.3 cimag.3 conjf.3 cimag.3 conjl.3 \ cimag.3 cproj.3 cimag.3 cprojf.3 cimag.3 cprojl.3 \ cimag.3 creal.3 cimag.3 crealf.3 cimag.3 creall.3 MLINKS+=clog.3 clogf.3 clog.3 clogl.3 MLINKS+=copysign.3 copysignf.3 copysign.3 copysignl.3 MLINKS+=cos.3 cosf.3 cos.3 cosl.3 MLINKS+=cosh.3 coshf.3 cosh.3 coshl.3 +MLINKS+=cospi.3 cospif.3 cospi.3 cospil.3 MLINKS+=cpow.3 cpowf.3 cpow.3 cpowl.3 MLINKS+=csqrt.3 csqrtf.3 csqrt.3 csqrtl.3 MLINKS+=erf.3 erfc.3 erf.3 erff.3 erf.3 erfcf.3 erf.3 erfl.3 erf.3 erfcl.3 MLINKS+=exp.3 expm1.3 exp.3 expm1f.3 exp.3 expm1l.3 exp.3 pow.3 exp.3 powf.3 \ exp.3 powl.3 exp.3 exp2.3 exp.3 exp2f.3 exp.3 exp2l.3 exp.3 expf.3 \ exp.3 expl.3 MLINKS+=fabs.3 fabsf.3 fabs.3 fabsl.3 MLINKS+=fdim.3 fdimf.3 fdim.3 fdiml.3 MLINKS+=feclearexcept.3 fegetexceptflag.3 feclearexcept.3 feraiseexcept.3 \ feclearexcept.3 fesetexceptflag.3 feclearexcept.3 fetestexcept.3 MLINKS+=feenableexcept.3 fedisableexcept.3 feenableexcept.3 fegetexcept.3 MLINKS+=fegetenv.3 feholdexcept.3 fegetenv.3 fesetenv.3 \ fegetenv.3 feupdateenv.3 MLINKS+=fegetround.3 fesetround.3 MLINKS+=floor.3 floorf.3 floor.3 floorl.3 MLINKS+=fma.3 fmaf.3 fma.3 fmal.3 MLINKS+=fmax.3 fmaxf.3 fmax.3 fmaxl.3 \ fmax.3 fmin.3 fmax.3 fminf.3 fmax.3 fminl.3 MLINKS+=fmod.3 fmodf.3 fmod.3 fmodl.3 MLINKS+=hypot.3 cabs.3 hypot.3 cabsf.3 hypot.3 cabsl.3 \ hypot.3 hypotf.3 hypot.3 hypotl.3 MLINKS+=ieee_test.3 scalb.3 ieee_test.3 scalbf.3 MLINKS+=ieee_test.3 significand.3 ieee_test.3 significandf.3 MLINKS+=ilogb.3 ilogbf.3 ilogb.3 ilogbl.3 \ ilogb.3 logb.3 ilogb.3 logbf.3 ilogb.3 logbl.3 MLINKS+=j0.3 j1.3 j0.3 jn.3 j0.3 y0.3 j0.3 y1.3 j0.3 y1f.3 j0.3 yn.3 MLINKS+=j0.3 j0f.3 j0.3 j1f.3 j0.3 jnf.3 j0.3 y0f.3 j0.3 ynf.3 MLINKS+=lgamma.3 gamma.3 lgamma.3 gammaf.3 \ lgamma.3 lgammaf.3 lgamma.3 lgammal.3 \ lgamma.3 tgamma.3 lgamma.3 tgammaf.3 MLINKS+=log.3 log10.3 log.3 log10f.3 log.3 log10l.3 \ log.3 log1p.3 log.3 log1pf.3 log.3 log1pl.3 \ log.3 logf.3 log.3 logl.3 \ log.3 log2.3 log.3 log2f.3 log.3 log2l.3 MLINKS+=lrint.3 llrint.3 lrint.3 llrintf.3 lrint.3 llrintl.3 \ lrint.3 lrintf.3 lrint.3 lrintl.3 MLINKS+=lround.3 llround.3 lround.3 llroundf.3 lround.3 llroundl.3 \ lround.3 lroundf.3 lround.3 lroundl.3 MLINKS+=nan.3 nanf.3 nan.3 nanl.3 MLINKS+=nextafter.3 nextafterf.3 nextafter.3 nextafterl.3 MLINKS+=nextafter.3 nexttoward.3 nextafter.3 nexttowardf.3 MLINKS+=nextafter.3 nexttowardl.3 MLINKS+=remainder.3 remainderf.3 remainder.3 remainderl.3 \ remainder.3 remquo.3 remainder.3 remquof.3 remainder.3 remquol.3 MLINKS+=rint.3 rintf.3 rint.3 rintl.3 \ rint.3 nearbyint.3 rint.3 nearbyintf.3 rint.3 nearbyintl.3 MLINKS+=round.3 roundf.3 round.3 roundl.3 MLINKS+=scalbn.3 scalbln.3 scalbn.3 scalblnf.3 scalbn.3 scalblnl.3 MLINKS+=scalbn.3 scalbnf.3 scalbn.3 scalbnl.3 MLINKS+=sin.3 sinf.3 sin.3 sinl.3 MLINKS+=sincos.3 sincosf.3 sin.3 sincosl.3 MLINKS+=sinh.3 sinhf.3 sinh.3 sinhl.3 +MLINKS+=sinpi.3 sinpif.3 sinpi.3 sinpil.3 MLINKS+=sqrt.3 cbrt.3 sqrt.3 cbrtf.3 sqrt.3 cbrtl.3 sqrt.3 sqrtf.3 \ sqrt.3 sqrtl.3 MLINKS+=tan.3 tanf.3 tan.3 tanl.3 MLINKS+=tanh.3 tanhf.3 tanh.3 tanhl.3 +MLINKS+=tanpi.3 tanpif.3 tanpi.3 tanpil.3 MLINKS+=trunc.3 truncf.3 trunc.3 truncl.3 .include HAS_TESTS= SUBDIR.${MK_TESTS}+= tests .include .if ${COMPILER_TYPE} == "clang" && ${COMPILER_VERSION} >= 120000 # Silence '#pragma FENV_ACCESS' is not supported on this target - ignored CWARNFLAGS+= -Wno-error=ignored-pragmas .endif .include diff --git a/lib/msun/Symbol.map b/lib/msun/Symbol.map index 51deded732f0..7229e7ef31fd 100644 --- a/lib/msun/Symbol.map +++ b/lib/msun/Symbol.map @@ -1,306 +1,319 @@ /* * $FreeBSD$ */ /* 7.0-CURRENT */ FBSD_1.0 { __fe_dfl_env; tgamma; acos; acosf; acosh; acoshf; asin; asinf; atan2; atan2f; atanh; atanhf; cosh; coshf; exp; expf; fmod; fmodf; gamma; gamma_r; gammaf; gammaf_r; hypot; hypotf; j0; y0; j0f; y0f; j1; y1; j1f; y1f; jn; yn; jnf; ynf; lgamma; lgamma_r; lgammaf; lgammaf_r; log; log10; log10f; logf; pow; powf; remainder; remainderf; scalb; scalbf; sinh; sinhf; sqrt; sqrtf; asinh; asinhf; atan; atanf; cbrt; cbrtf; ceil; ceilf; ceill; cimag; cimagf; cimagl; conj; conjf; conjl; copysign; copysignf; copysignl; cos; cosf; creal; crealf; creall; erf; erfc; erff; erfcf; exp2; exp2f; expm1; expm1f; fabs; fabsf; fabsl; fdim; fdimf; fdiml; finite; finitef; floor; floorf; floorl; fma; fmaf; fmal; fmax; fmaxf; fmaxl; fmin; fminf; fminl; frexp; frexpf; frexpl; ilogb; ilogbf; ilogbl; __isfinite; __isfinitef; __isfinitel; isnanf; __isnanl; __isnormal; __isnormalf; __isnormall; llrint; llrintf; llround; llroundf; llroundl; log1p; log1pf; logb; logbf; lrint; lrintf; lround; lroundf; lroundl; modff; modfl; nearbyint; nearbyintf; nextafter; nexttoward; nexttowardl; nextafterl; nextafterf; nexttowardf; remquo; remquof; rint; rintf; round; roundf; roundl; scalbln; scalblnf; scalblnl; scalbn; scalbnl; scalbnf; ldexpf; ldexpl; __signbit; __signbitf; __signbitl; signgam; significand; significandf; sin; sinf; tan; tanf; tanh; tanhf; trunc; truncf; truncl; cabs; cabsf; drem; dremf; }; /* First added in 8.0-CURRENT */ FBSD_1.1 { carg; cargf; csqrt; csqrtf; logbl; nan; nanf; nanl; llrintl; lrintl; nearbyintl; rintl; exp2l; sinl; cosl; tanl; tgammaf; sqrtl; hypotl; cabsl; csqrtl; remquol; remainderl; fmodl; acosl; asinl; atan2l; atanl; cargl; cproj; cprojf; cprojl; }; /* First added in 9.0-CURRENT */ FBSD_1.2 { __isnanf; cbrtl; cexp; cexpf; log2; log2f; }; /* First added in 10.0-CURRENT */ FBSD_1.3 { feclearexcept; fegetexceptflag; fetestexcept; fegetround; fesetround; fesetenv; acoshl; asinhl; atanhl; cacos; cacosf; cacosh; cacoshf; casin; casinf; casinh; casinhf; catan; catanf; catanh; catanhf; csin; csinf; csinh; csinhf; ccos; ccosf; ccosh; ccoshf; coshl; ctan; ctanf; ctanh; ctanhf; erfcl; erfl; expl; expm1l; lgammal; log10l; log1pl; log2l; logl; powl; sinhl; tanhl; /* Implemented as weak aliases for imprecise versions */ tgammal; }; /* First added in 11.0-CURRENT */ FBSD_1.4 { lgammal_r; }; /* First added in 12.0-CURRENT */ FBSD_1.5 { cacoshl; cacosl; casinhl; casinl; catanl; catanhl; clog; clogf; clogl; cpow; cpowf; cpowl; sincos; sincosf; sincosl; }; + +/* First added in 14.0-CURRENT */ +FBSD_1.7 { + cospi; + cospif; + cospil; + sinpi; + sinpif; + sinpil; + tanpi; + tanpif; + tanpil; +}; diff --git a/lib/msun/ld128/k_cospil.h b/lib/msun/ld128/k_cospil.h new file mode 100644 index 000000000000..592f19229532 --- /dev/null +++ b/lib/msun/ld128/k_cospil.h @@ -0,0 +1,42 @@ +/*- + * Copyright (c) 2017 Steven G. Kargl + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice unmodified, this list of conditions, and the following + * disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +/* + * See ../src/k_cospi.c for implementation details. + */ + +static inline long double +__kernel_cospil(long double x) +{ + long double hi, lo; + + hi = (double)x; + lo = x - hi; + lo = lo * (pi_lo + pi_hi) + hi * pi_lo; + hi *= pi_hi; + _2sumF(hi, lo); + return (__kernel_cosl(hi, lo)); +} diff --git a/lib/msun/ld128/k_sinpil.h b/lib/msun/ld128/k_sinpil.h new file mode 100644 index 000000000000..fa4e7d6336d7 --- /dev/null +++ b/lib/msun/ld128/k_sinpil.h @@ -0,0 +1,42 @@ +/*- + * Copyright (c) 2017 Steven G. Kargl + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice unmodified, this list of conditions, and the following + * disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +/* + * See ../src/k_sinpi.c for implementation details. + */ + +static inline long double +__kernel_sinpil(long double x) +{ + long double hi, lo; + + hi = (double)x; + lo = x - hi; + lo = lo * (pi_lo + pi_hi) + hi * pi_lo; + hi *= pi_hi; + _2sumF(hi, lo); + return (__kernel_sinl(hi, lo, 1)); +} diff --git a/lib/msun/ld128/s_cospil.c b/lib/msun/ld128/s_cospil.c new file mode 100644 index 000000000000..b4bc50bb4d89 --- /dev/null +++ b/lib/msun/ld128/s_cospil.c @@ -0,0 +1,109 @@ +/*- + * Copyright (c) 2017 Steven G. Kargl + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice unmodified, this list of conditions, and the following + * disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +/* + * See ../src/s_cospi.c for implementation details. + * + * FIXME: This has not been compiled nor has it been tested for accuracy. + * FIXME: This should use bit twiddling. + */ + +#include "math.h" +#include "math_private.h" + +/* + * pi_hi contains the leading 56 bits of a 169 bit approximation for pi. + */ +static const long double +pi_hi = 3.14159265358979322702026593105983920e+00L, +pi_lo = 1.14423774522196636802434264184180742e-17L; + +#include "k_cospil.h" +#include "k_sinpil.h" + +volatile static const double vzero = 0; + +long double +cospil(long double x) +{ + long double ax, c, xf; + uint32_t ix; + + ax = fabsl(x); + + if (ax < 1) { + if (ax < 0.25) { + if (ax < 0x1p-60) { + if ((int)x == 0) + return (1); + } + return (__kernel_cospil(ax)); + } + + if (ax < 0.5) + c = __kernel_sinpil(0.5 - ax); + else if (ax < 0.75) { + if (ax == 0.5) + return (0); + c = -__kernel_sinpil(ax - 0.5); + } else + c = -__kernel_cospil(1 - ax); + return (c); + } + + if (ax < 0x1p112) { + xf = floorl(ax); + ax -= xf; + if (x < 0.5) { + if (x < 0.25) + c = ax == 0 ? 1 : __kernel_cospil(ax); + else + c = __kernel_sinpil(0.5 - ax); + } else { + if (x < 0.75) { + if (ax == 0.5) + return (0); + c = -__kernel_sinpil(ax - 0.5); + } else + c = -__kernel_cospil(1 - ax); + } + + if (xf > 0x1p50) + xf -= 0x1p50; + if (xf > 0x1p30) + xf -= 0x1p30; + ix = (uint32_t)xf; + return (ix & 1 ? -c : c); + } + + if (isinf(x) || isnan(x)) + return (vzero / vzero); + + /* + * |x| >= 0x1p112 is always an even integer, so return 1. + */ + return (1); +} diff --git a/lib/msun/ld128/s_sinpil.c b/lib/msun/ld128/s_sinpil.c new file mode 100644 index 000000000000..39eed9b007bc --- /dev/null +++ b/lib/msun/ld128/s_sinpil.c @@ -0,0 +1,118 @@ +/*- + * Copyright (c) 2017 Steven G. Kargl + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice unmodified, this list of conditions, and the following + * disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +/* + * See ../src/s_sinpi.c for implementation details. + * + * FIXME: This has not been compiled nor has it been tested for accuracy. + * FIXME: This should use bit twiddling. + */ + +#include "math.h" +#include "math_private.h" + +/* + * pi_hi contains the leading 56 bits of a 169 bit approximation for pi. + */ +static const long double +pi_hi = 3.14159265358979322702026593105983920e+00L, +pi_lo = 1.14423774522196636802434264184180742e-17L; + +#include "k_cospil.h" +#include "k_sinpil.h" + +volatile static const double vzero = 0; + +long double +sinpil(long double x) +{ + long double ax, hi, lo, s, xf, xhi, xlo; + uint32_t ix; + + ax = fabsl(x); + + if (ax < 1) { + if (ax < 0.25) { + if (ax < 0x1p-60) { + if (x == 0) + return (x); + hi = (double)x; + hi *= 0x1p113L; + lo = x * 0x1p113L - hi; + s = (pi_lo + pi_hi) * lo + pi_lo * lo + + pi_hi * hi; + return (s * 0x1p-113L); + } + + s = __kernel_sinpil(ax); + return (copysignl(s, x)); + } + + if (ax < 0.5) + s = __kernel_cospil(0.5 - ax); + else if (ax < 0.75) + s = __kernel_cospil(ax - 0.5); + else + s = __kernel_sinpil(1 - ax); + return (copysignl(s, x)); + } + + if (ax < 0x1p112) { + xf = floorl(ax); + ax -= xf; + if (ax == 0) { + s = 0; + } else { + if (ax < 0.5) { + if (ax <= 0.25) + s = __kernel_sinpil(ax); + else + s = __kernel_cospil(0.5 - ax); + } else { + if (ax < 0.75) + s = __kernel_cospil(ax - 0.5); + else + s = __kernel_sinpil(1 - ax); + } + + if (xf > 0x1p50) + xf -= 0x1p50; + if (xf > 0x1p30) + xf -= 0x1p30; + ix = (uint32_t)xf; + if (ix & 1) s = -s; + } + return (copysignl(s, x)); + } + + if (isinf(x) || isnan(x)) + return (vzero / vzero); + + /* + * |x| >= 0x1p112 is always an integer, so return +-0. + */ + return (copysignl(0, x)); +} diff --git a/lib/msun/ld128/s_tanpil.c b/lib/msun/ld128/s_tanpil.c new file mode 100644 index 000000000000..33a61cf3115d --- /dev/null +++ b/lib/msun/ld128/s_tanpil.c @@ -0,0 +1,120 @@ +/*- + * Copyright (c) 2017 Steven G. Kargl + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice unmodified, this list of conditions, and the following + * disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +/* + * See ../src/s_tanpi.c for implementation details. + * + * FIXME: This has not been compiled nor has it been tested for accuracy. + * FIXME: This should use bit twiddling. + */ + +#include "math.h" +#include "math_private.h" + +/* + * pi_hi contains the leading 56 bits of a 169 bit approximation for pi. + */ +static const long double +pi_hi = 3.14159265358979322702026593105983920e+00L, +pi_lo = 1.14423774522196636802434264184180742e-17L; + +static inline long double +__kernel_tanpi(long double x) +{ + long double hi, lo, t; + + if (x < 0.25) { + hi = (double)x; + lo = x - hi; + lo = lo * (pi_lo + pi_hi) + hi * pi_lo; + hi *= pi_hi; + _2sumF(hi, lo); + t = __kernel_tanl(hi, lo, -1); + } else if (x > 0.25) { + x = 0.5 - x; + hi = (double)x; + lo = x - hi; + lo = lo * (pi_lo + pi_hi) + hi * pi_lo; + hi *= pi_hi; + _2sumF(hi, lo); + t = - __kernel_tanl(hi, lo, 1); + } else + t = 1; + + return (t); +} + +volatile static const double vzero = 0; + +long double +tanpil(long double x) +{ + long double ax, hi, lo, xf; + uint32_t ix; + + ax = fabsl(ax); + + if (ax < 1) { + if (ax < 0.5) { + if (ax < 0x1p-60) { + if (x == 0) + return (x); + hi = (double)x; + hi *= 0x1p113L + lo = x * 0x1p113L - hi; + t = (pi_lo + pi_hi) * lo + pi_lo * lo + + pi_hi * hi; + return (t * 0x1p-113L); + } + t = __kernel_tanpil(ax); + } else if (ax == 0.5) + return ((ax - ax) / (ax - ax)); + else + t = -__kernel_tanpil(1 - ax); + return (copysignl(t, x)); + } + + if (ix < 0x1p112) { + xf = floorl(ax); + ax -= xf; + if (ax < 0.5) + t = ax == 0 ? 0 : __kernel_tanpil(ax); + else if (ax == 0.5) + return ((ax - ax) / (ax - ax)); + else + t = -__kernel_tanpil(1 - ax); + return (copysignl(t, x)); + } + + /* x = +-inf or nan. */ + if (isinf(x) || isnan(x)) + return (vzero / vzero); + + /* + * |x| >= 0x1p53 is always an integer, so return +-0. + */ + return (copysignl(0, x)); +} diff --git a/lib/msun/ld80/k_cospil.h b/lib/msun/ld80/k_cospil.h new file mode 100644 index 000000000000..6e13ef02aea2 --- /dev/null +++ b/lib/msun/ld80/k_cospil.h @@ -0,0 +1,42 @@ +/*- + * Copyright (c) 2017 Steven G. Kargl + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice unmodified, this list of conditions, and the following + * disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +/* + * See ../src/k_cospi.c for implementation details. + */ + +static inline long double +__kernel_cospil(long double x) +{ + long double hi, lo; + + hi = (float)x; + lo = x - hi; + lo = lo * (pi_lo + pi_hi) + hi * pi_lo; + hi *= pi_hi; + _2sumF(hi, lo); + return (__kernel_cosl(hi, lo)); +} diff --git a/lib/msun/ld80/k_sinpil.h b/lib/msun/ld80/k_sinpil.h new file mode 100644 index 000000000000..00241b932e9e --- /dev/null +++ b/lib/msun/ld80/k_sinpil.h @@ -0,0 +1,42 @@ +/*- + * Copyright (c) 2017 Steven G. Kargl + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice unmodified, this list of conditions, and the following + * disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +/* + * See ../src/k_sinpi.c for implementation details. + */ + +static inline long double +__kernel_sinpil(long double x) +{ + long double hi, lo; + + hi = (float)x; + lo = x - hi; + lo = lo * (pi_lo + pi_hi) + hi * pi_lo; + hi *= pi_hi; + _2sumF(hi, lo); + return (__kernel_sinl(hi, lo, 1)); +} diff --git a/lib/msun/ld80/s_cospil.c b/lib/msun/ld80/s_cospil.c new file mode 100644 index 000000000000..199479e9eaf9 --- /dev/null +++ b/lib/msun/ld80/s_cospil.c @@ -0,0 +1,129 @@ +/*- + * Copyright (c) 2017 Steven G. Kargl + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice unmodified, this list of conditions, and the following + * disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +/* + * See ../src/s_cospi.c for implementation details. + */ + +#ifdef __i386__ +#include +#endif +#include + +#include "fpmath.h" +#include "math.h" +#include "math_private.h" + +static const double +pi_hi = 3.1415926814079285e+00, /* 0x400921fb 0x58000000 */ +pi_lo =-2.7818135228334233e-08; /* 0xbe5dde97 0x3dcb3b3a */ + +#include "k_cospil.h" +#include "k_sinpil.h" + +volatile static const double vzero = 0; + +long double +cospil(long double x) +{ + long double ax, c; + uint64_t lx, m; + uint32_t j0; + uint16_t hx, ix; + + EXTRACT_LDBL80_WORDS(hx, lx, x); + ix = hx & 0x7fff; + INSERT_LDBL80_WORDS(ax, ix, lx); + + ENTERI(); + + if (ix < 0x3fff) { /* |x| < 1 */ + if (ix < 0x3ffd) { /* |x| < 0.25 */ + if (ix < 0x3fdd) { /* |x| < 0x1p-34 */ + if ((int)x == 0) + RETURNI(1); + } + RETURNI(__kernel_cospil(ax)); + } + + if (ix < 0x3ffe) /* |x| < 0.5 */ + c = __kernel_sinpil(0.5 - ax); + else if (lx < 0xc000000000000000ull) { /* |x| < 0.75 */ + if (ax == 0.5) + RETURNI(0); + c = -__kernel_sinpil(ax - 0.5); + } else + c = -__kernel_cospil(1 - ax); + RETURNI(c); + } + + if (ix < 0x403e) { /* 1 <= |x| < 0x1p63 */ + /* Determine integer part of ax. */ + j0 = ix - 0x3fff + 1; + if (j0 < 32) { + lx = (lx >> 32) << 32; + lx &= ~(((lx << 32)-1) >> j0); + } else { + m = (uint64_t)-1 >> (j0 + 1); + if (lx & m) lx &= ~m; + } + INSERT_LDBL80_WORDS(x, ix, lx); + + ax -= x; + EXTRACT_LDBL80_WORDS(ix, lx, ax); + + if (ix < 0x3ffe) { /* |x| < 0.5 */ + if (ix < 0x3ffd) /* |x| < 0.25 */ + c = ix == 0 ? 1 : __kernel_cospil(ax); + else + c = __kernel_sinpil(0.5 - ax); + + } else { + if (lx < 0xc000000000000000ull) { /* |x| < 0.75 */ + if (ax == 0.5) + RETURNI(0); + c = -__kernel_sinpil(ax - 0.5); + } else + c = -__kernel_cospil(1 - ax); + } + + if (j0 > 40) + x -= 0x1p40; + if (j0 > 30) + x -= 0x1p30; + j0 = (uint32_t)x; + + RETURNI(j0 & 1 ? -c : c); + } + + if (ix >= 0x7fff) + RETURNI(vzero / vzero); + + /* + * |x| >= 0x1p63 is always an even integer, so return 1. + */ + RETURNI(1); +} diff --git a/lib/msun/ld80/s_sinpil.c b/lib/msun/ld80/s_sinpil.c new file mode 100644 index 000000000000..4cefa92352e1 --- /dev/null +++ b/lib/msun/ld80/s_sinpil.c @@ -0,0 +1,140 @@ +/*- + * Copyright (c) 2017 Steven G. Kargl + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice unmodified, this list of conditions, and the following + * disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +/* + * See ../src/s_sinpi.c for implementation details. + */ + +#ifdef __i386__ +#include +#endif +#include + +#include "fpmath.h" +#include "math.h" +#include "math_private.h" + +static const union IEEEl2bits +pi_hi_u = LD80C(0xc90fdaa200000000, 1, 3.14159265346825122833e+00L), +pi_lo_u = LD80C(0x85a308d313198a2e, -33, 1.21542010130123852029e-10L); +#define pi_hi (pi_hi_u.e) +#define pi_lo (pi_lo_u.e) + +#include "k_cospil.h" +#include "k_sinpil.h" + +volatile static const double vzero = 0; + +long double +sinpil(long double x) +{ + long double ax, hi, lo, s; + uint64_t lx, m; + uint32_t j0; + uint16_t hx, ix; + + EXTRACT_LDBL80_WORDS(hx, lx, x); + ix = hx & 0x7fff; + INSERT_LDBL80_WORDS(ax, ix, lx); + + ENTERI(); + + if (ix < 0x3fff) { /* |x| < 1 */ + if (ix < 0x3ffd) { /* |x| < 0.25 */ + if (ix < 0x3fdd) { /* |x| < 0x1p-34 */ + if (x == 0) + RETURNI(x); + INSERT_LDBL80_WORDS(hi, hx, + lx & 0xffffffff00000000ull); + hi *= 0x1p63L; + lo = x * 0x1p63L - hi; + s = (pi_lo + pi_hi) * lo + pi_lo * hi + + pi_hi * hi; + RETURNI(s * 0x1p-63L); + } + s = __kernel_sinpil(ax); + RETURNI((hx & 0x8000) ? -s : s); + } + + if (ix < 0x3ffe) /* |x| < 0.5 */ + s = __kernel_cospil(0.5 - ax); + else if (lx < 0xc000000000000000ull) /* |x| < 0.75 */ + s = __kernel_cospil(ax - 0.5); + else + s = __kernel_sinpil(1 - ax); + RETURNI((hx & 0x8000) ? -s : s); + } + + if (ix < 0x403e) { /* 1 <= |x| < 0x1p63 */ + /* Determine integer part of ax. */ + j0 = ix - 0x3fff + 1; + if (j0 < 32) { + lx = (lx >> 32) << 32; + lx &= ~(((lx << 32)-1) >> j0); + } else { + m = (uint64_t)-1 >> (j0 + 1); + if (lx & m) lx &= ~m; + } + INSERT_LDBL80_WORDS(x, ix, lx); + + ax -= x; + EXTRACT_LDBL80_WORDS(ix, lx, ax); + + if (ix == 0) { + s = 0; + } else { + if (ix < 0x3ffe) { /* |x| < 0.5 */ + if (ix < 0x3ffd) /* |x| < 0.25 */ + s = __kernel_sinpil(ax); + else + s = __kernel_cospil(0.5 - ax); + } else { + /* |x| < 0.75 */ + if (lx < 0xc000000000000000ull) + s = __kernel_cospil(ax - 0.5); + else + s = __kernel_sinpil(1 - ax); + } + + if (j0 > 40) + x -= 0x1p40; + if (j0 > 30) + x -= 0x1p30; + j0 = (uint32_t)x; + if (j0 & 1) s = -s; + } + RETURNI((hx & 0x8000) ? -s : s); + } + + /* x = +-inf or nan. */ + if (ix >= 0x7fff) + RETURNI(vzero / vzero); + + /* + * |x| >= 0x1p63 is always an integer, so return +-0. + */ + RETURNI(copysignl(0, x)); +} diff --git a/lib/msun/ld80/s_tanpil.c b/lib/msun/ld80/s_tanpil.c new file mode 100644 index 000000000000..02451e562025 --- /dev/null +++ b/lib/msun/ld80/s_tanpil.c @@ -0,0 +1,139 @@ +/*- + * Copyright (c) 2017 Steven G. Kargl + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice unmodified, this list of conditions, and the following + * disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +/* + * See ../src/s_tanpi.c for implementation details. + */ + +#ifdef __i386__ +#include +#endif +#include + +#include "fpmath.h" +#include "math.h" +#include "math_private.h" + +static const double +pi_hi = 3.1415926814079285e+00, /* 0x400921fb 0x58000000 */ +pi_lo = -2.7818135228334233e-08; /* 0xbe5dde97 0x3dcb3b3a */ + +static inline long double +__kernel_tanpil(long double x) +{ + long double hi, lo, t; + + if (x < 0.25) { + hi = (float)x; + lo = x - hi; + lo = lo * (pi_lo + pi_hi) + hi * pi_lo; + hi *= pi_hi; + _2sumF(hi, lo); + t = __kernel_tanl(hi, lo, -1); + } else if (x > 0.25) { + x = 0.5 - x; + hi = (float)x; + lo = x - hi; + lo = lo * (pi_lo + pi_hi) + hi * pi_lo; + hi *= pi_hi; + _2sumF(hi, lo); + t = - __kernel_tanl(hi, lo, 1); + } else + t = 1; + + return (t); +} + +volatile static const double vzero = 0; + +long double +tanpil(long double x) +{ + long double ax, hi, lo, t; + uint64_t lx, m; + uint32_t j0; + uint16_t hx, ix; + + EXTRACT_LDBL80_WORDS(hx, lx, x); + ix = hx & 0x7fff; + INSERT_LDBL80_WORDS(ax, ix, lx); + + ENTERI(); + + if (ix < 0x3fff) { /* |x| < 1 */ + if (ix < 0x3ffe) { /* |x| < 0.5 */ + if (ix < 0x3fdd) { /* |x| < 0x1p-34 */ + if (x == 0) + RETURNI(x); + INSERT_LDBL80_WORDS(hi, hx, + lx & 0xffffffff00000000ull); + hi *= 0x1p63L; + lo = x * 0x1p63L - hi; + t = (pi_lo + pi_hi) * lo + pi_lo * hi + + pi_hi * hi; + RETURNI(t * 0x1p-63L); + } + t = __kernel_tanpil(ax); + } else if (ax == 0.5) + RETURNI((ax - ax) / (ax - ax)); + else + t = -__kernel_tanpil(1 - ax); + RETURNI((hx & 0x8000) ? -t : t); + } + + if (ix < 0x403e) { /* 1 <= |x| < 0x1p63 */ + /* Determine integer part of ax. */ + j0 = ix - 0x3fff + 1; + if (j0 < 32) { + lx = (lx >> 32) << 32; + lx &= ~(((lx << 32)-1) >> j0); + } else { + m = (uint64_t)-1 >> (j0 + 1); + if (lx & m) lx &= ~m; + } + INSERT_LDBL80_WORDS(x, ix, lx); + + ax -= x; + EXTRACT_LDBL80_WORDS(ix, lx, ax); + + if (ix < 0x3ffe) /* |x| < 0.5 */ + t = ax == 0 ? 0 : __kernel_tanpil(ax); + else if (ax == 0.5) + RETURNI((ax - ax) / (ax - ax)); + else + t = -__kernel_tanpil(1 - ax); + RETURNI((hx & 0x8000) ? -t : t); + } + + /* x = +-inf or nan. */ + if (ix >= 0x7fff) + RETURNI(vzero / vzero); + + /* + * |x| >= 0x1p63 is always an integer, so return +-0. + */ + RETURNI(copysignl(0, x)); +} diff --git a/lib/msun/man/cospi.3 b/lib/msun/man/cospi.3 new file mode 100644 index 000000000000..b76e1858ed33 --- /dev/null +++ b/lib/msun/man/cospi.3 @@ -0,0 +1,111 @@ +.\" Copyright (c) 2017 Steven G. Kargl +.\" All rights reserved. +.\" +.\" Redistribution and use in source and binary forms, with or without +.\" modification, are permitted provided that the following conditions +.\" are met: +.\" 1. Redistributions of source code must retain the above copyright +.\" notice, this list of conditions and the following disclaimer. +.\" 2. Redistributions in binary form must reproduce the above copyright +.\" notice, this list of conditions and the following disclaimer in the +.\" documentation and/or other materials provided with the distribution. +.\" +.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND +.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE +.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS +.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) +.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT +.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY +.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF +.\" SUCH DAMAGE. +.\" +.\" $FreeBSD$ +.\" +.Dd April 1, 2017 +.Dt COSPI 3 +.Os +.Sh NAME +.Nm cospi , +.Nm cospif , +.Nm cospil +.Nd half\(encycle cosine functions +.Sh LIBRARY +.Lb libm +.Sh SYNOPSIS +.In math.h +.Ft double +.Fn cospi "double x" +.Ft float +.Fn cospif "float x" +.Ft long double +.Fn cospil "long double x" +.Sh DESCRIPTION +The +.Fn cospi , +.Fn cospif , +and +.Fn cospil +functions compute the cosine of +.Fa "\(*p \(mu x" . +and measure angles in half-cycles. +.Sh RETURN VALUES +The +.Fn cospi , +.Fn cospif , +and +.Fn cospil +functions returns +.Fn cos "\(*p \(mu x" . +If \*(Bax\*(Ba \*(Ge 2^(p - 1) +where p is the floating\(enpoint precision of +.Ar x , +then the returned value is 1 and it has no significance. +.Sh SPECIAL VALUES +.Bl -tag +.It +.Fn cospi \*(Pm0 +returns 1. +.It +.Fn cospi \*(Pmn/2 +returns 0 for positive integers +.Ar n . +.It +.Fn cospi n +returns 1 for even integers +.Ar n . +.It +.Fn cospi n +returns \-1 for odd integers +.Ar n . +.It +.Fn cospi \*(Pm\(if +return an \*(Na and raises an FE_INVALID exception. +.It +.Fn cospi \*(Na +return an \*(Na and raises an FE_INVALID exception. +.El +.Sh SEE ALSO +.Xr cos 3 , +.Xr fenv 3 , +.Xr math 3 , +.Xr sin 3 , +.Xr sinpi 3 , +.Xr tan 3 , +.Xr tanpi 3 +.Sh AUTHORS +The half\(encycle trignometric functions were written by +.An Steven G. Kargl Aq Mt kargl@FreeBSD.org . +.Sh STANDARDS +These functions conform to +IEEE Std 754\(tm\(en2008 , +\(dqIEEE Standard for Floating-Point Arithmetic\(dq +and to +ISO/IEC TS 18661-4 , +\(dqInformation technology \(em Programming languages, their environments, +and system software interfaces \(em Floating\(enpoint extensions for +C\(dq \(em Part 4: Supplementary functions. + + diff --git a/lib/msun/man/sinpi.3 b/lib/msun/man/sinpi.3 new file mode 100644 index 000000000000..4ae162bd1c74 --- /dev/null +++ b/lib/msun/man/sinpi.3 @@ -0,0 +1,102 @@ +.\" Copyright (c) 2017 Steven G. Kargl +.\" All rights reserved. +.\" +.\" Redistribution and use in source and binary forms, with or without +.\" modification, are permitted provided that the following conditions +.\" are met: +.\" 1. Redistributions of source code must retain the above copyright +.\" notice, this list of conditions and the following disclaimer. +.\" 2. Redistributions in binary form must reproduce the above copyright +.\" notice, this list of conditions and the following disclaimer in the +.\" documentation and/or other materials provided with the distribution. +.\" +.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND +.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE +.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS +.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) +.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT +.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY +.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF +.\" SUCH DAMAGE. +.\" +.\" $FreeBSD$ +.\" +.Dd April 1, 2017 +.Dt SINPI 3 +.Os +.Sh NAME +.Nm sinpi , +.Nm sinpif , +.Nm sinpil +.Nd half\(encycle sine functions +.Sh LIBRARY +.Lb libm +.Sh SYNOPSIS +.In math.h +.Ft double +.Fn sinpi "double x" +.Ft float +.Fn sinpif "float x" +.Ft long double +.Fn sinpil "long double x" +.Sh DESCRIPTION +The +.Fn sinpi , +.Fn sinpif , +and +.Fn sinpil +functions compute the sine of +.Fa "\(*p \(mu x" . +and measure angles in half-cycles. +.Sh RETURN VALUES +The +.Fn sinpi , +.Fn sinpif , +and +.Fn sinpil +functions returns +.Fn sin "\(*p \(mu x" . +If \*(Bax\*(Ba \*(Ge 2^(p - 1) +where p is the floating\(enpoint precision of +.Ar x , +then the returned value is \*(Pm0 and it has no significance. +.Sh SPECIAL VALUES +.Bl -tag +.It +.Fn sinpi \*(Pm0 +returns \*(Pm0. +.It +.Fn sinpi \*(Pmn +returns \*(Pm0 for positive integers +.Ar n . +.It +.Fn sinpi \*(Pm\(if +return an \*(Na and raises an FE_INVALID exception. +.It +.Fn sinpi \*(Na +return an \*(Na and raises an FE_INVALID exception. +.El +.Sh SEE ALSO +.Xr cos 3 , +.Xr cospi 3 , +.Xr fenv 3 , +.Xr math 3 , +.Xr sin 3 , +.Xr tan 3 , +.Xr tanpi 3 +.Sh AUTHORS +The half\(encycle trignometric functions were written by +.An Steven G. Kargl Aq Mt kargl@FreeBSD.org . +.Sh STANDARDS +These functions conform to +IEEE Std 754\(tm\(en2008 , +\(dqIEEE Standard for Floating-Point Arithmetic\(dq +and to +ISO/IEC TS 18661-4 , +\(dqInformation technology \(em Programming languages, their environments, +and system software interfaces \(em Floating\(enpoint extensions for +C\(dq \(em Part 4: Supplementary functions. + diff --git a/lib/msun/man/tanpi.3 b/lib/msun/man/tanpi.3 new file mode 100644 index 000000000000..9cbbf4efaf23 --- /dev/null +++ b/lib/msun/man/tanpi.3 @@ -0,0 +1,106 @@ +.\" Copyright (c) 2017 Steven G. Kargl +.\" All rights reserved. +.\" +.\" Redistribution and use in source and binary forms, with or without +.\" modification, are permitted provided that the following conditions +.\" are met: +.\" 1. Redistributions of source code must retain the above copyright +.\" notice, this list of conditions and the following disclaimer. +.\" 2. Redistributions in binary form must reproduce the above copyright +.\" notice, this list of conditions and the following disclaimer in the +.\" documentation and/or other materials provided with the distribution. +.\" +.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND +.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE +.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS +.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) +.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT +.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY +.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF +.\" SUCH DAMAGE. +.\" +.\" $FreeBSD$ +.\" +.Dd April 1, 2017 +.Dt TANPI 3 +.Os +.Sh NAME +.Nm tanpi , +.Nm tanpif , +.Nm tanpil +.Nd half\(encycle tangent functions +.Sh LIBRARY +.Lb libm +.Sh SYNOPSIS +.In math.h +.Ft double +.Fn tanpi "double x" +.Ft float +.Fn tanpif "float x" +.Ft long double +.Fn tanpil "long double x" +.Sh DESCRIPTION +The +.Fn tanpi , +.Fn tanpif , +and +.Fn tanpil +functions compute the tangent of +.Fa "\(*p \(mu x" +and measure angles in half-cycles. +.Sh RETURN VALUES +The +.Fn tanpi , +.Fn tanpif , +and +.Fn tanpil +functions returns +.Fn tan "\(*p \(mu x" . +If \*(Bax\*(Ba \*(Ge 2^(p - 1) +where p is the floating\(enpoint precision of +.Ar x , +then the returned value is \*(Pm0 and it has no significance. +.Sh SPECIAL VALUES +.Bl -tag +.It +.Fn tanpi \*(Pm0 +returns \*(Pm0. +.It +.Fn tanpi \*(Pmn +returns \*(Pm0 for positive integers +.Ar n . +.It +.Fn tanpi \*(Pmn/2 +returns \*(Na for n > 0 and raises an FE_INVALID exception. +.It +.Fn tanpi \*(Pm\(if +return an \*(Na and raises an FE_INVALID exception. +.It +.Fn tanpi \*(Na +return an \*(Na and raises an FE_INVALID exception. +.El +.Sh SEE ALSO +.Xr cos 3 , +.Xr cospi 3 , +.Xr fenv 3 , +.Xr math 3 , +.Xr sin 3 , +.Xr sinpi 3 , +.Xr tan 3 , +.Sh AUTHORS +The half\(encycle trignometric functions were written by +.An Steven G. Kargl Aq Mt kargl@FreeBSD.org . +.Sh STANDARDS +These functions conform to +IEEE Std 754\(tm\(en2008 , +\(dqIEEE Standard for Floating-Point Arithmetic\(dq +and to +ISO/IEC TS 18661-4 , +\(dqInformation technology \(em Programming languages, their environments, +and system software interfaces \(em Floating\(enpoint extensions for +C\(dq \(em Part 4: Supplementary functions. + + diff --git a/lib/msun/src/k_cospi.h b/lib/msun/src/k_cospi.h new file mode 100644 index 000000000000..32985be809ad --- /dev/null +++ b/lib/msun/src/k_cospi.h @@ -0,0 +1,44 @@ +/*- + * Copyright (c) 2017 Steven G. Kargl + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice unmodified, this list of conditions, and the following + * disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +/* + * The basic kernel for x in [0,0.25]. To use the kernel for cos(x), the + * argument to __kernel_cospi() must be multiplied by pi. + */ + +static inline double +__kernel_cospi(double x) +{ + double_t hi, lo; + + hi = (float)x; + lo = x - hi; + lo = lo * (pi_lo + pi_hi) + hi * pi_lo; + hi *= pi_hi; + _2sumF(hi, lo); + return (__kernel_cos(hi, lo)); +} + diff --git a/lib/msun/src/k_sinpi.h b/lib/msun/src/k_sinpi.h new file mode 100644 index 000000000000..66152ba9931f --- /dev/null +++ b/lib/msun/src/k_sinpi.h @@ -0,0 +1,43 @@ +/*- + * Copyright (c) 2017 Steven G. Kargl + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice unmodified, this list of conditions, and the following + * disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +/* + * The basic kernel for x in [0,0.25]. To use the kernel for sin(x), the + * argument to __kernel_sinpi() must be multiplied by pi. + */ + +static inline double +__kernel_sinpi(double x) +{ + double_t hi, lo; + + hi = (float)x; + lo = x - hi; + lo = lo * (pi_lo + pi_hi) + hi * pi_lo; + hi *= pi_hi; + _2sumF(hi, lo); + return (__kernel_sin(hi, lo, 1)); +} diff --git a/lib/msun/src/math.h b/lib/msun/src/math.h index 8156094313e4..98601f825bbe 100644 --- a/lib/msun/src/math.h +++ b/lib/msun/src/math.h @@ -1,515 +1,524 @@ /* * ==================================================== * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. * * Developed at SunPro, a Sun Microsystems, Inc. business. * Permission to use, copy, modify, and distribute this * software is freely granted, provided that this notice * is preserved. * ==================================================== */ /* * from: @(#)fdlibm.h 5.1 93/09/24 * $FreeBSD$ */ #ifndef _MATH_H_ #define _MATH_H_ #include #include #include /* * ANSI/POSIX */ extern const union __infinity_un { unsigned char __uc[8]; double __ud; } __infinity; extern const union __nan_un { unsigned char __uc[sizeof(float)]; float __uf; } __nan; #if __GNUC_PREREQ__(3, 3) #define __MATH_BUILTIN_CONSTANTS #endif #if __GNUC_PREREQ__(3, 0) #define __MATH_BUILTIN_RELOPS #endif #ifdef __MATH_BUILTIN_CONSTANTS #define HUGE_VAL __builtin_huge_val() #else #define HUGE_VAL (__infinity.__ud) #endif #if __ISO_C_VISIBLE >= 1999 #define FP_ILOGB0 (-__INT_MAX) #define FP_ILOGBNAN __INT_MAX #ifdef __MATH_BUILTIN_CONSTANTS #define HUGE_VALF __builtin_huge_valf() #define HUGE_VALL __builtin_huge_vall() #define INFINITY __builtin_inff() #define NAN __builtin_nanf("") #else #define HUGE_VALF (float)HUGE_VAL #define HUGE_VALL (long double)HUGE_VAL #define INFINITY HUGE_VALF #define NAN (__nan.__uf) #endif /* __MATH_BUILTIN_CONSTANTS */ #define MATH_ERRNO 1 #define MATH_ERREXCEPT 2 #define math_errhandling MATH_ERREXCEPT #define FP_FAST_FMAF 1 /* Symbolic constants to classify floating point numbers. */ #define FP_INFINITE 0x01 #define FP_NAN 0x02 #define FP_NORMAL 0x04 #define FP_SUBNORMAL 0x08 #define FP_ZERO 0x10 #if __STDC_VERSION__ >= 201112L || __has_extension(c_generic_selections) #define __fp_type_select(x, f, d, ld) __extension__ _Generic((x), \ float: f(x), \ double: d(x), \ long double: ld(x), \ volatile float: f(x), \ volatile double: d(x), \ volatile long double: ld(x), \ volatile const float: f(x), \ volatile const double: d(x), \ volatile const long double: ld(x), \ const float: f(x), \ const double: d(x), \ const long double: ld(x)) #elif __GNUC_PREREQ__(3, 1) && !defined(__cplusplus) #define __fp_type_select(x, f, d, ld) __builtin_choose_expr( \ __builtin_types_compatible_p(__typeof(x), long double), ld(x), \ __builtin_choose_expr( \ __builtin_types_compatible_p(__typeof(x), double), d(x), \ __builtin_choose_expr( \ __builtin_types_compatible_p(__typeof(x), float), f(x), (void)0))) #else #define __fp_type_select(x, f, d, ld) \ ((sizeof(x) == sizeof(float)) ? f(x) \ : (sizeof(x) == sizeof(double)) ? d(x) \ : ld(x)) #endif #define fpclassify(x) \ __fp_type_select(x, __fpclassifyf, __fpclassifyd, __fpclassifyl) #define isfinite(x) __fp_type_select(x, __isfinitef, __isfinite, __isfinitel) #define isinf(x) __fp_type_select(x, __isinff, __isinf, __isinfl) #define isnan(x) \ __fp_type_select(x, __inline_isnanf, __inline_isnan, __inline_isnanl) #define isnormal(x) __fp_type_select(x, __isnormalf, __isnormal, __isnormall) #ifdef __MATH_BUILTIN_RELOPS #define isgreater(x, y) __builtin_isgreater((x), (y)) #define isgreaterequal(x, y) __builtin_isgreaterequal((x), (y)) #define isless(x, y) __builtin_isless((x), (y)) #define islessequal(x, y) __builtin_islessequal((x), (y)) #define islessgreater(x, y) __builtin_islessgreater((x), (y)) #define isunordered(x, y) __builtin_isunordered((x), (y)) #else #define isgreater(x, y) (!isunordered((x), (y)) && (x) > (y)) #define isgreaterequal(x, y) (!isunordered((x), (y)) && (x) >= (y)) #define isless(x, y) (!isunordered((x), (y)) && (x) < (y)) #define islessequal(x, y) (!isunordered((x), (y)) && (x) <= (y)) #define islessgreater(x, y) (!isunordered((x), (y)) && \ ((x) > (y) || (y) > (x))) #define isunordered(x, y) (isnan(x) || isnan(y)) #endif /* __MATH_BUILTIN_RELOPS */ #define signbit(x) __fp_type_select(x, __signbitf, __signbit, __signbitl) typedef __double_t double_t; typedef __float_t float_t; #endif /* __ISO_C_VISIBLE >= 1999 */ /* * XOPEN/SVID */ #if __BSD_VISIBLE || __XSI_VISIBLE #define M_E 2.7182818284590452354 /* e */ #define M_LOG2E 1.4426950408889634074 /* log 2e */ #define M_LOG10E 0.43429448190325182765 /* log 10e */ #define M_LN2 0.69314718055994530942 /* log e2 */ #define M_LN10 2.30258509299404568402 /* log e10 */ #define M_PI 3.14159265358979323846 /* pi */ #define M_PI_2 1.57079632679489661923 /* pi/2 */ #define M_PI_4 0.78539816339744830962 /* pi/4 */ #define M_1_PI 0.31830988618379067154 /* 1/pi */ #define M_2_PI 0.63661977236758134308 /* 2/pi */ #define M_2_SQRTPI 1.12837916709551257390 /* 2/sqrt(pi) */ #define M_SQRT2 1.41421356237309504880 /* sqrt(2) */ #define M_SQRT1_2 0.70710678118654752440 /* 1/sqrt(2) */ #define MAXFLOAT ((float)3.40282346638528860e+38) extern int signgam; #endif /* __BSD_VISIBLE || __XSI_VISIBLE */ #if __BSD_VISIBLE #if 0 /* Old value from 4.4BSD-Lite math.h; this is probably better. */ #define HUGE HUGE_VAL #else #define HUGE MAXFLOAT #endif #endif /* __BSD_VISIBLE */ /* * Most of these functions depend on the rounding mode and have the side * effect of raising floating-point exceptions, so they are not declared * as __pure2. In C99, FENV_ACCESS affects the purity of these functions. */ __BEGIN_DECLS /* * ANSI/POSIX */ int __fpclassifyd(double) __pure2; int __fpclassifyf(float) __pure2; int __fpclassifyl(long double) __pure2; int __isfinitef(float) __pure2; int __isfinite(double) __pure2; int __isfinitel(long double) __pure2; int __isinff(float) __pure2; int __isinf(double) __pure2; int __isinfl(long double) __pure2; int __isnormalf(float) __pure2; int __isnormal(double) __pure2; int __isnormall(long double) __pure2; int __signbit(double) __pure2; int __signbitf(float) __pure2; int __signbitl(long double) __pure2; static __inline int __inline_isnan(__const double __x) { return (__x != __x); } static __inline int __inline_isnanf(__const float __x) { return (__x != __x); } static __inline int __inline_isnanl(__const long double __x) { return (__x != __x); } /* * Define the following aliases, for compatibility with glibc and CUDA. */ #define __isnan __inline_isnan #define __isnanf __inline_isnanf /* * Version 2 of the Single UNIX Specification (UNIX98) defined isnan() and * isinf() as functions taking double. C99, and the subsequent POSIX revisions * (SUSv3, POSIX.1-2001, define it as a macro that accepts any real floating * point type. If we are targeting SUSv2 and C99 or C11 (or C++11) then we * expose the newer definition, assuming that the language spec takes * precedence over the operating system interface spec. */ #if __XSI_VISIBLE > 0 && __XSI_VISIBLE < 600 && __ISO_C_VISIBLE < 1999 #undef isinf #undef isnan int isinf(double); int isnan(double); #endif double acos(double); double asin(double); double atan(double); double atan2(double, double); double cos(double); double sin(double); double tan(double); double cosh(double); double sinh(double); double tanh(double); double exp(double); double frexp(double, int *); /* fundamentally !__pure2 */ double ldexp(double, int); double log(double); double log10(double); double modf(double, double *); /* fundamentally !__pure2 */ double pow(double, double); double sqrt(double); double ceil(double); double fabs(double) __pure2; double floor(double); double fmod(double, double); /* * These functions are not in C90. */ #if __BSD_VISIBLE || __ISO_C_VISIBLE >= 1999 || __XSI_VISIBLE double acosh(double); double asinh(double); double atanh(double); double cbrt(double); double erf(double); double erfc(double); double exp2(double); double expm1(double); double fma(double, double, double); double hypot(double, double); int ilogb(double) __pure2; double lgamma(double); long long llrint(double); long long llround(double); double log1p(double); double log2(double); double logb(double); long lrint(double); long lround(double); double nan(const char *) __pure2; double nextafter(double, double); double remainder(double, double); double remquo(double, double, int *); double rint(double); #endif /* __BSD_VISIBLE || __ISO_C_VISIBLE >= 1999 || __XSI_VISIBLE */ #if __BSD_VISIBLE || __XSI_VISIBLE double j0(double); double j1(double); double jn(int, double); double y0(double); double y1(double); double yn(int, double); #if __XSI_VISIBLE <= 500 || __BSD_VISIBLE double gamma(double); #endif #if __XSI_VISIBLE <= 600 || __BSD_VISIBLE double scalb(double, double); #endif #endif /* __BSD_VISIBLE || __XSI_VISIBLE */ #if __BSD_VISIBLE || __ISO_C_VISIBLE >= 1999 double copysign(double, double) __pure2; double fdim(double, double); double fmax(double, double) __pure2; double fmin(double, double) __pure2; double nearbyint(double); double round(double); double scalbln(double, long); double scalbn(double, int); double tgamma(double); double trunc(double); #endif /* * BSD math library entry points */ #if __BSD_VISIBLE double drem(double, double); int finite(double) __pure2; int isnanf(float) __pure2; /* * Reentrant version of gamma & lgamma; passes signgam back by reference * as the second argument; user must allocate space for signgam. */ double gamma_r(double, int *); double lgamma_r(double, int *); /* * IEEE Test Vector */ double significand(double); #endif /* __BSD_VISIBLE */ /* float versions of ANSI/POSIX functions */ #if __ISO_C_VISIBLE >= 1999 float acosf(float); float asinf(float); float atanf(float); float atan2f(float, float); float cosf(float); float sinf(float); float tanf(float); float coshf(float); float sinhf(float); float tanhf(float); float exp2f(float); float expf(float); float expm1f(float); float frexpf(float, int *); /* fundamentally !__pure2 */ int ilogbf(float) __pure2; float ldexpf(float, int); float log10f(float); float log1pf(float); float log2f(float); float logf(float); float modff(float, float *); /* fundamentally !__pure2 */ float powf(float, float); float sqrtf(float); float ceilf(float); float fabsf(float) __pure2; float floorf(float); float fmodf(float, float); float roundf(float); float erff(float); float erfcf(float); float hypotf(float, float); float lgammaf(float); float tgammaf(float); float acoshf(float); float asinhf(float); float atanhf(float); float cbrtf(float); float logbf(float); float copysignf(float, float) __pure2; long long llrintf(float); long long llroundf(float); long lrintf(float); long lroundf(float); float nanf(const char *) __pure2; float nearbyintf(float); float nextafterf(float, float); float remainderf(float, float); float remquof(float, float, int *); float rintf(float); float scalblnf(float, long); float scalbnf(float, int); float truncf(float); float fdimf(float, float); float fmaf(float, float, float); float fmaxf(float, float) __pure2; float fminf(float, float) __pure2; #endif /* * float versions of BSD math library entry points */ #if __BSD_VISIBLE float dremf(float, float); int finitef(float) __pure2; float gammaf(float); float j0f(float); float j1f(float); float jnf(int, float); float scalbf(float, float); float y0f(float); float y1f(float); float ynf(int, float); /* * Float versions of reentrant version of gamma & lgamma; passes * signgam back by reference as the second argument; user must * allocate space for signgam. */ float gammaf_r(float, int *); float lgammaf_r(float, int *); /* * float version of IEEE Test Vector */ float significandf(float); #endif /* __BSD_VISIBLE */ /* * long double versions of ISO/POSIX math functions */ #if __ISO_C_VISIBLE >= 1999 long double acoshl(long double); long double acosl(long double); long double asinhl(long double); long double asinl(long double); long double atan2l(long double, long double); long double atanhl(long double); long double atanl(long double); long double cbrtl(long double); long double ceill(long double); long double copysignl(long double, long double) __pure2; long double coshl(long double); long double cosl(long double); long double erfcl(long double); long double erfl(long double); long double exp2l(long double); long double expl(long double); long double expm1l(long double); long double fabsl(long double) __pure2; long double fdiml(long double, long double); long double floorl(long double); long double fmal(long double, long double, long double); long double fmaxl(long double, long double) __pure2; long double fminl(long double, long double) __pure2; long double fmodl(long double, long double); long double frexpl(long double, int *); /* fundamentally !__pure2 */ long double hypotl(long double, long double); int ilogbl(long double) __pure2; long double ldexpl(long double, int); long double lgammal(long double); long long llrintl(long double); long long llroundl(long double); long double log10l(long double); long double log1pl(long double); long double log2l(long double); long double logbl(long double); long double logl(long double); long lrintl(long double); long lroundl(long double); long double modfl(long double, long double *); /* fundamentally !__pure2 */ long double nanl(const char *) __pure2; long double nearbyintl(long double); long double nextafterl(long double, long double); double nexttoward(double, long double); float nexttowardf(float, long double); long double nexttowardl(long double, long double); long double powl(long double, long double); long double remainderl(long double, long double); long double remquol(long double, long double, int *); long double rintl(long double); long double roundl(long double); long double scalblnl(long double, long); long double scalbnl(long double, int); long double sinhl(long double); long double sinl(long double); long double sqrtl(long double); long double tanhl(long double); long double tanl(long double); long double tgammal(long double); long double truncl(long double); #endif /* __ISO_C_VISIBLE >= 1999 */ #if __BSD_VISIBLE long double lgammal_r(long double, int *); void sincos(double, double *, double *); void sincosf(float, float *, float *); void sincosl(long double, long double *, long double *); +double cospi(double); +float cospif(float); +long double cospil(long double); +double sinpi(double); +float sinpif(float); +long double sinpil(long double); +double tanpi(double); +float tanpif(float); +long double tanpil(long double); #endif __END_DECLS #endif /* !_MATH_H_ */ diff --git a/lib/msun/src/s_cospi.c b/lib/msun/src/s_cospi.c new file mode 100644 index 000000000000..92a5f467c97f --- /dev/null +++ b/lib/msun/src/s_cospi.c @@ -0,0 +1,151 @@ +/*- + * Copyright (c) 2017 Steven G. Kargl + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice unmodified, this list of conditions, and the following + * disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +/** + * cospi(x) computes cos(pi*x) without multiplication by pi (almost). First, + * note that cospi(-x) = cospi(x), so the algorithm considers only |x|. The + * method used depends on the magnitude of x. + * + * 1. For small |x|, cospi(x) = 1 with FE_INEXACT raised where a sloppy + * threshold is used. The threshold is |x| < 0x1pN with N = -(P/2+M). + * P is the precision of the floating-point type and M = 2 to 4. + * + * 2. For |x| < 1, argument reduction is not required and sinpi(x) is + * computed by calling a kernel that leverages the kernels for sin(x) + * ans cos(x). See k_sinpi.c and k_cospi.c for details. + * + * 3. For 1 <= |x| < 0x1p(P-1), argument reduction is required where + * |x| = j0 + r with j0 an integer and the remainder r satisfies + * 0 <= r < 1. With the given domain, a simplified inline floor(x) + * is used. Also, note the following identity + * + * cospi(x) = cos(pi*(j0+r)) + * = cos(pi*j0) * cos(pi*r) - sin(pi*j0) * sin(pi*r) + * = cos(pi*j0) * cos(pi*r) + * = +-cospi(r) + * + * If j0 is even, then cos(pi*j0) = 1. If j0 is odd, then cos(pi*j0) = -1. + * cospi(r) is then computed via an appropriate kernel. + * + * 4. For |x| >= 0x1p(P-1), |x| is integral and cospi(x) = 1. + * + * 5. Special cases: + * + * cospi(+-0) = 1. + * cospi(n.5) = 0 for n an integer. + * cospi(+-inf) = nan. Raises the "invalid" floating-point exception. + * cospi(nan) = nan. Raises the "invalid" floating-point exception. + */ + +#include "math.h" +#include "math_private.h" + +static const double +pi_hi = 3.1415926814079285e+00, /* 0x400921fb 0x58000000 */ +pi_lo =-2.7818135228334233e-08; /* 0xbe5dde97 0x3dcb3b3a */ + +#include "k_cospi.h" +#include "k_sinpi.h" + +volatile static const double vzero = 0; + +double +cospi(double x) +{ + double ax, c; + uint32_t hx, ix, j0, lx; + + EXTRACT_WORDS(hx, lx, x); + ix = hx & 0x7fffffff; + INSERT_WORDS(ax, ix, lx); + + if (ix < 0x3ff00000) { /* |x| < 1 */ + if (ix < 0x3fd00000) { /* |x| < 0.25 */ + if (ix < 0x3e200000) { /* |x| < 0x1p-29 */ + if ((int)ax == 0) + return (1); + } + return (__kernel_cospi(ax)); + } + + if (ix < 0x3fe00000) /* |x| < 0.5 */ + c = __kernel_sinpi(0.5 - ax); + else if (ix < 0x3fe80000){ /* |x| < 0.75 */ + if (ax == 0.5) + return (0); + c = -__kernel_sinpi(ax - 0.5); + } else + c = -__kernel_cospi(1 - ax); + return (c); + } + + if (ix < 0x43300000) { /* 1 <= |x| < 0x1p52 */ + /* Determine integer part of ax. */ + j0 = ((ix >> 20) & 0x7ff) - 0x3ff; + if (j0 < 20) { + ix &= ~(0x000fffff >> j0); + lx = 0; + } else { + lx &= ~((uint32_t)0xffffffff >> (j0 - 20)); + } + INSERT_WORDS(x, ix, lx); + + ax -= x; + EXTRACT_WORDS(ix, lx, ax); + + + if (ix < 0x3fe00000) { /* |x| < 0.5 */ + if (ix < 0x3fd00000) /* |x| < 0.25 */ + c = ix == 0 ? 1 : __kernel_cospi(ax); + else + c = __kernel_sinpi(0.5 - ax); + } else { + if (ix < 0x3fe80000) { /* |x| < 0.75 */ + if (ax == 0.5) + return (0); + c = -__kernel_sinpi(ax - 0.5); + } else + c = -__kernel_cospi(1 - ax); + } + + if (j0 > 30) + x -= 0x1p30; + j0 = (uint32_t)x; + return (j0 & 1 ? -c : c); + } + + if (ix >= 0x7f800000) + return (vzero / vzero); + + /* + * |x| >= 0x1p52 is always an even integer, so return 1. + */ + return (1); +} + +#if LDBL_MANT_DIG == 53 +__weak_reference(cospi, cospil); +#endif diff --git a/lib/msun/src/s_cospif.c b/lib/msun/src/s_cospif.c new file mode 100644 index 000000000000..4dd881395baf --- /dev/null +++ b/lib/msun/src/s_cospif.c @@ -0,0 +1,109 @@ +/*- + * Copyright (c) 2017 Steven G. Kargl + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice unmodified, this list of conditions, and the following + * disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +/* + * See ../src/s_cospi.c for implementation details. + */ +#define INLINE_KERNEL_SINDF +#define INLINE_KERNEL_COSDF + +#include "math.h" +#include "math_private.h" +#include "k_cosf.c" +#include "k_sinf.c" + +#define __kernel_cospif(x) (__kernel_cosdf(M_PI * (x))) +#define __kernel_sinpif(x) (__kernel_sindf(M_PI * (x))) + +volatile static const float vzero = 0; + +float +cospif(float x) +{ + float ax, c; + uint32_t ix, j0; + + GET_FLOAT_WORD(ix, x); + ix = ix & 0x7fffffff; + SET_FLOAT_WORD(ax, ix); + + if (ix < 0x3f800000) { /* |x| < 1 */ + if (ix < 0x3e800000) { /* |x| < 0.25 */ + if (ix < 0x38800000) { /* |x| < 0x1p-14 */ + /* Raise inexact iff != 0. */ + if ((int)ax == 0) + return (1); + } + return (__kernel_cospif(ax)); + } + + if (ix < 0x3f000000) /* |x| < 0.5 */ + c = __kernel_sinpif(0.5F - ax); + else if (ix < 0x3f400000) { /* |x| < 0.75 */ + if (ix == 0x3f000000) + return (0); + c = -__kernel_sinpif(ax - 0.5F); + } else + c = -__kernel_cospif(1 - ax); + return (c); + } + + if (ix < 0x4b000000) { /* 1 <= |x| < 0x1p23 */ + /* Determine integer part of ax. */ + j0 = ((ix >> 23) & 0xff) - 0x7f; + ix &= ~(0x007fffff >> j0); + SET_FLOAT_WORD(x, ix); + + ax -= x; + GET_FLOAT_WORD(ix, ax); + + if (ix < 0x3f000000) { /* |x| < 0.5 */ + if (ix < 0x3e800000) /* |x| < 0.25 */ + c = ix == 0 ? 1 : __kernel_cospif(ax); + else + c = __kernel_sinpif(0.5F - ax); + } else { + if (ix < 0x3f400000) { /* |x| < 0.75 */ + if (ix == 0x3f000000) + return (0); + c = -__kernel_sinpif(ax - 0.5F); + } else + c = -__kernel_cospif(1 - ax); + } + + j0 = (uint32_t)x; + return (j0 & 1 ? -c : c); + } + + /* x = +-inf or nan. */ + if (ix >= 0x7f800000) + return (vzero / vzero); + + /* + * |x| >= 0x1p23 is always an even integer, so return 1. + */ + return (1); +} diff --git a/lib/msun/src/s_sinpi.c b/lib/msun/src/s_sinpi.c new file mode 100644 index 000000000000..b9731112a7eb --- /dev/null +++ b/lib/msun/src/s_sinpi.c @@ -0,0 +1,168 @@ +/*- + * Copyright (c) 2017 Steven G. Kargl + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice unmodified, this list of conditions, and the following + * disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +/** + * sinpi(x) computes sin(pi*x) without multiplication by pi (almost). First, + * note that sinpi(-x) = -sinpi(x), so the algorithm considers only |x| and + * includes reflection symmetry by considering the sign of x on output. The + * method used depends on the magnitude of x. + * + * 1. For small |x|, sinpi(x) = pi * x where a sloppy threshold is used. The + * threshold is |x| < 0x1pN with N = -(P/2+M). P is the precision of the + * floating-point type and M = 2 to 4. To achieve high accuracy, pi is + * decomposed into high and low parts with the high part containing a + * number of trailing zero bits. x is also split into high and low parts. + * + * 2. For |x| < 1, argument reduction is not required and sinpi(x) is + * computed by calling a kernel that leverages the kernels for sin(x) + * ans cos(x). See k_sinpi.c and k_cospi.c for details. + * + * 3. For 1 <= |x| < 0x1p(P-1), argument reduction is required where + * |x| = j0 + r with j0 an integer and the remainder r satisfies + * 0 <= r < 1. With the given domain, a simplified inline floor(x) + * is used. Also, note the following identity + * + * sinpi(x) = sin(pi*(j0+r)) + * = sin(pi*j0) * cos(pi*r) + cos(pi*j0) * sin(pi*r) + * = cos(pi*j0) * sin(pi*r) + * = +-sinpi(r) + * + * If j0 is even, then cos(pi*j0) = 1. If j0 is odd, then cos(pi*j0) = -1. + * sinpi(r) is then computed via an appropriate kernel. + * + * 4. For |x| >= 0x1p(P-1), |x| is integral and sinpi(x) = copysign(0,x). + * + * 5. Special cases: + * + * sinpi(+-0) = +-0 + * sinpi(+-n) = +-0, for positive integers n. + * sinpi(+-inf) = nan. Raises the "invalid" floating-point exception. + * sinpi(nan) = nan. Raises the "invalid" floating-point exception. + */ + +#include "math.h" +#include "math_private.h" + +static const double +pi_hi = 3.1415926814079285e+00, /* 0x400921fb 0x58000000 */ +pi_lo =-2.7818135228334233e-08; /* 0xbe5dde97 0x3dcb3b3a */ + +#include "k_cospi.h" +#include "k_sinpi.h" + +volatile static const double vzero = 0; + +double +sinpi(double x) +{ + double ax, hi, lo, s; + uint32_t hx, ix, j0, lx; + + EXTRACT_WORDS(hx, lx, x); + ix = hx & 0x7fffffff; + INSERT_WORDS(ax, ix, lx); + + if (ix < 0x3ff00000) { /* |x| < 1 */ + if (ix < 0x3fd00000) { /* |x| < 0.25 */ + if (ix < 0x3e200000) { /* |x| < 0x1p-29 */ + if (x == 0) + return (x); + /* + * To avoid issues with subnormal values, + * scale the computation and rescale on + * return. + */ + INSERT_WORDS(hi, hx, 0); + hi *= 0x1p53; + lo = x * 0x1p53 - hi; + s = (pi_lo + pi_hi) * lo + pi_lo * hi + + pi_hi * hi; + return (s * 0x1p-53); + } + + s = __kernel_sinpi(ax); + return ((hx & 0x80000000) ? -s : s); + } + + if (ix < 0x3fe00000) /* |x| < 0.5 */ + s = __kernel_cospi(0.5 - ax); + else if (ix < 0x3fe80000) /* |x| < 0.75 */ + s = __kernel_cospi(ax - 0.5); + else + s = __kernel_sinpi(1 - ax); + return ((hx & 0x80000000) ? -s : s); + } + + if (ix < 0x43300000) { /* 1 <= |x| < 0x1p52 */ + /* Determine integer part of ax. */ + j0 = ((ix >> 20) & 0x7ff) - 0x3ff; + if (j0 < 20) { + ix &= ~(0x000fffff >> j0); + lx = 0; + } else { + lx &= ~((uint32_t)0xffffffff >> (j0 - 20)); + } + INSERT_WORDS(x, ix, lx); + + ax -= x; + EXTRACT_WORDS(ix, lx, ax); + + if (ix == 0) + s = 0; + else { + if (ix < 0x3fe00000) { /* |x| < 0.5 */ + if (ix < 0x3fd00000) /* |x| < 0.25 */ + s = __kernel_sinpi(ax); + else + s = __kernel_cospi(0.5 - ax); + } else { + if (ix < 0x3fe80000) /* |x| < 0.75 */ + s = __kernel_cospi(ax - 0.5); + else + s = __kernel_sinpi(1 - ax); + } + + if (j0 > 30) + x -= 0x1p30; + j0 = (uint32_t)x; + if (j0 & 1) s = -s; + } + + return ((hx & 0x80000000) ? -s : s); + } + + if (ix >= 0x7f800000) + return (vzero / vzero); + + /* + * |x| >= 0x1p52 is always an integer, so return +-0. + */ + return (copysign(0, x)); +} + +#if LDBL_MANT_DIG == 53 +__weak_reference(sinpi, sinpil); +#endif diff --git a/lib/msun/src/s_sinpif.c b/lib/msun/src/s_sinpif.c new file mode 100644 index 000000000000..c9f76f8a2358 --- /dev/null +++ b/lib/msun/src/s_sinpif.c @@ -0,0 +1,122 @@ +/*- + * Copyright (c) 2017 Steven G. Kargl + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice unmodified, this list of conditions, and the following + * disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +/* + * See ../src/s_sinpi.c for implementation details. + */ + +#define INLINE_KERNEL_SINDF +#define INLINE_KERNEL_COSDF + +#include "math.h" +#include "math_private.h" +#include "k_cosf.c" +#include "k_sinf.c" + +#define __kernel_cospif(x) (__kernel_cosdf(M_PI * (x))) +#define __kernel_sinpif(x) (__kernel_sindf(M_PI * (x))) + +static const float +pi_hi = 3.14160156e+00F, /* 0x40491000 */ +pi_lo = -8.90890988e-06F; /* 0xb715777a */ + +volatile static const float vzero = 0; + +float +sinpif(float x) +{ + float ax, hi, lo, s; + uint32_t hx, ix, j0; + + GET_FLOAT_WORD(hx, x); + ix = hx & 0x7fffffff; + SET_FLOAT_WORD(ax, ix); + + if (ix < 0x3f800000) { /* |x| < 1 */ + if (ix < 0x3e800000) { /* |x| < 0.25 */ + if (ix < 0x38800000) { /* |x| < 0x1p-14 */ + if (x == 0) + return (x); + SET_FLOAT_WORD(hi, hx & 0xffff0000); + hi *= 0x1p23F; + lo = x * 0x1p23F - hi; + s = (pi_lo + pi_hi) * lo + pi_lo * hi + + pi_hi * hi; + return (s * 0x1p-23F); + } + + s = __kernel_sinpif(ax); + return ((hx & 0x80000000) ? -s : s); + } + + if (ix < 0x3f000000) /* |x| < 0.5 */ + s = __kernel_cospif(0.5F - ax); + else if (ix < 0x3f400000) /* |x| < 0.75 */ + s = __kernel_cospif(ax - 0.5F); + else + s = __kernel_sinpif(1 - ax); + return ((hx & 0x80000000) ? -s : s); + } + + if (ix < 0x4b000000) { /* 1 <= |x| < 0x1p23 */ + /* Determine integer part of ax. */ + j0 = ((ix >> 23) & 0xff) - 0x7f; + ix &= ~(0x007fffff >> j0); + SET_FLOAT_WORD(x, ix); + + ax -= x; + GET_FLOAT_WORD(ix, ax); + + if (ix == 0) + s = 0; + else { + if (ix < 0x3f000000) { /* |x| < 0.5 */ + if (ix < 0x3e800000) /* |x| < 0.25 */ + s = __kernel_sinpif(ax); + else + s = __kernel_cospif(0.5F - ax); + } else { + if (ix < 0x3f400000) /* |x| < 0.75 */ + s = __kernel_cospif(ax - 0.5F); + else + s = __kernel_sinpif(1 - ax); + } + + j0 = (uint32_t)x; + s = (j0 & 1) ? -s : s; + } + return ((hx & 0x80000000) ? -s : s); + } + + /* x = +-inf or nan. */ + if (ix >= 0x7f800000) + return (vzero / vzero); + + /* + * |x| >= 0x1p23 is always an integer, so return +-0. + */ + return (copysignf(0, x)); +} diff --git a/lib/msun/src/s_tanpi.c b/lib/msun/src/s_tanpi.c new file mode 100644 index 000000000000..e01917c94c15 --- /dev/null +++ b/lib/msun/src/s_tanpi.c @@ -0,0 +1,176 @@ +/*- + * Copyright (c) 2017 Steven G. Kargl + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice unmodified, this list of conditions, and the following + * disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +/** + * tanpi(x) computes tan(pi*x) without multiplication by pi (almost). First, + * note that tanpi(-x) = -tanpi(x), so the algorithm considers only |x| and + * includes reflection symmetry by considering the sign of x on output. The + * method used depends on the magnitude of x. + * + * 1. For small |x|, tanpi(x) = pi * x where a sloppy threshold is used. The + * threshold is |x| < 0x1pN with N = -(P/2+M). P is the precision of the + * floating-point type and M = 2 to 4. To achieve high accuracy, pi is + * decomposed into high and low parts with the high part containing a + * number of trailing zero bits. x is also split into high and low parts. + * + * 2. For |x| < 1, argument reduction is not required and tanpi(x) is + * computed by a direct call to a kernel, which uses the kernel for + * tan(x). See below. + * + * 3. For 1 <= |x| < 0x1p(P-1), argument reduction is required where + * |x| = j0 + r with j0 an integer and the remainder r satisfies + * 0 <= r < 1. With the given domain, a simplified inline floor(x) + * is used. Also, note the following identity + * + * tan(pi*j0) + tan(pi*r) + * tanpi(x) = tan(pi*(j0+r)) = ---------------------------- = tanpi(r) + * 1 - tan(pi*j0) * tan(pi*r) + * + * So, after argument reduction, the kernel is again invoked. + * + * 4. For |x| >= 0x1p(P-1), |x| is integral and tanpi(x) = copysign(0,x). + * + * 5. Special cases: + * + * tanpi(+-0) = +-0 + * tanpi(+-n) = +-0, for positive integers n. + * tanpi(+-n+1/4) = +-1, for positive integers n. + * tanpi(+-n+1/2) = NaN, for positive integers n. + * tanpi(+-inf) = NaN. Raises the "invalid" floating-point exception. + * tanpi(nan) = NaN. Raises the "invalid" floating-point exception. + */ + +#include "math.h" +#include "math_private.h" + +static const double +pi_hi = 3.1415926814079285e+00, /* 0x400921fb 0x58000000 */ +pi_lo = -2.7818135228334233e-08; /* 0xbe5dde97 0x3dcb3b3a */ + +/* + * The kernel for tanpi(x) multiplies x by an 80-bit approximation of + * pi, where the hi and lo parts are used with with kernel for tan(x). + */ +static inline double +__kernel_tanpi(double x) +{ + double_t hi, lo, t; + + if (x < 0.25) { + hi = (float)x; + lo = x - hi; + lo = lo * (pi_lo + pi_hi) + hi * pi_lo; + hi *= pi_hi; + _2sumF(hi, lo); + t = __kernel_tan(hi, lo, 1); + } else if (x > 0.25) { + x = 0.5 - x; + hi = (float)x; + lo = x - hi; + lo = lo * (pi_lo + pi_hi) + hi * pi_lo; + hi *= pi_hi; + _2sumF(hi, lo); + t = - __kernel_tan(hi, lo, -1); + } else + t = 1; + + return (t); +} + +volatile static const double vzero = 0; + +double +tanpi(double x) +{ + double ax, hi, lo, t; + uint32_t hx, ix, j0, lx; + + EXTRACT_WORDS(hx, lx, x); + ix = hx & 0x7fffffff; + INSERT_WORDS(ax, ix, lx); + + if (ix < 0x3ff00000) { /* |x| < 1 */ + if (ix < 0x3fe00000) { /* |x| < 0.5 */ + if (ix < 0x3e200000) { /* |x| < 0x1p-29 */ + if (x == 0) + return (x); + /* + * To avoid issues with subnormal values, + * scale the computation and rescale on + * return. + */ + INSERT_WORDS(hi, hx, 0); + hi *= 0x1p53; + lo = x * 0x1p53 - hi; + t = (pi_lo + pi_hi) * lo + pi_lo * hi + + pi_hi * hi; + return (t * 0x1p-53); + } + t = __kernel_tanpi(ax); + } else if (ax == 0.5) + return ((ax - ax) / (ax - ax)); + else + t = - __kernel_tanpi(1 - ax); + return ((hx & 0x80000000) ? -t : t); + } + + if (ix < 0x43300000) { /* 1 <= |x| < 0x1p52 */ + /* Determine integer part of ax. */ + j0 = ((ix >> 20) & 0x7ff) - 0x3ff; + if (j0 < 20) { + ix &= ~(0x000fffff >> j0); + lx = 0; + } else { + lx &= ~(((uint32_t)(0xffffffff)) >> (j0 - 20)); + } + INSERT_WORDS(x,ix,lx); + + ax -= x; + EXTRACT_WORDS(ix, lx, ax); + + if (ix < 0x3fe00000) /* |x| < 0.5 */ + t = ax == 0 ? 0 : __kernel_tanpi(ax); + else if (ax == 0.5) + return ((ax - ax) / (ax - ax)); + else + t = - __kernel_tanpi(1 - ax); + + return ((hx & 0x80000000) ? -t : t); + } + + /* x = +-inf or nan. */ + if (ix >= 0x7f800000) + return (vzero / vzero); + + /* + * |x| >= 0x1p52 is always an integer, so return +-0. + */ + return (copysign(0, x)); +} + +#if LDBL_MANT_DIG == 53 +__weak_reference(tanpi, tanpil); +#endif diff --git a/lib/msun/src/s_tanpif.c b/lib/msun/src/s_tanpif.c new file mode 100644 index 000000000000..6d4b627d1cf9 --- /dev/null +++ b/lib/msun/src/s_tanpif.c @@ -0,0 +1,114 @@ +/*- + * Copyright (c) 2017 Steven G. Kargl + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice unmodified, this list of conditions, and the following + * disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +/* + * See ../src/s_tanpi.c for implementation details. + */ + +#define INLINE_KERNEL_TANDF + +#include "math.h" +#include "math_private.h" +#include "k_tanf.c" + +static const float +pi_hi = 3.14160156e+00F, /* 0x40491000 */ +pi_lo = -8.90890988e-06F; /* 0xb715777a */ + +static inline float +__kernel_tanpif(float x) +{ + float t; + + if (x < 0.25F) + t = __kernel_tandf(M_PI * x, 1); + else if (x > 0.25F) + t = -__kernel_tandf(M_PI * (0.5 - x), -1); + else + t = 1; + + return (t); +} + +volatile static const float vzero = 0; + +float +tanpif(float x) +{ + float ax, hi, lo, t; + uint32_t hx, ix, j0; + + GET_FLOAT_WORD(hx, x); + ix = hx & 0x7fffffff; + SET_FLOAT_WORD(ax, ix); + + if (ix < 0x3f800000) { /* |x| < 1 */ + if (ix < 0x3f000000) { /* |x| < 0.5 */ + if (ix < 0x38800000) { /* |x| < 0x1p-14 */ + if (ix == 0) + return (x); + SET_FLOAT_WORD(hi, hx & 0xffff0000); + hi *= 0x1p23F; + lo = x * 0x1p23F - hi; + t = (pi_lo + pi_hi) * lo + pi_lo * hi + + pi_hi * hi; + return (t * 0x1p-23F); + } + t = __kernel_tanpif(ax); + } else if (ix == 0x3f000000) + return ((ax - ax) / (ax - ax)); + else + t = - __kernel_tanpif(1 - ax); + return ((hx & 0x80000000) ? -t : t); + } + + if (ix < 0x4b000000) { /* 1 <= |x| < 0x1p23 */ + /* Determine integer part of ax. */ + j0 = ((ix >> 23) & 0xff) - 0x7f; + ix &= ~(0x007fffff >> j0); + SET_FLOAT_WORD(x, ix); + + ax -= x; + GET_FLOAT_WORD(ix, ax); + + if (ix < 0x3f000000) /* |x| < 0.5 */ + t = ix == 0 ? 0 : __kernel_tanpif(ax); + else if (ix == 0x3f000000) + return ((ax - ax) / (ax - ax)); + else + t = - __kernel_tanpif(1 - ax); + return ((hx & 0x80000000) ? -t : t); + } + + /* x = +-inf or nan. */ + if (ix >= 0x7f800000) + return (vzero / vzero); + + /* + * |x| >= 0x1p23 is always an integer, so return +-0. + */ + return (copysignf(0, x)); +}