diff --git a/contrib/netbsd-tests/lib/libm/t_hypot.c b/contrib/netbsd-tests/lib/libm/t_hypot.c index deb7e86ad5ac..cbb056ee470e 100644 --- a/contrib/netbsd-tests/lib/libm/t_hypot.c +++ b/contrib/netbsd-tests/lib/libm/t_hypot.c @@ -1,81 +1,105 @@ /* $NetBSD: t_hypot.c,v 1.1 2016/01/24 20:26:47 gson Exp $ */ /*- * Copyright (c) 2016 The NetBSD Foundation, Inc. * 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 NETBSD FOUNDATION, INC. 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 FOUNDATION 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. */ #include #include ATF_TC(hypot_integer); ATF_TC_HEAD(hypot_integer, tc) { atf_tc_set_md_var(tc, "descr", "Test hypot with integer args"); } ATF_TC_BODY(hypot_integer, tc) { /* volatile so hypotf() won't be evaluated at compile time */ volatile double a = 5; volatile double b = 12; ATF_CHECK(hypot(a, b) == 13.0); } ATF_TC(hypotf_integer); ATF_TC_HEAD(hypotf_integer, tc) { atf_tc_set_md_var(tc, "descr", "Test hypotf with integer args"); } ATF_TC_BODY(hypotf_integer, tc) { volatile float a = 5; volatile float b = 12; ATF_CHECK(hypotf(a, b) == 13.0f); } ATF_TC(pr50698); ATF_TC_HEAD(pr50698, tc) { atf_tc_set_md_var(tc, "descr", "Check for the bug of PR 50698"); } ATF_TC_BODY(pr50698, tc) { volatile float a = 1e-18f; float val = hypotf(a, a); ATF_CHECK(!isinf(val)); ATF_CHECK(!isnan(val)); } +#if __LDBL_MANT_DIG__ == 64 +ATF_TC(hypotl_near_underflow); +ATF_TC_HEAD(hypotl_near_underflow, tc) +{ + atf_tc_set_md_var(tc, "descr", "Test hypotl near underflow"); +} + +ATF_TC_BODY(hypotl_near_underflow, tc) +{ + volatile long double a = 0x1.b2933cafa0bb7p-16383L; + volatile long double b = 0x1.fffffffffffffp-16351L; + volatile long double e = 0x1.fffffffffffffp-16351L; + volatile long double ulp = __LDBL_EPSILON__; + + volatile long double val = hypotl(a, b); + + ATF_CHECK(!isinf(val)); + ATF_CHECK(fabsl(val - e) <= 2 * ulp); +} +#endif /* __LDBL_MANT_DIG__ == 64 */ + ATF_TP_ADD_TCS(tp) { ATF_TP_ADD_TC(tp, hypot_integer); ATF_TP_ADD_TC(tp, hypotf_integer); ATF_TP_ADD_TC(tp, pr50698); +#if __LDBL_MANT_DIG__ == 64 + ATF_TP_ADD_TC(tp, hypotl_near_underflow); +#endif /* __LDBL_MANT_DIG__ == 64 */ return atf_no_error(); } diff --git a/lib/msun/src/e_hypotl.c b/lib/msun/src/e_hypotl.c index 9189b1fab54d..fc43538dfa65 100644 --- a/lib/msun/src/e_hypotl.c +++ b/lib/msun/src/e_hypotl.c @@ -1,124 +1,124 @@ /* From: @(#)e_hypot.c 1.3 95/01/18 */ /* * ==================================================== * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. * * Developed at SunSoft, a Sun Microsystems, Inc. business. * Permission to use, copy, modify, and distribute this * software is freely granted, provided that this notice * is preserved. * ==================================================== */ #include __FBSDID("$FreeBSD$"); /* long double version of hypot(). See e_hypot.c for most comments. */ #include #include "fpmath.h" #include "math.h" #include "math_private.h" #define GET_LDBL_MAN(h, l, v) do { \ union IEEEl2bits uv; \ \ uv.e = v; \ h = uv.bits.manh; \ l = uv.bits.manl; \ } while (0) #undef GET_HIGH_WORD #define GET_HIGH_WORD(i, v) GET_LDBL_EXPSIGN(i, v) #undef SET_HIGH_WORD #define SET_HIGH_WORD(v, i) SET_LDBL_EXPSIGN(v, i) #define DESW(exp) (exp) /* delta expsign word */ #define ESW(exp) (MAX_EXP - 1 + (exp)) /* expsign word */ #define MANT_DIG LDBL_MANT_DIG #define MAX_EXP LDBL_MAX_EXP #if LDBL_MANL_SIZE > 32 typedef uint64_t man_t; #else typedef uint32_t man_t; #endif long double hypotl(long double x, long double y) { long double a=x,b=y,t1,t2,y1,y2,w; int32_t j,k,ha,hb; GET_HIGH_WORD(ha,x); ha &= 0x7fff; GET_HIGH_WORD(hb,y); hb &= 0x7fff; if(hb > ha) {a=y;b=x;j=ha; ha=hb;hb=j;} else {a=x;b=y;} a = fabsl(a); b = fabsl(b); if((ha-hb)>DESW(MANT_DIG+7)) {return a+b;} /* x/y > 2**(MANT_DIG+7) */ k=0; if(ha > ESW(MAX_EXP/2-12)) { /* a>2**(MAX_EXP/2-12) */ if(ha >= ESW(MAX_EXP)) { /* Inf or NaN */ man_t manh, manl; /* Use original arg order iff result is NaN; quieten sNaNs. */ w = fabsl(x+0.0L)-fabsl(y+0); GET_LDBL_MAN(manh,manl,a); if (manh == LDBL_NBIT && manl == 0) w = a; GET_LDBL_MAN(manh,manl,b); if (hb >= ESW(MAX_EXP) && manh == LDBL_NBIT && manl == 0) w = b; return w; } /* scale a and b by 2**-(MAX_EXP/2+88) */ ha -= DESW(MAX_EXP/2+88); hb -= DESW(MAX_EXP/2+88); k += MAX_EXP/2+88; SET_HIGH_WORD(a,ha); SET_HIGH_WORD(b,hb); } if(hb < ESW(-(MAX_EXP/2-12))) { /* b < 2**-(MAX_EXP/2-12) */ if(hb <= 0) { /* subnormal b or 0 */ man_t manh, manl; GET_LDBL_MAN(manh,manl,b); if((manh|manl)==0) return a; - t1=0; + t1=1; SET_HIGH_WORD(t1,ESW(MAX_EXP-2)); /* t1=2^(MAX_EXP-2) */ b *= t1; a *= t1; k -= MAX_EXP-2; } else { /* scale a and b by 2^(MAX_EXP/2+88) */ ha += DESW(MAX_EXP/2+88); hb += DESW(MAX_EXP/2+88); k -= MAX_EXP/2+88; SET_HIGH_WORD(a,ha); SET_HIGH_WORD(b,hb); } } /* medium size a and b */ w = a-b; if (w>b) { t1 = a; union IEEEl2bits uv; uv.e = t1; uv.bits.manl = 0; t1 = uv.e; t2 = a-t1; w = sqrtl(t1*t1-(b*(-b)-t2*(a+t1))); } else { a = a+a; y1 = b; union IEEEl2bits uv; uv.e = y1; uv.bits.manl = 0; y1 = uv.e; y2 = b - y1; t1 = a; uv.e = t1; uv.bits.manl = 0; t1 = uv.e; t2 = a - t1; w = sqrtl(t1*y1-(w*(-w)-(t1*y2+t2*b))); } if(k!=0) { u_int32_t high; t1 = 1.0; GET_HIGH_WORD(high,t1); SET_HIGH_WORD(t1,high+DESW(k)); return t1*w; } else return w; } diff --git a/lib/msun/tests/Makefile b/lib/msun/tests/Makefile index 949b0a5b7aad..cd60f7c71a15 100644 --- a/lib/msun/tests/Makefile +++ b/lib/msun/tests/Makefile @@ -1,104 +1,105 @@ # $FreeBSD$ .include TESTSRC= ${SRCTOP}/contrib/netbsd-tests/lib/libm # All architectures on FreeBSD have fenv.h CFLAGS+= -DHAVE_FENV_H # For isqemu.h CFLAGS+= -I${TESTSRC:H}/libc/gen # Not sure why this isn't defined for all architectures, since most # have long double. .if ${MACHINE_CPUARCH} == "aarch64" || \ ${MACHINE_CPUARCH} == "amd64" || \ ${MACHINE_CPUARCH} == "i386" CFLAGS+= -D__HAVE_LONG_DOUBLE .endif NETBSD_ATF_TESTS_C= acos_test NETBSD_ATF_TESTS_C+= asin_test NETBSD_ATF_TESTS_C+= atan_test NETBSD_ATF_TESTS_C+= cbrt_test NETBSD_ATF_TESTS_C+= ceil_test NETBSD_ATF_TESTS_C+= casinh_test NETBSD_ATF_TESTS_C+= cos_test NETBSD_ATF_TESTS_C+= cosh_test NETBSD_ATF_TESTS_C+= erf_test NETBSD_ATF_TESTS_C+= exp_test NETBSD_ATF_TESTS_C+= fmod_test NETBSD_ATF_TESTS_C+= fe_round_test +NETBSD_ATF_TESTS_C+= hypot_test NETBSD_ATF_TESTS_C+= infinity_test NETBSD_ATF_TESTS_C+= ilogb_test NETBSD_ATF_TESTS_C+= ldexp_test NETBSD_ATF_TESTS_C+= log_test NETBSD_ATF_TESTS_C+= pow_test NETBSD_ATF_TESTS_C+= precision_test NETBSD_ATF_TESTS_C+= round_test NETBSD_ATF_TESTS_C+= scalbn_test NETBSD_ATF_TESTS_C+= sin_test NETBSD_ATF_TESTS_C+= sinh_test NETBSD_ATF_TESTS_C+= sqrt_test NETBSD_ATF_TESTS_C+= tan_test NETBSD_ATF_TESTS_C+= tanh_test TAP_TESTS_C+= cexp_test TAP_TESTS_C+= conj_test .if ${MACHINE_CPUARCH} != "aarch64" # Hits an assert in llvm when building for arm64: # https://llvm.org/bugs/show_bug.cgi?id=26081 TAP_TESTS_C+= csqrt_test .endif ATF_TESTS_C+= ctrig_test TAP_TESTS_C+= exponential_test TAP_TESTS_C+= fenv_test TAP_TESTS_C+= fma_test TAP_TESTS_C+= fmaxmin_test TAP_TESTS_C+= ilogb2_test TAP_TESTS_C+= invtrig_test TAP_TESTS_C+= invctrig_test TAP_TESTS_C+= logarithm_test TAP_TESTS_C+= lrint_test # XXX: the testcase crashes on all platforms, but only on head # (bug 205451) #TAP_TESTS_C+= lround_test TAP_TESTS_C+= nan_test TAP_TESTS_C+= nearbyint_test TAP_TESTS_C+= next_test TAP_TESTS_C+= rem_test ATF_TESTS_C+= trig_test .if !empty(PROG) && !empty(TAP_TESTS_C:M${PROG}) CFLAGS+= -O0 .endif CSTD= c99 #COPTS+= -Wfloat-equal IGNORE_PRAGMA= SRCS.ilogb2_test= ilogb_test.c LIBADD+= m WARNS?= 1 # Copied from lib/msun/Makefile .if ${MACHINE_CPUARCH} == "i386" ARCH_SUBDIR= i387 .else ARCH_SUBDIR= ${MACHINE_CPUARCH} .endif .include "../${ARCH_SUBDIR}/Makefile.inc" # XXX: for some odd reason float.h doesn't tell the full story about what the # precision is. CFLAGS+= -DLDBL_PREC=${LDBL_PREC} .include .include