Home | History | Annotate | Download | only in common
      1 /*
      2 ******************************************************************************
      3 *
      4 *   Copyright (C) 1997-2012, International Business Machines
      5 *   Corporation and others.  All Rights Reserved.
      6 *
      7 ******************************************************************************
      8 *
      9 *  FILE NAME : putilimp.h
     10 *
     11 *   Date        Name        Description
     12 *   10/17/04    grhoten     Move internal functions from putil.h to this file.
     13 ******************************************************************************
     14 */
     15 
     16 #ifndef PUTILIMP_H
     17 #define PUTILIMP_H
     18 
     19 #include "unicode/utypes.h"
     20 #include "unicode/putil.h"
     21 
     22 /**
     23  * \def U_SIGNED_RIGHT_SHIFT_IS_ARITHMETIC
     24  * Nearly all CPUs and compilers implement a right-shift of a signed integer
     25  * as an Arithmetic Shift Right which copies the sign bit (the Most Significant Bit (MSB))
     26  * into the vacated bits (sign extension).
     27  * For example, (int32_t)0xfff5fff3>>4 becomes 0xffff5fff and -1>>1=-1.
     28  *
     29  * This can be useful for storing a signed value in the upper bits
     30  * and another bit field in the lower bits.
     31  * The signed value can be retrieved by simple right-shifting.
     32  *
     33  * This is consistent with the Java language.
     34  *
     35  * However, the C standard allows compilers to implement a right-shift of a signed integer
     36  * as a Logical Shift Right which copies a 0 into the vacated bits.
     37  * For example, (int32_t)0xfff5fff3>>4 becomes 0x0fff5fff and -1>>1=0x7fffffff.
     38  *
     39  * Code that depends on the natural behavior should be guarded with this macro,
     40  * with an alternate path for unusual platforms.
     41  * @internal
     42  */
     43 #ifdef U_SIGNED_RIGHT_SHIFT_IS_ARITHMETIC
     44     /* Use the predefined value. */
     45 #else
     46     /*
     47      * Nearly all CPUs & compilers implement a right-shift of a signed integer
     48      * as an Arithmetic Shift Right (with sign extension).
     49      */
     50 #   define U_SIGNED_RIGHT_SHIFT_IS_ARITHMETIC 1
     51 #endif
     52 
     53 /** Define this to 1 if your platform supports IEEE 754 floating point,
     54    to 0 if it does not. */
     55 #ifndef IEEE_754
     56 #   define IEEE_754 1
     57 #endif
     58 
     59 /**
     60  * uintptr_t is an optional part of the standard definitions in stdint.h.
     61  * The opengroup.org documentation for stdint.h says
     62  * "On XSI-conformant systems, the intptr_t and uintptr_t types are required;
     63  * otherwise, they are optional."
     64  * We assume that when uintptr_t is defined, UINTPTR_MAX is defined as well.
     65  *
     66  * Do not use ptrdiff_t since it is signed. size_t is unsigned.
     67  */
     68 /* TODO: This check fails on some z environments. Filed a ticket #9357 for this. */
     69 #if !defined(__intptr_t_defined) && !defined(UINTPTR_MAX) && (U_PLATFORM != U_PF_OS390)
     70 typedef size_t uintptr_t;
     71 #endif
     72 
     73 /**
     74  * \def U_HAVE_MSVC_2003_OR_EARLIER
     75  * Flag for workaround of MSVC 2003 optimization bugs
     76  * @internal
     77  */
     78 #if !defined(U_HAVE_MSVC_2003_OR_EARLIER) && defined(_MSC_VER) && (_MSC_VER < 1400)
     79 #define U_HAVE_MSVC_2003_OR_EARLIER
     80 #endif
     81 
     82 /*===========================================================================*/
     83 /** @{ Information about POSIX support                                       */
     84 /*===========================================================================*/
     85 
     86 #ifdef U_HAVE_NL_LANGINFO_CODESET
     87     /* Use the predefined value. */
     88 #elif U_PLATFORM_HAS_WIN32_API
     89 #   define U_HAVE_NL_LANGINFO_CODESET 0
     90 #else
     91 #   define U_HAVE_NL_LANGINFO_CODESET 1
     92 #endif
     93 
     94 #ifdef U_NL_LANGINFO_CODESET
     95     /* Use the predefined value. */
     96 #elif !U_HAVE_NL_LANGINFO_CODESET
     97 #   define U_NL_LANGINFO_CODESET -1
     98 #elif U_PLATFORM == U_PF_OS400
     99    /* not defined */
    100 #else
    101 #   define U_NL_LANGINFO_CODESET CODESET
    102 #endif
    103 
    104 #ifdef U_TZSET
    105     /* Use the predefined value. */
    106 #elif U_PLATFORM_USES_ONLY_WIN32_API
    107 #   define U_TZSET _tzset
    108 #elif U_PLATFORM == U_PF_OS400
    109    /* not defined */
    110 #else
    111 #   define U_TZSET tzset
    112 #endif
    113 
    114 #ifdef U_TIMEZONE
    115     /* Use the predefined value. */
    116 #elif U_PLATFORM == U_PF_ANDROID
    117 #   define U_TIMEZONE timezone
    118 #elif U_PLATFORM_IS_LINUX_BASED
    119 #   define U_TIMEZONE __timezone
    120 #elif U_PLATFORM_USES_ONLY_WIN32_API
    121 #   define U_TIMEZONE _timezone
    122 #elif U_PLATFORM == U_PF_BSD && !defined(__NetBSD__)
    123    /* not defined */
    124 #elif U_PLATFORM == U_PF_OS400
    125    /* not defined */
    126 #else
    127 #   define U_TIMEZONE timezone
    128 #endif
    129 
    130 #ifdef U_TZNAME
    131     /* Use the predefined value. */
    132 #elif U_PLATFORM_USES_ONLY_WIN32_API
    133 #   define U_TZNAME _tzname
    134 #elif U_PLATFORM == U_PF_OS400
    135    /* not defined */
    136 #else
    137 #   define U_TZNAME tzname
    138 #endif
    139 
    140 #ifdef U_HAVE_MMAP
    141     /* Use the predefined value. */
    142 #elif U_PLATFORM_HAS_WIN32_API
    143 #   define U_HAVE_MMAP 0
    144 #else
    145 #   define U_HAVE_MMAP 1
    146 #endif
    147 
    148 #ifdef U_HAVE_POPEN
    149     /* Use the predefined value. */
    150 #elif U_PLATFORM_USES_ONLY_WIN32_API
    151 #   define U_HAVE_POPEN 0
    152 #elif U_PLATFORM == U_PF_OS400
    153 #   define U_HAVE_POPEN 0
    154 #else
    155 #   define U_HAVE_POPEN 1
    156 #endif
    157 
    158 /**
    159  * \def U_HAVE_DIRENT_H
    160  * Defines whether dirent.h is available.
    161  * @internal
    162  */
    163 #ifdef U_HAVE_DIRENT_H
    164     /* Use the predefined value. */
    165 #elif U_PLATFORM_HAS_WIN32_API
    166 #   define U_HAVE_DIRENT_H 0
    167 #else
    168 #   define U_HAVE_DIRENT_H 1
    169 #endif
    170 
    171 /** @} */
    172 
    173 /*===========================================================================*/
    174 /** @{ GCC built in functions for atomic memory operations                   */
    175 /*===========================================================================*/
    176 
    177 /**
    178  * \def U_HAVE_GCC_ATOMICS
    179  * @internal
    180  */
    181 #ifdef U_HAVE_GCC_ATOMICS
    182     /* Use the predefined value. */
    183 #elif U_GCC_MAJOR_MINOR >= 404
    184 #   define U_HAVE_GCC_ATOMICS 1
    185 #else
    186 #   define U_HAVE_GCC_ATOMICS 0
    187 #endif
    188 
    189 /** @} */
    190 
    191 /*===========================================================================*/
    192 /** @{ Code alignment                                                        */
    193 /*===========================================================================*/
    194 
    195 /**
    196  * \def U_ALIGN_CODE
    197  * This is used to align code fragments to a specific byte boundary.
    198  * This is useful for getting consistent performance test results.
    199  * @internal
    200  */
    201 #ifdef U_ALIGN_CODE
    202     /* Use the predefined value. */
    203 #elif defined(_MSC_VER) && defined(_M_IX86) && !defined(_MANAGED)
    204 #   define U_ALIGN_CODE(boundarySize) __asm  align boundarySize
    205 #else
    206 #   define U_ALIGN_CODE(boundarySize)
    207 #endif
    208 
    209 /** @} */
    210 
    211 /*===========================================================================*/
    212 /** @{ Programs used by ICU code                                             */
    213 /*===========================================================================*/
    214 
    215 /**
    216  * \def U_MAKE_IS_NMAKE
    217  * Defines whether the "make" program is Windows nmake.
    218  */
    219 #ifdef U_MAKE_IS_NMAKE
    220     /* Use the predefined value. */
    221 #elif U_PLATFORM == U_PF_WINDOWS
    222 #   define U_MAKE_IS_NMAKE 1
    223 #else
    224 #   define U_MAKE_IS_NMAKE 0
    225 #endif
    226 
    227 /** @} */
    228 
    229 /*==========================================================================*/
    230 /* Platform utilities                                                       */
    231 /*==========================================================================*/
    232 
    233 /**
    234  * Platform utilities isolates the platform dependencies of the
    235  * libarary.  For each platform which this code is ported to, these
    236  * functions may have to be re-implemented.
    237  */
    238 
    239 /**
    240  * Floating point utility to determine if a double is Not a Number (NaN).
    241  * @internal
    242  */
    243 U_INTERNAL UBool   U_EXPORT2 uprv_isNaN(double d);
    244 /**
    245  * Floating point utility to determine if a double has an infinite value.
    246  * @internal
    247  */
    248 U_INTERNAL UBool   U_EXPORT2 uprv_isInfinite(double d);
    249 /**
    250  * Floating point utility to determine if a double has a positive infinite value.
    251  * @internal
    252  */
    253 U_INTERNAL UBool   U_EXPORT2 uprv_isPositiveInfinity(double d);
    254 /**
    255  * Floating point utility to determine if a double has a negative infinite value.
    256  * @internal
    257  */
    258 U_INTERNAL UBool   U_EXPORT2 uprv_isNegativeInfinity(double d);
    259 /**
    260  * Floating point utility that returns a Not a Number (NaN) value.
    261  * @internal
    262  */
    263 U_INTERNAL double  U_EXPORT2 uprv_getNaN(void);
    264 /**
    265  * Floating point utility that returns an infinite value.
    266  * @internal
    267  */
    268 U_INTERNAL double  U_EXPORT2 uprv_getInfinity(void);
    269 
    270 /**
    271  * Floating point utility to truncate a double.
    272  * @internal
    273  */
    274 U_INTERNAL double  U_EXPORT2 uprv_trunc(double d);
    275 /**
    276  * Floating point utility to calculate the floor of a double.
    277  * @internal
    278  */
    279 U_INTERNAL double  U_EXPORT2 uprv_floor(double d);
    280 /**
    281  * Floating point utility to calculate the ceiling of a double.
    282  * @internal
    283  */
    284 U_INTERNAL double  U_EXPORT2 uprv_ceil(double d);
    285 /**
    286  * Floating point utility to calculate the absolute value of a double.
    287  * @internal
    288  */
    289 U_INTERNAL double  U_EXPORT2 uprv_fabs(double d);
    290 /**
    291  * Floating point utility to calculate the fractional and integer parts of a double.
    292  * @internal
    293  */
    294 U_INTERNAL double  U_EXPORT2 uprv_modf(double d, double* pinteger);
    295 /**
    296  * Floating point utility to calculate the remainder of a double divided by another double.
    297  * @internal
    298  */
    299 U_INTERNAL double  U_EXPORT2 uprv_fmod(double d, double y);
    300 /**
    301  * Floating point utility to calculate d to the power of exponent (d^exponent).
    302  * @internal
    303  */
    304 U_INTERNAL double  U_EXPORT2 uprv_pow(double d, double exponent);
    305 /**
    306  * Floating point utility to calculate 10 to the power of exponent (10^exponent).
    307  * @internal
    308  */
    309 U_INTERNAL double  U_EXPORT2 uprv_pow10(int32_t exponent);
    310 /**
    311  * Floating point utility to calculate the maximum value of two doubles.
    312  * @internal
    313  */
    314 U_INTERNAL double  U_EXPORT2 uprv_fmax(double d, double y);
    315 /**
    316  * Floating point utility to calculate the minimum value of two doubles.
    317  * @internal
    318  */
    319 U_INTERNAL double  U_EXPORT2 uprv_fmin(double d, double y);
    320 /**
    321  * Private utility to calculate the maximum value of two integers.
    322  * @internal
    323  */
    324 U_INTERNAL int32_t U_EXPORT2 uprv_max(int32_t d, int32_t y);
    325 /**
    326  * Private utility to calculate the minimum value of two integers.
    327  * @internal
    328  */
    329 U_INTERNAL int32_t U_EXPORT2 uprv_min(int32_t d, int32_t y);
    330 
    331 #if U_IS_BIG_ENDIAN
    332 #   define uprv_isNegative(number) (*((signed char *)&(number))<0)
    333 #else
    334 #   define uprv_isNegative(number) (*((signed char *)&(number)+sizeof(number)-1)<0)
    335 #endif
    336 
    337 /**
    338  * Return the largest positive number that can be represented by an integer
    339  * type of arbitrary bit length.
    340  * @internal
    341  */
    342 U_INTERNAL double  U_EXPORT2 uprv_maxMantissa(void);
    343 
    344 /**
    345  * Floating point utility to calculate the logarithm of a double.
    346  * @internal
    347  */
    348 U_INTERNAL double  U_EXPORT2 uprv_log(double d);
    349 
    350 /**
    351  * Does common notion of rounding e.g. uprv_floor(x + 0.5);
    352  * @param x the double number
    353  * @return the rounded double
    354  * @internal
    355  */
    356 U_INTERNAL double  U_EXPORT2 uprv_round(double x);
    357 
    358 #if 0
    359 /**
    360  * Returns the number of digits after the decimal point in a double number x.
    361  *
    362  * @param x the double number
    363  * @return the number of digits after the decimal point in a double number x.
    364  * @internal
    365  */
    366 /*U_INTERNAL int32_t  U_EXPORT2 uprv_digitsAfterDecimal(double x);*/
    367 #endif
    368 
    369 #if !U_CHARSET_IS_UTF8
    370 /**
    371  * Please use ucnv_getDefaultName() instead.
    372  * Return the default codepage for this platform and locale.
    373  * This function can call setlocale() on Unix platforms. Please read the
    374  * platform documentation on setlocale() before calling this function.
    375  * @return the default codepage for this platform
    376  * @internal
    377  */
    378 U_INTERNAL const char*  U_EXPORT2 uprv_getDefaultCodepage(void);
    379 #endif
    380 
    381 /**
    382  * Please use uloc_getDefault() instead.
    383  * Return the default locale ID string by querying ths system, or
    384  *     zero if one cannot be found.
    385  * This function can call setlocale() on Unix platforms. Please read the
    386  * platform documentation on setlocale() before calling this function.
    387  * @return the default locale ID string
    388  * @internal
    389  */
    390 U_INTERNAL const char*  U_EXPORT2 uprv_getDefaultLocaleID(void);
    391 
    392 /**
    393  * Time zone utilities
    394  *
    395  * Wrappers for C runtime library functions relating to timezones.
    396  * The t_tzset() function (similar to tzset) uses the current setting
    397  * of the environment variable TZ to assign values to three global
    398  * variables: daylight, timezone, and tzname. These variables have the
    399  * following meanings, and are declared in &lt;time.h&gt;.
    400  *
    401  *   daylight   Nonzero if daylight-saving-time zone (DST) is specified
    402  *              in TZ; otherwise, 0. Default value is 1.
    403  *   timezone   Difference in seconds between coordinated universal
    404  *              time and local time. E.g., -28,800 for PST (GMT-8hrs)
    405  *   tzname(0)  Three-letter time-zone name derived from TZ environment
    406  *              variable. E.g., "PST".
    407  *   tzname(1)  Three-letter DST zone name derived from TZ environment
    408  *              variable.  E.g., "PDT". If DST zone is omitted from TZ,
    409  *              tzname(1) is an empty string.
    410  *
    411  * Notes: For example, to set the TZ environment variable to correspond
    412  * to the current time zone in Germany, you can use one of the
    413  * following statements:
    414  *
    415  *   set TZ=GST1GDT
    416  *   set TZ=GST+1GDT
    417  *
    418  * If the TZ value is not set, t_tzset() attempts to use the time zone
    419  * information specified by the operating system. Under Windows NT
    420  * and Windows 95, this information is specified in the Control Panel's
    421  * Date/Time application.
    422  * @internal
    423  */
    424 U_INTERNAL void     U_EXPORT2 uprv_tzset(void);
    425 
    426 /**
    427  * Difference in seconds between coordinated universal
    428  * time and local time. E.g., -28,800 for PST (GMT-8hrs)
    429  * @return the difference in seconds between coordinated universal time and local time.
    430  * @internal
    431  */
    432 U_INTERNAL int32_t  U_EXPORT2 uprv_timezone(void);
    433 
    434 /**
    435  *   tzname(0)  Three-letter time-zone name derived from TZ environment
    436  *              variable. E.g., "PST".
    437  *   tzname(1)  Three-letter DST zone name derived from TZ environment
    438  *              variable.  E.g., "PDT". If DST zone is omitted from TZ,
    439  *              tzname(1) is an empty string.
    440  * @internal
    441  */
    442 U_INTERNAL const char* U_EXPORT2 uprv_tzname(int n);
    443 
    444 /**
    445  * Get UTC (GMT) time measured in milliseconds since 0:00 on 1/1/1970.
    446  * This function is affected by 'faketime' and should be the bottleneck for all user-visible ICU time functions.
    447  * @return the UTC time measured in milliseconds
    448  * @internal
    449  */
    450 U_INTERNAL UDate U_EXPORT2 uprv_getUTCtime(void);
    451 
    452 /**
    453  * Get UTC (GMT) time measured in milliseconds since 0:00 on 1/1/1970.
    454  * This function is not affected by 'faketime', so it should only be used by low level test functions- not by anything that
    455  * exposes time to the end user.
    456  * @return the UTC time measured in milliseconds
    457  * @internal
    458  */
    459 U_INTERNAL UDate U_EXPORT2 uprv_getRawUTCtime(void);
    460 
    461 /**
    462  * Determine whether a pathname is absolute or not, as defined by the platform.
    463  * @param path Pathname to test
    464  * @return TRUE if the path is absolute
    465  * @internal (ICU 3.0)
    466  */
    467 U_INTERNAL UBool U_EXPORT2 uprv_pathIsAbsolute(const char *path);
    468 
    469 /**
    470  * Use U_MAX_PTR instead of this function.
    471  * @param void pointer to test
    472  * @return the largest possible pointer greater than the base
    473  * @internal (ICU 3.8)
    474  */
    475 U_INTERNAL void * U_EXPORT2 uprv_maximumPtr(void *base);
    476 
    477 /**
    478  * Maximum value of a (void*) - use to indicate the limit of an 'infinite' buffer.
    479  * In fact, buffer sizes must not exceed 2GB so that the difference between
    480  * the buffer limit and the buffer start can be expressed in an int32_t.
    481  *
    482  * The definition of U_MAX_PTR must fulfill the following conditions:
    483  * - return the largest possible pointer greater than base
    484  * - return a valid pointer according to the machine architecture (AS/400, 64-bit, etc.)
    485  * - avoid wrapping around at high addresses
    486  * - make sure that the returned pointer is not farther from base than 0x7fffffff bytes
    487  *
    488  * @param base The beginning of a buffer to find the maximum offset from
    489  * @internal
    490  */
    491 #ifndef U_MAX_PTR
    492 #  if U_PLATFORM == U_PF_OS390 && !defined(_LP64)
    493     /* We have 31-bit pointers. */
    494 #    define U_MAX_PTR(base) ((void *)0x7fffffff)
    495 #  elif U_PLATFORM == U_PF_OS400
    496 #    define U_MAX_PTR(base) uprv_maximumPtr((void *)base)
    497 #  elif 0
    498     /*
    499      * For platforms where pointers are scalar values (which is normal, but unlike i5/OS)
    500      * but that do not define uintptr_t.
    501      *
    502      * However, this does not work on modern compilers:
    503      * The C++ standard does not define pointer overflow, and allows compilers to
    504      * assume that p+u>p for any pointer p and any integer u>0.
    505      * Thus, modern compilers optimize away the ">" comparison.
    506      * (See ICU tickets #7187 and #8096.)
    507      */
    508 #    define U_MAX_PTR(base) \
    509     ((void *)(((char *)(base)+0x7fffffffu) > (char *)(base) \
    510         ? ((char *)(base)+0x7fffffffu) \
    511         : (char *)-1))
    512 #  else
    513     /* Default version. C++ standard compliant for scalar pointers. */
    514 #    define U_MAX_PTR(base) \
    515     ((void *)(((uintptr_t)(base)+0x7fffffffu) > (uintptr_t)(base) \
    516         ? ((uintptr_t)(base)+0x7fffffffu) \
    517         : (uintptr_t)-1))
    518 #  endif
    519 #endif
    520 
    521 /*  Dynamic Library Functions */
    522 
    523 typedef void (UVoidFunction)(void);
    524 
    525 #if U_ENABLE_DYLOAD
    526 /**
    527  * Load a library
    528  * @internal (ICU 4.4)
    529  */
    530 U_INTERNAL void * U_EXPORT2 uprv_dl_open(const char *libName, UErrorCode *status);
    531 
    532 /**
    533  * Close a library
    534  * @internal (ICU 4.4)
    535  */
    536 U_INTERNAL void U_EXPORT2 uprv_dl_close( void *lib, UErrorCode *status);
    537 
    538 /**
    539  * Extract a symbol from a library (function)
    540  * @internal (ICU 4.8)
    541  */
    542 U_INTERNAL UVoidFunction* U_EXPORT2 uprv_dlsym_func( void *lib, const char *symbolName, UErrorCode *status);
    543 
    544 /**
    545  * Extract a symbol from a library (function)
    546  * Not implemented, no clients.
    547  * @internal
    548  */
    549 /* U_INTERNAL void * U_EXPORT2 uprv_dlsym_data( void *lib, const char *symbolName, UErrorCode *status); */
    550 
    551 #endif
    552 
    553 /**
    554  * Define malloc and related functions
    555  * @internal
    556  */
    557 #if U_PLATFORM == U_PF_OS400
    558 # define uprv_default_malloc(x) _C_TS_malloc(x)
    559 # define uprv_default_realloc(x,y) _C_TS_realloc(x,y)
    560 # define uprv_default_free(x) _C_TS_free(x)
    561 /* also _C_TS_calloc(x) */
    562 #else
    563 /* C defaults */
    564 # define uprv_default_malloc(x) malloc(x)
    565 # define uprv_default_realloc(x,y) realloc(x,y)
    566 # define uprv_default_free(x) free(x)
    567 #endif
    568 
    569 
    570 #endif
    571