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 /* 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
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
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 /* __timezone is only available in glibc */
121 # define U_TIMEZONE __timezone
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
130 # define U_TIMEZONE timezone
134 /* Use the predefined value. */
135 #elif U_PLATFORM_USES_ONLY_WIN32_API
136 # define U_TZNAME _tzname
137 #elif U_PLATFORM == U_PF_OS400
140 # define U_TZNAME tzname
144 /* Use the predefined value. */
145 #elif U_PLATFORM_HAS_WIN32_API
146 # define U_HAVE_MMAP 0
148 # define U_HAVE_MMAP 1
152 /* Use the predefined value. */
153 #elif U_PLATFORM_USES_ONLY_WIN32_API
154 # define U_HAVE_POPEN 0
155 #elif U_PLATFORM == U_PF_OS400
156 # define U_HAVE_POPEN 0
158 # define U_HAVE_POPEN 1
162 * \def U_HAVE_DIRENT_H
163 * Defines whether dirent.h is available.
166 #ifdef U_HAVE_DIRENT_H
167 /* Use the predefined value. */
168 #elif U_PLATFORM_HAS_WIN32_API
169 # define U_HAVE_DIRENT_H 0
171 # define U_HAVE_DIRENT_H 1
176 /*===========================================================================*/
177 /** @{ GCC built in functions for atomic memory operations */
178 /*===========================================================================*/
181 * \def U_HAVE_GCC_ATOMICS
184 #ifdef U_HAVE_GCC_ATOMICS
185 /* Use the predefined value. */
186 #elif U_GCC_MAJOR_MINOR >= 404
187 # define U_HAVE_GCC_ATOMICS 1
189 # define U_HAVE_GCC_ATOMICS 0
194 /*===========================================================================*/
195 /** @{ Code alignment */
196 /*===========================================================================*/
200 * This is used to align code fragments to a specific byte boundary.
201 * This is useful for getting consistent performance test results.
205 /* Use the predefined value. */
206 #elif defined(_MSC_VER) && defined(_M_IX86) && !defined(_MANAGED)
207 # define U_ALIGN_CODE(boundarySize) __asm align boundarySize
209 # define U_ALIGN_CODE(boundarySize)
214 /*===========================================================================*/
215 /** @{ Programs used by ICU code */
216 /*===========================================================================*/
219 * \def U_MAKE_IS_NMAKE
220 * Defines whether the "make" program is Windows nmake.
222 #ifdef U_MAKE_IS_NMAKE
223 /* Use the predefined value. */
224 #elif U_PLATFORM == U_PF_WINDOWS
225 # define U_MAKE_IS_NMAKE 1
227 # define U_MAKE_IS_NMAKE 0
232 /*==========================================================================*/
233 /* Platform utilities */
234 /*==========================================================================*/
237 * Platform utilities isolates the platform dependencies of the
238 * libarary. For each platform which this code is ported to, these
239 * functions may have to be re-implemented.
243 * Floating point utility to determine if a double is Not a Number (NaN).
246 U_INTERNAL UBool U_EXPORT2
uprv_isNaN(double d
);
248 * Floating point utility to determine if a double has an infinite value.
251 U_INTERNAL UBool U_EXPORT2
uprv_isInfinite(double d
);
253 * Floating point utility to determine if a double has a positive infinite value.
256 U_INTERNAL UBool U_EXPORT2
uprv_isPositiveInfinity(double d
);
258 * Floating point utility to determine if a double has a negative infinite value.
261 U_INTERNAL UBool U_EXPORT2
uprv_isNegativeInfinity(double d
);
263 * Floating point utility that returns a Not a Number (NaN) value.
266 U_INTERNAL
double U_EXPORT2
uprv_getNaN(void);
268 * Floating point utility that returns an infinite value.
271 U_INTERNAL
double U_EXPORT2
uprv_getInfinity(void);
274 * Floating point utility to truncate a double.
277 U_INTERNAL
double U_EXPORT2
uprv_trunc(double d
);
279 * Floating point utility to calculate the floor of a double.
282 U_INTERNAL
double U_EXPORT2
uprv_floor(double d
);
284 * Floating point utility to calculate the ceiling of a double.
287 U_INTERNAL
double U_EXPORT2
uprv_ceil(double d
);
289 * Floating point utility to calculate the absolute value of a double.
292 U_INTERNAL
double U_EXPORT2
uprv_fabs(double d
);
294 * Floating point utility to calculate the fractional and integer parts of a double.
297 U_INTERNAL
double U_EXPORT2
uprv_modf(double d
, double* pinteger
);
299 * Floating point utility to calculate the remainder of a double divided by another double.
302 U_INTERNAL
double U_EXPORT2
uprv_fmod(double d
, double y
);
304 * Floating point utility to calculate d to the power of exponent (d^exponent).
307 U_INTERNAL
double U_EXPORT2
uprv_pow(double d
, double exponent
);
309 * Floating point utility to calculate 10 to the power of exponent (10^exponent).
312 U_INTERNAL
double U_EXPORT2
uprv_pow10(int32_t exponent
);
314 * Floating point utility to calculate the maximum value of two doubles.
317 U_INTERNAL
double U_EXPORT2
uprv_fmax(double d
, double y
);
319 * Floating point utility to calculate the minimum value of two doubles.
322 U_INTERNAL
double U_EXPORT2
uprv_fmin(double d
, double y
);
324 * Private utility to calculate the maximum value of two integers.
327 U_INTERNAL
int32_t U_EXPORT2
uprv_max(int32_t d
, int32_t y
);
329 * Private utility to calculate the minimum value of two integers.
332 U_INTERNAL
int32_t U_EXPORT2
uprv_min(int32_t d
, int32_t y
);
335 # define uprv_isNegative(number) (*((signed char *)&(number))<0)
337 # define uprv_isNegative(number) (*((signed char *)&(number)+sizeof(number)-1)<0)
341 * Return the largest positive number that can be represented by an integer
342 * type of arbitrary bit length.
345 U_INTERNAL
double U_EXPORT2
uprv_maxMantissa(void);
348 * Floating point utility to calculate the logarithm of a double.
351 U_INTERNAL
double U_EXPORT2
uprv_log(double d
);
354 * Does common notion of rounding e.g. uprv_floor(x + 0.5);
355 * @param x the double number
356 * @return the rounded double
359 U_INTERNAL
double U_EXPORT2
uprv_round(double x
);
363 * Returns the number of digits after the decimal point in a double number x.
365 * @param x the double number
366 * @return the number of digits after the decimal point in a double number x.
369 /*U_INTERNAL int32_t U_EXPORT2 uprv_digitsAfterDecimal(double x);*/
372 #if !U_CHARSET_IS_UTF8
374 * Please use ucnv_getDefaultName() instead.
375 * Return the default codepage for this platform and locale.
376 * This function can call setlocale() on Unix platforms. Please read the
377 * platform documentation on setlocale() before calling this function.
378 * @return the default codepage for this platform
381 U_INTERNAL
const char* U_EXPORT2
uprv_getDefaultCodepage(void);
385 * Please use uloc_getDefault() instead.
386 * Return the default locale ID string by querying ths system, or
387 * zero if one cannot be found.
388 * This function can call setlocale() on Unix platforms. Please read the
389 * platform documentation on setlocale() before calling this function.
390 * @return the default locale ID string
393 U_INTERNAL
const char* U_EXPORT2
uprv_getDefaultLocaleID(void);
396 * Time zone utilities
398 * Wrappers for C runtime library functions relating to timezones.
399 * The t_tzset() function (similar to tzset) uses the current setting
400 * of the environment variable TZ to assign values to three global
401 * variables: daylight, timezone, and tzname. These variables have the
402 * following meanings, and are declared in <time.h>.
404 * daylight Nonzero if daylight-saving-time zone (DST) is specified
405 * in TZ; otherwise, 0. Default value is 1.
406 * timezone Difference in seconds between coordinated universal
407 * time and local time. E.g., -28,800 for PST (GMT-8hrs)
408 * tzname(0) Three-letter time-zone name derived from TZ environment
409 * variable. E.g., "PST".
410 * tzname(1) Three-letter DST zone name derived from TZ environment
411 * variable. E.g., "PDT". If DST zone is omitted from TZ,
412 * tzname(1) is an empty string.
414 * Notes: For example, to set the TZ environment variable to correspond
415 * to the current time zone in Germany, you can use one of the
416 * following statements:
421 * If the TZ value is not set, t_tzset() attempts to use the time zone
422 * information specified by the operating system. Under Windows NT
423 * and Windows 95, this information is specified in the Control Panel's
424 * Date/Time application.
427 U_INTERNAL
void U_EXPORT2
uprv_tzset(void);
430 * Difference in seconds between coordinated universal
431 * time and local time. E.g., -28,800 for PST (GMT-8hrs)
432 * @return the difference in seconds between coordinated universal time and local time.
435 U_INTERNAL
int32_t U_EXPORT2
uprv_timezone(void);
438 * tzname(0) Three-letter time-zone name derived from TZ environment
439 * variable. E.g., "PST".
440 * tzname(1) Three-letter DST zone name derived from TZ environment
441 * variable. E.g., "PDT". If DST zone is omitted from TZ,
442 * tzname(1) is an empty string.
445 U_INTERNAL
const char* U_EXPORT2
uprv_tzname(int n
);
448 * Get UTC (GMT) time measured in milliseconds since 0:00 on 1/1/1970.
449 * This function is affected by 'faketime' and should be the bottleneck for all user-visible ICU time functions.
450 * @return the UTC time measured in milliseconds
453 U_INTERNAL UDate U_EXPORT2
uprv_getUTCtime(void);
456 * Get UTC (GMT) time measured in milliseconds since 0:00 on 1/1/1970.
457 * This function is not affected by 'faketime', so it should only be used by low level test functions- not by anything that
458 * exposes time to the end user.
459 * @return the UTC time measured in milliseconds
462 U_INTERNAL UDate U_EXPORT2
uprv_getRawUTCtime(void);
465 * Determine whether a pathname is absolute or not, as defined by the platform.
466 * @param path Pathname to test
467 * @return TRUE if the path is absolute
468 * @internal (ICU 3.0)
470 U_INTERNAL UBool U_EXPORT2
uprv_pathIsAbsolute(const char *path
);
473 * Use U_MAX_PTR instead of this function.
474 * @param void pointer to test
475 * @return the largest possible pointer greater than the base
476 * @internal (ICU 3.8)
478 U_INTERNAL
void * U_EXPORT2
uprv_maximumPtr(void *base
);
481 * Maximum value of a (void*) - use to indicate the limit of an 'infinite' buffer.
482 * In fact, buffer sizes must not exceed 2GB so that the difference between
483 * the buffer limit and the buffer start can be expressed in an int32_t.
485 * The definition of U_MAX_PTR must fulfill the following conditions:
486 * - return the largest possible pointer greater than base
487 * - return a valid pointer according to the machine architecture (AS/400, 64-bit, etc.)
488 * - avoid wrapping around at high addresses
489 * - make sure that the returned pointer is not farther from base than 0x7fffffff bytes
491 * @param base The beginning of a buffer to find the maximum offset from
495 # if U_PLATFORM == U_PF_OS390 && !defined(_LP64)
496 /* We have 31-bit pointers. */
497 # define U_MAX_PTR(base) ((void *)0x7fffffff)
498 # elif U_PLATFORM == U_PF_OS400
499 # define U_MAX_PTR(base) uprv_maximumPtr((void *)base)
502 * For platforms where pointers are scalar values (which is normal, but unlike i5/OS)
503 * but that do not define uintptr_t.
505 * However, this does not work on modern compilers:
506 * The C++ standard does not define pointer overflow, and allows compilers to
507 * assume that p+u>p for any pointer p and any integer u>0.
508 * Thus, modern compilers optimize away the ">" comparison.
509 * (See ICU tickets #7187 and #8096.)
511 # define U_MAX_PTR(base) \
512 ((void *)(((char *)(base)+0x7fffffffu) > (char *)(base) \
513 ? ((char *)(base)+0x7fffffffu) \
516 /* Default version. C++ standard compliant for scalar pointers. */
517 # define U_MAX_PTR(base) \
518 ((void *)(((uintptr_t)(base)+0x7fffffffu) > (uintptr_t)(base) \
519 ? ((uintptr_t)(base)+0x7fffffffu) \
524 /* Dynamic Library Functions */
526 typedef void (UVoidFunction
)(void);
531 * @internal (ICU 4.4)
533 U_INTERNAL
void * U_EXPORT2
uprv_dl_open(const char *libName
, UErrorCode
*status
);
537 * @internal (ICU 4.4)
539 U_INTERNAL
void U_EXPORT2
uprv_dl_close( void *lib
, UErrorCode
*status
);
542 * Extract a symbol from a library (function)
543 * @internal (ICU 4.8)
545 U_INTERNAL UVoidFunction
* U_EXPORT2
uprv_dlsym_func( void *lib
, const char *symbolName
, UErrorCode
*status
);
548 * Extract a symbol from a library (function)
549 * Not implemented, no clients.
552 /* U_INTERNAL void * U_EXPORT2 uprv_dlsym_data( void *lib, const char *symbolName, UErrorCode *status); */
557 * Define malloc and related functions
560 #if U_PLATFORM == U_PF_OS400
561 # define uprv_default_malloc(x) _C_TS_malloc(x)
562 # define uprv_default_realloc(x,y) _C_TS_realloc(x,y)
563 # define uprv_default_free(x) _C_TS_free(x)
564 /* also _C_TS_calloc(x) */
567 # define uprv_default_malloc(x) malloc(x)
568 # define uprv_default_realloc(x,y) realloc(x,y)
569 # define uprv_default_free(x) free(x)