From owner-freebsd-toolchain@freebsd.org Wed Mar 13 02:45:51 2019 Return-Path: Delivered-To: freebsd-toolchain@mailman.ysv.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mailman.ysv.freebsd.org (Postfix) with ESMTP id 05EAC15476FD; Wed, 13 Mar 2019 02:45:51 +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) server-signature RSA-PSS (4096 bits) client-signature RSA-PSS (2048 bits) client-digest SHA256) (Client CN "troutmask", Issuer "troutmask" (not verified)) by mx1.freebsd.org (Postfix) with ESMTPS id 84B589130B; Wed, 13 Mar 2019 02:45:49 +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.15.2/8.15.2) with ESMTPS id x2D2jfSc031816 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NO); Tue, 12 Mar 2019 19:45:41 -0700 (PDT) (envelope-from sgk@troutmask.apl.washington.edu) Received: (from sgk@localhost) by troutmask.apl.washington.edu (8.15.2/8.15.2/Submit) id x2D2jfJD031815; Tue, 12 Mar 2019 19:45:41 -0700 (PDT) (envelope-from sgk) Date: Tue, 12 Mar 2019 19:45:41 -0700 From: Steve Kargl To: freebsd-toolchain@freebsd.org, freebsd-current@freebsd.org Subject: Optimization bug with floating-point? Message-ID: <20190313024506.GA31746@troutmask.apl.washington.edu> Reply-To: sgk@troutmask.apl.washington.edu MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline User-Agent: Mutt/1.11.2 (2019-01-07) X-Rspamd-Queue-Id: 84B589130B X-Spamd-Bar: ++++ Authentication-Results: mx1.freebsd.org X-Spamd-Result: default: False [4.32 / 15.00]; ARC_NA(0.00)[]; HAS_REPLYTO(0.00)[sgk@troutmask.apl.washington.edu]; FROM_HAS_DN(0.00)[]; TO_MATCH_ENVRCPT_ALL(0.00)[]; NEURAL_SPAM_MEDIUM(0.75)[0.747,0]; MIME_GOOD(-0.10)[text/plain]; MIME_TRACE(0.00)[0:+]; TO_DN_NONE(0.00)[]; AUTH_NA(1.00)[]; DMARC_NA(0.00)[washington.edu]; RCVD_COUNT_THREE(0.00)[3]; RCVD_TLS_LAST(0.00)[]; NEURAL_SPAM_SHORT(0.97)[0.968,0]; RCVD_IN_DNSWL_MED(-0.20)[21.76.95.128.list.dnswl.org : 127.0.11.2]; RCPT_COUNT_TWO(0.00)[2]; REPLYTO_ADDR_EQ_FROM(0.00)[]; MX_GOOD(-0.01)[troutmask.apl.washington.edu]; NEURAL_SPAM_LONG(0.89)[0.891,0]; R_SPF_NA(0.00)[]; FROM_EQ_ENVFROM(0.00)[]; R_DKIM_NA(0.00)[]; SUBJECT_ENDS_QUESTION(1.00)[]; ASN(0.00)[asn:73, ipnet:128.95.0.0/16, country:US]; MID_RHS_MATCH_FROM(0.00)[]; IP_SCORE(0.02)[ip: (0.09), ipnet: 128.95.0.0/16(0.09), asn: 73(0.01), country: US(-0.07)] X-BeenThere: freebsd-toolchain@freebsd.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Maintenance of FreeBSD's integrated toolchain List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 13 Mar 2019 02:45:51 -0000 All, There seems to an optimization bug with clang on % uname -a FreeBSD mobile 13.0-CURRENT FreeBSD 13.0-CURRENT r344653 MOBILE i386 IOW, if you do numerica work on i386, you may want to check your results. The program demonstrating the issue is at the end of this email. gcc8 --version gcc8 (FreeBSD Ports Collection) 8.3.0 gcc8 -fno-builtin -o z a.c -lm && ./z gcc8 -O -fno-builtin -o z a.c -lm && ./z gcc8 -O2 -fno-builtin -o z a.c -lm && ./z gcc8 -O3 -fno-builtin -o z a.c -lm && ./z Max ULP: 2.297073 Count: 0 (# of ULP that exceed 21) cc --version FreeBSD clang version 7.0.1 (tags/RELEASE_701/final 349250) (based on LLVM 7.0.1) Target: i386-unknown-freebsd13.0 cc -fno-builtin -o z a.c -lm && ./z Max ULP: 2.297073 Count: 0 cc -O -fno-builtin -o z a.c -lm && ./z cc -O2 -fno-builtin -o z a.c -lm && ./z cc -O3 -fno-builtin -o z a.c -lm && ./z ur ui: 21.588761 7.006300 x y: 9.5623927 1.4993777 csinhf: 5.07348328e+02 7.09178613e+03 dp_csinh: 5.07348986e+02 7.09178955e+03 sinhf: 7.10991113e+03 cosf: 7.13578984e-02 Max ULP: 23.061242 Count: 39 (# of ULP that exceeds 21) Things are much worse than this toy program shows. My test program used in development of libm is giving Restrict x < 10 ./testf -u -X 10 Max ULP Re: 136628.340239 Max ULP Im: 1891176.003955 Restrict c < 50 ./testf -u -X 10 Max ULP Re: 3615923.332529 Max ULP Im: 13677733.591783 /* * Compute 1 million valus of csinhf() and then compute the ULP for * for the real and imaginary parts. */ #include #include #include #include #include #include /* Return 0 <= x < 1. */ double ranged(void) { union { double x; struct { uint32_t lo; uint32_t hi; } u; } v; v.u.hi = (uint32_t)random(); v.u.hi = ((v.u.hi << 11) >> 11) | 0x3ff00000; v.u.lo = (uint32_t)random(); return (v.x - 1); } float rangef(void) { float s; s = (float)ranged(); return (s); } /* Double precision csinh() without using C's double complex.s */ void dp_csinh(double x, double y, double *re, double *im) { double c, s; sincos(y, &s, &c); *re = sinh(x) * c; *im = cosh(x) * s; } /* ULP estimate. */ double ulpfd(float app, double acc) { int n; double f; f = frexp(acc, &n); f = fabs(acc - app); f = ldexp(f, FLT_MANT_DIG - n); return (f); } int main(void) { double re, im, u, ur, ui; float complex f; float x, y; int cnt, i; srandom(19632019); ur = ui = 0; for (cnt = 0, i = 0; i < 10000000; i++) { x = rangef() + 9; y = rangef() + 0.5; f = csinhf(CMPLXF(x,y)); dp_csinh((double)x, (double)y, &re, &im); ur = ulpfd(crealf(f), re); if (ur > u) u = ur; ui = ulpfd(cimagf(f), im); if (ui > u) u = ui; if (ur > 21 || ui > 21) { printf(" ur ui: %f %f\n", ur, ui); printf(" x y: %.7f %.7f\n", x, y); printf(" csinhf: %.8e %.8e\n", crealf(f), cimagf(f)); printf("dp_csinh: %.8le %.8le\n", re, im); printf(" sinhf: %.8e\n", sinhf(x)); printf(" cosf: %.8e\n\n", cosf(y)); cnt++; } } printf("Max ULP: %f\n", u); printf("Count: %d\n", cnt); return (0); } -- Steve