Home | History | Annotate | Line # | Download | only in src
      1 /* auxiliary data to generate special IEEE floats (NaN, +Inf, -Inf)
      2 
      3 Copyright 1999-2023 Free Software Foundation, Inc.
      4 Contributed by the AriC and Caramba projects, INRIA.
      5 
      6 This file is part of the GNU MPFR Library.
      7 
      8 The GNU MPFR Library is free software; you can redistribute it and/or modify
      9 it under the terms of the GNU Lesser General Public License as published by
     10 the Free Software Foundation; either version 3 of the License, or (at your
     11 option) any later version.
     12 
     13 The GNU MPFR Library is distributed in the hope that it will be useful, but
     14 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
     15 or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
     16 License for more details.
     17 
     18 You should have received a copy of the GNU Lesser General Public License
     19 along with the GNU MPFR Library; see the file COPYING.LESSER.  If not, see
     20 https://www.gnu.org/licenses/ or write to the Free Software Foundation, Inc.,
     21 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. */
     22 
     23 /* "double" NaN and infinities are written as explicit bytes to be sure of
     24    getting what we want, and to be sure of not depending on libm.
     25 
     26    Could use 4-byte "float" values and let the code convert them, but it
     27    seems more direct to give exactly what we want.  Certainly for gcc 3.0.2
     28    on alphaev56-unknown-freebsd4.3 the NaN must be 8-bytes, since that
     29    compiler+system was seen incorrectly converting from a "float" NaN.  */
     30 
     31 #if _MPFR_IEEE_FLOATS
     32 
     33 /* The "d" field guarantees alignment to a suitable boundary for a double.
     34    Could use a union instead, if we checked the compiler supports union
     35    initializers.  */
     36 union dbl_bytes {
     37   unsigned char b[8];
     38   double d;
     39 };
     40 
     41 #define MPFR_DBL_INFP  (dbl_infp.d)
     42 #define MPFR_DBL_INFM  (dbl_infm.d)
     43 #define MPFR_DBL_NAN   DBL_NAN
     44 
     45 /* For NaN, we use DBL_NAN since the memory representation of a NaN depends
     46    on the processor: a fixed memory representation could yield either a
     47    quiet NaN (qNaN) or a signaling NaN (sNaN). For instance, HP PA-RISC
     48    is known to do the opposite way of the usual choice recommended in
     49    IEEE 754-2008; see:
     50      https://grouper.ieee.org/groups/1788/email/msg03272.html
     51 
     52    Moreover, the right choice is to generate a qNaN in particular because
     53    signaling NaNs are not supported by all compilers (note that the support
     54    must be in the compiler used to build the user-end application because
     55    this is where the sNaN will be obtained). */
     56 
     57 #ifdef HAVE_DOUBLE_IEEE_LITTLE_ENDIAN
     58 static const union dbl_bytes dbl_infp =
     59   { { 0, 0, 0, 0, 0, 0, 0xF0, 0x7F } };
     60 static const union dbl_bytes dbl_infm =
     61   { { 0, 0, 0, 0, 0, 0, 0xF0, 0xFF } };
     62 #endif
     63 
     64 #ifdef HAVE_DOUBLE_IEEE_BIG_ENDIAN
     65 static const union dbl_bytes dbl_infp =
     66   { { 0x7F, 0xF0, 0, 0, 0, 0, 0, 0 } };
     67 static const union dbl_bytes dbl_infm =
     68   { { 0xFF, 0xF0, 0, 0, 0, 0, 0, 0 } };
     69 #endif
     70 
     71 #else /* _MPFR_IEEE_FLOATS */
     72 
     73 #define MPFR_DBL_INFP DBL_POS_INF
     74 #define MPFR_DBL_INFM DBL_NEG_INF
     75 #define MPFR_DBL_NAN DBL_NAN
     76 
     77 #endif /* _MPFR_IEEE_FLOATS */
     78