1 // © 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
4 ******************************************************************************
6 * Copyright (C) 1997-2016, International Business Machines
7 * Corporation and others. All Rights Reserved.
9 ******************************************************************************
11 * FILE NAME : putilimp.h
13 * Date Name Description
14 * 10/17/04 grhoten Move internal functions from putil.h to this file.
15 ******************************************************************************
21 #include "unicode/utypes.h"
22 #include "unicode/putil.h"
25 * \def U_SIGNED_RIGHT_SHIFT_IS_ARITHMETIC
26 * Nearly all CPUs and compilers implement a right-shift of a signed integer
27 * as an Arithmetic Shift Right which copies the sign bit (the Most Significant Bit (MSB))
28 * into the vacated bits (sign extension).
29 * For example, (int32_t)0xfff5fff3>>4 becomes 0xffff5fff and -1>>1=-1.
31 * This can be useful for storing a signed value in the upper bits
32 * and another bit field in the lower bits.
33 * The signed value can be retrieved by simple right-shifting.
35 * This is consistent with the Java language.
37 * However, the C standard allows compilers to implement a right-shift of a signed integer
38 * as a Logical Shift Right which copies a 0 into the vacated bits.
39 * For example, (int32_t)0xfff5fff3>>4 becomes 0x0fff5fff and -1>>1=0x7fffffff.
41 * Code that depends on the natural behavior should be guarded with this macro,
42 * with an alternate path for unusual platforms.
45 #ifdef U_SIGNED_RIGHT_SHIFT_IS_ARITHMETIC
46 /* Use the predefined value. */
49 * Nearly all CPUs & compilers implement a right-shift of a signed integer
50 * as an Arithmetic Shift Right (with sign extension).
52 # define U_SIGNED_RIGHT_SHIFT_IS_ARITHMETIC 1
55 /** Define this to 1 if your platform supports IEEE 754 floating point,
56 to 0 if it does not. */
62 * uintptr_t is an optional part of the standard definitions in stdint.h.
63 * The opengroup.org documentation for stdint.h says
64 * "On XSI-conformant systems, the intptr_t and uintptr_t types are required;
65 * otherwise, they are optional."
66 * We assume that when uintptr_t is defined, UINTPTR_MAX is defined as well.
68 * Do not use ptrdiff_t since it is signed. size_t is unsigned.
70 /* TODO: This check fails on some z environments. Filed a ticket #9357 for this. */
71 #if !defined(__intptr_t_defined) && !defined(UINTPTR_MAX) && (U_PLATFORM != U_PF_OS390)
72 typedef size_t uintptr_t;
75 /*===========================================================================*/
76 /** @{ Information about POSIX support */
77 /*===========================================================================*/
79 #ifdef U_HAVE_NL_LANGINFO_CODESET
80 /* Use the predefined value. */
81 #elif U_PLATFORM_USES_ONLY_WIN32_API || U_PLATFORM == U_PF_ANDROID || U_PLATFORM == U_PF_QNX
82 # define U_HAVE_NL_LANGINFO_CODESET 0
84 # define U_HAVE_NL_LANGINFO_CODESET 1
87 #ifdef U_NL_LANGINFO_CODESET
88 /* Use the predefined value. */
89 #elif !U_HAVE_NL_LANGINFO_CODESET
90 # define U_NL_LANGINFO_CODESET -1
91 #elif U_PLATFORM == U_PF_OS400
94 # define U_NL_LANGINFO_CODESET CODESET
97 #if defined(U_TZSET) || defined(U_HAVE_TZSET)
98 /* Use the predefined value. */
99 #elif U_PLATFORM_USES_ONLY_WIN32_API
100 // UWP doesn't support tzset or environment variables for tz
101 #if U_PLATFORM_HAS_WINUWP_API == 0
102 # define U_TZSET _tzset
104 #elif U_PLATFORM == U_PF_OS400
107 # define U_TZSET tzset
110 #if defined(U_TIMEZONE) || defined(U_HAVE_TIMEZONE)
111 /* Use the predefined value. */
112 #elif U_PLATFORM == U_PF_ANDROID
113 # define U_TIMEZONE timezone
114 #elif defined(__UCLIBC__)
115 // uClibc does not have __timezone or _timezone.
116 #elif defined(_NEWLIB_VERSION)
117 # define U_TIMEZONE _timezone
118 #elif defined(__GLIBC__)
120 # define U_TIMEZONE __timezone
121 #elif U_PLATFORM_IS_LINUX_BASED
123 #elif U_PLATFORM_USES_ONLY_WIN32_API
124 # define U_TIMEZONE _timezone
125 #elif U_PLATFORM == U_PF_BSD && !defined(__NetBSD__)
127 #elif U_PLATFORM == U_PF_OS400
129 #elif U_PLATFORM == U_PF_IPHONE
132 # define U_TIMEZONE timezone
135 #if defined(U_TZNAME) || defined(U_HAVE_TZNAME)
136 /* Use the predefined value. */
137 #elif U_PLATFORM_USES_ONLY_WIN32_API
138 /* not usable on all windows platforms */
139 #if U_PLATFORM_HAS_WINUWP_API == 0
140 # define U_TZNAME _tzname
142 #elif U_PLATFORM == U_PF_OS400
145 # define U_TZNAME tzname
149 /* Use the predefined value. */
150 #elif U_PLATFORM_USES_ONLY_WIN32_API
151 # define U_HAVE_MMAP 0
153 # define U_HAVE_MMAP 1
157 /* Use the predefined value. */
158 #elif U_PLATFORM_USES_ONLY_WIN32_API
159 # define U_HAVE_POPEN 0
160 #elif U_PLATFORM == U_PF_OS400
161 # define U_HAVE_POPEN 0
163 # define U_HAVE_POPEN 1
167 * \def U_HAVE_DIRENT_H
168 * Defines whether dirent.h is available.
171 #ifdef U_HAVE_DIRENT_H
172 /* Use the predefined value. */
173 #elif U_PLATFORM_USES_ONLY_WIN32_API
174 # define U_HAVE_DIRENT_H 0
176 # define U_HAVE_DIRENT_H 1
181 /*===========================================================================*/
182 /** @{ GCC built in functions for atomic memory operations */
183 /*===========================================================================*/
186 * \def U_HAVE_GCC_ATOMICS
189 #ifdef U_HAVE_GCC_ATOMICS
190 /* Use the predefined value. */
191 #elif U_PLATFORM == U_PF_MINGW
192 #define U_HAVE_GCC_ATOMICS 0
193 #elif U_GCC_MAJOR_MINOR >= 404 || defined(__clang__)
194 /* TODO: Intel icc and IBM xlc on AIX also support gcc atomics. (Intel originated them.)
195 * Add them for these compilers.
196 * Note: Clang sets __GNUC__ defines for version 4.2, so misses the 4.4 test here.
198 # define U_HAVE_GCC_ATOMICS 1
200 # define U_HAVE_GCC_ATOMICS 0
206 * \def U_HAVE_STD_ATOMICS
207 * Defines whether to use the C++11 std::atomic functions.
208 * If false, ICU will fall back to compiler or platform specific alternatives.
209 * Note: support for these fall back options for atomics will be removed in a future version
210 * of ICU, and the use of C++ 11 atomics will be required.
213 #ifdef U_HAVE_STD_ATOMICS
214 /* Use the predefined value. */
216 # define U_HAVE_STD_ATOMICS 1
220 * \def U_HAVE_CLANG_ATOMICS
221 * Defines whether Clang c11 style built-in atomics are available.
222 * These are used in preference to gcc atomics when both are available.
224 #ifdef U_HAVE_CLANG_ATOMICS
225 /* Use the predefined value. */
226 #elif __has_builtin(__c11_atomic_load) && \
227 __has_builtin(__c11_atomic_store) && \
228 __has_builtin(__c11_atomic_fetch_add) && \
229 __has_builtin(__c11_atomic_fetch_sub)
230 # define U_HAVE_CLANG_ATOMICS 1
232 # define U_HAVE_CLANG_ATOMICS 0
237 * \def U_HAVE_STD_MUTEX
238 * Defines whether to use the C++11 std::mutex functions.
239 * If false, ICU will fall back to compiler or platform specific alternatives.
240 * std::mutex is preferred, and used by default unless this setting is overridden.
241 * Note: support for other options for mutexes will be removed in a future version
242 * of ICU, and the use of std::mutex will be required.
245 #ifdef U_HAVE_STD_MUTEX
246 /* Use the predefined value. */
248 # define U_HAVE_STD_MUTEX 1
251 /*===========================================================================*/
252 /** @{ Programs used by ICU code */
253 /*===========================================================================*/
256 * \def U_MAKE_IS_NMAKE
257 * Defines whether the "make" program is Windows nmake.
259 #ifdef U_MAKE_IS_NMAKE
260 /* Use the predefined value. */
261 #elif U_PLATFORM == U_PF_WINDOWS
262 # define U_MAKE_IS_NMAKE 1
264 # define U_MAKE_IS_NMAKE 0
269 /*==========================================================================*/
270 /* Platform utilities */
271 /*==========================================================================*/
274 * Platform utilities isolates the platform dependencies of the
275 * library. For each platform which this code is ported to, these
276 * functions may have to be re-implemented.
280 * Floating point utility to determine if a double is Not a Number (NaN).
283 U_INTERNAL UBool U_EXPORT2
uprv_isNaN(double d
);
285 * Floating point utility to determine if a double has an infinite value.
288 U_INTERNAL UBool U_EXPORT2
uprv_isInfinite(double d
);
290 * Floating point utility to determine if a double has a positive infinite value.
293 U_INTERNAL UBool U_EXPORT2
uprv_isPositiveInfinity(double d
);
295 * Floating point utility to determine if a double has a negative infinite value.
298 U_INTERNAL UBool U_EXPORT2
uprv_isNegativeInfinity(double d
);
300 * Floating point utility that returns a Not a Number (NaN) value.
303 U_INTERNAL
double U_EXPORT2
uprv_getNaN(void);
305 * Floating point utility that returns an infinite value.
308 U_INTERNAL
double U_EXPORT2
uprv_getInfinity(void);
311 * Floating point utility to truncate a double.
314 U_INTERNAL
double U_EXPORT2
uprv_trunc(double d
);
316 * Floating point utility to calculate the floor of a double.
319 U_INTERNAL
double U_EXPORT2
uprv_floor(double d
);
321 * Floating point utility to calculate the ceiling of a double.
324 U_INTERNAL
double U_EXPORT2
uprv_ceil(double d
);
326 * Floating point utility to calculate the absolute value of a double.
329 U_INTERNAL
double U_EXPORT2
uprv_fabs(double d
);
331 * Floating point utility to calculate the fractional and integer parts of a double.
334 U_INTERNAL
double U_EXPORT2
uprv_modf(double d
, double* pinteger
);
336 * Floating point utility to calculate the remainder of a double divided by another double.
339 U_INTERNAL
double U_EXPORT2
uprv_fmod(double d
, double y
);
341 * Floating point utility to calculate d to the power of exponent (d^exponent).
344 U_INTERNAL
double U_EXPORT2
uprv_pow(double d
, double exponent
);
346 * Floating point utility to calculate 10 to the power of exponent (10^exponent).
349 U_INTERNAL
double U_EXPORT2
uprv_pow10(int32_t exponent
);
351 * Floating point utility to calculate the maximum value of two doubles.
354 U_INTERNAL
double U_EXPORT2
uprv_fmax(double d
, double y
);
356 * Floating point utility to calculate the minimum value of two doubles.
359 U_INTERNAL
double U_EXPORT2
uprv_fmin(double d
, double y
);
361 * Private utility to calculate the maximum value of two integers.
364 U_INTERNAL
int32_t U_EXPORT2
uprv_max(int32_t d
, int32_t y
);
366 * Private utility to calculate the minimum value of two integers.
369 U_INTERNAL
int32_t U_EXPORT2
uprv_min(int32_t d
, int32_t y
);
372 # define uprv_isNegative(number) (*((signed char *)&(number))<0)
374 # define uprv_isNegative(number) (*((signed char *)&(number)+sizeof(number)-1)<0)
378 * Return the largest positive number that can be represented by an integer
379 * type of arbitrary bit length.
382 U_INTERNAL
double U_EXPORT2
uprv_maxMantissa(void);
385 * Floating point utility to calculate the logarithm of a double.
388 U_INTERNAL
double U_EXPORT2
uprv_log(double d
);
391 * Does common notion of rounding e.g. uprv_floor(x + 0.5);
392 * @param x the double number
393 * @return the rounded double
396 U_INTERNAL
double U_EXPORT2
uprv_round(double x
);
399 * Adds the signed integers a and b, storing the result in res.
400 * Checks for signed integer overflow.
401 * Similar to the GCC/Clang extension __builtin_add_overflow
403 * @param a The first operand.
404 * @param b The second operand.
406 * @return true if overflow occurred; false if no overflow occurred.
409 U_INTERNAL UBool U_EXPORT2
uprv_add32_overflow(int32_t a
, int32_t b
, int32_t* res
);
412 * Multiplies the signed integers a and b, storing the result in res.
413 * Checks for signed integer overflow.
414 * Similar to the GCC/Clang extension __builtin_mul_overflow
416 * @param a The first multiplicand.
417 * @param b The second multiplicand.
419 * @return true if overflow occurred; false if no overflow occurred.
422 U_INTERNAL UBool U_EXPORT2
uprv_mul32_overflow(int32_t a
, int32_t b
, int32_t* res
);
426 * Returns the number of digits after the decimal point in a double number x.
428 * @param x the double number
429 * @return the number of digits after the decimal point in a double number x.
432 /*U_INTERNAL int32_t U_EXPORT2 uprv_digitsAfterDecimal(double x);*/
435 #if !U_CHARSET_IS_UTF8
437 * Please use ucnv_getDefaultName() instead.
438 * Return the default codepage for this platform and locale.
439 * This function can call setlocale() on Unix platforms. Please read the
440 * platform documentation on setlocale() before calling this function.
441 * @return the default codepage for this platform
444 U_INTERNAL
const char* U_EXPORT2
uprv_getDefaultCodepage(void);
448 * Please use uloc_getDefault() instead.
449 * Return the default locale ID string by querying the system, or
450 * zero if one cannot be found.
451 * This function can call setlocale() on Unix platforms. Please read the
452 * platform documentation on setlocale() before calling this function.
453 * @return the default locale ID string
456 U_INTERNAL
const char* U_EXPORT2
uprv_getDefaultLocaleID(void);
459 * Time zone utilities
461 * Wrappers for C runtime library functions relating to timezones.
462 * The t_tzset() function (similar to tzset) uses the current setting
463 * of the environment variable TZ to assign values to three global
464 * variables: daylight, timezone, and tzname. These variables have the
465 * following meanings, and are declared in <time.h>.
467 * daylight Nonzero if daylight-saving-time zone (DST) is specified
468 * in TZ; otherwise, 0. Default value is 1.
469 * timezone Difference in seconds between coordinated universal
470 * time and local time. E.g., -28,800 for PST (GMT-8hrs)
471 * tzname(0) Three-letter time-zone name derived from TZ environment
472 * variable. E.g., "PST".
473 * tzname(1) Three-letter DST zone name derived from TZ environment
474 * variable. E.g., "PDT". If DST zone is omitted from TZ,
475 * tzname(1) is an empty string.
477 * Notes: For example, to set the TZ environment variable to correspond
478 * to the current time zone in Germany, you can use one of the
479 * following statements:
484 * If the TZ value is not set, t_tzset() attempts to use the time zone
485 * information specified by the operating system. Under Windows NT
486 * and Windows 95, this information is specified in the Control Panel's
487 * Date/Time application.
490 U_INTERNAL
void U_EXPORT2
uprv_tzset(void);
493 * Difference in seconds between coordinated universal
494 * time and local time. E.g., -28,800 for PST (GMT-8hrs)
495 * @return the difference in seconds between coordinated universal time and local time.
498 U_INTERNAL
int32_t U_EXPORT2
uprv_timezone(void);
501 * tzname(0) Three-letter time-zone name derived from TZ environment
502 * variable. E.g., "PST".
503 * tzname(1) Three-letter DST zone name derived from TZ environment
504 * variable. E.g., "PDT". If DST zone is omitted from TZ,
505 * tzname(1) is an empty string.
508 U_INTERNAL
const char* U_EXPORT2
uprv_tzname(int n
);
511 * Reset the global tzname cache.
514 U_INTERNAL
void uprv_tzname_clear_cache();
517 * Get UTC (GMT) time measured in milliseconds since 0:00 on 1/1/1970.
518 * This function is affected by 'faketime' and should be the bottleneck for all user-visible ICU time functions.
519 * @return the UTC time measured in milliseconds
522 U_INTERNAL UDate U_EXPORT2
uprv_getUTCtime(void);
525 * Get UTC (GMT) time measured in milliseconds since 0:00 on 1/1/1970.
526 * This function is not affected by 'faketime', so it should only be used by low level test functions- not by anything that
527 * exposes time to the end user.
528 * @return the UTC time measured in milliseconds
531 U_INTERNAL UDate U_EXPORT2
uprv_getRawUTCtime(void);
534 * Determine whether a pathname is absolute or not, as defined by the platform.
535 * @param path Pathname to test
536 * @return TRUE if the path is absolute
537 * @internal (ICU 3.0)
539 U_INTERNAL UBool U_EXPORT2
uprv_pathIsAbsolute(const char *path
);
542 * Use U_MAX_PTR instead of this function.
543 * @param void pointer to test
544 * @return the largest possible pointer greater than the base
545 * @internal (ICU 3.8)
547 U_INTERNAL
void * U_EXPORT2
uprv_maximumPtr(void *base
);
550 * Maximum value of a (void*) - use to indicate the limit of an 'infinite' buffer.
551 * In fact, buffer sizes must not exceed 2GB so that the difference between
552 * the buffer limit and the buffer start can be expressed in an int32_t.
554 * The definition of U_MAX_PTR must fulfill the following conditions:
555 * - return the largest possible pointer greater than base
556 * - return a valid pointer according to the machine architecture (AS/400, 64-bit, etc.)
557 * - avoid wrapping around at high addresses
558 * - make sure that the returned pointer is not farther from base than 0x7fffffff bytes
560 * @param base The beginning of a buffer to find the maximum offset from
564 # if U_PLATFORM == U_PF_OS390 && !defined(_LP64)
565 /* We have 31-bit pointers. */
566 # define U_MAX_PTR(base) ((void *)0x7fffffff)
567 # elif U_PLATFORM == U_PF_OS400
568 # define U_MAX_PTR(base) uprv_maximumPtr((void *)base)
571 * For platforms where pointers are scalar values (which is normal, but unlike i5/OS)
572 * but that do not define uintptr_t.
574 * However, this does not work on modern compilers:
575 * The C++ standard does not define pointer overflow, and allows compilers to
576 * assume that p+u>p for any pointer p and any integer u>0.
577 * Thus, modern compilers optimize away the ">" comparison.
578 * (See ICU tickets #7187 and #8096.)
580 # define U_MAX_PTR(base) \
581 ((void *)(((char *)(base)+0x7fffffffu) > (char *)(base) \
582 ? ((char *)(base)+0x7fffffffu) \
585 /* Default version. C++ standard compliant for scalar pointers. */
586 # define U_MAX_PTR(base) \
587 ((void *)(((uintptr_t)(base)+0x7fffffffu) > (uintptr_t)(base) \
588 ? ((uintptr_t)(base)+0x7fffffffu) \
596 * Pin a buffer capacity such that doing pointer arithmetic
597 * on the destination pointer and capacity cannot overflow.
599 * The pinned capacity must fulfill the following conditions (for positive capacities):
600 * - dest + capacity is a valid pointer according to the machine arcitecture (AS/400, 64-bit, etc.)
601 * - (dest + capacity) >= dest
602 * - The size (in bytes) of T[capacity] does not exceed 0x7fffffff
604 * @param dest the destination buffer pointer.
605 * @param capacity the requested buffer capacity, in units of type T.
606 * @return the pinned capacity.
609 template <typename T
>
610 inline int32_t pinCapacity(T
*dest
, int32_t capacity
) {
611 if (capacity
<= 0) { return capacity
; }
613 uintptr_t destInt
= (uintptr_t)dest
;
616 # if U_PLATFORM == U_PF_OS390 && !defined(_LP64)
617 // We have 31-bit pointers.
619 # elif U_PLATFORM == U_PF_OS400
620 maxInt
= (uintptr_t)uprv_maximumPtr((void *)dest
);
622 maxInt
= destInt
+ 0x7fffffffu
;
623 if (maxInt
< destInt
) {
624 // Less than 2GB to the end of the address space.
625 // Pin to that to prevent address overflow.
626 maxInt
= (uintptr_t)-1;
630 uintptr_t maxBytes
= maxInt
- destInt
; // max. 2GB
631 int32_t maxCapacity
= (int32_t)(maxBytes
/ sizeof(T
));
632 return capacity
<= maxCapacity
? capacity
: maxCapacity
;
634 #endif // __cplusplus
636 /* Dynamic Library Functions */
638 typedef void (UVoidFunction
)(void);
643 * @internal (ICU 4.4)
645 U_INTERNAL
void * U_EXPORT2
uprv_dl_open(const char *libName
, UErrorCode
*status
);
649 * @internal (ICU 4.4)
651 U_INTERNAL
void U_EXPORT2
uprv_dl_close( void *lib
, UErrorCode
*status
);
654 * Extract a symbol from a library (function)
655 * @internal (ICU 4.8)
657 U_INTERNAL UVoidFunction
* U_EXPORT2
uprv_dlsym_func( void *lib
, const char *symbolName
, UErrorCode
*status
);
660 * Extract a symbol from a library (function)
661 * Not implemented, no clients.
664 /* U_INTERNAL void * U_EXPORT2 uprv_dlsym_data( void *lib, const char *symbolName, UErrorCode *status); */
669 * Define malloc and related functions
672 #if U_PLATFORM == U_PF_OS400
673 # define uprv_default_malloc(x) _C_TS_malloc(x)
674 # define uprv_default_realloc(x,y) _C_TS_realloc(x,y)
675 # define uprv_default_free(x) _C_TS_free(x)
676 /* also _C_TS_calloc(x) */
679 # define uprv_default_malloc(x) malloc(x)
680 # define uprv_default_realloc(x,y) realloc(x,y)
681 # define uprv_default_free(x) free(x)