1 // Copyright (C) 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;
76 * \def U_HAVE_MSVC_2003_OR_EARLIER
77 * Flag for workaround of MSVC 2003 optimization bugs
80 #if !defined(U_HAVE_MSVC_2003_OR_EARLIER) && defined(_MSC_VER) && (_MSC_VER < 1400)
81 #define U_HAVE_MSVC_2003_OR_EARLIER
84 /*===========================================================================*/
85 /** @{ Information about POSIX support */
86 /*===========================================================================*/
88 #ifdef U_HAVE_NL_LANGINFO_CODESET
89 /* Use the predefined value. */
90 #elif U_PLATFORM_HAS_WIN32_API || U_PLATFORM == U_PF_ANDROID || U_PLATFORM == U_PF_QNX
91 # define U_HAVE_NL_LANGINFO_CODESET 0
93 # define U_HAVE_NL_LANGINFO_CODESET 1
96 #ifdef U_NL_LANGINFO_CODESET
97 /* Use the predefined value. */
98 #elif !U_HAVE_NL_LANGINFO_CODESET
99 # define U_NL_LANGINFO_CODESET -1
100 #elif U_PLATFORM == U_PF_OS400
103 # define U_NL_LANGINFO_CODESET CODESET
107 /* Use the predefined value. */
108 #elif U_PLATFORM_USES_ONLY_WIN32_API
109 # define U_TZSET _tzset
110 #elif U_PLATFORM == U_PF_OS400
113 # define U_TZSET tzset
116 #if defined(U_TIMEZONE) || defined(U_HAVE_TIMEZONE)
117 /* Use the predefined value. */
118 #elif U_PLATFORM == U_PF_ANDROID
119 # define U_TIMEZONE timezone
120 #elif U_PLATFORM_IS_LINUX_BASED
121 # if defined(__UCLIBC__)
122 /* uClibc does not have __timezone or _timezone. */
123 # elif defined(_NEWLIB_VERSION)
124 # define U_TIMEZONE _timezone
125 # elif defined(__GLIBC__)
127 # define U_TIMEZONE __timezone
129 #elif U_PLATFORM_USES_ONLY_WIN32_API
130 # define U_TIMEZONE _timezone
131 #elif U_PLATFORM == U_PF_BSD && !defined(__NetBSD__)
133 #elif U_PLATFORM == U_PF_OS400
135 #elif U_PLATFORM == U_PF_IPHONE
138 # define U_TIMEZONE timezone
142 /* Use the predefined value. */
143 #elif U_PLATFORM_USES_ONLY_WIN32_API
144 # define U_TZNAME _tzname
145 #elif U_PLATFORM == U_PF_OS400
148 # define U_TZNAME tzname
152 /* Use the predefined value. */
153 #elif U_PLATFORM_USES_ONLY_WIN32_API
154 # define U_HAVE_MMAP 0
156 # define U_HAVE_MMAP 1
160 /* Use the predefined value. */
161 #elif U_PLATFORM_USES_ONLY_WIN32_API
162 # define U_HAVE_POPEN 0
163 #elif U_PLATFORM == U_PF_OS400
164 # define U_HAVE_POPEN 0
166 # define U_HAVE_POPEN 1
170 * \def U_HAVE_DIRENT_H
171 * Defines whether dirent.h is available.
174 #ifdef U_HAVE_DIRENT_H
175 /* Use the predefined value. */
176 #elif U_PLATFORM_USES_ONLY_WIN32_API
177 # define U_HAVE_DIRENT_H 0
179 # define U_HAVE_DIRENT_H 1
184 /*===========================================================================*/
185 /** @{ GCC built in functions for atomic memory operations */
186 /*===========================================================================*/
189 * \def U_HAVE_GCC_ATOMICS
192 #ifdef U_HAVE_GCC_ATOMICS
193 /* Use the predefined value. */
194 #elif U_PLATFORM == U_PF_MINGW
195 #define U_HAVE_GCC_ATOMICS 0
196 #elif U_GCC_MAJOR_MINOR >= 404 || defined(__clang__)
197 /* TODO: Intel icc and IBM xlc on AIX also support gcc atomics. (Intel originated them.)
198 * Add them for these compilers.
199 * Note: Clang sets __GNUC__ defines for version 4.2, so misses the 4.4 test here.
201 # define U_HAVE_GCC_ATOMICS 1
203 # define U_HAVE_GCC_ATOMICS 0
209 * \def U_HAVE_STD_ATOMICS
210 * Defines whether the standard C++11 <atomic> is available.
211 * ICU will use this when avialable,
212 * otherwise will fall back to compiler or platform specific alternatives.
215 #ifdef U_HAVE_STD_ATOMICS
216 /* Use the predefined value. */
217 #elif U_CPLUSPLUS_VERSION < 11
218 /* Not C++11, disable use of atomics */
219 # define U_HAVE_STD_ATOMICS 0
220 #elif __clang__ && __clang_major__==3 && __clang_minor__<=1
221 /* Clang 3.1, has atomic variable initializer bug. */
222 # define U_HAVE_STD_ATOMICS 0
224 /* U_HAVE_ATOMIC is typically set by an autoconf test of #include <atomic> */
225 /* Can be set manually, or left undefined, on platforms without autoconf. */
226 # if defined(U_HAVE_ATOMIC) && U_HAVE_ATOMIC
227 # define U_HAVE_STD_ATOMICS 1
229 # define U_HAVE_STD_ATOMICS 0
235 * \def U_HAVE_CLANG_ATOMICS
236 * Defines whether Clang c11 style built-in atomics are avaialable.
237 * These are used in preference to gcc atomics when both are available.
239 #ifdef U_HAVE_CLANG_ATOMICS
240 /* Use the predefined value. */
241 #elif __has_builtin(__c11_atomic_load) && \
242 __has_builtin(__c11_atomic_store) && \
243 __has_builtin(__c11_atomic_fetch_add) && \
244 __has_builtin(__c11_atomic_fetch_sub)
245 # define U_HAVE_CLANG_ATOMICS 1
247 # define U_HAVE_CLANG_ATOMICS 0
250 /*===========================================================================*/
251 /** @{ Programs used by ICU code */
252 /*===========================================================================*/
255 * \def U_MAKE_IS_NMAKE
256 * Defines whether the "make" program is Windows nmake.
258 #ifdef U_MAKE_IS_NMAKE
259 /* Use the predefined value. */
260 #elif U_PLATFORM == U_PF_WINDOWS
261 # define U_MAKE_IS_NMAKE 1
263 # define U_MAKE_IS_NMAKE 0
268 /*==========================================================================*/
269 /* Platform utilities */
270 /*==========================================================================*/
273 * Platform utilities isolates the platform dependencies of the
274 * libarary. For each platform which this code is ported to, these
275 * functions may have to be re-implemented.
279 * Floating point utility to determine if a double is Not a Number (NaN).
282 U_INTERNAL UBool U_EXPORT2 uprv_isNaN(double d);
284 * Floating point utility to determine if a double has an infinite value.
287 U_INTERNAL UBool U_EXPORT2 uprv_isInfinite(double d);
289 * Floating point utility to determine if a double has a positive infinite value.
292 U_INTERNAL UBool U_EXPORT2 uprv_isPositiveInfinity(double d);
294 * Floating point utility to determine if a double has a negative infinite value.
297 U_INTERNAL UBool U_EXPORT2 uprv_isNegativeInfinity(double d);
299 * Floating point utility that returns a Not a Number (NaN) value.
302 U_INTERNAL double U_EXPORT2 uprv_getNaN(void);
304 * Floating point utility that returns an infinite value.
307 U_INTERNAL double U_EXPORT2 uprv_getInfinity(void);
310 * Floating point utility to truncate a double.
313 U_INTERNAL double U_EXPORT2 uprv_trunc(double d);
315 * Floating point utility to calculate the floor of a double.
318 U_INTERNAL double U_EXPORT2 uprv_floor(double d);
320 * Floating point utility to calculate the ceiling of a double.
323 U_INTERNAL double U_EXPORT2 uprv_ceil(double d);
325 * Floating point utility to calculate the absolute value of a double.
328 U_INTERNAL double U_EXPORT2 uprv_fabs(double d);
330 * Floating point utility to calculate the fractional and integer parts of a double.
333 U_INTERNAL double U_EXPORT2 uprv_modf(double d, double* pinteger);
335 * Floating point utility to calculate the remainder of a double divided by another double.
338 U_INTERNAL double U_EXPORT2 uprv_fmod(double d, double y);
340 * Floating point utility to calculate d to the power of exponent (d^exponent).
343 U_INTERNAL double U_EXPORT2 uprv_pow(double d, double exponent);
345 * Floating point utility to calculate 10 to the power of exponent (10^exponent).
348 U_INTERNAL double U_EXPORT2 uprv_pow10(int32_t exponent);
350 * Floating point utility to calculate the maximum value of two doubles.
353 U_INTERNAL double U_EXPORT2 uprv_fmax(double d, double y);
355 * Floating point utility to calculate the minimum value of two doubles.
358 U_INTERNAL double U_EXPORT2 uprv_fmin(double d, double y);
360 * Private utility to calculate the maximum value of two integers.
363 U_INTERNAL int32_t U_EXPORT2 uprv_max(int32_t d, int32_t y);
365 * Private utility to calculate the minimum value of two integers.
368 U_INTERNAL int32_t U_EXPORT2 uprv_min(int32_t d, int32_t y);
371 # define uprv_isNegative(number) (*((signed char *)&(number))<0)
373 # define uprv_isNegative(number) (*((signed char *)&(number)+sizeof(number)-1)<0)
377 * Return the largest positive number that can be represented by an integer
378 * type of arbitrary bit length.
381 U_INTERNAL double U_EXPORT2 uprv_maxMantissa(void);
384 * Floating point utility to calculate the logarithm of a double.
387 U_INTERNAL double U_EXPORT2 uprv_log(double d);
390 * Does common notion of rounding e.g. uprv_floor(x + 0.5);
391 * @param x the double number
392 * @return the rounded double
395 U_INTERNAL double U_EXPORT2 uprv_round(double x);
399 * Returns the number of digits after the decimal point in a double number x.
401 * @param x the double number
402 * @return the number of digits after the decimal point in a double number x.
405 /*U_INTERNAL int32_t U_EXPORT2 uprv_digitsAfterDecimal(double x);*/
408 #if !U_CHARSET_IS_UTF8
410 * Please use ucnv_getDefaultName() instead.
411 * Return the default codepage for this platform and locale.
412 * This function can call setlocale() on Unix platforms. Please read the
413 * platform documentation on setlocale() before calling this function.
414 * @return the default codepage for this platform
417 U_INTERNAL const char* U_EXPORT2 uprv_getDefaultCodepage(void);
421 * Please use uloc_getDefault() instead.
422 * Return the default locale ID string by querying ths system, or
423 * zero if one cannot be found.
424 * This function can call setlocale() on Unix platforms. Please read the
425 * platform documentation on setlocale() before calling this function.
426 * @return the default locale ID string
429 U_INTERNAL const char* U_EXPORT2 uprv_getDefaultLocaleID(void);
432 * Time zone utilities
434 * Wrappers for C runtime library functions relating to timezones.
435 * The t_tzset() function (similar to tzset) uses the current setting
436 * of the environment variable TZ to assign values to three global
437 * variables: daylight, timezone, and tzname. These variables have the
438 * following meanings, and are declared in <time.h>.
440 * daylight Nonzero if daylight-saving-time zone (DST) is specified
441 * in TZ; otherwise, 0. Default value is 1.
442 * timezone Difference in seconds between coordinated universal
443 * time and local time. E.g., -28,800 for PST (GMT-8hrs)
444 * tzname(0) Three-letter time-zone name derived from TZ environment
445 * variable. E.g., "PST".
446 * tzname(1) Three-letter DST zone name derived from TZ environment
447 * variable. E.g., "PDT". If DST zone is omitted from TZ,
448 * tzname(1) is an empty string.
450 * Notes: For example, to set the TZ environment variable to correspond
451 * to the current time zone in Germany, you can use one of the
452 * following statements:
457 * If the TZ value is not set, t_tzset() attempts to use the time zone
458 * information specified by the operating system. Under Windows NT
459 * and Windows 95, this information is specified in the Control Panel's
460 * Date/Time application.
463 U_INTERNAL void U_EXPORT2 uprv_tzset(void);
466 * Difference in seconds between coordinated universal
467 * time and local time. E.g., -28,800 for PST (GMT-8hrs)
468 * @return the difference in seconds between coordinated universal time and local time.
471 U_INTERNAL int32_t U_EXPORT2 uprv_timezone(void);
474 * tzname(0) Three-letter time-zone name derived from TZ environment
475 * variable. E.g., "PST".
476 * tzname(1) Three-letter DST zone name derived from TZ environment
477 * variable. E.g., "PDT". If DST zone is omitted from TZ,
478 * tzname(1) is an empty string.
481 U_INTERNAL const char* U_EXPORT2 uprv_tzname(int n);
484 * Reset the global tzname cache.
487 U_INTERNAL void uprv_tzname_clear_cache();
490 * Get UTC (GMT) time measured in milliseconds since 0:00 on 1/1/1970.
491 * This function is affected by 'faketime' and should be the bottleneck for all user-visible ICU time functions.
492 * @return the UTC time measured in milliseconds
495 U_INTERNAL UDate U_EXPORT2 uprv_getUTCtime(void);
498 * Get UTC (GMT) time measured in milliseconds since 0:00 on 1/1/1970.
499 * This function is not affected by 'faketime', so it should only be used by low level test functions- not by anything that
500 * exposes time to the end user.
501 * @return the UTC time measured in milliseconds
504 U_INTERNAL UDate U_EXPORT2 uprv_getRawUTCtime(void);
507 * Determine whether a pathname is absolute or not, as defined by the platform.
508 * @param path Pathname to test
509 * @return TRUE if the path is absolute
510 * @internal (ICU 3.0)
512 U_INTERNAL UBool U_EXPORT2 uprv_pathIsAbsolute(const char *path);
515 * Use U_MAX_PTR instead of this function.
516 * @param void pointer to test
517 * @return the largest possible pointer greater than the base
518 * @internal (ICU 3.8)
520 U_INTERNAL void * U_EXPORT2 uprv_maximumPtr(void *base);
523 * Maximum value of a (void*) - use to indicate the limit of an 'infinite' buffer.
524 * In fact, buffer sizes must not exceed 2GB so that the difference between
525 * the buffer limit and the buffer start can be expressed in an int32_t.
527 * The definition of U_MAX_PTR must fulfill the following conditions:
528 * - return the largest possible pointer greater than base
529 * - return a valid pointer according to the machine architecture (AS/400, 64-bit, etc.)
530 * - avoid wrapping around at high addresses
531 * - make sure that the returned pointer is not farther from base than 0x7fffffff bytes
533 * @param base The beginning of a buffer to find the maximum offset from
537 # if U_PLATFORM == U_PF_OS390 && !defined(_LP64)
538 /* We have 31-bit pointers. */
539 # define U_MAX_PTR(base) ((void *)0x7fffffff)
540 # elif U_PLATFORM == U_PF_OS400
541 # define U_MAX_PTR(base) uprv_maximumPtr((void *)base)
544 * For platforms where pointers are scalar values (which is normal, but unlike i5/OS)
545 * but that do not define uintptr_t.
547 * However, this does not work on modern compilers:
548 * The C++ standard does not define pointer overflow, and allows compilers to
549 * assume that p+u>p for any pointer p and any integer u>0.
550 * Thus, modern compilers optimize away the ">" comparison.
551 * (See ICU tickets #7187 and #8096.)
553 # define U_MAX_PTR(base) \
554 ((void *)(((char *)(base)+0x7fffffffu) > (char *)(base) \
555 ? ((char *)(base)+0x7fffffffu) \
558 /* Default version. C++ standard compliant for scalar pointers. */
559 # define U_MAX_PTR(base) \
560 ((void *)(((uintptr_t)(base)+0x7fffffffu) > (uintptr_t)(base) \
561 ? ((uintptr_t)(base)+0x7fffffffu) \
566 /* Dynamic Library Functions */
568 typedef void (UVoidFunction)(void);
573 * @internal (ICU 4.4)
575 U_INTERNAL void * U_EXPORT2 uprv_dl_open(const char *libName, UErrorCode *status);
579 * @internal (ICU 4.4)
581 U_INTERNAL void U_EXPORT2 uprv_dl_close( void *lib, UErrorCode *status);
584 * Extract a symbol from a library (function)
585 * @internal (ICU 4.8)
587 U_INTERNAL UVoidFunction* U_EXPORT2 uprv_dlsym_func( void *lib, const char *symbolName, UErrorCode *status);
590 * Extract a symbol from a library (function)
591 * Not implemented, no clients.
594 /* U_INTERNAL void * U_EXPORT2 uprv_dlsym_data( void *lib, const char *symbolName, UErrorCode *status); */
599 * Define malloc and related functions
602 #if U_PLATFORM == U_PF_OS400
603 # define uprv_default_malloc(x) _C_TS_malloc(x)
604 # define uprv_default_realloc(x,y) _C_TS_realloc(x,y)
605 # define uprv_default_free(x) _C_TS_free(x)
606 /* also _C_TS_calloc(x) */
609 # define uprv_default_malloc(x) malloc(x)
610 # define uprv_default_realloc(x,y) realloc(x,y)
611 # define uprv_default_free(x) free(x)