From nobody Sat Dec 04 18:53:52 2021 X-Original-To: freebsd-current@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id BF79F18D1BF1; Sat, 4 Dec 2021 18:54:01 +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 4J5zPN6tq1z565j; Sat, 4 Dec 2021 18:54:00 +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 1B4Irq6M020460 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NO); Sat, 4 Dec 2021 10:53:53 -0800 (PST) (envelope-from sgk@troutmask.apl.washington.edu) Received: (from sgk@localhost) by troutmask.apl.washington.edu (8.16.1/8.16.1/Submit) id 1B4IrqGQ020459; Sat, 4 Dec 2021 10:53:52 -0800 (PST) (envelope-from sgk) Date: Sat, 4 Dec 2021 10:53:52 -0800 From: Steve Kargl To: freebsd-hackers@freebsd.org, freebsd-current@freebsd.org Subject: What to do about tgammal? Message-ID: <20211204185352.GA20452@troutmask.apl.washington.edu> List-Id: Discussions about the use of FreeBSD-current List-Archive: https://lists.freebsd.org/archives/freebsd-current List-Help: List-Post: List-Subscribe: List-Unsubscribe: Sender: owner-freebsd-current@freebsd.org MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline X-Rspamd-Queue-Id: 4J5zPN6tq1z565j 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.04 / 15.00]; MID_RHS_MATCH_FROM(0.00)[]; ARC_NA(0.00)[]; DMARC_POLICY_SOFTFAIL(0.10)[washington.edu : No valid SPF, No valid DKIM,none]; FROM_HAS_DN(0.00)[]; TO_MATCH_ENVRCPT_ALL(0.00)[]; NEURAL_HAM_LONG(-1.00)[-0.997]; MIME_GOOD(-0.10)[text/plain]; TO_DN_NONE(0.00)[]; NEURAL_SPAM_MEDIUM(0.04)[0.042]; NEURAL_SPAM_SHORT(0.99)[0.990]; 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]; SUBJECT_ENDS_QUESTION(1.00)[]; RCVD_TLS_ALL(0.00)[]; RCVD_COUNT_TWO(0.00)[2] X-ThisMailContainsUnwantedMimeParts: N What to do about tgammal? A long time ago (2013-09-06), theraven@ committed a kludge that mapped several missing long double math functions to double math functions (e.g., tanhl(x) was mapped to tanh(x)). Over the next few years, I (along with bde and das reviews) provided Intel 80-bit (ld80) and IEEE 128-bit (ld128) implementations for some of these functions; namely, coshl(x), sinhl(x), tanhl(x), erfl(x), erfcl(x), and lgamma(x). The last remaining function is tgammal(x). If one links a program that uses tgammal(x) with libm, one sees /usr/local/bin/ld: fcn_list.o: in function `build_fcn_list': fcn_list.c:(.text+0x7c4): warning: tgammal has lower than advertised precision The warning is actually misleading. Not only does tgammal(x) have a *MUCH* lower precision, it has a reduced domain. That is, tgammal(x) produces +inf for x > 172 whereas tgammal(x) should produce a finite result for values of x up to 1755 (or so). On amd64-*-freebsd, testing 1000000 in the below intervals demonstrates pathetic accuracy. Current implmentation via imprecise.c Interval | Max ULP -------------------+------------ [6,171] | 1340542.2 [1.0662,6] | 14293.3 [1.01e-17,1.0661] | 3116.1 [-1.9999,-1.0001] | 15330369.3 -------------------+------------ Well, I finally have gotten around to removing theraven@'s last kludge for FreeBSD on systems that support ld80. This is done with a straight forward modification of the msun/bsdsrc code. The limitation on domain is removed and the accuracy substantially improved. Interval | Max ULP -------------------+---------- [6,1755] | 8.457 [1.0662,6] | 11.710 [1.01e-17,1.0661] | 11.689 [-1.9999,-1.0001] | 11.871 -------------------+---------- My modifications leverage the fact that tgamma(x) (ie., double function) uses extend arithmetic to do the computations (approximately 85 bits of precision). To get the Max ULP below 1 (the desired upper limit), a few minimax polynomials need to be determined and the mystery around a few magic numbers need to be unraveled. Extending what I have done to an ld128 implementation requires much more effort than I have time and energy to pursue. Someone with interest in floating point math on ld128 system can provide an implementation. So, is anyone interested in seeing a massive patch? -- Steve