1 /*
2 ******************************************************************************
3 *
4 *   Copyright (C) 1997-2015, International Business Machines
5 *   Corporation and others.  All Rights Reserved.
6 *
7 ******************************************************************************
8 *
9 *  FILE NAME : putilimp.h
10 *
11 *   Date        Name        Description
12 *   10/17/04    grhoten     Move internal functions from putil.h to this file.
13 ******************************************************************************
14 */
15 
16 #ifndef PUTILIMP_H
17 #define PUTILIMP_H
18 
19 #include "unicode/utypes.h"
20 #include "unicode/putil.h"
21 
22 /**
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.
28  *
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.
32  *
33  * This is consistent with the Java language.
34  *
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.
38  *
39  * Code that depends on the natural behavior should be guarded with this macro,
40  * with an alternate path for unusual platforms.
41  * @internal
42  */
43 #ifdef U_SIGNED_RIGHT_SHIFT_IS_ARITHMETIC
44     /* Use the predefined value. */
45 #else
46     /*
47      * Nearly all CPUs & compilers implement a right-shift of a signed integer
48      * as an Arithmetic Shift Right (with sign extension).
49      */
50 #   define U_SIGNED_RIGHT_SHIFT_IS_ARITHMETIC 1
51 #endif
52 
53 /** Define this to 1 if your platform supports IEEE 754 floating point,
54    to 0 if it does not. */
55 #ifndef IEEE_754
56 #   define IEEE_754 1
57 #endif
58 
59 /**
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.
65  *
66  * Do not use ptrdiff_t since it is signed. size_t is unsigned.
67  */
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;
71 #endif
72 
73 /**
74  * \def U_HAVE_MSVC_2003_OR_EARLIER
75  * Flag for workaround of MSVC 2003 optimization bugs
76  * @internal
77  */
78 #if !defined(U_HAVE_MSVC_2003_OR_EARLIER) && defined(_MSC_VER) && (_MSC_VER < 1400)
79 #define U_HAVE_MSVC_2003_OR_EARLIER
80 #endif
81 
82 /*===========================================================================*/
83 /** @{ Information about POSIX support                                       */
84 /*===========================================================================*/
85 
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
90 #else
91 #   define U_HAVE_NL_LANGINFO_CODESET 1
92 #endif
93 
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
99    /* not defined */
100 #else
101 #   define U_NL_LANGINFO_CODESET CODESET
102 #endif
103 
104 #ifdef U_TZSET
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
109    /* not defined */
110 #else
111 #   define U_TZSET tzset
112 #endif
113 
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__)
124        /* glibc */
125 #      define U_TIMEZONE      __timezone
126 #   endif
127 #elif U_PLATFORM_USES_ONLY_WIN32_API
128 #   define U_TIMEZONE _timezone
129 #elif U_PLATFORM == U_PF_BSD && !defined(__NetBSD__)
130    /* not defined */
131 #elif U_PLATFORM == U_PF_OS400
132    /* not defined */
133 #elif U_PLATFORM == U_PF_IPHONE
134    /* not defined */
135 #else
136 #   define U_TIMEZONE timezone
137 #endif
138 
139 #ifdef U_TZNAME
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
144    /* not defined */
145 #else
146 #   define U_TZNAME tzname
147 #endif
148 
149 #ifdef U_HAVE_MMAP
150     /* Use the predefined value. */
151 #elif U_PLATFORM_HAS_WIN32_API
152 #   define U_HAVE_MMAP 0
153 #else
154 #   define U_HAVE_MMAP 1
155 #endif
156 
157 #ifdef U_HAVE_POPEN
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
163 #else
164 #   define U_HAVE_POPEN 1
165 #endif
166 
167 /**
168  * \def U_HAVE_DIRENT_H
169  * Defines whether dirent.h is available.
170  * @internal
171  */
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
176 #else
177 #   define U_HAVE_DIRENT_H 1
178 #endif
179 
180 /** @} */
181 
182 /*===========================================================================*/
183 /** @{ GCC built in functions for atomic memory operations                   */
184 /*===========================================================================*/
185 
186 /**
187  * \def U_HAVE_GCC_ATOMICS
188  * @internal
189  */
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.
198      */
199 #   define U_HAVE_GCC_ATOMICS 1
200 #else
201 #   define U_HAVE_GCC_ATOMICS 0
202 #endif
203 
204 /** @} */
205 
206 /**
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.
211  * @internal
212  */
213 #ifdef U_HAVE_STD_ATOMICS
214     /* Use the predefined value. */
215 #elif U_CPLUSPLUS_VERSION < 11
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
221 #else
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
226 #   else
227 #      define U_HAVE_STD_ATOMICS 0
228 #   endif
229 #endif
230 
231 
232 /**
233  *  \def U_HAVE_CLANG_ATOMICS
234  *  Defines whether Clang c11 style built-in atomics are avaialable.
235  *  These are used in preference to gcc atomics when both are available.
236  */
237 #ifdef U_HAVE_CLANG_ATOMICS
238     /* Use the predefined value. */
239 #elif __has_builtin(__c11_atomic_load) && \
240     __has_builtin(__c11_atomic_store) && \
241     __has_builtin(__c11_atomic_fetch_add) && \
242     __has_builtin(__c11_atomic_fetch_sub)
243 #    define U_HAVE_CLANG_ATOMICS 1
244 #else
245 #    define U_HAVE_CLANG_ATOMICS 0
246 #endif
247 
248 /*===========================================================================*/
249 /** @{ Programs used by ICU code                                             */
250 /*===========================================================================*/
251 
252 /**
253  * \def U_MAKE_IS_NMAKE
254  * Defines whether the "make" program is Windows nmake.
255  */
256 #ifdef U_MAKE_IS_NMAKE
257     /* Use the predefined value. */
258 #elif U_PLATFORM == U_PF_WINDOWS
259 #   define U_MAKE_IS_NMAKE 1
260 #else
261 #   define U_MAKE_IS_NMAKE 0
262 #endif
263 
264 /** @} */
265 
266 /*==========================================================================*/
267 /* Platform utilities                                                       */
268 /*==========================================================================*/
269 
270 /**
271  * Platform utilities isolates the platform dependencies of the
272  * libarary.  For each platform which this code is ported to, these
273  * functions may have to be re-implemented.
274  */
275 
276 /**
277  * Floating point utility to determine if a double is Not a Number (NaN).
278  * @internal
279  */
280 U_INTERNAL UBool   U_EXPORT2 uprv_isNaN(double d);
281 /**
282  * Floating point utility to determine if a double has an infinite value.
283  * @internal
284  */
285 U_INTERNAL UBool   U_EXPORT2 uprv_isInfinite(double d);
286 /**
287  * Floating point utility to determine if a double has a positive infinite value.
288  * @internal
289  */
290 U_INTERNAL UBool   U_EXPORT2 uprv_isPositiveInfinity(double d);
291 /**
292  * Floating point utility to determine if a double has a negative infinite value.
293  * @internal
294  */
295 U_INTERNAL UBool   U_EXPORT2 uprv_isNegativeInfinity(double d);
296 /**
297  * Floating point utility that returns a Not a Number (NaN) value.
298  * @internal
299  */
300 U_INTERNAL double  U_EXPORT2 uprv_getNaN(void);
301 /**
302  * Floating point utility that returns an infinite value.
303  * @internal
304  */
305 U_INTERNAL double  U_EXPORT2 uprv_getInfinity(void);
306 
307 /**
308  * Floating point utility to truncate a double.
309  * @internal
310  */
311 U_INTERNAL double  U_EXPORT2 uprv_trunc(double d);
312 /**
313  * Floating point utility to calculate the floor of a double.
314  * @internal
315  */
316 U_INTERNAL double  U_EXPORT2 uprv_floor(double d);
317 /**
318  * Floating point utility to calculate the ceiling of a double.
319  * @internal
320  */
321 U_INTERNAL double  U_EXPORT2 uprv_ceil(double d);
322 /**
323  * Floating point utility to calculate the absolute value of a double.
324  * @internal
325  */
326 U_INTERNAL double  U_EXPORT2 uprv_fabs(double d);
327 /**
328  * Floating point utility to calculate the fractional and integer parts of a double.
329  * @internal
330  */
331 U_INTERNAL double  U_EXPORT2 uprv_modf(double d, double* pinteger);
332 /**
333  * Floating point utility to calculate the remainder of a double divided by another double.
334  * @internal
335  */
336 U_INTERNAL double  U_EXPORT2 uprv_fmod(double d, double y);
337 /**
338  * Floating point utility to calculate d to the power of exponent (d^exponent).
339  * @internal
340  */
341 U_INTERNAL double  U_EXPORT2 uprv_pow(double d, double exponent);
342 /**
343  * Floating point utility to calculate 10 to the power of exponent (10^exponent).
344  * @internal
345  */
346 U_INTERNAL double  U_EXPORT2 uprv_pow10(int32_t exponent);
347 /**
348  * Floating point utility to calculate the maximum value of two doubles.
349  * @internal
350  */
351 U_INTERNAL double  U_EXPORT2 uprv_fmax(double d, double y);
352 /**
353  * Floating point utility to calculate the minimum value of two doubles.
354  * @internal
355  */
356 U_INTERNAL double  U_EXPORT2 uprv_fmin(double d, double y);
357 /**
358  * Private utility to calculate the maximum value of two integers.
359  * @internal
360  */
361 U_INTERNAL int32_t U_EXPORT2 uprv_max(int32_t d, int32_t y);
362 /**
363  * Private utility to calculate the minimum value of two integers.
364  * @internal
365  */
366 U_INTERNAL int32_t U_EXPORT2 uprv_min(int32_t d, int32_t y);
367 
368 #if U_IS_BIG_ENDIAN
369 #   define uprv_isNegative(number) (*((signed char *)&(number))<0)
370 #else
371 #   define uprv_isNegative(number) (*((signed char *)&(number)+sizeof(number)-1)<0)
372 #endif
373 
374 /**
375  * Return the largest positive number that can be represented by an integer
376  * type of arbitrary bit length.
377  * @internal
378  */
379 U_INTERNAL double  U_EXPORT2 uprv_maxMantissa(void);
380 
381 /**
382  * Floating point utility to calculate the logarithm of a double.
383  * @internal
384  */
385 U_INTERNAL double  U_EXPORT2 uprv_log(double d);
386 
387 /**
388  * Does common notion of rounding e.g. uprv_floor(x + 0.5);
389  * @param x the double number
390  * @return the rounded double
391  * @internal
392  */
393 U_INTERNAL double  U_EXPORT2 uprv_round(double x);
394 
395 #if 0
396 /**
397  * Returns the number of digits after the decimal point in a double number x.
398  *
399  * @param x the double number
400  * @return the number of digits after the decimal point in a double number x.
401  * @internal
402  */
403 /*U_INTERNAL int32_t  U_EXPORT2 uprv_digitsAfterDecimal(double x);*/
404 #endif
405 
406 #if !U_CHARSET_IS_UTF8
407 /**
408  * Please use ucnv_getDefaultName() instead.
409  * Return the default codepage for this platform and locale.
410  * This function can call setlocale() on Unix platforms. Please read the
411  * platform documentation on setlocale() before calling this function.
412  * @return the default codepage for this platform
413  * @internal
414  */
415 U_INTERNAL const char*  U_EXPORT2 uprv_getDefaultCodepage(void);
416 #endif
417 
418 /**
419  * Please use uloc_getDefault() instead.
420  * Return the default locale ID string by querying ths system, or
421  *     zero if one cannot be found.
422  * This function can call setlocale() on Unix platforms. Please read the
423  * platform documentation on setlocale() before calling this function.
424  * @return the default locale ID string
425  * @internal
426  */
427 U_INTERNAL const char*  U_EXPORT2 uprv_getDefaultLocaleID(void);
428 
429 /**
430  * Time zone utilities
431  *
432  * Wrappers for C runtime library functions relating to timezones.
433  * The t_tzset() function (similar to tzset) uses the current setting
434  * of the environment variable TZ to assign values to three global
435  * variables: daylight, timezone, and tzname. These variables have the
436  * following meanings, and are declared in &lt;time.h&gt;.
437  *
438  *   daylight   Nonzero if daylight-saving-time zone (DST) is specified
439  *              in TZ; otherwise, 0. Default value is 1.
440  *   timezone   Difference in seconds between coordinated universal
441  *              time and local time. E.g., -28,800 for PST (GMT-8hrs)
442  *   tzname(0)  Three-letter time-zone name derived from TZ environment
443  *              variable. E.g., "PST".
444  *   tzname(1)  Three-letter DST zone name derived from TZ environment
445  *              variable.  E.g., "PDT". If DST zone is omitted from TZ,
446  *              tzname(1) is an empty string.
447  *
448  * Notes: For example, to set the TZ environment variable to correspond
449  * to the current time zone in Germany, you can use one of the
450  * following statements:
451  *
452  *   set TZ=GST1GDT
453  *   set TZ=GST+1GDT
454  *
455  * If the TZ value is not set, t_tzset() attempts to use the time zone
456  * information specified by the operating system. Under Windows NT
457  * and Windows 95, this information is specified in the Control Panel's
458  * Date/Time application.
459  * @internal
460  */
461 U_INTERNAL void     U_EXPORT2 uprv_tzset(void);
462 
463 /**
464  * Difference in seconds between coordinated universal
465  * time and local time. E.g., -28,800 for PST (GMT-8hrs)
466  * @return the difference in seconds between coordinated universal time and local time.
467  * @internal
468  */
469 U_INTERNAL int32_t  U_EXPORT2 uprv_timezone(void);
470 
471 /**
472  *   tzname(0)  Three-letter time-zone name derived from TZ environment
473  *              variable. E.g., "PST".
474  *   tzname(1)  Three-letter DST zone name derived from TZ environment
475  *              variable.  E.g., "PDT". If DST zone is omitted from TZ,
476  *              tzname(1) is an empty string.
477  * @internal
478  */
479 U_INTERNAL const char* U_EXPORT2 uprv_tzname(int n);
480 
481 /**
482  * Get UTC (GMT) time measured in milliseconds since 0:00 on 1/1/1970.
483  * This function is affected by 'faketime' and should be the bottleneck for all user-visible ICU time functions.
484  * @return the UTC time measured in milliseconds
485  * @internal
486  */
487 U_INTERNAL UDate U_EXPORT2 uprv_getUTCtime(void);
488 
489 /**
490  * Get UTC (GMT) time measured in milliseconds since 0:00 on 1/1/1970.
491  * This function is not affected by 'faketime', so it should only be used by low level test functions- not by anything that
492  * exposes time to the end user.
493  * @return the UTC time measured in milliseconds
494  * @internal
495  */
496 U_INTERNAL UDate U_EXPORT2 uprv_getRawUTCtime(void);
497 
498 /**
499  * Determine whether a pathname is absolute or not, as defined by the platform.
500  * @param path Pathname to test
501  * @return TRUE if the path is absolute
502  * @internal (ICU 3.0)
503  */
504 U_INTERNAL UBool U_EXPORT2 uprv_pathIsAbsolute(const char *path);
505 
506 /**
507  * Use U_MAX_PTR instead of this function.
508  * @param void pointer to test
509  * @return the largest possible pointer greater than the base
510  * @internal (ICU 3.8)
511  */
512 U_INTERNAL void * U_EXPORT2 uprv_maximumPtr(void *base);
513 
514 /**
515  * Maximum value of a (void*) - use to indicate the limit of an 'infinite' buffer.
516  * In fact, buffer sizes must not exceed 2GB so that the difference between
517  * the buffer limit and the buffer start can be expressed in an int32_t.
518  *
519  * The definition of U_MAX_PTR must fulfill the following conditions:
520  * - return the largest possible pointer greater than base
521  * - return a valid pointer according to the machine architecture (AS/400, 64-bit, etc.)
522  * - avoid wrapping around at high addresses
523  * - make sure that the returned pointer is not farther from base than 0x7fffffff bytes
524  *
525  * @param base The beginning of a buffer to find the maximum offset from
526  * @internal
527  */
528 #ifndef U_MAX_PTR
529 #  if U_PLATFORM == U_PF_OS390 && !defined(_LP64)
530     /* We have 31-bit pointers. */
531 #    define U_MAX_PTR(base) ((void *)0x7fffffff)
532 #  elif U_PLATFORM == U_PF_OS400
533 #    define U_MAX_PTR(base) uprv_maximumPtr((void *)base)
534 #  elif 0
535     /*
536      * For platforms where pointers are scalar values (which is normal, but unlike i5/OS)
537      * but that do not define uintptr_t.
538      *
539      * However, this does not work on modern compilers:
540      * The C++ standard does not define pointer overflow, and allows compilers to
541      * assume that p+u>p for any pointer p and any integer u>0.
542      * Thus, modern compilers optimize away the ">" comparison.
543      * (See ICU tickets #7187 and #8096.)
544      */
545 #    define U_MAX_PTR(base) \
546     ((void *)(((char *)(base)+0x7fffffffu) > (char *)(base) \
547         ? ((char *)(base)+0x7fffffffu) \
548         : (char *)-1))
549 #  else
550     /* Default version. C++ standard compliant for scalar pointers. */
551 #    define U_MAX_PTR(base) \
552     ((void *)(((uintptr_t)(base)+0x7fffffffu) > (uintptr_t)(base) \
553         ? ((uintptr_t)(base)+0x7fffffffu) \
554         : (uintptr_t)-1))
555 #  endif
556 #endif
557 
558 /*  Dynamic Library Functions */
559 
560 typedef void (UVoidFunction)(void);
561 
562 #if U_ENABLE_DYLOAD
563 /**
564  * Load a library
565  * @internal (ICU 4.4)
566  */
567 U_INTERNAL void * U_EXPORT2 uprv_dl_open(const char *libName, UErrorCode *status);
568 
569 /**
570  * Close a library
571  * @internal (ICU 4.4)
572  */
573 U_INTERNAL void U_EXPORT2 uprv_dl_close( void *lib, UErrorCode *status);
574 
575 /**
576  * Extract a symbol from a library (function)
577  * @internal (ICU 4.8)
578  */
579 U_INTERNAL UVoidFunction* U_EXPORT2 uprv_dlsym_func( void *lib, const char *symbolName, UErrorCode *status);
580 
581 /**
582  * Extract a symbol from a library (function)
583  * Not implemented, no clients.
584  * @internal
585  */
586 /* U_INTERNAL void * U_EXPORT2 uprv_dlsym_data( void *lib, const char *symbolName, UErrorCode *status); */
587 
588 #endif
589 
590 /**
591  * Define malloc and related functions
592  * @internal
593  */
594 #if U_PLATFORM == U_PF_OS400
595 # define uprv_default_malloc(x) _C_TS_malloc(x)
596 # define uprv_default_realloc(x,y) _C_TS_realloc(x,y)
597 # define uprv_default_free(x) _C_TS_free(x)
598 /* also _C_TS_calloc(x) */
599 #else
600 /* C defaults */
601 # define uprv_default_malloc(x) malloc(x)
602 # define uprv_default_realloc(x,y) realloc(x,y)
603 # define uprv_default_free(x) free(x)
604 #endif
605 
606 
607 #endif
608