summaryrefslogtreecommitdiff
path: root/src/math/hypotl.c
diff options
context:
space:
mode:
authorRich Felker <dalias@aerifal.cx>2012-03-13 01:17:53 -0400
committerRich Felker <dalias@aerifal.cx>2012-03-13 01:17:53 -0400
commitb69f695acedd4ce2798ef9ea28d834ceccc789bd (patch)
treeeafd98b9b75160210f3295ac074d699f863d958e /src/math/hypotl.c
parentd46cf2e14cc4df7cc75e77d7009fcb6df1f48a33 (diff)
downloadmusl-b69f695acedd4ce2798ef9ea28d834ceccc789bd.tar.gz
first commit of the new libm!
thanks to the hard work of Szabolcs Nagy (nsz), identifying the best (from correctness and license standpoint) implementations from freebsd and openbsd and cleaning them up! musl should now fully support c99 float and long double math functions, and has near-complete complex math support. tgmath should also work (fully on gcc-compatible compilers, and mostly on any c99 compiler). based largely on commit 0376d44a890fea261506f1fc63833e7a686dca19 from nsz's libm git repo, with some additions (dummy versions of a few missing long double complex functions, etc.) by me. various cleanups still need to be made, including re-adding (if they're correct) some asm functions that were dropped.
Diffstat (limited to 'src/math/hypotl.c')
-rw-r--r--src/math/hypotl.c148
1 files changed, 148 insertions, 0 deletions
diff --git a/src/math/hypotl.c b/src/math/hypotl.c
new file mode 100644
index 00000000..f4a64f74
--- /dev/null
+++ b/src/math/hypotl.c
@@ -0,0 +1,148 @@
+/* origin: FreeBSD /usr/src/lib/msun/src/e_hypotl.c */
+/*
+ * ====================================================
+ * 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.
+ * ====================================================
+ */
+/* long double version of hypot(). See comments in hypot.c. */
+
+#include "libm.h"
+
+#if LDBL_MANT_DIG == 53 && LDBL_MAX_EXP == 1024
+long double hypotl(long double x, long double y)
+{
+ return hypot(x, y);
+}
+#elif (LDBL_MANT_DIG == 64 || LDBL_MANT_DIG == 113) && LDBL_MAX_EXP == 16384
+
+#define GET_LDBL_EXPSIGN(i, v) do { \
+ union IEEEl2bits uv; \
+ \
+ uv.e = v; \
+ i = uv.xbits.expsign; \
+} while (0)
+
+#define GET_LDBL_MAN(h, l, v) do { \
+ union IEEEl2bits uv; \
+ \
+ uv.e = v; \
+ h = uv.bits.manh; \
+ l = uv.bits.manl; \
+} while (0)
+
+#define SET_LDBL_EXPSIGN(v, i) do { \
+ union IEEEl2bits uv; \
+ \
+ uv.e = v; \
+ uv.xbits.expsign = i; \
+ v = uv.e; \
+} 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)) /* x/y > 2**(MANT_DIG+7) */
+ return a+b;
+ 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.0)-fabsl(y+0.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;
+ 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) {
+ uint32_t high;
+ t1 = 1.0;
+ GET_HIGH_WORD(high, t1);
+ SET_HIGH_WORD(t1, high+DESW(k));
+ return t1*w;
+ }
+ return w;
+}
+#endif