2 ******************************************************************************
4 * Copyright (C) 1997-2012, 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 #if !defined(__intptr_t_defined) && !defined(UINTPTR_MAX)
69 typedef size_t uintptr_t;
73 * \def U_HAVE_MSVC_2003_OR_EARLIER
74 * Flag for workaround of MSVC 2003 optimization bugs
77 #if !defined(U_HAVE_MSVC_2003_OR_EARLIER) && defined(_MSC_VER) && (_MSC_VER < 1400)
78 #define U_HAVE_MSVC_2003_OR_EARLIER
81 /*===========================================================================*/
82 /** @{ Information about POSIX support */
83 /*===========================================================================*/
85 #ifdef U_HAVE_NL_LANGINFO_CODESET
86 /* Use the predefined value. */
87 #elif U_PLATFORM_HAS_WIN32_API
88 # define U_HAVE_NL_LANGINFO_CODESET 0
90 # define U_HAVE_NL_LANGINFO_CODESET 1
93 #ifdef U_NL_LANGINFO_CODESET
94 /* Use the predefined value. */
95 #elif !U_HAVE_NL_LANGINFO_CODESET
96 # define U_NL_LANGINFO_CODESET -1
97 #elif U_PLATFORM == U_PF_OS400
100 # define U_NL_LANGINFO_CODESET CODESET
104 /* Use the predefined value. */
105 #elif U_PLATFORM_USES_ONLY_WIN32_API
106 # define U_TZSET _tzset
107 #elif U_PLATFORM == U_PF_OS400
110 # define U_TZSET tzset
114 /* Use the predefined value. */
115 #elif U_PLATFORM_IS_LINUX_BASED
116 # define U_TIMEZONE __timezone
117 #elif U_PLATFORM_USES_ONLY_WIN32_API
118 # define U_TIMEZONE _timezone
119 #elif U_PLATFORM == U_PF_OS400
122 # define U_TIMEZONE timezone
126 /* Use the predefined value. */
127 #elif U_PLATFORM_USES_ONLY_WIN32_API
128 # define U_TZNAME _tzname
129 #elif U_PLATFORM == U_PF_OS400
132 # define U_TZNAME tzname
136 /* Use the predefined value. */
137 #elif U_PLATFORM_HAS_WIN32_API
138 # define U_HAVE_MMAP 0
140 # define U_HAVE_MMAP 1
144 /* Use the predefined value. */
145 #elif U_PLATFORM_USES_ONLY_WIN32_API
146 # define U_HAVE_POPEN 0
147 #elif U_PLATFORM == U_PF_OS400
148 # define U_HAVE_POPEN 0
150 # define U_HAVE_POPEN 1
154 * \def U_HAVE_DIRENT_H
155 * Defines whether dirent.h is available.
158 #ifdef U_HAVE_DIRENT_H
159 /* Use the predefined value. */
160 #elif U_PLATFORM_HAS_WIN32_API
161 # define U_HAVE_DIRENT_H 0
163 # define U_HAVE_DIRENT_H 1
168 /*===========================================================================*/
169 /** @{ GCC built in functions for atomic memory operations */
170 /*===========================================================================*/
173 * \def U_HAVE_GCC_ATOMICS
176 #ifdef U_HAVE_GCC_ATOMICS
177 /* Use the predefined value. */
178 #elif defined(__GNUC__) && ((__GNUC__ * 100 + __GNUC_MINOR__) >= 401)
179 # define U_HAVE_GCC_ATOMICS 1
181 # define U_HAVE_GCC_ATOMICS 0
186 /*===========================================================================*/
187 /** @{ Code alignment */
188 /*===========================================================================*/
192 * This is used to align code fragments to a specific byte boundary.
193 * This is useful for getting consistent performance test results.
197 /* Use the predefined value. */
198 #elif defined(_MSC_VER) && defined(_M_IX86) && !defined(_MANAGED)
199 # define U_ALIGN_CODE(boundarySize) __asm align boundarySize
201 # define U_ALIGN_CODE(boundarySize)
206 /*===========================================================================*/
207 /** @{ Programs used by ICU code */
208 /*===========================================================================*/
211 * \def U_MAKE_IS_NMAKE
212 * Defines whether the "make" program is Windows nmake.
214 #ifdef U_MAKE_IS_NMAKE
215 /* Use the predefined value. */
216 #elif U_PLATFORM == U_PF_WINDOWS
217 # define U_MAKE_IS_NMAKE 1
219 # define U_MAKE_IS_NMAKE 0
224 /*==========================================================================*/
225 /* Platform utilities */
226 /*==========================================================================*/
229 * Platform utilities isolates the platform dependencies of the
230 * libarary. For each platform which this code is ported to, these
231 * functions may have to be re-implemented.
235 * Floating point utility to determine if a double is Not a Number (NaN).
238 U_INTERNAL UBool U_EXPORT2
uprv_isNaN(double d
);
240 * Floating point utility to determine if a double has an infinite value.
243 U_INTERNAL UBool U_EXPORT2
uprv_isInfinite(double d
);
245 * Floating point utility to determine if a double has a positive infinite value.
248 U_INTERNAL UBool U_EXPORT2
uprv_isPositiveInfinity(double d
);
250 * Floating point utility to determine if a double has a negative infinite value.
253 U_INTERNAL UBool U_EXPORT2
uprv_isNegativeInfinity(double d
);
255 * Floating point utility that returns a Not a Number (NaN) value.
258 U_INTERNAL
double U_EXPORT2
uprv_getNaN(void);
260 * Floating point utility that returns an infinite value.
263 U_INTERNAL
double U_EXPORT2
uprv_getInfinity(void);
266 * Floating point utility to truncate a double.
269 U_INTERNAL
double U_EXPORT2
uprv_trunc(double d
);
271 * Floating point utility to calculate the floor of a double.
274 U_INTERNAL
double U_EXPORT2
uprv_floor(double d
);
276 * Floating point utility to calculate the ceiling of a double.
279 U_INTERNAL
double U_EXPORT2
uprv_ceil(double d
);
281 * Floating point utility to calculate the absolute value of a double.
284 U_INTERNAL
double U_EXPORT2
uprv_fabs(double d
);
286 * Floating point utility to calculate the fractional and integer parts of a double.
289 U_INTERNAL
double U_EXPORT2
uprv_modf(double d
, double* pinteger
);
291 * Floating point utility to calculate the remainder of a double divided by another double.
294 U_INTERNAL
double U_EXPORT2
uprv_fmod(double d
, double y
);
296 * Floating point utility to calculate d to the power of exponent (d^exponent).
299 U_INTERNAL
double U_EXPORT2
uprv_pow(double d
, double exponent
);
301 * Floating point utility to calculate 10 to the power of exponent (10^exponent).
304 U_INTERNAL
double U_EXPORT2
uprv_pow10(int32_t exponent
);
306 * Floating point utility to calculate the maximum value of two doubles.
309 U_INTERNAL
double U_EXPORT2
uprv_fmax(double d
, double y
);
311 * Floating point utility to calculate the minimum value of two doubles.
314 U_INTERNAL
double U_EXPORT2
uprv_fmin(double d
, double y
);
316 * Private utility to calculate the maximum value of two integers.
319 U_INTERNAL
int32_t U_EXPORT2
uprv_max(int32_t d
, int32_t y
);
321 * Private utility to calculate the minimum value of two integers.
324 U_INTERNAL
int32_t U_EXPORT2
uprv_min(int32_t d
, int32_t y
);
327 # define uprv_isNegative(number) (*((signed char *)&(number))<0)
329 # define uprv_isNegative(number) (*((signed char *)&(number)+sizeof(number)-1)<0)
333 * Return the largest positive number that can be represented by an integer
334 * type of arbitrary bit length.
337 U_INTERNAL
double U_EXPORT2
uprv_maxMantissa(void);
340 * Floating point utility to calculate the logarithm of a double.
343 U_INTERNAL
double U_EXPORT2
uprv_log(double d
);
346 * Does common notion of rounding e.g. uprv_floor(x + 0.5);
347 * @param x the double number
348 * @return the rounded double
351 U_INTERNAL
double U_EXPORT2
uprv_round(double x
);
355 * Returns the number of digits after the decimal point in a double number x.
357 * @param x the double number
358 * @return the number of digits after the decimal point in a double number x.
361 /*U_INTERNAL int32_t U_EXPORT2 uprv_digitsAfterDecimal(double x);*/
364 #if !U_CHARSET_IS_UTF8
366 * Please use ucnv_getDefaultName() instead.
367 * Return the default codepage for this platform and locale.
368 * This function can call setlocale() on Unix platforms. Please read the
369 * platform documentation on setlocale() before calling this function.
370 * @return the default codepage for this platform
373 U_INTERNAL
const char* U_EXPORT2
uprv_getDefaultCodepage(void);
377 * Please use uloc_getDefault() instead.
378 * Return the default locale ID string by querying ths system, or
379 * zero if one cannot be found.
380 * This function can call setlocale() on Unix platforms. Please read the
381 * platform documentation on setlocale() before calling this function.
382 * @return the default locale ID string
385 U_INTERNAL
const char* U_EXPORT2
uprv_getDefaultLocaleID(void);
388 * Time zone utilities
390 * Wrappers for C runtime library functions relating to timezones.
391 * The t_tzset() function (similar to tzset) uses the current setting
392 * of the environment variable TZ to assign values to three global
393 * variables: daylight, timezone, and tzname. These variables have the
394 * following meanings, and are declared in <time.h>.
396 * daylight Nonzero if daylight-saving-time zone (DST) is specified
397 * in TZ; otherwise, 0. Default value is 1.
398 * timezone Difference in seconds between coordinated universal
399 * time and local time. E.g., -28,800 for PST (GMT-8hrs)
400 * tzname(0) Three-letter time-zone name derived from TZ environment
401 * variable. E.g., "PST".
402 * tzname(1) Three-letter DST zone name derived from TZ environment
403 * variable. E.g., "PDT". If DST zone is omitted from TZ,
404 * tzname(1) is an empty string.
406 * Notes: For example, to set the TZ environment variable to correspond
407 * to the current time zone in Germany, you can use one of the
408 * following statements:
413 * If the TZ value is not set, t_tzset() attempts to use the time zone
414 * information specified by the operating system. Under Windows NT
415 * and Windows 95, this information is specified in the Control Panel's
416 * Date/Time application.
419 U_INTERNAL
void U_EXPORT2
uprv_tzset(void);
422 * Difference in seconds between coordinated universal
423 * time and local time. E.g., -28,800 for PST (GMT-8hrs)
424 * @return the difference in seconds between coordinated universal time and local time.
427 U_INTERNAL
int32_t U_EXPORT2
uprv_timezone(void);
430 * tzname(0) Three-letter time-zone name derived from TZ environment
431 * variable. E.g., "PST".
432 * tzname(1) Three-letter DST zone name derived from TZ environment
433 * variable. E.g., "PDT". If DST zone is omitted from TZ,
434 * tzname(1) is an empty string.
437 U_INTERNAL
const char* U_EXPORT2
uprv_tzname(int n
);
440 * Get UTC (GMT) time measured in milliseconds since 0:00 on 1/1/1970.
441 * This function is affected by 'faketime' and should be the bottleneck for all user-visible ICU time functions.
442 * @return the UTC time measured in milliseconds
445 U_INTERNAL UDate U_EXPORT2
uprv_getUTCtime(void);
448 * Get UTC (GMT) time measured in milliseconds since 0:00 on 1/1/1970.
449 * This function is not affected by 'faketime', so it should only be used by low level test functions- not by anything that
450 * exposes time to the end user.
451 * @return the UTC time measured in milliseconds
454 U_INTERNAL UDate U_EXPORT2
uprv_getRawUTCtime(void);
457 * Determine whether a pathname is absolute or not, as defined by the platform.
458 * @param path Pathname to test
459 * @return TRUE if the path is absolute
460 * @internal (ICU 3.0)
462 U_INTERNAL UBool U_EXPORT2
uprv_pathIsAbsolute(const char *path
);
465 * Use U_MAX_PTR instead of this function.
466 * @param void pointer to test
467 * @return the largest possible pointer greater than the base
468 * @internal (ICU 3.8)
470 U_INTERNAL
void * U_EXPORT2
uprv_maximumPtr(void *base
);
473 * Maximum value of a (void*) - use to indicate the limit of an 'infinite' buffer.
474 * In fact, buffer sizes must not exceed 2GB so that the difference between
475 * the buffer limit and the buffer start can be expressed in an int32_t.
477 * The definition of U_MAX_PTR must fulfill the following conditions:
478 * - return the largest possible pointer greater than base
479 * - return a valid pointer according to the machine architecture (AS/400, 64-bit, etc.)
480 * - avoid wrapping around at high addresses
481 * - make sure that the returned pointer is not farther from base than 0x7fffffff bytes
483 * @param base The beginning of a buffer to find the maximum offset from
487 # if U_PLATFORM == U_PF_OS390 && !defined(_LP64)
488 /* We have 31-bit pointers. */
489 # define U_MAX_PTR(base) ((void *)0x7fffffff)
490 # elif U_PLATFORM == U_PF_OS400
491 # define U_MAX_PTR(base) uprv_maximumPtr((void *)base)
494 * For platforms where pointers are scalar values (which is normal, but unlike i5/OS)
495 * but that do not define uintptr_t.
497 * However, this does not work on modern compilers:
498 * The C++ standard does not define pointer overflow, and allows compilers to
499 * assume that p+u>p for any pointer p and any integer u>0.
500 * Thus, modern compilers optimize away the ">" comparison.
501 * (See ICU tickets #7187 and #8096.)
503 # define U_MAX_PTR(base) \
504 ((void *)(((char *)(base)+0x7fffffffu) > (char *)(base) \
505 ? ((char *)(base)+0x7fffffffu) \
508 /* Default version. C++ standard compliant for scalar pointers. */
509 # define U_MAX_PTR(base) \
510 ((void *)(((uintptr_t)(base)+0x7fffffffu) > (uintptr_t)(base) \
511 ? ((uintptr_t)(base)+0x7fffffffu) \
516 /* Dynamic Library Functions */
518 typedef void (UVoidFunction
)(void);
523 * @internal (ICU 4.4)
525 U_INTERNAL
void * U_EXPORT2
uprv_dl_open(const char *libName
, UErrorCode
*status
);
529 * @internal (ICU 4.4)
531 U_INTERNAL
void U_EXPORT2
uprv_dl_close( void *lib
, UErrorCode
*status
);
534 * Extract a symbol from a library (function)
535 * @internal (ICU 4.8)
537 U_INTERNAL UVoidFunction
* U_EXPORT2
uprv_dlsym_func( void *lib
, const char *symbolName
, UErrorCode
*status
);
540 * Extract a symbol from a library (function)
541 * Not implemented, no clients.
544 /* U_INTERNAL void * U_EXPORT2 uprv_dlsym_data( void *lib, const char *symbolName, UErrorCode *status); */
549 * Define malloc and related functions
552 #if U_PLATFORM == U_PF_OS400
553 # define uprv_default_malloc(x) _C_TS_malloc(x)
554 # define uprv_default_realloc(x,y) _C_TS_realloc(x,y)
555 # define uprv_default_free(x) _C_TS_free(x)
556 /* also _C_TS_calloc(x) */
559 # define uprv_default_malloc(x) malloc(x)
560 # define uprv_default_realloc(x,y) realloc(x,y)
561 # define uprv_default_free(x) free(x)