2 ******************************************************************************
4 * Copyright (C) 1997-2015, International Business Machines
5 * Corporation and others. All Rights Reserved.
7 ******************************************************************************
9 * FILE NAME : putilimp.h
11 * Date Name Description
12 * 10/17/04 grhoten Move internal functions from putil.h to this file.
13 ******************************************************************************
19 #include "unicode/utypes.h"
20 #include "unicode/putil.h"
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.
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.
33 * This is consistent with the Java language.
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.
39 * Code that depends on the natural behavior should be guarded with this macro,
40 * with an alternate path for unusual platforms.
43 #ifdef U_SIGNED_RIGHT_SHIFT_IS_ARITHMETIC
44 /* Use the predefined value. */
47 * Nearly all CPUs & compilers implement a right-shift of a signed integer
48 * as an Arithmetic Shift Right (with sign extension).
50 # define U_SIGNED_RIGHT_SHIFT_IS_ARITHMETIC 1
53 /** Define this to 1 if your platform supports IEEE 754 floating point,
54 to 0 if it does not. */
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.
66 * Do not use ptrdiff_t since it is signed. size_t is unsigned.
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;
74 * \def U_HAVE_MSVC_2003_OR_EARLIER
75 * Flag for workaround of MSVC 2003 optimization bugs
78 #if !defined(U_HAVE_MSVC_2003_OR_EARLIER) && defined(_MSC_VER) && (_MSC_VER < 1400)
79 #define U_HAVE_MSVC_2003_OR_EARLIER
82 /*===========================================================================*/
83 /** @{ Information about POSIX support */
84 /*===========================================================================*/
86 #ifdef U_HAVE_NL_LANGINFO_CODESET
87 /* Use the predefined value. */
88 #elif U_PLATFORM_HAS_WIN32_API || U_PLATFORM == U_PF_ANDROID || U_PLATFORM == U_PF_QNX
89 # define U_HAVE_NL_LANGINFO_CODESET 0
91 # define U_HAVE_NL_LANGINFO_CODESET 1
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
101 # define U_NL_LANGINFO_CODESET CODESET
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
111 # define U_TZSET tzset
114 #if defined(U_TIMEZONE) || defined(U_HAVE_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 # if defined(__UCLIBC__)
120 /* uClibc does not have __timezone or _timezone. */
121 # elif defined(_NEWLIB_VERSION)
122 # define U_TIMEZONE _timezone
123 # elif defined(__GLIBC__)
125 # define U_TIMEZONE __timezone
127 #elif U_PLATFORM_USES_ONLY_WIN32_API
128 # define U_TIMEZONE _timezone
129 #elif U_PLATFORM == U_PF_BSD && !defined(__NetBSD__)
131 #elif U_PLATFORM == U_PF_OS400
133 #elif U_PLATFORM == U_PF_IPHONE
136 # define U_TIMEZONE timezone
140 /* Use the predefined value. */
141 #elif U_PLATFORM_USES_ONLY_WIN32_API
142 # define U_TZNAME _tzname
143 #elif U_PLATFORM == U_PF_OS400
146 # define U_TZNAME tzname
150 /* Use the predefined value. */
151 #elif U_PLATFORM_HAS_WIN32_API
152 # define U_HAVE_MMAP 0
154 # define U_HAVE_MMAP 1
158 /* Use the predefined value. */
159 #elif U_PLATFORM_USES_ONLY_WIN32_API
160 # define U_HAVE_POPEN 0
161 #elif U_PLATFORM == U_PF_OS400
162 # define U_HAVE_POPEN 0
164 # define U_HAVE_POPEN 1
168 * \def U_HAVE_DIRENT_H
169 * Defines whether dirent.h is available.
172 #ifdef U_HAVE_DIRENT_H
173 /* Use the predefined value. */
174 #elif U_PLATFORM_HAS_WIN32_API
175 # define U_HAVE_DIRENT_H 0
177 # define U_HAVE_DIRENT_H 1
182 /*===========================================================================*/
183 /** @{ GCC built in functions for atomic memory operations */
184 /*===========================================================================*/
187 * \def U_HAVE_GCC_ATOMICS
190 #ifdef U_HAVE_GCC_ATOMICS
191 /* Use the predefined value. */
192 #elif U_PLATFORM == U_PF_MINGW
193 #define U_HAVE_GCC_ATOMICS 0
194 #elif U_GCC_MAJOR_MINOR >= 404 || defined(__clang__)
195 /* TODO: Intel icc and IBM xlc on AIX also support gcc atomics. (Intel originated them.)
196 * Add them for these compilers.
197 * Note: Clang sets __GNUC__ defines for version 4.2, so misses the 4.4 test here.
199 # define U_HAVE_GCC_ATOMICS 1
201 # define U_HAVE_GCC_ATOMICS 0
207 * \def U_HAVE_STD_ATOMICS
208 * Defines whether the standard C++11 <atomic> is available.
209 * ICU will use this when avialable,
210 * otherwise will fall back to compiler or platform specific alternatives.
213 #ifdef U_HAVE_STD_ATOMICS
214 /* Use the predefined value. */
215 #elif U_CPLUSPLUS_VERSION < 11
216 /* Not C++11, disable use of atomics */
217 # define U_HAVE_STD_ATOMICS 0
218 #elif __clang__ && __clang_major__==3 && __clang_minor__<=1
219 /* Clang 3.1, has atomic variable initializer bug. */
220 # define U_HAVE_STD_ATOMICS 0
222 /* U_HAVE_ATOMIC is typically set by an autoconf test of #include <atomic> */
223 /* Can be set manually, or left undefined, on platforms without autoconf. */
224 # if defined(U_HAVE_ATOMIC) && U_HAVE_ATOMIC
225 # define U_HAVE_STD_ATOMICS 1
227 # define U_HAVE_STD_ATOMICS 0
233 * \def U_HAVE_CLANG_ATOMICS
234 * Defines whether Clang c11 style built-in atomics are avaialable.
235 * These are used in preference to gcc atomics when both are available.
237 #ifdef U_HAVE_CLANG_ATOMICS
238 /* Use the predefined value. */
239 #elif __has_builtin(__c11_atomic_load) && \
240 __has_builtin(__c11_atomic_store) && \
241 __has_builtin(__c11_atomic_fetch_add) && \
242 __has_builtin(__c11_atomic_fetch_sub)
243 # define U_HAVE_CLANG_ATOMICS 1
245 # define U_HAVE_CLANG_ATOMICS 0
248 /*===========================================================================*/
249 /** @{ Programs used by ICU code */
250 /*===========================================================================*/
253 * \def U_MAKE_IS_NMAKE
254 * Defines whether the "make" program is Windows nmake.
256 #ifdef U_MAKE_IS_NMAKE
257 /* Use the predefined value. */
258 #elif U_PLATFORM == U_PF_WINDOWS
259 # define U_MAKE_IS_NMAKE 1
261 # define U_MAKE_IS_NMAKE 0
266 /*==========================================================================*/
267 /* Platform utilities */
268 /*==========================================================================*/
271 * Platform utilities isolates the platform dependencies of the
272 * libarary. For each platform which this code is ported to, these
273 * functions may have to be re-implemented.
277 * Floating point utility to determine if a double is Not a Number (NaN).
280 U_INTERNAL UBool U_EXPORT2
uprv_isNaN(double d
);
282 * Floating point utility to determine if a double has an infinite value.
285 U_INTERNAL UBool U_EXPORT2
uprv_isInfinite(double d
);
287 * Floating point utility to determine if a double has a positive infinite value.
290 U_INTERNAL UBool U_EXPORT2
uprv_isPositiveInfinity(double d
);
292 * Floating point utility to determine if a double has a negative infinite value.
295 U_INTERNAL UBool U_EXPORT2
uprv_isNegativeInfinity(double d
);
297 * Floating point utility that returns a Not a Number (NaN) value.
300 U_INTERNAL
double U_EXPORT2
uprv_getNaN(void);
302 * Floating point utility that returns an infinite value.
305 U_INTERNAL
double U_EXPORT2
uprv_getInfinity(void);
308 * Floating point utility to truncate a double.
311 U_INTERNAL
double U_EXPORT2
uprv_trunc(double d
);
313 * Floating point utility to calculate the floor of a double.
316 U_INTERNAL
double U_EXPORT2
uprv_floor(double d
);
318 * Floating point utility to calculate the ceiling of a double.
321 U_INTERNAL
double U_EXPORT2
uprv_ceil(double d
);
323 * Floating point utility to calculate the absolute value of a double.
326 U_INTERNAL
double U_EXPORT2
uprv_fabs(double d
);
328 * Floating point utility to calculate the fractional and integer parts of a double.
331 U_INTERNAL
double U_EXPORT2
uprv_modf(double d
, double* pinteger
);
333 * Floating point utility to calculate the remainder of a double divided by another double.
336 U_INTERNAL
double U_EXPORT2
uprv_fmod(double d
, double y
);
338 * Floating point utility to calculate d to the power of exponent (d^exponent).
341 U_INTERNAL
double U_EXPORT2
uprv_pow(double d
, double exponent
);
343 * Floating point utility to calculate 10 to the power of exponent (10^exponent).
346 U_INTERNAL
double U_EXPORT2
uprv_pow10(int32_t exponent
);
348 * Floating point utility to calculate the maximum value of two doubles.
351 U_INTERNAL
double U_EXPORT2
uprv_fmax(double d
, double y
);
353 * Floating point utility to calculate the minimum value of two doubles.
356 U_INTERNAL
double U_EXPORT2
uprv_fmin(double d
, double y
);
358 * Private utility to calculate the maximum value of two integers.
361 U_INTERNAL
int32_t U_EXPORT2
uprv_max(int32_t d
, int32_t y
);
363 * Private utility to calculate the minimum value of two integers.
366 U_INTERNAL
int32_t U_EXPORT2
uprv_min(int32_t d
, int32_t y
);
369 # define uprv_isNegative(number) (*((signed char *)&(number))<0)
371 # define uprv_isNegative(number) (*((signed char *)&(number)+sizeof(number)-1)<0)
375 * Return the largest positive number that can be represented by an integer
376 * type of arbitrary bit length.
379 U_INTERNAL
double U_EXPORT2
uprv_maxMantissa(void);
382 * Floating point utility to calculate the logarithm of a double.
385 U_INTERNAL
double U_EXPORT2
uprv_log(double d
);
388 * Does common notion of rounding e.g. uprv_floor(x + 0.5);
389 * @param x the double number
390 * @return the rounded double
393 U_INTERNAL
double U_EXPORT2
uprv_round(double x
);
397 * Returns the number of digits after the decimal point in a double number x.
399 * @param x the double number
400 * @return the number of digits after the decimal point in a double number x.
403 /*U_INTERNAL int32_t U_EXPORT2 uprv_digitsAfterDecimal(double x);*/
406 #if !U_CHARSET_IS_UTF8
408 * Please use ucnv_getDefaultName() instead.
409 * Return the default codepage for this platform and locale.
410 * This function can call setlocale() on Unix platforms. Please read the
411 * platform documentation on setlocale() before calling this function.
412 * @return the default codepage for this platform
415 U_INTERNAL
const char* U_EXPORT2
uprv_getDefaultCodepage(void);
419 * Please use uloc_getDefault() instead.
420 * Return the default locale ID string by querying ths system, or
421 * zero if one cannot be found.
422 * This function can call setlocale() on Unix platforms. Please read the
423 * platform documentation on setlocale() before calling this function.
424 * @return the default locale ID string
427 U_INTERNAL
const char* U_EXPORT2
uprv_getDefaultLocaleID(void);
430 * Time zone utilities
432 * Wrappers for C runtime library functions relating to timezones.
433 * The t_tzset() function (similar to tzset) uses the current setting
434 * of the environment variable TZ to assign values to three global
435 * variables: daylight, timezone, and tzname. These variables have the
436 * following meanings, and are declared in <time.h>.
438 * daylight Nonzero if daylight-saving-time zone (DST) is specified
439 * in TZ; otherwise, 0. Default value is 1.
440 * timezone Difference in seconds between coordinated universal
441 * time and local time. E.g., -28,800 for PST (GMT-8hrs)
442 * tzname(0) Three-letter time-zone name derived from TZ environment
443 * variable. E.g., "PST".
444 * tzname(1) Three-letter DST zone name derived from TZ environment
445 * variable. E.g., "PDT". If DST zone is omitted from TZ,
446 * tzname(1) is an empty string.
448 * Notes: For example, to set the TZ environment variable to correspond
449 * to the current time zone in Germany, you can use one of the
450 * following statements:
455 * If the TZ value is not set, t_tzset() attempts to use the time zone
456 * information specified by the operating system. Under Windows NT
457 * and Windows 95, this information is specified in the Control Panel's
458 * Date/Time application.
461 U_INTERNAL
void U_EXPORT2
uprv_tzset(void);
464 * Difference in seconds between coordinated universal
465 * time and local time. E.g., -28,800 for PST (GMT-8hrs)
466 * @return the difference in seconds between coordinated universal time and local time.
469 U_INTERNAL
int32_t U_EXPORT2
uprv_timezone(void);
472 * tzname(0) Three-letter time-zone name derived from TZ environment
473 * variable. E.g., "PST".
474 * tzname(1) Three-letter DST zone name derived from TZ environment
475 * variable. E.g., "PDT". If DST zone is omitted from TZ,
476 * tzname(1) is an empty string.
479 U_INTERNAL
const char* U_EXPORT2
uprv_tzname(int n
);
482 * Get UTC (GMT) time measured in milliseconds since 0:00 on 1/1/1970.
483 * This function is affected by 'faketime' and should be the bottleneck for all user-visible ICU time functions.
484 * @return the UTC time measured in milliseconds
487 U_INTERNAL UDate U_EXPORT2
uprv_getUTCtime(void);
490 * Get UTC (GMT) time measured in milliseconds since 0:00 on 1/1/1970.
491 * This function is not affected by 'faketime', so it should only be used by low level test functions- not by anything that
492 * exposes time to the end user.
493 * @return the UTC time measured in milliseconds
496 U_INTERNAL UDate U_EXPORT2
uprv_getRawUTCtime(void);
499 * Determine whether a pathname is absolute or not, as defined by the platform.
500 * @param path Pathname to test
501 * @return TRUE if the path is absolute
502 * @internal (ICU 3.0)
504 U_INTERNAL UBool U_EXPORT2
uprv_pathIsAbsolute(const char *path
);
507 * Use U_MAX_PTR instead of this function.
508 * @param void pointer to test
509 * @return the largest possible pointer greater than the base
510 * @internal (ICU 3.8)
512 U_INTERNAL
void * U_EXPORT2
uprv_maximumPtr(void *base
);
515 * Maximum value of a (void*) - use to indicate the limit of an 'infinite' buffer.
516 * In fact, buffer sizes must not exceed 2GB so that the difference between
517 * the buffer limit and the buffer start can be expressed in an int32_t.
519 * The definition of U_MAX_PTR must fulfill the following conditions:
520 * - return the largest possible pointer greater than base
521 * - return a valid pointer according to the machine architecture (AS/400, 64-bit, etc.)
522 * - avoid wrapping around at high addresses
523 * - make sure that the returned pointer is not farther from base than 0x7fffffff bytes
525 * @param base The beginning of a buffer to find the maximum offset from
529 # if U_PLATFORM == U_PF_OS390 && !defined(_LP64)
530 /* We have 31-bit pointers. */
531 # define U_MAX_PTR(base) ((void *)0x7fffffff)
532 # elif U_PLATFORM == U_PF_OS400
533 # define U_MAX_PTR(base) uprv_maximumPtr((void *)base)
536 * For platforms where pointers are scalar values (which is normal, but unlike i5/OS)
537 * but that do not define uintptr_t.
539 * However, this does not work on modern compilers:
540 * The C++ standard does not define pointer overflow, and allows compilers to
541 * assume that p+u>p for any pointer p and any integer u>0.
542 * Thus, modern compilers optimize away the ">" comparison.
543 * (See ICU tickets #7187 and #8096.)
545 # define U_MAX_PTR(base) \
546 ((void *)(((char *)(base)+0x7fffffffu) > (char *)(base) \
547 ? ((char *)(base)+0x7fffffffu) \
550 /* Default version. C++ standard compliant for scalar pointers. */
551 # define U_MAX_PTR(base) \
552 ((void *)(((uintptr_t)(base)+0x7fffffffu) > (uintptr_t)(base) \
553 ? ((uintptr_t)(base)+0x7fffffffu) \
558 /* Dynamic Library Functions */
560 typedef void (UVoidFunction
)(void);
565 * @internal (ICU 4.4)
567 U_INTERNAL
void * U_EXPORT2
uprv_dl_open(const char *libName
, UErrorCode
*status
);
571 * @internal (ICU 4.4)
573 U_INTERNAL
void U_EXPORT2
uprv_dl_close( void *lib
, UErrorCode
*status
);
576 * Extract a symbol from a library (function)
577 * @internal (ICU 4.8)
579 U_INTERNAL UVoidFunction
* U_EXPORT2
uprv_dlsym_func( void *lib
, const char *symbolName
, UErrorCode
*status
);
582 * Extract a symbol from a library (function)
583 * Not implemented, no clients.
586 /* U_INTERNAL void * U_EXPORT2 uprv_dlsym_data( void *lib, const char *symbolName, UErrorCode *status); */
591 * Define malloc and related functions
594 #if U_PLATFORM == U_PF_OS400
595 # define uprv_default_malloc(x) _C_TS_malloc(x)
596 # define uprv_default_realloc(x,y) _C_TS_realloc(x,y)
597 # define uprv_default_free(x) _C_TS_free(x)
598 /* also _C_TS_calloc(x) */
601 # define uprv_default_malloc(x) malloc(x)
602 # define uprv_default_realloc(x,y) realloc(x,y)
603 # define uprv_default_free(x) free(x)