2 ******************************************************************************
4 * Copyright (C) 1997-2014, 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 !defined(__cplusplus) || __cplusplus<201103L
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
232 /*===========================================================================*/
233 /** @{ Code alignment */
234 /*===========================================================================*/
238 * This is used to align code fragments to a specific byte boundary.
239 * This is useful for getting consistent performance test results.
243 /* Use the predefined value. */
244 #elif defined(_MSC_VER) && defined(_M_IX86) && !defined(_MANAGED)
245 # define U_ALIGN_CODE(boundarySize) __asm align boundarySize
247 # define U_ALIGN_CODE(boundarySize)
252 /*===========================================================================*/
253 /** @{ Programs used by ICU code */
254 /*===========================================================================*/
257 * \def U_MAKE_IS_NMAKE
258 * Defines whether the "make" program is Windows nmake.
260 #ifdef U_MAKE_IS_NMAKE
261 /* Use the predefined value. */
262 #elif U_PLATFORM == U_PF_WINDOWS
263 # define U_MAKE_IS_NMAKE 1
265 # define U_MAKE_IS_NMAKE 0
270 /*==========================================================================*/
271 /* Platform utilities */
272 /*==========================================================================*/
275 * Platform utilities isolates the platform dependencies of the
276 * libarary. For each platform which this code is ported to, these
277 * functions may have to be re-implemented.
281 * Floating point utility to determine if a double is Not a Number (NaN).
284 U_INTERNAL UBool U_EXPORT2
uprv_isNaN(double d
);
286 * Floating point utility to determine if a double has an infinite value.
289 U_INTERNAL UBool U_EXPORT2
uprv_isInfinite(double d
);
291 * Floating point utility to determine if a double has a positive infinite value.
294 U_INTERNAL UBool U_EXPORT2
uprv_isPositiveInfinity(double d
);
296 * Floating point utility to determine if a double has a negative infinite value.
299 U_INTERNAL UBool U_EXPORT2
uprv_isNegativeInfinity(double d
);
301 * Floating point utility that returns a Not a Number (NaN) value.
304 U_INTERNAL
double U_EXPORT2
uprv_getNaN(void);
306 * Floating point utility that returns an infinite value.
309 U_INTERNAL
double U_EXPORT2
uprv_getInfinity(void);
312 * Floating point utility to truncate a double.
315 U_INTERNAL
double U_EXPORT2
uprv_trunc(double d
);
317 * Floating point utility to calculate the floor of a double.
320 U_INTERNAL
double U_EXPORT2
uprv_floor(double d
);
322 * Floating point utility to calculate the ceiling of a double.
325 U_INTERNAL
double U_EXPORT2
uprv_ceil(double d
);
327 * Floating point utility to calculate the absolute value of a double.
330 U_INTERNAL
double U_EXPORT2
uprv_fabs(double d
);
332 * Floating point utility to calculate the fractional and integer parts of a double.
335 U_INTERNAL
double U_EXPORT2
uprv_modf(double d
, double* pinteger
);
337 * Floating point utility to calculate the remainder of a double divided by another double.
340 U_INTERNAL
double U_EXPORT2
uprv_fmod(double d
, double y
);
342 * Floating point utility to calculate d to the power of exponent (d^exponent).
345 U_INTERNAL
double U_EXPORT2
uprv_pow(double d
, double exponent
);
347 * Floating point utility to calculate 10 to the power of exponent (10^exponent).
350 U_INTERNAL
double U_EXPORT2
uprv_pow10(int32_t exponent
);
352 * Floating point utility to calculate the maximum value of two doubles.
355 U_INTERNAL
double U_EXPORT2
uprv_fmax(double d
, double y
);
357 * Floating point utility to calculate the minimum value of two doubles.
360 U_INTERNAL
double U_EXPORT2
uprv_fmin(double d
, double y
);
362 * Private utility to calculate the maximum value of two integers.
365 U_INTERNAL
int32_t U_EXPORT2
uprv_max(int32_t d
, int32_t y
);
367 * Private utility to calculate the minimum value of two integers.
370 U_INTERNAL
int32_t U_EXPORT2
uprv_min(int32_t d
, int32_t y
);
373 # define uprv_isNegative(number) (*((signed char *)&(number))<0)
375 # define uprv_isNegative(number) (*((signed char *)&(number)+sizeof(number)-1)<0)
379 * Return the largest positive number that can be represented by an integer
380 * type of arbitrary bit length.
383 U_INTERNAL
double U_EXPORT2
uprv_maxMantissa(void);
386 * Floating point utility to calculate the logarithm of a double.
389 U_INTERNAL
double U_EXPORT2
uprv_log(double d
);
392 * Does common notion of rounding e.g. uprv_floor(x + 0.5);
393 * @param x the double number
394 * @return the rounded double
397 U_INTERNAL
double U_EXPORT2
uprv_round(double x
);
401 * Returns the number of digits after the decimal point in a double number x.
403 * @param x the double number
404 * @return the number of digits after the decimal point in a double number x.
407 /*U_INTERNAL int32_t U_EXPORT2 uprv_digitsAfterDecimal(double x);*/
410 #if !U_CHARSET_IS_UTF8
412 * Please use ucnv_getDefaultName() instead.
413 * Return the default codepage for this platform and locale.
414 * This function can call setlocale() on Unix platforms. Please read the
415 * platform documentation on setlocale() before calling this function.
416 * @return the default codepage for this platform
419 U_INTERNAL
const char* U_EXPORT2
uprv_getDefaultCodepage(void);
423 * Please use uloc_getDefault() instead.
424 * Return the default locale ID string by querying ths system, or
425 * zero if one cannot be found.
426 * This function can call setlocale() on Unix platforms. Please read the
427 * platform documentation on setlocale() before calling this function.
428 * @return the default locale ID string
431 U_INTERNAL
const char* U_EXPORT2
uprv_getDefaultLocaleID(void);
434 * Time zone utilities
436 * Wrappers for C runtime library functions relating to timezones.
437 * The t_tzset() function (similar to tzset) uses the current setting
438 * of the environment variable TZ to assign values to three global
439 * variables: daylight, timezone, and tzname. These variables have the
440 * following meanings, and are declared in <time.h>.
442 * daylight Nonzero if daylight-saving-time zone (DST) is specified
443 * in TZ; otherwise, 0. Default value is 1.
444 * timezone Difference in seconds between coordinated universal
445 * time and local time. E.g., -28,800 for PST (GMT-8hrs)
446 * tzname(0) Three-letter time-zone name derived from TZ environment
447 * variable. E.g., "PST".
448 * tzname(1) Three-letter DST zone name derived from TZ environment
449 * variable. E.g., "PDT". If DST zone is omitted from TZ,
450 * tzname(1) is an empty string.
452 * Notes: For example, to set the TZ environment variable to correspond
453 * to the current time zone in Germany, you can use one of the
454 * following statements:
459 * If the TZ value is not set, t_tzset() attempts to use the time zone
460 * information specified by the operating system. Under Windows NT
461 * and Windows 95, this information is specified in the Control Panel's
462 * Date/Time application.
465 U_INTERNAL
void U_EXPORT2
uprv_tzset(void);
468 * Difference in seconds between coordinated universal
469 * time and local time. E.g., -28,800 for PST (GMT-8hrs)
470 * @return the difference in seconds between coordinated universal time and local time.
473 U_INTERNAL
int32_t U_EXPORT2
uprv_timezone(void);
476 * tzname(0) Three-letter time-zone name derived from TZ environment
477 * variable. E.g., "PST".
478 * tzname(1) Three-letter DST zone name derived from TZ environment
479 * variable. E.g., "PDT". If DST zone is omitted from TZ,
480 * tzname(1) is an empty string.
483 U_INTERNAL
const char* U_EXPORT2
uprv_tzname(int n
);
486 * Get UTC (GMT) time measured in milliseconds since 0:00 on 1/1/1970.
487 * This function is affected by 'faketime' and should be the bottleneck for all user-visible ICU time functions.
488 * @return the UTC time measured in milliseconds
491 U_INTERNAL UDate U_EXPORT2
uprv_getUTCtime(void);
494 * Get UTC (GMT) time measured in milliseconds since 0:00 on 1/1/1970.
495 * This function is not affected by 'faketime', so it should only be used by low level test functions- not by anything that
496 * exposes time to the end user.
497 * @return the UTC time measured in milliseconds
500 U_INTERNAL UDate U_EXPORT2
uprv_getRawUTCtime(void);
503 * Determine whether a pathname is absolute or not, as defined by the platform.
504 * @param path Pathname to test
505 * @return TRUE if the path is absolute
506 * @internal (ICU 3.0)
508 U_INTERNAL UBool U_EXPORT2
uprv_pathIsAbsolute(const char *path
);
511 * Use U_MAX_PTR instead of this function.
512 * @param void pointer to test
513 * @return the largest possible pointer greater than the base
514 * @internal (ICU 3.8)
516 U_INTERNAL
void * U_EXPORT2
uprv_maximumPtr(void *base
);
519 * Maximum value of a (void*) - use to indicate the limit of an 'infinite' buffer.
520 * In fact, buffer sizes must not exceed 2GB so that the difference between
521 * the buffer limit and the buffer start can be expressed in an int32_t.
523 * The definition of U_MAX_PTR must fulfill the following conditions:
524 * - return the largest possible pointer greater than base
525 * - return a valid pointer according to the machine architecture (AS/400, 64-bit, etc.)
526 * - avoid wrapping around at high addresses
527 * - make sure that the returned pointer is not farther from base than 0x7fffffff bytes
529 * @param base The beginning of a buffer to find the maximum offset from
533 # if U_PLATFORM == U_PF_OS390 && !defined(_LP64)
534 /* We have 31-bit pointers. */
535 # define U_MAX_PTR(base) ((void *)0x7fffffff)
536 # elif U_PLATFORM == U_PF_OS400
537 # define U_MAX_PTR(base) uprv_maximumPtr((void *)base)
540 * For platforms where pointers are scalar values (which is normal, but unlike i5/OS)
541 * but that do not define uintptr_t.
543 * However, this does not work on modern compilers:
544 * The C++ standard does not define pointer overflow, and allows compilers to
545 * assume that p+u>p for any pointer p and any integer u>0.
546 * Thus, modern compilers optimize away the ">" comparison.
547 * (See ICU tickets #7187 and #8096.)
549 # define U_MAX_PTR(base) \
550 ((void *)(((char *)(base)+0x7fffffffu) > (char *)(base) \
551 ? ((char *)(base)+0x7fffffffu) \
554 /* Default version. C++ standard compliant for scalar pointers. */
555 # define U_MAX_PTR(base) \
556 ((void *)(((uintptr_t)(base)+0x7fffffffu) > (uintptr_t)(base) \
557 ? ((uintptr_t)(base)+0x7fffffffu) \
562 /* Dynamic Library Functions */
564 typedef void (UVoidFunction
)(void);
569 * @internal (ICU 4.4)
571 U_INTERNAL
void * U_EXPORT2
uprv_dl_open(const char *libName
, UErrorCode
*status
);
575 * @internal (ICU 4.4)
577 U_INTERNAL
void U_EXPORT2
uprv_dl_close( void *lib
, UErrorCode
*status
);
580 * Extract a symbol from a library (function)
581 * @internal (ICU 4.8)
583 U_INTERNAL UVoidFunction
* U_EXPORT2
uprv_dlsym_func( void *lib
, const char *symbolName
, UErrorCode
*status
);
586 * Extract a symbol from a library (function)
587 * Not implemented, no clients.
590 /* U_INTERNAL void * U_EXPORT2 uprv_dlsym_data( void *lib, const char *symbolName, UErrorCode *status); */
595 * Define malloc and related functions
598 #if U_PLATFORM == U_PF_OS400
599 # define uprv_default_malloc(x) _C_TS_malloc(x)
600 # define uprv_default_realloc(x,y) _C_TS_realloc(x,y)
601 # define uprv_default_free(x) _C_TS_free(x)
602 /* also _C_TS_calloc(x) */
605 # define uprv_default_malloc(x) malloc(x)
606 # define uprv_default_realloc(x,y) realloc(x,y)
607 # define uprv_default_free(x) free(x)