Source-Changes-HG archive
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]
[src/trunk]: src Add FreeBSD's truncl and minimal testing.
details: https://anonhg.NetBSD.org/src/rev/878e8585f265
branches: trunk
changeset: 791323:878e8585f265
user: joerg <joerg%NetBSD.org@localhost>
date: Wed Nov 13 12:58:11 2013 +0000
description:
Add FreeBSD's truncl and minimal testing.
diffstat:
lib/libm/Makefile | 5 +-
lib/libm/src/namespace.h | 3 +-
lib/libm/src/s_trunc.c | 7 +-
lib/libm/src/s_truncl.c | 176 +++++++-----------------
tests/lib/libm/t_ceil.c | 328 ++++++++++++++++++++++++++++++++++++++++++++++-
5 files changed, 389 insertions(+), 130 deletions(-)
diffs (truncated from 622 to 300 lines):
diff -r 335671921c61 -r 878e8585f265 lib/libm/Makefile
--- a/lib/libm/Makefile Wed Nov 13 11:12:24 2013 +0000
+++ b/lib/libm/Makefile Wed Nov 13 12:58:11 2013 +0000
@@ -1,4 +1,4 @@
-# $NetBSD: Makefile,v 1.147 2013/11/12 17:36:14 joerg Exp $
+# $NetBSD: Makefile,v 1.148 2013/11/13 12:58:11 joerg Exp $
#
# @(#)Makefile 5.1beta 93/09/24
#
@@ -173,7 +173,8 @@
s_nextafterf.c s_remquo.c s_remquof.c s_rint.c s_rintf.c \
s_round.c s_roundf.c s_roundl.c s_scalbn.c \
s_scalbnf.c s_scalbnl.c s_signgam.c s_significand.c s_significandf.c s_sin.c \
- s_sinf.c s_tan.c s_tanf.c s_tanh.c s_tanhf.c s_tgammaf.c s_trunc.c s_truncf.c \
+ s_sinf.c s_tan.c s_tanf.c s_tanh.c s_tanhf.c s_tgammaf.c \
+ s_trunc.c s_truncf.c s_truncl.c \
w_acos.c w_acosf.c w_acosh.c w_acoshf.c w_asin.c w_asinf.c w_atan2.c \
w_atan2f.c w_atanh.c w_atanhf.c w_cosh.c w_coshf.c \
w_drem.c w_dremf.c w_exp.c w_expf.c w_fmod.c w_fmodf.c w_fmodl.c \
diff -r 335671921c61 -r 878e8585f265 lib/libm/src/namespace.h
--- a/lib/libm/src/namespace.h Wed Nov 13 11:12:24 2013 +0000
+++ b/lib/libm/src/namespace.h Wed Nov 13 12:58:11 2013 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: namespace.h,v 1.8 2013/11/12 17:36:14 joerg Exp $ */
+/* $NetBSD: namespace.h,v 1.9 2013/11/13 12:58:11 joerg Exp $ */
#define atan2 _atan2
#define atan2f _atan2f
@@ -50,6 +50,7 @@
#define floorl _floorl
#define roundl _roundl
#define fmodl _fmodl
+#define truncl _truncl
#define exp2l _exp2l
#define cosl _cosl
diff -r 335671921c61 -r 878e8585f265 lib/libm/src/s_trunc.c
--- a/lib/libm/src/s_trunc.c Wed Nov 13 11:12:24 2013 +0000
+++ b/lib/libm/src/s_trunc.c Wed Nov 13 12:58:11 2013 +0000
@@ -15,7 +15,7 @@
__FBSDID("$FreeBSD: src/lib/msun/src/s_trunc.c,v 1.1 2004/06/20 09:25:43 das Exp $");
#endif
#if defined(LIBM_SCCS) && !defined(lint)
-__RCSID("$NetBSD: s_trunc.c,v 1.3 2008/04/25 22:21:53 christos Exp $");
+__RCSID("$NetBSD: s_trunc.c,v 1.4 2013/11/13 12:58:11 joerg Exp $");
#endif
/*
@@ -30,6 +30,11 @@
#include "math.h"
#include "math_private.h"
+#ifndef __HAVE_LONG_DOUBLE
+__strong_alias(_truncl, trunc)
+__weak_alias(truncl, trunc)
+#endif
+
static const double huge = 1.0e300;
double
diff -r 335671921c61 -r 878e8585f265 lib/libm/src/s_truncl.c
--- a/lib/libm/src/s_truncl.c Wed Nov 13 11:12:24 2013 +0000
+++ b/lib/libm/src/s_truncl.c Wed Nov 13 12:58:11 2013 +0000
@@ -1,151 +1,79 @@
-/*-
- * Copyright (c) 2012 The NetBSD Foundation, Inc.
- * All rights reserved.
- *
- * This code is derived from software contributed to The NetBSD Foundation
- * by Matt Thomas of 3am Software Foundry.
+/*
+ * ====================================================
+ * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
*
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- * 1. Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- * 2. Redistributions in binary form must reproduce the above copyright
- * notice, this list of conditions and the following disclaimer in the
- * documentation and/or other materials provided with the distribution.
+ * Developed at SunPro, a Sun Microsystems, Inc. business.
+ * Permission to use, copy, modify, and distribute this
+ * software is freely granted, provided that this notice
+ * is preserved.
+ * ====================================================
*
- * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
- * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
- * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
- * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
- * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
- * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
- * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
- * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
- * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
- * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
- * POSSIBILITY OF SUCH DAMAGE.
+ * From: @(#)s_floor.c 5.1 93/09/24
*/
#include <sys/cdefs.h>
-#if defined(LIBM_SCCS) && !defined(lint)
-__RCSID("$NetBSD: s_truncl.c,v 1.3 2013/11/11 23:57:34 joerg Exp $");
+__RCSID("$NetBSD: s_truncl.c,v 1.4 2013/11/13 12:58:11 joerg Exp $");
+#if 0
+__FBSDID("$FreeBSD: head/lib/msun/src/s_truncl.c 176280 2008-02-14 15:10:34Z bde $");
#endif
/*
- * trunc(x)
+ * truncl(x)
* Return x rounded toward 0 to integral value
* Method:
* Bit twiddling.
* Exception:
- * Inexact flag raised if x was not equal to trunc(x).
+ * Inexact flag raised if x not equal to truncl(x).
*/
+#include "namespace.h"
+#include <float.h>
+#include <math.h>
+#include <stdint.h>
#include <machine/ieee.h>
-#include <float.h>
-#include <stdint.h>
-#include "math.h"
-#include "math_private.h"
#ifdef __HAVE_LONG_DOUBLE
-static const long double huge = LDBL_MAX;
+#ifdef __weak_alias
+__weak_alias(truncl, _truncl)
+#endif
+
+#ifdef LDBL_IMPLICIT_NBIT
+#define MANH_SIZE (EXT_FRACHBITS + 1)
+#else
+#define MANH_SIZE EXT_FRACHBITS
+#endif
+
+static const long double huge = 1.0e300;
+static const float zero[] = { 0.0, -0.0 };
long double
truncl(long double x)
{
union ieee_ext_u ux = { .extu_ld = x, };
- int32_t exponent = ux.extu_exp - EXT_EXP_BIAS;
-#if LDBL_MANT_DIG == EXT_FRACBITS
- /*
- * If there is no hidden bit, don't count it
- */
- const int32_t frach_bits = EXT_FRACHBITS - 1;
- const int32_t frac_bits = EXT_FRACBITS - 1;
-#else
- const int32_t frach_bits = EXT_FRACHBITS;
- const int32_t frac_bits = EXT_FRACBITS;
-#endif
-
- /*
- * If this number is big enough to have no fractional digits...
- * (or is an inf or nan).
- */
- if (exponent >= frac_bits) {
- if (exponent == EXT_EXP_NAN - EXT_EXP_BIAS)
- return x+x; /* x is inf or nan */
- return x; /* x is integral */
- }
-
- /*
- * If this number is too small enough to have any integral digits...
- */
- if (exponent < 0 && huge - x > 0.0) {
- /* set inexact if x != 0 */
- /* |x|<1, so return 0*sign(x) */
- return ux.extu_sign ? -0.0 : 0.0;
- }
-
- uint32_t frach_mask = __BIT(frach_bits) - 1;
-#ifdef EXT_FRACHMBITS
- uint32_t frachm_mask = __BIT(EXT_FRACHMBITS) - 1;
-#endif
-#ifdef EXT_FRACLMBITS
- uint32_t fraclm_mask = __BIT(EXT_FRACLMBITS) - 1;
-#endif
-#ifdef EXT_FRACHMBITS
- uint32_t frachl_mask = __BIT(EXT_FRACLMBITS) - 1;
-#endif
- uint32_t fracl_mask = __BIT(EXT_FRACLBITS) - 1;
+ int e = ux.extu_exp - LDBL_MAX_EXP + 1;
- if (exponent < frach_bits) {
- frach_mask >>= exponent;
-#ifdef EXT_FRACHMBITS
- } else if (exponent < frach_bits + EXT_FRACHM_BITS) {
- frach_mask = 0; exponent -= frach_bits;
- frachm_mask >>= exponent;
-#endif
-#ifdef EXT_FRACLMBITS
- } else if (exponent < frach_bits + EXT_FRACHM_BITS + EXT_FRACLMBITS) {
- frach_mask = 0; exponent -= frach_bits;
- frachm_mask = 0; exponent -= EXT_FRACHMBITS;
- fraclm_mask >>= exponent;
-#endif
- } else {
- frach_mask = 0; exponent -= frach_bits;
-#ifdef EXT_FRACHMBITS
- frachm_mask = 0; exponent -= EXT_FRACHMBITS;
-#endif
-#ifdef EXT_FRACLMBITS
- fraclm_mask = 0; exponent -= EXT_FRACLMBITS;
-#endif
- fraclm_mask >>= exponent;
+ if (e < MANH_SIZE - 1) {
+ if (e < 0) { /* raise inexact if x != 0 */
+ if (huge + x > 0.0)
+ ux.extu_ld = zero[ux.extu_sign];
+ } else {
+ uint64_t m = ((1llu << MANH_SIZE) - 1) >> (e + 1);
+ if (((ux.extu_frach & m) | ux.extu_fracl) == 0)
+ return (x); /* x is integral */
+ if (huge + x > 0.0) { /* raise inexact flag */
+ ux.extu_frach &= ~m;
+ ux.extu_fracl = 0;
+ }
+ }
+ } else if (e < LDBL_MANT_DIG - 1) {
+ uint64_t m = (uint64_t)-1 >> (64 - LDBL_MANT_DIG + e + 1);
+ if ((ux.extu_fracl & m) == 0)
+ return (x); /* x is integral */
+ if (huge + x > 0.0) /* raise inexact flag */
+ ux.extu_fracl &= ~m;
}
-
- if ((ux.extu_frach & frach_mask) == 0
-#ifdef EXT_FRACHMBITS
- && (ux.extu_frachm & frachm_mask) == 0
-#endif
-#ifdef EXT_FRACLMBITS
- && (ux.extu_fraclm & frachm_mask) == 0
-#endif
- && (ux.extu_fracl & fracl_mask) == 0)
- return x; /* x is integral */
-
- if (huge - x > 0.0) { /* set inexact flag */
- /*
- * Clear any fractional bits...
- */
- ux.extu_frach &= ~frach_mask;
-#ifdef EXT_FRACHMBITS
- ux.extu_frachm &= ~frachm_mask;
-#endif
-#ifdef EXT_FRACLMBITS
- ux.extu_fraclm &= ~fraclm_mask;
-#endif
- ux.extu_fracl &= ~fracl_mask;
- return ux.extu_ld;
- }
+ return (ux.extu_ld);
}
-#endif /* __HAVE_LONG_DOUBLE */
+#endif /* __HAVE_LONG_DOUBLE */
diff -r 335671921c61 -r 878e8585f265 tests/lib/libm/t_ceil.c
--- a/tests/lib/libm/t_ceil.c Wed Nov 13 11:12:24 2013 +0000
+++ b/tests/lib/libm/t_ceil.c Wed Nov 13 12:58:11 2013 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: t_ceil.c,v 1.8 2013/11/11 23:57:34 joerg Exp $ */
+/* $NetBSD: t_ceil.c,v 1.9 2013/11/13 12:58:11 joerg Exp $ */
/*-
* Copyright (c) 2011 The NetBSD Foundation, Inc.
@@ -29,7 +29,7 @@
* POSSIBILITY OF SUCH DAMAGE.
*/
#include <sys/cdefs.h>
-__RCSID("$NetBSD: t_ceil.c,v 1.8 2013/11/11 23:57:34 joerg Exp $");
+__RCSID("$NetBSD: t_ceil.c,v 1.9 2013/11/13 12:58:11 joerg Exp $");
#include <atf-c.h>
#include <math.h>
@@ -648,6 +648,309 @@
#endif
}
+/*
+ * trunc(3)
+ */
+ATF_TC(trunc_basic);
+ATF_TC_HEAD(trunc_basic, tc)
+{
+ atf_tc_set_md_var(tc, "descr", "A basic test of trunc(3)");
+}
+
+ATF_TC_BODY(trunc_basic, tc)
+{
Home |
Main Index |
Thread Index |
Old Index