Reimplement isinf/isnan/fpclassify.

Also move isinf and isnan into libc like everyone else.

Also move fpclassify to libc like the BSDs (but unlike glibc). We need
this to be able to upgrade our float/double/long double parsing to gdtoa.

Also add some missing aliases. We now have all of:

  isnan, __isnan, isnanf, __isnanf, isnanl, __isnanl,
  isinf, __isinf, isinff, __isinff, isinfl, __isinfl,
  __fpclassify, __fpclassifyd, __fpclassifyf, __fpclassifyl.

Bug: 13469877
Change-Id: I407ffbac06c765a6c5fffda8106c37d7db04f27d
This commit is contained in:
Elliott Hughes
2014-04-11 17:02:20 -07:00
parent 0558906866
commit 02c78a3867
13 changed files with 300 additions and 296 deletions

View File

@@ -1,10 +1,10 @@
LOCAL_PATH:= $(call my-dir)
# TODO: these come from from upstream's libc, not libm!
# TODO: this comes from from upstream's libc, not libm, but it's an
# implementation detail that should have hidden visibility, so it needs
# to be in whatever library the math code is in.
libm_common_src_files := \
digittoint.c \
fpclassify.c \
isinf.c \
# TODO: this is not in the BSDs.
libm_common_src_files += \
@@ -129,7 +129,6 @@ libm_common_src_files += \
upstream-freebsd/lib/msun/src/s_ilogb.c \
upstream-freebsd/lib/msun/src/s_ilogbf.c \
upstream-freebsd/lib/msun/src/s_isfinite.c \
upstream-freebsd/lib/msun/src/s_isnan.c \
upstream-freebsd/lib/msun/src/s_isnormal.c \
upstream-freebsd/lib/msun/src/s_llrint.c \
upstream-freebsd/lib/msun/src/s_llrintf.c \
@@ -237,6 +236,7 @@ libm_common_cflags := \
-include $(LOCAL_PATH)/freebsd-compat.h \
libm_common_includes := $(LOCAL_PATH)/upstream-freebsd/lib/msun/src/
libm_ld_includes := $(LOCAL_PATH)/upstream-freebsd/lib/msun/ld128/
#

View File

@@ -17,11 +17,6 @@
#include <float.h>
#include <math.h>
extern int __isinf(double); /* isinf.c */
int (isinf)(double a1) { return __isinf(a1); }
int (isnanf)(float a1) { return __isnanf(a1); }
/*
* On LP64 sizeof(long double) > sizeof(double) so functions which fall back
* to their double variants lose precision. Emit a warning whenever something
@@ -58,10 +53,7 @@ WARN_IMPRECISE(powl)
* that call the regular "double" function.
*/
int __fpclassifyl(long double a1) { return __fpclassifyd(a1); }
int __isfinitel(long double a1) { return __isfinite(a1); }
int __isinfl(long double a1) { return __isinf(a1); }
int __isnanl(long double a1) { return (isnan)(a1); }
int __isnormall(long double a1) { return __isnormal(a1); }
int __signbitl(long double a1) { return __signbit(a1); }

View File

@@ -1,94 +0,0 @@
/*-
* Copyright (c) 2003 Mike Barcroft <mike@FreeBSD.org>
* Copyright (c) 2002, 2003 David Schultz <das@FreeBSD.ORG>
* 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.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR 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 AUTHOR 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.
*
* $FreeBSD: src/lib/libc/gen/fpclassify.c,v 1.2 2005/02/06 03:23:31 das Exp $
*/
#include <sys/endian.h>
#include <math.h>
#include <stdint.h>
#include "fpmath.h"
int
__fpclassifyf(float f)
{
union IEEEf2bits u;
u.f = f;
if (u.bits.exp == 0) {
if (u.bits.man == 0)
return (FP_ZERO);
return (FP_SUBNORMAL);
}
if (u.bits.exp == 255) {
if (u.bits.man == 0)
return (FP_INFINITE);
return (FP_NAN);
}
return (FP_NORMAL);
}
int
__fpclassifyd(double d)
{
union IEEEd2bits u;
u.d = d;
if (u.bits.exp == 0) {
if ((u.bits.manl | u.bits.manh) == 0)
return (FP_ZERO);
return (FP_SUBNORMAL);
}
if (u.bits.exp == 2047) {
if ((u.bits.manl | u.bits.manh) == 0)
return (FP_INFINITE);
return (FP_NAN);
}
return (FP_NORMAL);
}
int
__fpclassifyl(long double e)
{
union IEEEl2bits u;
u.e = e;
if (u.bits.exp == 0) {
if ((u.bits.manl | u.bits.manh) == 0)
return (FP_ZERO);
return (FP_SUBNORMAL);
}
mask_nbit_l(u); /* Mask normalization bit if applicable. */
if (u.bits.exp == 32767) {
if ((u.bits.manl | u.bits.manh) == 0)
return (FP_INFINITE);
return (FP_NAN);
}
return (FP_NORMAL);
}

View File

@@ -1,68 +0,0 @@
/*-
* Copyright (c) 2004 David Schultz <das@FreeBSD.ORG>
* 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.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR 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 AUTHOR 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.
*
* $FreeBSD$
*/
#include <math.h>
#include <sys/cdefs.h>
#include "fpmath.h"
/*
* XXX These routines belong in libm, but they must remain in libc for
* binary compat until we can bump libm's major version number.
*/
int
__isinf(double d)
{
union IEEEd2bits u;
u.d = d;
return (u.bits.exp == 2047 && u.bits.manl == 0 && u.bits.manh == 0);
}
int
__isinff(float f)
{
union IEEEf2bits u;
u.f = f;
return (u.bits.exp == 255 && u.bits.man == 0);
}
int
__isinfl(long double e)
{
union IEEEl2bits u;
u.e = e;
mask_nbit_l(u);
#ifndef __alpha__
return (u.bits.exp == 32767 && u.bits.manl == 0 && u.bits.manh == 0);
#else
return (u.bits.exp == 2047 && u.bits.manl == 0 && u.bits.manh == 0);
#endif
}

View File

@@ -1,65 +0,0 @@
/*-
* Copyright (c) 2004 David Schultz <das@FreeBSD.ORG>
* 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.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR 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 AUTHOR 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.
*
* $FreeBSD$
*/
#include <math.h>
#include "fpmath.h"
/* Provided by libc.so */
#ifndef PIC
#undef isnan
int
isnan(double d)
{
union IEEEd2bits u;
u.d = d;
return (u.bits.exp == 2047 && (u.bits.manl != 0 || u.bits.manh != 0));
}
#endif /* !PIC */
int
__isnanf(float f)
{
union IEEEf2bits u;
u.f = f;
return (u.bits.exp == 255 && u.bits.man != 0);
}
int
__isnanl(long double e)
{
union IEEEl2bits u;
u.e = e;
mask_nbit_l(u);
return (u.bits.exp == 32767 && (u.bits.manl != 0 || u.bits.manh != 0));
}
__weak_reference(__isnanf, isnanf);