Home | History | Annotate | Line # | Download | only in src
      1 /* MPFR internal header related to intmax_t.
      2 
      3 Copyright 2004-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 /* If you need something defined here, do not include this header file
     24    directly, but define the MPFR_NEED_INTMAX_H macro before including
     25    "mpfr-impl.h" (or "mpfr-test.h" in the tests). This will ensure that
     26    this header file "mpfr-intmax.h" is included in a consistent way,
     27    thus avoiding the various cases that could otherwise be obtained on
     28    different platforms and compilation options. Note that in particular,
     29    it needs <limits.h> (always included first in "mpfr-impl.h") if
     30    <inttypes.h> or <stdint.h> does not exist or does not work. It also
     31    needs "config.h" if used (HAVE_CONFIG_H defined), but "config.h" is
     32    also included first in "mpfr-impl.h". */
     33 
     34 #ifndef MPFR_NEED_INTMAX_H
     35 # error "Never include mpfr-intmax.h directly; define MPFR_NEED_INTMAX_H instead."
     36 #endif
     37 
     38 #ifndef __MPFR_INTMAX_H__
     39 #define __MPFR_INTMAX_H__
     40 
     41 /* The ISO C99 standard specifies that in C++ implementations the
     42  * INTMAX_MAX, ... macros should only be defined if explicitly requested.
     43  */
     44 #if defined __cplusplus
     45 # define __STDC_LIMIT_MACROS
     46 # define __STDC_CONSTANT_MACROS
     47 #endif
     48 
     49 /* The definition of MPFR_USE_INTMAX_T is needed on systems for which
     50  * the current (non-standard) macro tests in mpfr.h is not sufficient.
     51  * This will force the support of intmax_t/uintmax_t if <inttypes.h>
     52  * and/or <stdint.h> are available. This also avoids a failure in the
     53  * tests (replace the macro tests in mpfr.h by just
     54  *   #if defined (MPFR_USE_INTMAX_T)
     55  * to simulate such a system and reproduce the problem).
     56  * Note: if this makes the build fail on some systems (because these
     57  * headers are broken), we will need a configure test to undefine
     58  * HAVE_INTTYPES_H and HAVE_STDINT_H in such a case.
     59  */
     60 
     61 #if HAVE_INTTYPES_H
     62 # include <inttypes.h>
     63 # define MPFR_USE_INTMAX_T
     64 #endif
     65 
     66 #if HAVE_STDINT_H
     67 # include <stdint.h>
     68 # define MPFR_USE_INTMAX_T
     69 #endif
     70 
     71 /* Largest integer type available and fully working for the MPFR build
     72    (may be smaller than intmax_t / uintmax_t if NPRINTF_J is defined). */
     73 #if defined(MPFR_USE_INTMAX_T) && !defined(NPRINTF_J)
     74 typedef uintmax_t mpfr_uintmax_t;
     75 typedef intmax_t mpfr_intmax_t;
     76 # define MPFR_UINTMAX_MAX UINTMAX_MAX
     77 # define MPFR_INTMAX_MAX INTMAX_MAX
     78 # define MPFR_INTMAX_MIN INTMAX_MIN
     79 # define MPFR_INTMAX_FSPEC "j"
     80 #elif defined(HAVE_LONG_LONG) && !defined(NPRINTF_LL)
     81 typedef unsigned long long mpfr_uintmax_t;
     82 typedef long long mpfr_intmax_t;
     83 #if defined(ULLONG_MAX)
     84 /* standard */
     85 # define MPFR_UINTMAX_MAX ULLONG_MAX
     86 # define MPFR_INTMAX_MAX LLONG_MAX
     87 # define MPFR_INTMAX_MIN LLONG_MIN
     88 #elif defined(ULONGLONG_MAX)
     89 /* Silicon Graphics IRIX 6.5 with native /usr/bin/cc */
     90 # define MPFR_UINTMAX_MAX ULONGLONG_MAX
     91 # define MPFR_INTMAX_MAX LONGLONG_MAX
     92 # define MPFR_INTMAX_MIN LONGLONG_MIN
     93 #else
     94 /* We do not know any platform in this case (but this case would be a bug).
     95    Let's give an error. A workaround should be possible, but it should be
     96    tested first. */
     97 # error "Neither ULLONG_MAX nor ULONGLONG_MAX is defined."
     98 #endif
     99 # define MPFR_INTMAX_FSPEC "ll"
    100 #else
    101 typedef unsigned long mpfr_uintmax_t;
    102 typedef long mpfr_intmax_t;
    103 # define MPFR_UINTMAX_MAX ULONG_MAX
    104 # define MPFR_INTMAX_MAX LONG_MAX
    105 # define MPFR_INTMAX_MIN LONG_MIN
    106 # define MPFR_INTMAX_FSPEC "l"
    107 #endif
    108 
    109 #endif
    110