From owner-freebsd-hackers@freebsd.org Sat Sep 19 23:58:59 2020 Return-Path: Delivered-To: freebsd-hackers@mailman.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mailman.nyi.freebsd.org (Postfix) with ESMTP id 3C36F3F2A73; Sat, 19 Sep 2020 23:58:59 +0000 (UTC) (envelope-from sgk@troutmask.apl.washington.edu) Received: from troutmask.apl.washington.edu (troutmask.apl.washington.edu [128.95.76.21]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (2048 bits) client-digest SHA256) (Client CN "troutmask", Issuer "troutmask" (not verified)) by mx1.freebsd.org (Postfix) with ESMTPS id 4Bv72n6sk6z3RNy; Sat, 19 Sep 2020 23:58:57 +0000 (UTC) (envelope-from sgk@troutmask.apl.washington.edu) Received: from troutmask.apl.washington.edu (localhost [127.0.0.1]) by troutmask.apl.washington.edu (8.16.1/8.16.1) with ESMTPS id 08JNwnQG047724 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NO); Sat, 19 Sep 2020 16:58:49 -0700 (PDT) (envelope-from sgk@troutmask.apl.washington.edu) Received: (from sgk@localhost) by troutmask.apl.washington.edu (8.16.1/8.16.1/Submit) id 08JNwnLl047723; Sat, 19 Sep 2020 16:58:49 -0700 (PDT) (envelope-from sgk) Date: Sat, 19 Sep 2020 16:58:49 -0700 From: Steve Kargl To: freebsd-hackers@freebsd.org, freebsd-current@freebsd.org Subject: [PATCH] libm -- fix expl kernels Message-ID: <20200919235849.GA47701@troutmask.apl.washington.edu> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline X-Rspamd-Queue-Id: 4Bv72n6sk6z3RNy X-Spamd-Bar: - Authentication-Results: mx1.freebsd.org; dkim=none; dmarc=fail reason="No valid SPF, No valid DKIM" header.from=washington.edu (policy=none); spf=none (mx1.freebsd.org: domain of sgk@troutmask.apl.washington.edu has no SPF policy when checking 128.95.76.21) smtp.mailfrom=sgk@troutmask.apl.washington.edu X-Spamd-Result: default: False [-1.48 / 15.00]; RCVD_TLS_ALL(0.00)[]; ARC_NA(0.00)[]; MID_RHS_MATCH_FROM(0.00)[]; FROM_HAS_DN(0.00)[]; TO_MATCH_ENVRCPT_ALL(0.00)[]; NEURAL_HAM_LONG(-0.37)[-0.367]; MIME_GOOD(-0.10)[text/plain]; TO_DN_NONE(0.00)[]; NEURAL_HAM_MEDIUM(-0.56)[-0.558]; NEURAL_HAM_SHORT(-0.55)[-0.553]; RCPT_COUNT_TWO(0.00)[2]; R_SPF_NA(0.00)[no SPF record]; FROM_EQ_ENVFROM(0.00)[]; R_DKIM_NA(0.00)[]; MIME_TRACE(0.00)[0:+]; ASN(0.00)[asn:73, ipnet:128.95.0.0/16, country:US]; RCVD_COUNT_TWO(0.00)[2]; MAILMAN_DEST(0.00)[freebsd-hackers,freebsd-current]; DMARC_POLICY_SOFTFAIL(0.10)[washington.edu : No valid SPF, No valid DKIM, none] X-BeenThere: freebsd-hackers@freebsd.org X-Mailman-Version: 2.1.33 Precedence: list List-Id: Technical Discussions relating to FreeBSD List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sat, 19 Sep 2020 23:58:59 -0000 The following patch fixes the ld80 and ld128 kernels for expl(x). I can't test ld128, so that patch is untested. . Micro-optimization: use sincosl(x) instead of a call to cosl(x) and a call to sinl(x). Argument reduction is done once not twice. . Use a long double constant instead of an invalid double constant. . Spell scale2 correctly --- /usr/src/lib/msun/ld80/k_expl.h 2019-02-23 07:45:04.364785000 -0800 +++ ld80/k_expl.h 2019-12-26 08:15:29.366146000 -0800 @@ -277,7 +277,7 @@ static inline long double complex __ldexp_cexpl(long double complex z, int expt) { - long double exp_x, hi, lo; + long double c, exp_x, hi, lo, s; long double x, y, scale1, scale2; int half_expt, k; @@ -285,16 +285,17 @@ y = cimagl(z); __k_expl(x, &hi, &lo, &k); - exp_x = (lo + hi) * 0x1p16382; + exp_x = (lo + hi) * 0x1p16382L; expt += k - 16382; scale1 = 1; half_expt = expt / 2; SET_LDBL_EXPSIGN(scale1, BIAS + half_expt); scale2 = 1; - SET_LDBL_EXPSIGN(scale1, BIAS + expt - half_expt); + SET_LDBL_EXPSIGN(scale2, BIAS + expt - half_expt); - return (CMPLXL(cos(y) * exp_x * scale1 * scale2, - sinl(y) * exp_x * scale1 * scale2)); + sincosl(y, &s, &c); + return (CMPLXL(c * exp_x * scale1 * scale2, + s * exp_x * scale1 * scale2)); } #endif /* _COMPLEX_H */ --- /usr/src/lib/msun/ld128/k_expl.h 2020-03-07 09:48:15.719876000 -0800 +++ ld128/k_expl.h 2020-09-19 16:51:43.577195000 -0700 @@ -300,7 +300,7 @@ static inline long double complex __ldexp_cexpl(long double complex z, int expt) { - long double exp_x, hi, lo; + long double c, exp_x, hi, lo, s; long double x, y, scale1, scale2; int half_expt, k; @@ -308,16 +308,17 @@ y = cimagl(z); __k_expl(x, &hi, &lo, &k); - exp_x = (lo + hi) * 0x1p16382; + exp_x = (lo + hi) * 0x1p16382L; expt += k - 16382; scale1 = 1; half_expt = expt / 2; SET_LDBL_EXPSIGN(scale1, BIAS + half_expt); scale2 = 1; - SET_LDBL_EXPSIGN(scale1, BIAS + expt - half_expt); + SET_LDBL_EXPSIGN(scale2, BIAS + expt - half_expt); - return (CMPLXL(cos(y) * exp_x * scale1 * scale2, - sinl(y) * exp_x * scale1 * scale2)); + sincosl(y, &s, &c); + return (CMPLXL(c * exp_x * scale1 * scale2, + s * exp_x * scale1 * scale2)); } #endif /* _COMPLEX_H */ -- Steve