[libcxx+newlib] nexttoward{f,l,} shims for _LDBL_EQ_DBL systems

Jonathan Roelofs jonathan@codesourcery.com
Wed Dec 3 15:10:00 GMT 2014


Sigh, trying again with less context in the diff. Apparently the mailer daemon 
doesn't like big patches.


Jon

On 12/2/14 4:58 PM, Jonathan Roelofs wrote:
> JF reminds me that I forgot to attach the patch... oops.
>
> Jon
>
> On 12/2/14 4:45 PM, Jonathan Roelofs wrote:
>> Here's a rebased version with the broken nexttowardf replaced by the one from
>> musl.
>>
>>
>> Cheers,
>>
>> Jon
>>
>> On 12/2/14 11:55 AM, Jonathan Roelofs wrote:
>>> JF,
>>>
>>> No. I've been told by one of my coworkers that nexttowardf cannot be implemented
>>> correctly on top of nextafterf. For that one we'll need a different
>>> implementation, perhaps we can copy the one in musl?... I'm not sure if the
>>> license is compatible with doing that.
>>>
>>> Everything in this patch aside from the nexttowardf implementation is ready to
>>> go as far as I'm concerned. Craig, how about on your end?
>>>
>>>
>>> Cheers,
>>>
>>> Jon
>>>
>>> On 12/2/14 11:45 AM, JF Bastien wrote:
>>>> Is this change ready to land?
>>>>
>>>> On Wed, Sep 24, 2014 at 9:38 AM, Jonathan Roelofs
>>>> <jonathan@codesourcery.com> wrote:
>>>>> Craig,
>>>>>
>>>>> Thanks for the review!
>>>>>
>>>>> On 9/23/14 7:17 PM, Craig Howland wrote:
>>>>>>
>>>>>> On 09/23/2014 04:33 PM, Jonathan Roelofs wrote:
>>>>>>>
>>>>>>> Please see the attached patch.
>>>>>>>
>>>>>> Needs a ChangeLog entry.  Missing the patch for libm/common/Makefile.am.
>>>>>
>>>>> Oops. I'm not very familiar with Newlib processes, so please let me know if
>>>>> I'm missing other things that you'd normally expect from such an upstream
>>>>> submission.... For example, some of my co-workers reminded me to also update
>>>>> COPYING.NEWLIB, and add copyright headers to the new files.
>>>>>>
>>>>>>
>>>>>> Operationally, all looks OK, but aesthetically, the use of casting is
>>>>>> inconsistent.  For example:
>>>>>>
>>>>>> +long double
>>>>>> +nexttowardl (long double x, long double y)
>>>>>> +{
>>>>>> +  return nextafter((double)x, (double)y);
>>>>>> +}
>>>>>>
>>>>>> The x and y arguments are cast to double to pass to nextafter(), but the
>>>>>> return
>>>>>> value is not cast to long double.  It works OK, but could be confusing.
>>>>>> Are the
>>>>>> casts intended to highlight the spots where the double/long double
>>>>>> equality is
>>>>>> required?
>>>>>
>>>>> Yeah, that was my intent.
>>>>>>
>>>>>> If so, it would probably be better to either cast in all places where
>>>>>> it is theoretically needed, or to just leave them out.  The other existing
>>>>>> functions of this style left all of them out (i.e. no casts from ld to d
>>>>>> or d to
>>>>>> ld)--see, for example, libm/common/nextafterl.c.  I suggest that the
>>>>>> precedent
>>>>>> is the better way.
>>>>>
>>>>> Agreed, consistency is probably better.
>>>>>
>>>>> Attached is a new version of this patch with these things addressed, and
>>>>> I've added another similar shim for log2l.
>>>>>
>>>>>
>>>>> Cheers,
>>>>>
>>>>> Jon
>>>>>>
>>>>>>
>>>>>> Craig
>>>>>
>>>>>
>>>>> --
>>>>> Jon Roelofs
>>>>> jonathan@codesourcery.com
>>>>> CodeSourcery / Mentor Embedded
>>>
>>
>

-- 
Jon Roelofs
jonathan@codesourcery.com
CodeSourcery / Mentor Embedded
-------------- next part --------------
diff --git a/newlib/ChangeLog b/newlib/ChangeLog
index cf69c05..cdce050 100644
--- a/newlib/ChangeLog
+++ b/newlib/ChangeLog
@@ -1,3 +1,15 @@
+2014-12-02  Jonathan Roelofs  <jonathan@codesourcery.com>
+
+	* libc/include/math.h: Add log2l,logbl,nexttowardf,
+	nexttoward,nexttowardl
+	* libm/common/Makefile.am: Reference new files
+	* libm/common/Makefile.in: Reference new files
+	* libm/common/log2l.c: New File
+	* libm/common/logbl.c: Likewise
+	* libm/common/nexttowardf.c: Likewise
+	* libm/common/nexttoward.c: Likewise
+	* libm/common/nexttowardl.c: Likewise
+
 2014-11-28  Matthew Fortune  <matthew.fortune@imgtec.com>
 
 	* libc/include/machine/setjmp.h [__mips__]: Remove __mips_fpr == 64
diff --git a/newlib/libc/include/math.h b/newlib/libc/include/math.h
index 6717cb3..d16ce30 100644
--- a/newlib/libc/include/math.h
+++ b/newlib/libc/include/math.h
@@ -423,6 +423,11 @@ extern int ilogbl _PARAMS((long double));
 extern long double asinhl _PARAMS((long double));
 extern long double cbrtl _PARAMS((long double));
 extern long double nextafterl _PARAMS((long double, long double));
+extern float nexttowardf _PARAMS((float, long double));
+extern double nexttoward _PARAMS((double, long double));
+extern long double nexttowardl _PARAMS((long double, long double));
+extern long double logbl _PARAMS((long double));
+extern long double log2l _PARAMS((long double));
 extern long double rintl _PARAMS((long double));
 extern long double scalbnl _PARAMS((long double, int));
 extern long double exp2l _PARAMS((long double));
diff --git a/newlib/libm/common/Makefile.am b/newlib/libm/common/Makefile.am
index ed556ef..b370b2e 100644
--- a/newlib/libm/common/Makefile.am
+++ b/newlib/libm/common/Makefile.am
@@ -30,7 +30,9 @@ lsrc =	atanl.c cosl.c sinl.c tanl.c tanhl.c frexpl.c modfl.c ceill.c fabsl.c \
 	copysignl.c nanl.c ilogbl.c asinhl.c cbrtl.c nextafterl.c rintl.c \
 	scalbnl.c exp2l.c scalblnl.c tgammal.c nearbyintl.c lrintl.c llrintl.c \
 	roundl.c lroundl.c llroundl.c truncl.c remquol.c fdiml.c fmaxl.c fminl.c \
-	fmal.c acoshl.c atanhl.c remainderl.c lgammal.c erfl.c erfcl.c
+	fmal.c acoshl.c atanhl.c remainderl.c lgammal.c erfl.c erfcl.c \
+	logbl.c nexttowardf.c nexttoward.c nexttowardl.c log2l.c
+
 
 libcommon_la_LDFLAGS = -Xcompiler -nostdlib
 
diff --git a/newlib/libm/common/Makefile.in b/newlib/libm/common/Makefile.in
index 12b5a30..7964531 100644
--- a/newlib/libm/common/Makefile.in
+++ b/newlib/libm/common/Makefile.in
@@ -136,7 +136,11 @@ am__objects_3 = lib_a-atanl.$(OBJEXT) lib_a-cosl.$(OBJEXT) \
 	lib_a-fminl.$(OBJEXT) lib_a-fmal.$(OBJEXT) \
 	lib_a-acoshl.$(OBJEXT) lib_a-atanhl.$(OBJEXT) \
 	lib_a-remainderl.$(OBJEXT) lib_a-lgammal.$(OBJEXT) \
-	lib_a-erfl.$(OBJEXT) lib_a-erfcl.$(OBJEXT)
+	lib_a-erfl.$(OBJEXT) lib_a-erfcl.$(OBJEXT) \
+	lib_a-logbl.$(OBJEXT) lib_a-nexttoward.$(OBJEXT) \
+	lib_a-nexttowardl.$(OBJEXT) lib_a-nexttowardf.$(OBJEXT) \
+	lib_a-log2l.$(OBJEXT)
+
 @HAVE_LONG_DOUBLE_TRUE@@USE_LIBTOOL_FALSE@am__objects_4 =  \
 @HAVE_LONG_DOUBLE_TRUE@@USE_LIBTOOL_FALSE@	$(am__objects_3)
 @USE_LIBTOOL_FALSE@am_lib_a_OBJECTS = $(am__objects_1) \
@@ -169,7 +173,8 @@ am__objects_7 = atanl.lo cosl.lo sinl.lo tanl.lo tanhl.lo frexpl.lo \
 	tgammal.lo nearbyintl.lo lrintl.lo llrintl.lo roundl.lo \
 	lroundl.lo llroundl.lo truncl.lo remquol.lo fdiml.lo fmaxl.lo \
 	fminl.lo fmal.lo acoshl.lo atanhl.lo remainderl.lo lgammal.lo \
-	erfl.lo erfcl.lo
+	erfl.lo erfcl.lo logbl.lo nexttowardf.lo nexttoward.lo nexttowardl.lo \
+	log2l.lo
 @HAVE_LONG_DOUBLE_TRUE@@USE_LIBTOOL_TRUE@am__objects_8 =  \
 @HAVE_LONG_DOUBLE_TRUE@@USE_LIBTOOL_TRUE@	$(am__objects_7)
 @USE_LIBTOOL_TRUE@am_libcommon_la_OBJECTS = $(am__objects_5) \
@@ -355,7 +360,8 @@ lsrc = atanl.c cosl.c sinl.c tanl.c tanhl.c frexpl.c modfl.c ceill.c fabsl.c \
 	copysignl.c nanl.c ilogbl.c asinhl.c cbrtl.c nextafterl.c rintl.c \
 	scalbnl.c exp2l.c scalblnl.c tgammal.c nearbyintl.c lrintl.c llrintl.c \
 	roundl.c lroundl.c llroundl.c truncl.c remquol.c fdiml.c fmaxl.c fminl.c \
-	fmal.c acoshl.c atanhl.c remainderl.c lgammal.c erfl.c erfcl.c
+	fmal.c acoshl.c atanhl.c remainderl.c lgammal.c erfl.c erfcl.c \
+	logbl.c nexttowardf.c nexttoward.c nexttowardl.c log2l.c
 
 libcommon_la_LDFLAGS = -Xcompiler -nostdlib
 @USE_LIBTOOL_TRUE@noinst_LTLIBRARIES = libcommon.la
@@ -1065,6 +1071,36 @@ lib_a-nextafterl.o: nextafterl.c
 lib_a-nextafterl.obj: nextafterl.c
 	$(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(lib_a_CFLAGS) $(CFLAGS) -c -o lib_a-nextafterl.obj `if test -f 'nextafterl.c'; then $(CYGPATH_W) 'nextafterl.c'; else $(CYGPATH_W) '$(srcdir)/nextafterl.c'; fi`
 
+lib_a-logbl.o: logbl.c
+	$(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(lib_a_CFLAGS) $(CFLAGS) -c -o lib_a-logbl.o `test -f 'logbl.c' || echo '$(srcdir)/'`logbl.c
+
+lib_a-logbl.obj: logbl.c
+	$(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(lib_a_CFLAGS) $(CFLAGS) -c -o lib_a-logbl.obj `if test -f 'logbl.c'; then $(CYGPATH_W) 'logbl.c'; else $(CYGPATH_W) '$(srcdir)/logbll.c'; fi`
+
+lib_a-log2l.o: log2l.c
+	$(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(lib_a_CFLAGS) $(CFLAGS) -c -o lib_a-log2l.o `test -f 'log2l.c' || echo '$(srcdir)/'`log2l.c
+
+lib_a-log2l.obj: log2l.c
+	$(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(lib_a_CFLAGS) $(CFLAGS) -c -o lib_a-log2l.obj `if test -f 'log2l.c'; then $(CYGPATH_W) 'log2l.c'; else $(CYGPATH_W) '$(srcdir)/log2ll.c'; fi`
+
+lib_a-nexttowardf.o: nexttowardf.c
+	$(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(lib_a_CFLAGS) $(CFLAGS) -c -o lib_a-nexttowardf.o `test -f 'nexttowardf.c' || echo '$(srcdir)/'`nexttowardf.c
+
+lib_a-nexttowardf.obj: nexttowardf.c
+	$(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(lib_a_CFLAGS) $(CFLAGS) -c -o lib_a-nexttowardf.obj `if test -f 'nexttowardf.c'; then $(CYGPATH_W) 'nexttowardf.c'; else $(CYGPATH_W) '$(srcdir)/nexttowardfl.c'; fi`
+
+lib_a-nexttoward.o: nexttoward.c
+	$(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(lib_a_CFLAGS) $(CFLAGS) -c -o lib_a-nexttoward.o `test -f 'nexttoward.c' || echo '$(srcdir)/'`nexttoward.c
+
+lib_a-nexttoward.obj: nexttoward.c
+	$(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(lib_a_CFLAGS) $(CFLAGS) -c -o lib_a-nexttoward.obj `if test -f 'nexttoward.c'; then $(CYGPATH_W) 'nexttoward.c'; else $(CYGPATH_W) '$(srcdir)/nexttowardl.c'; fi`
+
+lib_a-nexttowardl.o: nexttowardl.c
+	$(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(lib_a_CFLAGS) $(CFLAGS) -c -o lib_a-nexttowardl.o `test -f 'nexttowardl.c' || echo '$(srcdir)/'`nexttowardl.c
+
+lib_a-nexttowardl.obj: nexttowardl.c
+	$(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(lib_a_CFLAGS) $(CFLAGS) -c -o lib_a-nexttowardl.obj `if test -f 'nexttowardl.c'; then $(CYGPATH_W) 'nexttowardl.c'; else $(CYGPATH_W) '$(srcdir)/nexttowardll.c'; fi`
+
 lib_a-rintl.o: rintl.c
 	$(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(lib_a_CFLAGS) $(CFLAGS) -c -o lib_a-rintl.o `test -f 'rintl.c' || echo '$(srcdir)/'`rintl.c
 
diff --git a/newlib/libm/common/log2l.c b/newlib/libm/common/log2l.c
new file mode 100644
index 0000000..e92e9eb
--- /dev/null
+++ b/newlib/libm/common/log2l.c
@@ -0,0 +1,38 @@
+/*
+Copyright (c) 2014 Mentor Graphics, 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:
+    * Redistributions of source code must retain the above copyright
+      notice, this list of conditions and the following disclaimer.
+    * 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.
+    * Neither the name of Mentor Graphics nor the
+      names of its contributors may be used to endorse or promote products
+      derived from this software without specific prior written permission.
+
+THIS SOFTWARE IS PROVIDED BY CODESOURCERY, INC. ``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 CODESOURCERY 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.
+*/
+
+#include <math.h>
+#include "local.h"
+
+#ifdef _LDBL_EQ_DBL
+long double
+log2l (long double x)
+{
+  return log2(x);
+}
+#endif
+
diff --git a/newlib/libm/common/logbl.c b/newlib/libm/common/logbl.c
new file mode 100644
index 0000000..59d24a2
--- /dev/null
+++ b/newlib/libm/common/logbl.c
@@ -0,0 +1,38 @@
+/*
+Copyright (c) 2014 Mentor Graphics, 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:
+    * Redistributions of source code must retain the above copyright
+      notice, this list of conditions and the following disclaimer.
+    * 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.
+    * Neither the name of Mentor Graphics nor the
+      names of its contributors may be used to endorse or promote products
+      derived from this software without specific prior written permission.
+
+THIS SOFTWARE IS PROVIDED BY CODESOURCERY, INC. ``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 CODESOURCERY 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.
+*/
+
+#include <math.h>
+#include "local.h"
+
+#ifdef _LDBL_EQ_DBL
+long double
+logbl (long double x)
+{
+  return logb(x);
+}
+#endif
+
diff --git a/newlib/libm/common/nexttoward.c b/newlib/libm/common/nexttoward.c
new file mode 100644
index 0000000..71dd71d
--- /dev/null
+++ b/newlib/libm/common/nexttoward.c
@@ -0,0 +1,38 @@
+/*
+Copyright (c) 2014 Mentor Graphics, 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:
+    * Redistributions of source code must retain the above copyright
+      notice, this list of conditions and the following disclaimer.
+    * 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.
+    * Neither the name of Mentor Graphics nor the
+      names of its contributors may be used to endorse or promote products
+      derived from this software without specific prior written permission.
+
+THIS SOFTWARE IS PROVIDED BY CODESOURCERY, INC. ``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 CODESOURCERY 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.
+*/
+
+#include <math.h>
+#include "local.h"
+
+#ifdef _LDBL_EQ_DBL
+double
+nexttoward (double x, long double y)
+{
+  return nextafter(x, y);
+}
+#endif
+
diff --git a/newlib/libm/common/nexttowardf.c b/newlib/libm/common/nexttowardf.c
new file mode 100644
index 0000000..87cabc2
--- /dev/null
+++ b/newlib/libm/common/nexttowardf.c
@@ -0,0 +1,80 @@
+/*
+ * Copyright © 2005-2014 Rich Felker, et al.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
+ * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
+ * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
+ * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
+ * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ */
+
+#include <math.h>
+#include <inttypes.h>
+#include "local.h"
+
+#define FORCE_EVAL(x) do {                   \
+ if (sizeof(x) == sizeof(float)) {           \
+   volatile float __x;                       \
+   __x = (x);                                \
+ } else if (sizeof(x) == sizeof(double)) {   \
+   volatile double __x;                      \
+   __x = (x);                                \
+ } else {                                    \
+   volatile long double __x;                 \
+   __x = (x);                                \
+ }                                           \
+} while(0)
+
+union fshape {
+  float value;
+  uint32_t bits;
+};
+
+float
+nexttowardf (float x, long double y)
+{
+  union fshape ux;
+  uint32_t e;
+
+  if (isnan(x) || isnan(y))
+    return x + y;
+  if (x == y)
+    return y;
+  ux.value = x;
+  if (x == 0) {
+    ux.bits = 1;
+    if (signbit(y))
+      ux.bits |= 0x80000000;
+  } else if (x < y) {
+    if (signbit(x))
+      ux.bits--;
+    else
+      ux.bits++;
+  } else {
+    if (signbit(x))
+      ux.bits++;
+    else
+      ux.bits--;
+  }
+  e = ux.bits & 0x7f800000;
+  /* raise overflow if ux.value is infinite and x is finite */
+  if (e == 0x7f800000)
+    FORCE_EVAL(x+x);
+  /* raise underflow if ux.value is subnormal or zero */
+  if (e == 0)
+    FORCE_EVAL(x*x + ux.value*ux.value);
+  return ux.value;
+}
diff --git a/newlib/libm/common/nexttowardl.c b/newlib/libm/common/nexttowardl.c
new file mode 100644
index 0000000..59af4ce
--- /dev/null
+++ b/newlib/libm/common/nexttowardl.c
@@ -0,0 +1,38 @@
+/*
+Copyright (c) 2014 Mentor Graphics, 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:
+    * Redistributions of source code must retain the above copyright
+      notice, this list of conditions and the following disclaimer.
+    * 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.
+    * Neither the name of Mentor Graphics nor the
+      names of its contributors may be used to endorse or promote products
+      derived from this software without specific prior written permission.
+
+THIS SOFTWARE IS PROVIDED BY CODESOURCERY, INC. ``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 CODESOURCERY 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.
+*/
+
+#include <math.h>
+#include "local.h"
+
+#ifdef _LDBL_EQ_DBL
+long double
+nexttowardl (long double x, long double y)
+{
+  return nextafter(x, y);
+}
+#endif
+


More information about the Newlib mailing list