summaryrefslogtreecommitdiff
path: root/src/lib/libast/comp/frexp.c
diff options
context:
space:
mode:
authorIgor Pashev <pashev.igor@gmail.com>2012-06-24 22:28:35 +0000
committerIgor Pashev <pashev.igor@gmail.com>2012-06-24 22:28:35 +0000
commit3950ffe2a485479f6561c27364d3d7df5a21d124 (patch)
tree468c6e14449d1b1e279222ec32f676b0311917d2 /src/lib/libast/comp/frexp.c
downloadksh-upstream.tar.gz
Imported Upstream version 93u+upstream
Diffstat (limited to 'src/lib/libast/comp/frexp.c')
-rw-r--r--src/lib/libast/comp/frexp.c153
1 files changed, 153 insertions, 0 deletions
diff --git a/src/lib/libast/comp/frexp.c b/src/lib/libast/comp/frexp.c
new file mode 100644
index 0000000..02cd50f
--- /dev/null
+++ b/src/lib/libast/comp/frexp.c
@@ -0,0 +1,153 @@
+/***********************************************************************
+* *
+* This software is part of the ast package *
+* Copyright (c) 1985-2011 AT&T Intellectual Property *
+* and is licensed under the *
+* Eclipse Public License, Version 1.0 *
+* by AT&T Intellectual Property *
+* *
+* A copy of the License is available at *
+* http://www.eclipse.org/org/documents/epl-v10.html *
+* (with md5 checksum b35adb5213ca9657e911e9befb180842) *
+* *
+* Information and Software Systems Research *
+* AT&T Research *
+* Florham Park NJ *
+* *
+* Glenn Fowler <gsf@research.att.com> *
+* David Korn <dgk@research.att.com> *
+* Phong Vo <kpv@research.att.com> *
+* *
+***********************************************************************/
+#pragma prototyped
+
+/*
+ * frexp/ldexp implementation
+ */
+
+#include <ast.h>
+
+#include "FEATURE/float"
+
+#if _lib_frexp && _lib_ldexp
+
+NoN(frexp)
+
+#else
+
+#if defined(_ast_dbl_exp_index) && defined(_ast_dbl_exp_shift)
+
+#define INIT() _ast_dbl_exp_t _pow_
+#define pow2(i) (_pow_.f=1,_pow_.e[_ast_dbl_exp_index]+=((i)<<_ast_dbl_exp_shift),_pow_.f)
+
+#else
+
+static double pow2tab[DBL_MAX_EXP + 1];
+
+static int
+init(void)
+{
+ register int x;
+ double g;
+
+ g = 1;
+ for (x = 0; x < elementsof(pow2tab); x++)
+ {
+ pow2tab[x] = g;
+ g *= 2;
+ }
+ return 0;
+}
+
+#define INIT() (pow2tab[0]?0:init())
+
+#define pow2(i) pow2tab[i]
+
+#endif
+
+#if !_lib_frexp
+
+extern double
+frexp(double f, int* p)
+{
+ register int k;
+ register int x;
+ double g;
+
+ INIT();
+
+ /*
+ * normalize
+ */
+
+ x = k = DBL_MAX_EXP / 2;
+ if (f < 1)
+ {
+ g = 1.0L / f;
+ for (;;)
+ {
+ k = (k + 1) / 2;
+ if (g < pow2(x))
+ x -= k;
+ else if (k == 1 && g < pow2(x+1))
+ break;
+ else
+ x += k;
+ }
+ if (g == pow2(x))
+ x--;
+ x = -x;
+ }
+ else if (f > 1)
+ {
+ for (;;)
+ {
+ k = (k + 1) / 2;
+ if (f > pow2(x))
+ x += k;
+ else if (k == 1 && f > pow2(x-1))
+ break;
+ else
+ x -= k;
+ }
+ if (f == pow2(x))
+ x++;
+ }
+ else
+ x = 1;
+ *p = x;
+
+ /*
+ * shift
+ */
+
+ x = -x;
+ if (x < 0)
+ f /= pow2(-x);
+ else if (x < DBL_MAX_EXP)
+ f *= pow2(x);
+ else
+ f = (f * pow2(DBL_MAX_EXP - 1)) * pow2(x - (DBL_MAX_EXP - 1));
+ return f;
+}
+
+#endif
+
+#if !_lib_ldexp
+
+extern double
+ldexp(double f, register int x)
+{
+ INIT();
+ if (x < 0)
+ f /= pow2(-x);
+ else if (x < DBL_MAX_EXP)
+ f *= pow2(x);
+ else
+ f = (f * pow2(DBL_MAX_EXP - 1)) * pow2(x - (DBL_MAX_EXP - 1));
+ return f;
+}
+
+#endif
+
+#endif