Imported Upstream version 58.1
[platform/upstream/icu.git] / source / common / putilimp.h
1 // Copyright (C) 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
3 /*
4 ******************************************************************************
5 *
6 *   Copyright (C) 1997-2016, International Business Machines
7 *   Corporation and others.  All Rights Reserved.
8 *
9 ******************************************************************************
10 *
11 *  FILE NAME : putilimp.h
12 *
13 *   Date        Name        Description
14 *   10/17/04    grhoten     Move internal functions from putil.h to this file.
15 ******************************************************************************
16 */
17
18 #ifndef PUTILIMP_H
19 #define PUTILIMP_H
20
21 #include "unicode/utypes.h"
22 #include "unicode/putil.h"
23
24 /**
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.
30  *
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.
34  *
35  * This is consistent with the Java language.
36  *
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.
40  *
41  * Code that depends on the natural behavior should be guarded with this macro,
42  * with an alternate path for unusual platforms.
43  * @internal
44  */
45 #ifdef U_SIGNED_RIGHT_SHIFT_IS_ARITHMETIC
46     /* Use the predefined value. */
47 #else
48     /*
49      * Nearly all CPUs & compilers implement a right-shift of a signed integer
50      * as an Arithmetic Shift Right (with sign extension).
51      */
52 #   define U_SIGNED_RIGHT_SHIFT_IS_ARITHMETIC 1
53 #endif
54
55 /** Define this to 1 if your platform supports IEEE 754 floating point,
56    to 0 if it does not. */
57 #ifndef IEEE_754
58 #   define IEEE_754 1
59 #endif
60
61 /**
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.
67  *
68  * Do not use ptrdiff_t since it is signed. size_t is unsigned.
69  */
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;
73 #endif
74
75 /**
76  * \def U_HAVE_MSVC_2003_OR_EARLIER
77  * Flag for workaround of MSVC 2003 optimization bugs
78  * @internal
79  */
80 #if !defined(U_HAVE_MSVC_2003_OR_EARLIER) && defined(_MSC_VER) && (_MSC_VER < 1400)
81 #define U_HAVE_MSVC_2003_OR_EARLIER
82 #endif
83
84 /*===========================================================================*/
85 /** @{ Information about POSIX support                                       */
86 /*===========================================================================*/
87
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
92 #else
93 #   define U_HAVE_NL_LANGINFO_CODESET 1
94 #endif
95
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
101    /* not defined */
102 #else
103 #   define U_NL_LANGINFO_CODESET CODESET
104 #endif
105
106 #ifdef U_TZSET
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
111    /* not defined */
112 #else
113 #   define U_TZSET tzset
114 #endif
115
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__)
126        /* glibc */
127 #      define U_TIMEZONE      __timezone
128 #   endif
129 #elif U_PLATFORM_USES_ONLY_WIN32_API
130 #   define U_TIMEZONE _timezone
131 #elif U_PLATFORM == U_PF_BSD && !defined(__NetBSD__)
132    /* not defined */
133 #elif U_PLATFORM == U_PF_OS400
134    /* not defined */
135 #elif U_PLATFORM == U_PF_IPHONE
136    /* not defined */
137 #else
138 #   define U_TIMEZONE timezone
139 #endif
140
141 #ifdef U_TZNAME
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
146    /* not defined */
147 #else
148 #   define U_TZNAME tzname
149 #endif
150
151 #ifdef U_HAVE_MMAP
152     /* Use the predefined value. */
153 #elif U_PLATFORM_USES_ONLY_WIN32_API
154 #   define U_HAVE_MMAP 0
155 #else
156 #   define U_HAVE_MMAP 1
157 #endif
158
159 #ifdef U_HAVE_POPEN
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
165 #else
166 #   define U_HAVE_POPEN 1
167 #endif
168
169 /**
170  * \def U_HAVE_DIRENT_H
171  * Defines whether dirent.h is available.
172  * @internal
173  */
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
178 #else
179 #   define U_HAVE_DIRENT_H 1
180 #endif
181
182 /** @} */
183
184 /*===========================================================================*/
185 /** @{ GCC built in functions for atomic memory operations                   */
186 /*===========================================================================*/
187
188 /**
189  * \def U_HAVE_GCC_ATOMICS
190  * @internal
191  */
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.
200      */
201 #   define U_HAVE_GCC_ATOMICS 1
202 #else
203 #   define U_HAVE_GCC_ATOMICS 0
204 #endif
205
206 /** @} */
207
208 /**
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.
213  * @internal
214  */
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
223 #else 
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
228 #   else
229 #      define U_HAVE_STD_ATOMICS 0
230 #   endif
231 #endif
232
233
234 /**
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.
238  */
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
246 #else
247 #    define U_HAVE_CLANG_ATOMICS 0
248 #endif
249
250 /*===========================================================================*/
251 /** @{ Programs used by ICU code                                             */
252 /*===========================================================================*/
253
254 /**
255  * \def U_MAKE_IS_NMAKE
256  * Defines whether the "make" program is Windows nmake.
257  */
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
262 #else
263 #   define U_MAKE_IS_NMAKE 0
264 #endif
265
266 /** @} */
267
268 /*==========================================================================*/
269 /* Platform utilities                                                       */
270 /*==========================================================================*/
271
272 /**
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.
276  */
277
278 /**
279  * Floating point utility to determine if a double is Not a Number (NaN).
280  * @internal
281  */
282 U_INTERNAL UBool   U_EXPORT2 uprv_isNaN(double d);
283 /**
284  * Floating point utility to determine if a double has an infinite value.
285  * @internal
286  */
287 U_INTERNAL UBool   U_EXPORT2 uprv_isInfinite(double d);
288 /**
289  * Floating point utility to determine if a double has a positive infinite value.
290  * @internal
291  */
292 U_INTERNAL UBool   U_EXPORT2 uprv_isPositiveInfinity(double d);
293 /**
294  * Floating point utility to determine if a double has a negative infinite value.
295  * @internal
296  */
297 U_INTERNAL UBool   U_EXPORT2 uprv_isNegativeInfinity(double d);
298 /**
299  * Floating point utility that returns a Not a Number (NaN) value.
300  * @internal
301  */
302 U_INTERNAL double  U_EXPORT2 uprv_getNaN(void);
303 /**
304  * Floating point utility that returns an infinite value.
305  * @internal
306  */
307 U_INTERNAL double  U_EXPORT2 uprv_getInfinity(void);
308
309 /**
310  * Floating point utility to truncate a double.
311  * @internal
312  */
313 U_INTERNAL double  U_EXPORT2 uprv_trunc(double d);
314 /**
315  * Floating point utility to calculate the floor of a double.
316  * @internal
317  */
318 U_INTERNAL double  U_EXPORT2 uprv_floor(double d);
319 /**
320  * Floating point utility to calculate the ceiling of a double.
321  * @internal
322  */
323 U_INTERNAL double  U_EXPORT2 uprv_ceil(double d);
324 /**
325  * Floating point utility to calculate the absolute value of a double.
326  * @internal
327  */
328 U_INTERNAL double  U_EXPORT2 uprv_fabs(double d);
329 /**
330  * Floating point utility to calculate the fractional and integer parts of a double.
331  * @internal
332  */
333 U_INTERNAL double  U_EXPORT2 uprv_modf(double d, double* pinteger);
334 /**
335  * Floating point utility to calculate the remainder of a double divided by another double.
336  * @internal
337  */
338 U_INTERNAL double  U_EXPORT2 uprv_fmod(double d, double y);
339 /**
340  * Floating point utility to calculate d to the power of exponent (d^exponent).
341  * @internal
342  */
343 U_INTERNAL double  U_EXPORT2 uprv_pow(double d, double exponent);
344 /**
345  * Floating point utility to calculate 10 to the power of exponent (10^exponent).
346  * @internal
347  */
348 U_INTERNAL double  U_EXPORT2 uprv_pow10(int32_t exponent);
349 /**
350  * Floating point utility to calculate the maximum value of two doubles.
351  * @internal
352  */
353 U_INTERNAL double  U_EXPORT2 uprv_fmax(double d, double y);
354 /**
355  * Floating point utility to calculate the minimum value of two doubles.
356  * @internal
357  */
358 U_INTERNAL double  U_EXPORT2 uprv_fmin(double d, double y);
359 /**
360  * Private utility to calculate the maximum value of two integers.
361  * @internal
362  */
363 U_INTERNAL int32_t U_EXPORT2 uprv_max(int32_t d, int32_t y);
364 /**
365  * Private utility to calculate the minimum value of two integers.
366  * @internal
367  */
368 U_INTERNAL int32_t U_EXPORT2 uprv_min(int32_t d, int32_t y);
369
370 #if U_IS_BIG_ENDIAN
371 #   define uprv_isNegative(number) (*((signed char *)&(number))<0)
372 #else
373 #   define uprv_isNegative(number) (*((signed char *)&(number)+sizeof(number)-1)<0)
374 #endif
375
376 /**
377  * Return the largest positive number that can be represented by an integer
378  * type of arbitrary bit length.
379  * @internal
380  */
381 U_INTERNAL double  U_EXPORT2 uprv_maxMantissa(void);
382
383 /**
384  * Floating point utility to calculate the logarithm of a double.
385  * @internal
386  */
387 U_INTERNAL double  U_EXPORT2 uprv_log(double d);
388
389 /**
390  * Does common notion of rounding e.g. uprv_floor(x + 0.5);
391  * @param x the double number
392  * @return the rounded double
393  * @internal
394  */
395 U_INTERNAL double  U_EXPORT2 uprv_round(double x);
396
397 #if 0
398 /**
399  * Returns the number of digits after the decimal point in a double number x.
400  *
401  * @param x the double number
402  * @return the number of digits after the decimal point in a double number x.
403  * @internal
404  */
405 /*U_INTERNAL int32_t  U_EXPORT2 uprv_digitsAfterDecimal(double x);*/
406 #endif
407
408 #if !U_CHARSET_IS_UTF8
409 /**
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 
415  * @internal
416  */
417 U_INTERNAL const char*  U_EXPORT2 uprv_getDefaultCodepage(void);
418 #endif
419
420 /**
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
427  * @internal
428  */
429 U_INTERNAL const char*  U_EXPORT2 uprv_getDefaultLocaleID(void);
430
431 /**
432  * Time zone utilities
433  *
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 &lt;time.h&gt;.
439  *
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.
449  *
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:
453  *
454  *   set TZ=GST1GDT
455  *   set TZ=GST+1GDT
456  *
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.
461  * @internal
462  */
463 U_INTERNAL void     U_EXPORT2 uprv_tzset(void);
464
465 /**
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.
469  * @internal
470  */
471 U_INTERNAL int32_t  U_EXPORT2 uprv_timezone(void);
472
473 /**
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.
479  * @internal
480  */
481 U_INTERNAL const char* U_EXPORT2 uprv_tzname(int n);
482
483 /**
484  * Reset the global tzname cache.
485  * @internal
486  */
487 U_INTERNAL void uprv_tzname_clear_cache();
488
489 /**
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
493  * @internal
494  */
495 U_INTERNAL UDate U_EXPORT2 uprv_getUTCtime(void);
496
497 /**
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
502  * @internal
503  */
504 U_INTERNAL UDate U_EXPORT2 uprv_getRawUTCtime(void);
505
506 /**
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)
511  */
512 U_INTERNAL UBool U_EXPORT2 uprv_pathIsAbsolute(const char *path);
513
514 /**
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)
519  */
520 U_INTERNAL void * U_EXPORT2 uprv_maximumPtr(void *base);
521
522 /**
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.
526  *
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
532  *
533  * @param base The beginning of a buffer to find the maximum offset from
534  * @internal
535  */
536 #ifndef U_MAX_PTR
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)
542 #  elif 0
543     /*
544      * For platforms where pointers are scalar values (which is normal, but unlike i5/OS)
545      * but that do not define uintptr_t.
546      *
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.)
552      */
553 #    define U_MAX_PTR(base) \
554     ((void *)(((char *)(base)+0x7fffffffu) > (char *)(base) \
555         ? ((char *)(base)+0x7fffffffu) \
556         : (char *)-1))
557 #  else
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) \
562         : (uintptr_t)-1))
563 #  endif
564 #endif
565
566 /*  Dynamic Library Functions */
567
568 typedef void (UVoidFunction)(void);
569
570 #if U_ENABLE_DYLOAD
571 /**
572  * Load a library
573  * @internal (ICU 4.4)
574  */
575 U_INTERNAL void * U_EXPORT2 uprv_dl_open(const char *libName, UErrorCode *status);
576
577 /**
578  * Close a library
579  * @internal (ICU 4.4)
580  */
581 U_INTERNAL void U_EXPORT2 uprv_dl_close( void *lib, UErrorCode *status);
582
583 /**
584  * Extract a symbol from a library (function)
585  * @internal (ICU 4.8)
586  */
587 U_INTERNAL UVoidFunction* U_EXPORT2 uprv_dlsym_func( void *lib, const char *symbolName, UErrorCode *status);
588
589 /**
590  * Extract a symbol from a library (function)
591  * Not implemented, no clients.
592  * @internal
593  */
594 /* U_INTERNAL void * U_EXPORT2 uprv_dlsym_data( void *lib, const char *symbolName, UErrorCode *status); */
595
596 #endif
597
598 /**
599  * Define malloc and related functions
600  * @internal
601  */
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) */
607 #else
608 /* C defaults */
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)
612 #endif
613
614
615 #endif