0.9.29 no trunc support in libm

Kevin Day thekevinday at gmail.com
Sat May 26 00:32:00 UTC 2007


Okay, so not many programs use trunc, but eventually I ran across one that did.
This is when I noticed trunc was not in 0.9.29.

I decided to take one of my 0.9.28 patches that added trunc code,
which is simply just a ripped glibc trunc function code and add it to
my 0.9.29.

However, looking into the uClibc-0.9.29 structured, this caught my
eye.  ftrunc() is defined within libm as a wrapper to trunc().  And
trunc is not defined?

Here is my patch that is directly from glibc that adds trunc support,
without changing your ftrunc() implimentation.


--- uClibc-0.9.29/libm/Makefile.in.orig	2007-05-25 19:07:10 -0500
+++ uClibc-0.9.29/libm/Makefile.in	2007-05-25 19:07:20 -0500
@@ -72,7 +72,7 @@
 	w_cosh.c w_drem.c w_exp.c w_fmod.c w_gamma.c w_gamma_r.c \
 	w_hypot.c w_j0.c w_j1.c w_jn.c w_lgamma.c w_lgamma_r.c \
 	w_log.c w_log10.c w_pow.c w_remainder.c w_scalb.c w_sinh.c \
-	w_sqrt.c fpmacros.c nan.c carg.c s_llrint.c
+	w_sqrt.c fpmacros.c nan.c carg.c s_llrint.c s_trunc.c
 FL_MOBJ := \
 	acosf.o acoshf.o asinf.o asinhf.o atan2f.o atanf.o atanhf.o cbrtf.o \
 	ceilf.o copysignf.o cosf.o coshf.o erfcf.o erff.o exp2f.o expf.o \
--- uClibc-0.9.28/libm/s_trunc.c.orig	2006-06-04 11:54:40.000000000 -0500
+++ uClibc-0.9.28/libm/s_trunc.c	2006-06-04 12:00:19.000000000 -0500
@@ -0,0 +1,56 @@
+/* Truncate argument to nearest integral value not larger than the argument.
+   Copyright (C) 1997, 1998 Free Software Foundation, Inc.
+   This file is part of the GNU C Library.
+   Contributed by Ulrich Drepper <drepper at cygnus.com>, 1997.
+
+   The GNU C Library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Lesser General Public
+   License as published by the Free Software Foundation; either
+   version 2.1 of the License, or (at your option) any later version.
+
+   The GNU C Library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General Public
+   License along with the GNU C Library; if not, write to the Free
+   Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
+   02111-1307 USA.  */
+
+#include <math.h>
+
+#include "math_private.h"
+
+
+double
+trunc (double x)
+{
+  int32_t i0, j0;
+  u_int32_t i1;
+  int sx;
+
+  EXTRACT_WORDS (i0, i1, x);
+  sx = i0 & 0x80000000;
+  j0 = ((i0 >> 20) & 0x7ff) - 0x3ff;
+  if (j0 < 20)
+    {
+      if (j0 < 0)
+	/* The magnitude of the number is < 1 so the result is +-0.  */
+	INSERT_WORDS (x, sx, 0);
+      else
+	INSERT_WORDS (x, sx | (i0 & ~(0x000fffff >> j0)), 0);
+    }
+  else if (j0 > 51)
+    {
+      if (j0 == 0x400)
+	/* x is inf or NaN.  */
+	return x + x;
+    }
+  else
+    {
+      INSERT_WORDS (x, i0, i1 & ~(0xffffffffu >> (j0 - 20)));
+    }
+
+  return x;
+}

-- 
Kevin Day



More information about the uClibc mailing list