Home | History | Annotate | Download | only in Include
      1 /** @file
      2   Symbols and macros to supply platform-independent interfaces to basic
      3   C language & library operations whose spellings vary across platforms.
      4 
      5   Copyright (c) 2015, Daryl McDaniel. All rights reserved.<BR>
      6   Copyright (c) 2011, Intel Corporation. All rights reserved.<BR>
      7   This program and the accompanying materials are licensed and made available under
      8   the terms and conditions of the BSD License that accompanies this distribution.
      9   The full text of the license may be found at
     10   http://opensource.org/licenses/bsd-license.
     11 
     12   THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
     13   WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
     14 **/
     15 
     16 #ifndef Py_PYPORT_H
     17 #define Py_PYPORT_H
     18 
     19 #include "pyconfig.h" /* include for defines */
     20 
     21 /* Some versions of HP-UX & Solaris need inttypes.h for int32_t,
     22    INT32_MAX, etc. */
     23 #ifdef HAVE_INTTYPES_H
     24 #include <inttypes.h>
     25 #endif
     26 
     27 #ifdef HAVE_STDINT_H
     28 #include <stdint.h>
     29 #endif
     30 
     31 /**************************************************************************
     32 Symbols and macros to supply platform-independent interfaces to basic
     33 C language & library operations whose spellings vary across platforms.
     34 
     35 Please try to make documentation here as clear as possible:  by definition,
     36 the stuff here is trying to illuminate C's darkest corners.
     37 
     38 Config #defines referenced here:
     39 
     40 SIGNED_RIGHT_SHIFT_ZERO_FILLS
     41 Meaning:  To be defined iff i>>j does not extend the sign bit when i is a
     42           signed integral type and i < 0.
     43 Used in:  Py_ARITHMETIC_RIGHT_SHIFT
     44 
     45 Py_DEBUG
     46 Meaning:  Extra checks compiled in for debug mode.
     47 Used in:  Py_SAFE_DOWNCAST
     48 
     49 HAVE_UINTPTR_T
     50 Meaning:  The C9X type uintptr_t is supported by the compiler
     51 Used in:  Py_uintptr_t
     52 
     53 HAVE_LONG_LONG
     54 Meaning:  The compiler supports the C type "long long"
     55 Used in:  PY_LONG_LONG
     56 
     57 **************************************************************************/
     58 
     59 
     60 /* For backward compatibility only. Obsolete, do not use. */
     61 #ifdef HAVE_PROTOTYPES
     62 #define Py_PROTO(x) x
     63 #else
     64 #define Py_PROTO(x) ()
     65 #endif
     66 #ifndef Py_FPROTO
     67 #define Py_FPROTO(x) Py_PROTO(x)
     68 #endif
     69 
     70 /* typedefs for some C9X-defined synonyms for integral types.
     71  *
     72  * The names in Python are exactly the same as the C9X names, except with a
     73  * Py_ prefix.  Until C9X is universally implemented, this is the only way
     74  * to ensure that Python gets reliable names that don't conflict with names
     75  * in non-Python code that are playing their own tricks to define the C9X
     76  * names.
     77  *
     78  * NOTE: don't go nuts here!  Python has no use for *most* of the C9X
     79  * integral synonyms.  Only define the ones we actually need.
     80  */
     81 
     82 #ifdef HAVE_LONG_LONG
     83 #ifndef PY_LONG_LONG
     84 #define PY_LONG_LONG long long
     85 #if defined(LLONG_MAX)
     86 /* If LLONG_MAX is defined in limits.h, use that. */
     87 #define PY_LLONG_MIN LLONG_MIN
     88 #define PY_LLONG_MAX LLONG_MAX
     89 #define PY_ULLONG_MAX ULLONG_MAX
     90 #elif defined(__LONG_LONG_MAX__)
     91 /* Otherwise, if GCC has a builtin define, use that. */
     92 #define PY_LLONG_MAX __LONG_LONG_MAX__
     93 #define PY_LLONG_MIN (-PY_LLONG_MAX-1)
     94 #define PY_ULLONG_MAX (__LONG_LONG_MAX__*2ULL + 1ULL)
     95 #else
     96 /* Otherwise, rely on two's complement. */
     97 #define PY_ULLONG_MAX (~0ULL)
     98 #define PY_LLONG_MAX  ((long long)(PY_ULLONG_MAX>>1))
     99 #define PY_LLONG_MIN (-PY_LLONG_MAX-1)
    100 #endif /* LLONG_MAX */
    101 #endif
    102 #endif /* HAVE_LONG_LONG */
    103 
    104 /* a build with 30-bit digits for Python long integers needs an exact-width
    105  * 32-bit unsigned integer type to store those digits.  (We could just use
    106  * type 'unsigned long', but that would be wasteful on a system where longs
    107  * are 64-bits.)  On Unix systems, the autoconf macro AC_TYPE_UINT32_T defines
    108  * uint32_t to be such a type unless stdint.h or inttypes.h defines uint32_t.
    109  * However, it doesn't set HAVE_UINT32_T, so we do that here.
    110  */
    111 #ifdef uint32_t
    112 #define HAVE_UINT32_T 1
    113 #endif
    114 
    115 #ifdef HAVE_UINT32_T
    116 #ifndef PY_UINT32_T
    117 #define PY_UINT32_T uint32_t
    118 #endif
    119 #endif
    120 
    121 /* Macros for a 64-bit unsigned integer type; used for type 'twodigits' in the
    122  * long integer implementation, when 30-bit digits are enabled.
    123  */
    124 #ifdef uint64_t
    125 #define HAVE_UINT64_T 1
    126 #endif
    127 
    128 #ifdef HAVE_UINT64_T
    129 #ifndef PY_UINT64_T
    130 #define PY_UINT64_T uint64_t
    131 #endif
    132 #endif
    133 
    134 /* Signed variants of the above */
    135 #ifdef int32_t
    136 #define HAVE_INT32_T 1
    137 #endif
    138 
    139 #ifdef HAVE_INT32_T
    140 #ifndef PY_INT32_T
    141 #define PY_INT32_T int32_t
    142 #endif
    143 #endif
    144 
    145 #ifdef int64_t
    146 #define HAVE_INT64_T 1
    147 #endif
    148 
    149 #ifdef HAVE_INT64_T
    150 #ifndef PY_INT64_T
    151 #define PY_INT64_T int64_t
    152 #endif
    153 #endif
    154 
    155 /* If PYLONG_BITS_IN_DIGIT is not defined then we'll use 30-bit digits if all
    156    the necessary integer types are available, and we're on a 64-bit platform
    157    (as determined by SIZEOF_VOID_P); otherwise we use 15-bit digits. */
    158 
    159 #ifndef PYLONG_BITS_IN_DIGIT
    160 #if (defined HAVE_UINT64_T && defined HAVE_INT64_T && \
    161      defined HAVE_UINT32_T && defined HAVE_INT32_T && SIZEOF_VOID_P >= 8)
    162 #define PYLONG_BITS_IN_DIGIT 30
    163 #else
    164 #define PYLONG_BITS_IN_DIGIT 15
    165 #endif
    166 #endif
    167 
    168 /* uintptr_t is the C9X name for an unsigned integral type such that a
    169  * legitimate void* can be cast to uintptr_t and then back to void* again
    170  * without loss of information.  Similarly for intptr_t, wrt a signed
    171  * integral type.
    172  */
    173 #ifdef HAVE_UINTPTR_T
    174 typedef uintptr_t       Py_uintptr_t;
    175 typedef intptr_t        Py_intptr_t;
    176 
    177 #elif SIZEOF_VOID_P <= SIZEOF_INT
    178 typedef unsigned int    Py_uintptr_t;
    179 typedef int             Py_intptr_t;
    180 
    181 #elif SIZEOF_VOID_P <= SIZEOF_LONG
    182 typedef unsigned long   Py_uintptr_t;
    183 typedef long            Py_intptr_t;
    184 
    185 #elif defined(HAVE_LONG_LONG) && (SIZEOF_VOID_P <= SIZEOF_LONG_LONG)
    186 typedef unsigned PY_LONG_LONG   Py_uintptr_t;
    187 typedef PY_LONG_LONG            Py_intptr_t;
    188 
    189 #else
    190 #   error "Python needs a typedef for Py_uintptr_t in pyport.h."
    191 #endif /* HAVE_UINTPTR_T */
    192 
    193 /* Py_ssize_t is a signed integral type such that sizeof(Py_ssize_t) ==
    194  * sizeof(size_t).  C99 doesn't define such a thing directly (size_t is an
    195  * unsigned integral type).  See PEP 353 for details.
    196  */
    197 #ifdef HAVE_SSIZE_T
    198 typedef ssize_t         Py_ssize_t;
    199 #elif SIZEOF_VOID_P == SIZEOF_SIZE_T
    200 typedef Py_intptr_t     Py_ssize_t;
    201 #else
    202 #   error "Python needs a typedef for Py_ssize_t in pyport.h."
    203 #endif
    204 
    205 /* Largest possible value of size_t.
    206    SIZE_MAX is part of C99, so it might be defined on some
    207    platforms. If it is not defined, (size_t)-1 is a portable
    208    definition for C89, due to the way signed->unsigned
    209    conversion is defined. */
    210 #ifdef SIZE_MAX
    211 #define PY_SIZE_MAX SIZE_MAX
    212 #else
    213 #define PY_SIZE_MAX ((size_t)-1)
    214 #endif
    215 
    216 /* Largest positive value of type Py_ssize_t. */
    217 #define PY_SSIZE_T_MAX ((Py_ssize_t)(((size_t)-1)>>1))
    218 /* Smallest negative value of type Py_ssize_t. */
    219 #define PY_SSIZE_T_MIN (-PY_SSIZE_T_MAX-1)
    220 
    221 #if SIZEOF_PID_T > SIZEOF_LONG
    222 #   error "Python doesn't support sizeof(pid_t) > sizeof(long)"
    223 #endif
    224 
    225 /* PY_FORMAT_SIZE_T is a platform-specific modifier for use in a printf
    226  * format to convert an argument with the width of a size_t or Py_ssize_t.
    227  * C99 introduced "z" for this purpose, but not all platforms support that;
    228  * e.g., MS compilers use "I" instead.
    229  *
    230  * These "high level" Python format functions interpret "z" correctly on
    231  * all platforms (Python interprets the format string itself, and does whatever
    232  * the platform C requires to convert a size_t/Py_ssize_t argument):
    233  *
    234  *     PyString_FromFormat
    235  *     PyErr_Format
    236  *     PyString_FromFormatV
    237  *
    238  * Lower-level uses require that you interpolate the correct format modifier
    239  * yourself (e.g., calling printf, fprintf, sprintf, PyOS_snprintf); for
    240  * example,
    241  *
    242  *     Py_ssize_t index;
    243  *     fprintf(stderr, "index %" PY_FORMAT_SIZE_T "d sucks\n", index);
    244  *
    245  * That will expand to %ld, or %Id, or to something else correct for a
    246  * Py_ssize_t on the platform.
    247  */
    248 #ifndef PY_FORMAT_SIZE_T
    249 #   if SIZEOF_SIZE_T == SIZEOF_INT && !defined(__APPLE__)
    250 #       define PY_FORMAT_SIZE_T ""
    251 #   elif SIZEOF_SIZE_T == SIZEOF_LONG
    252 #       define PY_FORMAT_SIZE_T "l"
    253 #   elif defined(MS_WINDOWS)
    254 #       define PY_FORMAT_SIZE_T "I"
    255 #   else
    256 #       error "This platform's pyconfig.h needs to define PY_FORMAT_SIZE_T"
    257 #   endif
    258 #endif
    259 
    260 /* PY_FORMAT_LONG_LONG is analogous to PY_FORMAT_SIZE_T above, but for
    261  * the long long type instead of the size_t type.  It's only available
    262  * when HAVE_LONG_LONG is defined. The "high level" Python format
    263  * functions listed above will interpret "lld" or "llu" correctly on
    264  * all platforms.
    265  */
    266 #ifdef HAVE_LONG_LONG
    267 #   ifndef PY_FORMAT_LONG_LONG
    268 #       if defined(MS_WIN64) || defined(MS_WINDOWS)
    269 #           define PY_FORMAT_LONG_LONG "I64"
    270 #       else
    271 #           error "This platform's pyconfig.h needs to define PY_FORMAT_LONG_LONG"
    272 #       endif
    273 #   endif
    274 #endif
    275 
    276 /* Py_LOCAL can be used instead of static to get the fastest possible calling
    277  * convention for functions that are local to a given module.
    278  *
    279  * Py_LOCAL_INLINE does the same thing, and also explicitly requests inlining,
    280  * for platforms that support that.
    281  *
    282  * If PY_LOCAL_AGGRESSIVE is defined before python.h is included, more
    283  * "aggressive" inlining/optimizaion is enabled for the entire module.  This
    284  * may lead to code bloat, and may slow things down for those reasons.  It may
    285  * also lead to errors, if the code relies on pointer aliasing.  Use with
    286  * care.
    287  *
    288  * NOTE: You can only use this for functions that are entirely local to a
    289  * module; functions that are exported via method tables, callbacks, etc,
    290  * should keep using static.
    291  */
    292 
    293 #undef USE_INLINE /* XXX - set via configure? */
    294 
    295 #if defined(_MSC_VER)
    296 #if defined(PY_LOCAL_AGGRESSIVE)
    297 /* enable more aggressive optimization for visual studio */
    298 //#pragma optimize("agtw", on)
    299 #pragma optimize("gt", on)    // a and w are not legal for VS2005
    300 #endif
    301 /* ignore warnings if the compiler decides not to inline a function */
    302 #pragma warning(disable: 4710)
    303 /* fastest possible local call under MSVC */
    304 #define Py_LOCAL(type) static type __fastcall
    305 #define Py_LOCAL_INLINE(type) static __inline type __fastcall
    306 #elif defined(USE_INLINE)
    307 #define Py_LOCAL(type) static type
    308 #define Py_LOCAL_INLINE(type) static inline type
    309 #else
    310 #define Py_LOCAL(type) static type
    311 #define Py_LOCAL_INLINE(type) static type
    312 #endif
    313 
    314 /* Py_MEMCPY can be used instead of memcpy in cases where the copied blocks
    315  * are often very short.  While most platforms have highly optimized code for
    316  * large transfers, the setup costs for memcpy are often quite high.  MEMCPY
    317  * solves this by doing short copies "in line".
    318  */
    319 
    320 #if defined(_MSC_VER)
    321 #define Py_MEMCPY(target, source, length) do {                          \
    322         size_t i_, n_ = (length);                                       \
    323         char *t_ = (void*) (target);                                    \
    324         const char *s_ = (void*) (source);                              \
    325         if (n_ >= 16)                                                   \
    326             memcpy(t_, s_, n_);                                         \
    327         else                                                            \
    328             for (i_ = 0; i_ < n_; i_++)                                 \
    329                 t_[i_] = s_[i_];                                        \
    330     } while (0)
    331 #else
    332 #define Py_MEMCPY memcpy
    333 #endif
    334 
    335 #include <stdlib.h>
    336 
    337 #ifdef HAVE_IEEEFP_H
    338 #include <ieeefp.h>  /* needed for 'finite' declaration on some platforms */
    339 #endif
    340 
    341 #include <math.h> /* Moved here from the math section, before extern "C" */
    342 
    343 /********************************************
    344  * WRAPPER FOR <time.h> and/or <sys/time.h> *
    345  ********************************************/
    346 
    347 #ifdef TIME_WITH_SYS_TIME
    348 #include <sys/time.h>
    349 #include <time.h>
    350 #else /* !TIME_WITH_SYS_TIME */
    351 #ifdef HAVE_SYS_TIME_H
    352 #include <sys/time.h>
    353 #else /* !HAVE_SYS_TIME_H */
    354 #include <time.h>
    355 #endif /* !HAVE_SYS_TIME_H */
    356 #endif /* !TIME_WITH_SYS_TIME */
    357 
    358 
    359 /******************************
    360  * WRAPPER FOR <sys/select.h> *
    361  ******************************/
    362 
    363 /* NB caller must include <sys/types.h> */
    364 
    365 #ifdef HAVE_SYS_SELECT_H
    366 
    367 #include <sys/select.h>
    368 
    369 #endif /* !HAVE_SYS_SELECT_H */
    370 
    371 /*******************************
    372  * stat() and fstat() fiddling *
    373  *******************************/
    374 
    375 /* We expect that stat and fstat exist on most systems.
    376  *  It's confirmed on Unix, Mac and Windows.
    377  *  If you don't have them, add
    378  *      #define DONT_HAVE_STAT
    379  * and/or
    380  *      #define DONT_HAVE_FSTAT
    381  * to your pyconfig.h. Python code beyond this should check HAVE_STAT and
    382  * HAVE_FSTAT instead.
    383  * Also
    384  *      #define HAVE_SYS_STAT_H
    385  * if <sys/stat.h> exists on your platform, and
    386  *      #define HAVE_STAT_H
    387  * if <stat.h> does.
    388  */
    389 #ifndef DONT_HAVE_STAT
    390 #define HAVE_STAT
    391 #endif
    392 
    393 #ifndef DONT_HAVE_FSTAT
    394 #define HAVE_FSTAT
    395 #endif
    396 
    397 #ifdef RISCOS
    398 #include <sys/types.h>
    399 #include "unixstuff.h"
    400 #endif
    401 
    402 #ifdef HAVE_SYS_STAT_H
    403 #if defined(PYOS_OS2) && defined(PYCC_GCC)
    404 #include <sys/types.h>
    405 #endif
    406 #include <sys/stat.h>
    407 #elif defined(HAVE_STAT_H)
    408 #include <stat.h>
    409 #endif
    410 
    411 #if defined(PYCC_VACPP)
    412 /* VisualAge C/C++ Failed to Define MountType Field in sys/stat.h */
    413 #define S_IFMT (S_IFDIR|S_IFCHR|S_IFREG)
    414 #endif
    415 
    416 #ifndef S_ISREG
    417 #define S_ISREG(x) (((x) & S_IFMT) == S_IFREG)
    418 #endif
    419 
    420 #ifndef S_ISDIR
    421 #define S_ISDIR(x) (((x) & S_IFMT) == S_IFDIR)
    422 #endif
    423 
    424 
    425 #ifdef __cplusplus
    426 /* Move this down here since some C++ #include's don't like to be included
    427    inside an extern "C" */
    428 extern "C" {
    429 #endif
    430 
    431 
    432 /* Py_ARITHMETIC_RIGHT_SHIFT
    433  * C doesn't define whether a right-shift of a signed integer sign-extends
    434  * or zero-fills.  Here a macro to force sign extension:
    435  * Py_ARITHMETIC_RIGHT_SHIFT(TYPE, I, J)
    436  *    Return I >> J, forcing sign extension.  Arithmetically, return the
    437  *    floor of I/2**J.
    438  * Requirements:
    439  *    I should have signed integer type.  In the terminology of C99, this can
    440  *    be either one of the five standard signed integer types (signed char,
    441  *    short, int, long, long long) or an extended signed integer type.
    442  *    J is an integer >= 0 and strictly less than the number of bits in the
    443  *    type of I (because C doesn't define what happens for J outside that
    444  *    range either).
    445  *    TYPE used to specify the type of I, but is now ignored.  It's been left
    446  *    in for backwards compatibility with versions <= 2.6 or 3.0.
    447  * Caution:
    448  *    I may be evaluated more than once.
    449  */
    450 #ifdef SIGNED_RIGHT_SHIFT_ZERO_FILLS
    451 #define Py_ARITHMETIC_RIGHT_SHIFT(TYPE, I, J) \
    452     ((I) < 0 ? -1-((-1-(I)) >> (J)) : (I) >> (J))
    453 #else
    454 #define Py_ARITHMETIC_RIGHT_SHIFT(TYPE, I, J) ((I) >> (J))
    455 #endif
    456 
    457 /* Py_FORCE_EXPANSION(X)
    458  * "Simply" returns its argument.  However, macro expansions within the
    459  * argument are evaluated.  This unfortunate trickery is needed to get
    460  * token-pasting to work as desired in some cases.
    461  */
    462 #define Py_FORCE_EXPANSION(X) X
    463 
    464 /* Py_SAFE_DOWNCAST(VALUE, WIDE, NARROW)
    465  * Cast VALUE to type NARROW from type WIDE.  In Py_DEBUG mode, this
    466  * assert-fails if any information is lost.
    467  * Caution:
    468  *    VALUE may be evaluated more than once.
    469  */
    470 #ifdef Py_DEBUG
    471 #define Py_SAFE_DOWNCAST(VALUE, WIDE, NARROW) \
    472     (assert((WIDE)(NARROW)(VALUE) == (VALUE)), (NARROW)(VALUE))
    473 #else
    474 #define Py_SAFE_DOWNCAST(VALUE, WIDE, NARROW) (NARROW)(VALUE)
    475 #endif
    476 
    477 /* Py_SET_ERRNO_ON_MATH_ERROR(x)
    478  * If a libm function did not set errno, but it looks like the result
    479  * overflowed or not-a-number, set errno to ERANGE or EDOM.  Set errno
    480  * to 0 before calling a libm function, and invoke this macro after,
    481  * passing the function result.
    482  * Caution:
    483  *    This isn't reliable.  See Py_OVERFLOWED comments.
    484  *    X is evaluated more than once.
    485  */
    486 #if defined(__FreeBSD__) || defined(__OpenBSD__) || (defined(__hpux) && defined(__ia64))
    487 #define _Py_SET_EDOM_FOR_NAN(X) if (isnan(X)) errno = EDOM;
    488 #else
    489 #define _Py_SET_EDOM_FOR_NAN(X) ;
    490 #endif
    491 #define Py_SET_ERRNO_ON_MATH_ERROR(X) \
    492     do { \
    493         if (errno == 0) { \
    494             if ((X) == Py_HUGE_VAL || (X) == -Py_HUGE_VAL) \
    495                 errno = ERANGE; \
    496             else _Py_SET_EDOM_FOR_NAN(X) \
    497         } \
    498     } while(0)
    499 
    500 /* Py_SET_ERANGE_ON_OVERFLOW(x)
    501  * An alias of Py_SET_ERRNO_ON_MATH_ERROR for backward-compatibility.
    502  */
    503 #define Py_SET_ERANGE_IF_OVERFLOW(X) Py_SET_ERRNO_ON_MATH_ERROR(X)
    504 
    505 /* Py_ADJUST_ERANGE1(x)
    506  * Py_ADJUST_ERANGE2(x, y)
    507  * Set errno to 0 before calling a libm function, and invoke one of these
    508  * macros after, passing the function result(s) (Py_ADJUST_ERANGE2 is useful
    509  * for functions returning complex results).  This makes two kinds of
    510  * adjustments to errno:  (A) If it looks like the platform libm set
    511  * errno=ERANGE due to underflow, clear errno. (B) If it looks like the
    512  * platform libm overflowed but didn't set errno, force errno to ERANGE.  In
    513  * effect, we're trying to force a useful implementation of C89 errno
    514  * behavior.
    515  * Caution:
    516  *    This isn't reliable.  See Py_OVERFLOWED comments.
    517  *    X and Y may be evaluated more than once.
    518  */
    519 #define Py_ADJUST_ERANGE1(X)                                            \
    520     do {                                                                \
    521         if (errno == 0) {                                               \
    522             if ((X) == Py_HUGE_VAL || (X) == -Py_HUGE_VAL)              \
    523                 errno = ERANGE;                                         \
    524         }                                                               \
    525         else if (errno == ERANGE && (X) == 0.0)                         \
    526             errno = 0;                                                  \
    527     } while(0)
    528 
    529 #define Py_ADJUST_ERANGE2(X, Y)                                         \
    530     do {                                                                \
    531         if ((X) == Py_HUGE_VAL || (X) == -Py_HUGE_VAL ||                \
    532             (Y) == Py_HUGE_VAL || (Y) == -Py_HUGE_VAL) {                \
    533                         if (errno == 0)                                 \
    534                                 errno = ERANGE;                         \
    535         }                                                               \
    536         else if (errno == ERANGE)                                       \
    537             errno = 0;                                                  \
    538     } while(0)
    539 
    540 /*  The functions _Py_dg_strtod and _Py_dg_dtoa in Python/dtoa.c (which are
    541  *  required to support the short float repr introduced in Python 3.1) require
    542  *  that the floating-point unit that's being used for arithmetic operations
    543  *  on C doubles is set to use 53-bit precision.  It also requires that the
    544  *  FPU rounding mode is round-half-to-even, but that's less often an issue.
    545  *
    546  *  If your FPU isn't already set to 53-bit precision/round-half-to-even, and
    547  *  you want to make use of _Py_dg_strtod and _Py_dg_dtoa, then you should
    548  *
    549  *     #define HAVE_PY_SET_53BIT_PRECISION 1
    550  *
    551  *  and also give appropriate definitions for the following three macros:
    552  *
    553  *    _PY_SET_53BIT_PRECISION_START : store original FPU settings, and
    554  *        set FPU to 53-bit precision/round-half-to-even
    555  *    _PY_SET_53BIT_PRECISION_END : restore original FPU settings
    556  *    _PY_SET_53BIT_PRECISION_HEADER : any variable declarations needed to
    557  *        use the two macros above.
    558  *
    559  * The macros are designed to be used within a single C function: see
    560  * Python/pystrtod.c for an example of their use.
    561  */
    562 
    563 /* get and set x87 control word for gcc/x86 */
    564 #ifdef HAVE_GCC_ASM_FOR_X87
    565 #define HAVE_PY_SET_53BIT_PRECISION 1
    566 /* _Py_get/set_387controlword functions are defined in Python/pymath.c */
    567 #define _Py_SET_53BIT_PRECISION_HEADER                          \
    568     unsigned short old_387controlword, new_387controlword
    569 #define _Py_SET_53BIT_PRECISION_START                                   \
    570     do {                                                                \
    571         old_387controlword = _Py_get_387controlword();                  \
    572         new_387controlword = (old_387controlword & ~0x0f00) | 0x0200; \
    573         if (new_387controlword != old_387controlword)                   \
    574             _Py_set_387controlword(new_387controlword);                 \
    575     } while (0)
    576 #define _Py_SET_53BIT_PRECISION_END                             \
    577     if (new_387controlword != old_387controlword)               \
    578         _Py_set_387controlword(old_387controlword)
    579 #endif
    580 
    581 /* get and set x87 control word for VisualStudio/x86 */
    582 #if defined(_MSC_VER) && !defined(_WIN64) && !defined(UEFI_C_SOURCE) /* x87 not supported in 64-bit */
    583 #define HAVE_PY_SET_53BIT_PRECISION 1
    584 #define _Py_SET_53BIT_PRECISION_HEADER \
    585     unsigned int old_387controlword, new_387controlword, out_387controlword
    586 /* We use the __control87_2 function to set only the x87 control word.
    587    The SSE control word is unaffected. */
    588 #define _Py_SET_53BIT_PRECISION_START                                   \
    589     do {                                                                \
    590         __control87_2(0, 0, &old_387controlword, NULL);                 \
    591         new_387controlword =                                            \
    592           (old_387controlword & ~(_MCW_PC | _MCW_RC)) | (_PC_53 | _RC_NEAR); \
    593         if (new_387controlword != old_387controlword)                   \
    594             __control87_2(new_387controlword, _MCW_PC | _MCW_RC,        \
    595                           &out_387controlword, NULL);                   \
    596     } while (0)
    597 #define _Py_SET_53BIT_PRECISION_END                                     \
    598     do {                                                                \
    599         if (new_387controlword != old_387controlword)                   \
    600             __control87_2(old_387controlword, _MCW_PC | _MCW_RC,        \
    601                           &out_387controlword, NULL);                   \
    602     } while (0)
    603 #endif
    604 
    605 /* default definitions are empty */
    606 #ifndef HAVE_PY_SET_53BIT_PRECISION
    607 #define _Py_SET_53BIT_PRECISION_HEADER
    608 #define _Py_SET_53BIT_PRECISION_START
    609 #define _Py_SET_53BIT_PRECISION_END
    610 #endif
    611 
    612 /* If we can't guarantee 53-bit precision, don't use the code
    613    in Python/dtoa.c, but fall back to standard code.  This
    614    means that repr of a float will be long (17 sig digits).
    615 
    616    Realistically, there are two things that could go wrong:
    617 
    618    (1) doubles aren't IEEE 754 doubles, or
    619    (2) we're on x86 with the rounding precision set to 64-bits
    620        (extended precision), and we don't know how to change
    621        the rounding precision.
    622  */
    623 
    624 #if !defined(DOUBLE_IS_LITTLE_ENDIAN_IEEE754) && \
    625     !defined(DOUBLE_IS_BIG_ENDIAN_IEEE754) && \
    626     !defined(DOUBLE_IS_ARM_MIXED_ENDIAN_IEEE754)
    627 #define PY_NO_SHORT_FLOAT_REPR
    628 #endif
    629 
    630 /* double rounding is symptomatic of use of extended precision on x86.  If
    631    we're seeing double rounding, and we don't have any mechanism available for
    632    changing the FPU rounding precision, then don't use Python/dtoa.c. */
    633 #if defined(X87_DOUBLE_ROUNDING) && !defined(HAVE_PY_SET_53BIT_PRECISION)
    634 #define PY_NO_SHORT_FLOAT_REPR
    635 #endif
    636 
    637 /* Py_DEPRECATED(version)
    638  * Declare a variable, type, or function deprecated.
    639  * Usage:
    640  *    extern int old_var Py_DEPRECATED(2.3);
    641  *    typedef int T1 Py_DEPRECATED(2.4);
    642  *    extern int x() Py_DEPRECATED(2.5);
    643  */
    644 #if defined(__GNUC__) && ((__GNUC__ >= 4) || \
    645               (__GNUC__ == 3) && (__GNUC_MINOR__ >= 1))
    646 #define Py_DEPRECATED(VERSION_UNUSED) __attribute__((__deprecated__))
    647 #else
    648 #define Py_DEPRECATED(VERSION_UNUSED)
    649 #endif
    650 
    651 /**************************************************************************
    652 Prototypes that are missing from the standard include files on some systems
    653 (and possibly only some versions of such systems.)
    654 
    655 Please be conservative with adding new ones, document them and enclose them
    656 in platform-specific #ifdefs.
    657 **************************************************************************/
    658 
    659 #ifdef SOLARIS
    660 /* Unchecked */
    661 extern int gethostname(char *, int);
    662 #endif
    663 
    664 #ifdef __BEOS__
    665 /* Unchecked */
    666 /* It's in the libs, but not the headers... - [cjh] */
    667 int shutdown( int, int );
    668 #endif
    669 
    670 #ifdef HAVE__GETPTY
    671 #include <sys/types.h>          /* we need to import mode_t */
    672 extern char * _getpty(int *, int, mode_t, int);
    673 #endif
    674 
    675 /* On QNX 6, struct termio must be declared by including sys/termio.h
    676    if TCGETA, TCSETA, TCSETAW, or TCSETAF are used.  sys/termio.h must
    677    be included before termios.h or it will generate an error. */
    678 #if defined(HAVE_SYS_TERMIO_H) && !defined(__hpux)
    679 #include <sys/termio.h>
    680 #endif
    681 
    682 #if defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY)
    683 #if !defined(HAVE_PTY_H) && !defined(HAVE_LIBUTIL_H) && !defined(HAVE_UTIL_H)
    684 /* BSDI does not supply a prototype for the 'openpty' and 'forkpty'
    685    functions, even though they are included in libutil. */
    686 #include <termios.h>
    687 extern int openpty(int *, int *, char *, struct termios *, struct winsize *);
    688 extern pid_t forkpty(int *, char *, struct termios *, struct winsize *);
    689 #endif /* !defined(HAVE_PTY_H) && !defined(HAVE_LIBUTIL_H) */
    690 #endif /* defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) */
    691 
    692 
    693 /* These are pulled from various places. It isn't obvious on what platforms
    694    they are necessary, nor what the exact prototype should look like (which
    695    is likely to vary between platforms!) If you find you need one of these
    696    declarations, please move them to a platform-specific block and include
    697    proper prototypes. */
    698 #if 0
    699 
    700 /* From Modules/resource.c */
    701 extern int getrusage();
    702 extern int getpagesize();
    703 
    704 /* From Python/sysmodule.c and Modules/posixmodule.c */
    705 extern int fclose(FILE *);
    706 
    707 /* From Modules/posixmodule.c */
    708 extern int fdatasync(int);
    709 #endif /* 0 */
    710 
    711 
    712 /* On 4.4BSD-descendants, ctype functions serves the whole range of
    713  * wchar_t character set rather than single byte code points only.
    714  * This characteristic can break some operations of string object
    715  * including str.upper() and str.split() on UTF-8 locales.  This
    716  * workaround was provided by Tim Robbins of FreeBSD project.
    717  */
    718 
    719 #ifdef __FreeBSD__
    720 #include <osreldate.h>
    721 #if __FreeBSD_version > 500039
    722 # define _PY_PORT_CTYPE_UTF8_ISSUE
    723 #endif
    724 #endif
    725 
    726 
    727 #if defined(__APPLE__)
    728 # define _PY_PORT_CTYPE_UTF8_ISSUE
    729 #endif
    730 
    731 #ifdef _PY_PORT_CTYPE_UTF8_ISSUE
    732 #include <ctype.h>
    733 #include <wctype.h>
    734 #undef isalnum
    735 #define isalnum(c) iswalnum(btowc(c))
    736 #undef isalpha
    737 #define isalpha(c) iswalpha(btowc(c))
    738 #undef islower
    739 #define islower(c) iswlower(btowc(c))
    740 #undef isspace
    741 #define isspace(c) iswspace(btowc(c))
    742 #undef isupper
    743 #define isupper(c) iswupper(btowc(c))
    744 #undef tolower
    745 #define tolower(c) towlower(btowc(c))
    746 #undef toupper
    747 #define toupper(c) towupper(btowc(c))
    748 #endif
    749 
    750 
    751 /* Declarations for symbol visibility.
    752 
    753   PyAPI_FUNC(type): Declares a public Python API function and return type
    754   PyAPI_DATA(type): Declares public Python data and its type
    755   PyMODINIT_FUNC:   A Python module init function.  If these functions are
    756                     inside the Python core, they are private to the core.
    757                     If in an extension module, it may be declared with
    758                     external linkage depending on the platform.
    759 
    760   As a number of platforms support/require "__declspec(dllimport/dllexport)",
    761   we support a HAVE_DECLSPEC_DLL macro to save duplication.
    762 */
    763 
    764 /*
    765   All windows ports, except cygwin, are handled in PC/pyconfig.h.
    766 
    767   BeOS and cygwin are the only other autoconf platform requiring special
    768   linkage handling and both of these use __declspec().
    769 */
    770 #if defined(__CYGWIN__) || defined(__BEOS__)
    771 #       define HAVE_DECLSPEC_DLL
    772 #endif
    773 
    774 /* only get special linkage if built as shared or platform is Cygwin */
    775 #if defined(Py_ENABLE_SHARED) || defined(__CYGWIN__)
    776 #       if defined(HAVE_DECLSPEC_DLL)
    777 #               ifdef Py_BUILD_CORE
    778 #                       define PyAPI_FUNC(RTYPE) __declspec(dllexport) RTYPE
    779 #                       define PyAPI_DATA(RTYPE) extern __declspec(dllexport) RTYPE
    780         /* module init functions inside the core need no external linkage */
    781         /* except for Cygwin to handle embedding (FIXME: BeOS too?) */
    782 #                       if defined(__CYGWIN__)
    783 #                               define PyMODINIT_FUNC __declspec(dllexport) void
    784 #                       else /* __CYGWIN__ */
    785 #                               define PyMODINIT_FUNC void
    786 #                       endif /* __CYGWIN__ */
    787 #               else /* Py_BUILD_CORE */
    788         /* Building an extension module, or an embedded situation */
    789         /* public Python functions and data are imported */
    790         /* Under Cygwin, auto-import functions to prevent compilation */
    791         /* failures similar to those described at the bottom of 4.1: */
    792         /* http://docs.python.org/extending/windows.html#a-cookbook-approach */
    793 #                       if !defined(__CYGWIN__)
    794 #                               define PyAPI_FUNC(RTYPE) __declspec(dllimport) RTYPE
    795 #                       endif /* !__CYGWIN__ */
    796 #                       define PyAPI_DATA(RTYPE) extern __declspec(dllimport) RTYPE
    797         /* module init functions outside the core must be exported */
    798 #                       if defined(__cplusplus)
    799 #                               define PyMODINIT_FUNC extern "C" __declspec(dllexport) void
    800 #                       else /* __cplusplus */
    801 #                               define PyMODINIT_FUNC __declspec(dllexport) void
    802 #                       endif /* __cplusplus */
    803 #               endif /* Py_BUILD_CORE */
    804 #       endif /* HAVE_DECLSPEC */
    805 #endif /* Py_ENABLE_SHARED */
    806 
    807 /* If no external linkage macros defined by now, create defaults */
    808 #ifndef PyAPI_FUNC
    809 #       define PyAPI_FUNC(RTYPE) RTYPE
    810 #endif
    811 #ifndef PyAPI_DATA
    812 #       define PyAPI_DATA(RTYPE) extern RTYPE
    813 #endif
    814 #ifndef PyMODINIT_FUNC
    815 #       if defined(__cplusplus)
    816 #               define PyMODINIT_FUNC extern "C" void
    817 #       else /* __cplusplus */
    818 #               define PyMODINIT_FUNC void
    819 #       endif /* __cplusplus */
    820 #endif
    821 
    822 /* Deprecated DL_IMPORT and DL_EXPORT macros */
    823 #if defined(Py_ENABLE_SHARED) && defined (HAVE_DECLSPEC_DLL)
    824 #       if defined(Py_BUILD_CORE)
    825 #               define DL_IMPORT(RTYPE) __declspec(dllexport) RTYPE
    826 #               define DL_EXPORT(RTYPE) __declspec(dllexport) RTYPE
    827 #       else
    828 #               define DL_IMPORT(RTYPE) __declspec(dllimport) RTYPE
    829 #               define DL_EXPORT(RTYPE) __declspec(dllexport) RTYPE
    830 #       endif
    831 #endif
    832 #ifndef DL_EXPORT
    833 #       define DL_EXPORT(RTYPE) RTYPE
    834 #endif
    835 #ifndef DL_IMPORT
    836 #       define DL_IMPORT(RTYPE) RTYPE
    837 #endif
    838 /* End of deprecated DL_* macros */
    839 
    840 /* If the fd manipulation macros aren't defined,
    841    here is a set that should do the job */
    842 
    843 #if 0 /* disabled and probably obsolete */
    844 
    845 #ifndef FD_SETSIZE
    846 #define FD_SETSIZE      256
    847 #endif
    848 
    849 #ifndef FD_SET
    850 
    851 typedef long fd_mask;
    852 
    853 #define NFDBITS (sizeof(fd_mask) * NBBY)        /* bits per mask */
    854 #ifndef howmany
    855 #define howmany(x, y)   (((x)+((y)-1))/(y))
    856 #endif /* howmany */
    857 
    858 typedef struct fd_set {
    859     fd_mask     fds_bits[howmany(FD_SETSIZE, NFDBITS)];
    860 } fd_set;
    861 
    862 #define FD_SET(n, p)    ((p)->fds_bits[(n)/NFDBITS] |= (1 << ((n) % NFDBITS)))
    863 #define FD_CLR(n, p)    ((p)->fds_bits[(n)/NFDBITS] &= ~(1 << ((n) % NFDBITS)))
    864 #define FD_ISSET(n, p)  ((p)->fds_bits[(n)/NFDBITS] & (1 << ((n) % NFDBITS)))
    865 #define FD_ZERO(p)      memset((char *)(p), '\0', sizeof(*(p)))
    866 
    867 #endif /* FD_SET */
    868 
    869 #endif /* fd manipulation macros */
    870 
    871 
    872 /* limits.h constants that may be missing */
    873 
    874 #ifndef INT_MAX
    875 #define INT_MAX 2147483647
    876 #endif
    877 
    878 #ifndef LONG_MAX
    879 #if SIZEOF_LONG == 4
    880 #define LONG_MAX 0X7FFFFFFFL
    881 #elif SIZEOF_LONG == 8
    882 #define LONG_MAX 0X7FFFFFFFFFFFFFFFL
    883 #else
    884 #error "could not set LONG_MAX in pyport.h"
    885 #endif
    886 #endif
    887 
    888 #ifndef LONG_MIN
    889 #define LONG_MIN (-LONG_MAX-1)
    890 #endif
    891 
    892 #ifndef LONG_BIT
    893 #define LONG_BIT (8 * SIZEOF_LONG)
    894 #endif
    895 
    896 #if LONG_BIT != 8 * SIZEOF_LONG
    897 /* 04-Oct-2000 LONG_BIT is apparently (mis)defined as 64 on some recent
    898  * 32-bit platforms using gcc.  We try to catch that here at compile-time
    899  * rather than waiting for integer multiplication to trigger bogus
    900  * overflows.
    901  */
    902 #error "LONG_BIT definition appears wrong for platform (bad gcc/glibc config?)."
    903 #endif
    904 
    905 #ifdef __cplusplus
    906 }
    907 #endif
    908 
    909 /*
    910  * Hide GCC attributes from compilers that don't support them.
    911  */
    912 #if (!defined(__GNUC__) || __GNUC__ < 2 || \
    913      (__GNUC__ == 2 && __GNUC_MINOR__ < 7) ) && \
    914     !defined(RISCOS)
    915 #define Py_GCC_ATTRIBUTE(x)
    916 #else
    917 #define Py_GCC_ATTRIBUTE(x) __attribute__(x)
    918 #endif
    919 
    920 /*
    921  * Add PyArg_ParseTuple format where available.
    922  */
    923 #ifdef HAVE_ATTRIBUTE_FORMAT_PARSETUPLE
    924 #define Py_FORMAT_PARSETUPLE(func,p1,p2) __attribute__((format(func,p1,p2)))
    925 #else
    926 #define Py_FORMAT_PARSETUPLE(func,p1,p2)
    927 #endif
    928 
    929 /*
    930  * Specify alignment on compilers that support it.
    931  */
    932 #if defined(__GNUC__) && __GNUC__ >= 3
    933 #define Py_ALIGNED(x) __attribute__((aligned(x)))
    934 #else
    935 #define Py_ALIGNED(x)
    936 #endif
    937 
    938 /* Eliminate end-of-loop code not reached warnings from SunPro C
    939  * when using do{...}while(0) macros
    940  */
    941 #ifdef __SUNPRO_C
    942 #pragma error_messages (off,E_END_OF_LOOP_CODE_NOT_REACHED)
    943 #endif
    944 
    945 /*
    946  * Older Microsoft compilers don't support the C99 long long literal suffixes,
    947  * so these will be defined in PC/pyconfig.h for those compilers.
    948  */
    949 #ifndef Py_LL
    950 #define Py_LL(x) x##LL
    951 #endif
    952 
    953 #ifndef Py_ULL
    954 #define Py_ULL(x) Py_LL(x##U)
    955 #endif
    956 
    957 #endif /* Py_PYPORT_H */
    958