]> git.saurik.com Git - apple/icu.git/blob - icuSources/common/putilimp.h
ICU-62109.0.1.tar.gz
[apple/icu.git] / icuSources / common / putilimp.h
1 // © 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 /** @{ Information about POSIX support */
77 /*===========================================================================*/
78
79 #ifdef U_HAVE_NL_LANGINFO_CODESET
80 /* Use the predefined value. */
81 #elif U_PLATFORM_USES_ONLY_WIN32_API || U_PLATFORM == U_PF_ANDROID || U_PLATFORM == U_PF_QNX
82 # define U_HAVE_NL_LANGINFO_CODESET 0
83 #else
84 # define U_HAVE_NL_LANGINFO_CODESET 1
85 #endif
86
87 #ifdef U_NL_LANGINFO_CODESET
88 /* Use the predefined value. */
89 #elif !U_HAVE_NL_LANGINFO_CODESET
90 # define U_NL_LANGINFO_CODESET -1
91 #elif U_PLATFORM == U_PF_OS400
92 /* not defined */
93 #else
94 # define U_NL_LANGINFO_CODESET CODESET
95 #endif
96
97 #ifdef U_TZSET
98 /* Use the predefined value. */
99 #elif U_PLATFORM_USES_ONLY_WIN32_API
100 // UWP doesn't support tzset or environment variables for tz
101 #if U_PLATFORM_HAS_WINUWP_API == 0
102 # define U_TZSET _tzset
103 #endif
104 #elif U_PLATFORM == U_PF_OS400
105 /* not defined */
106 #else
107 # define U_TZSET tzset
108 #endif
109
110 #if defined(U_TIMEZONE) || defined(U_HAVE_TIMEZONE)
111 /* Use the predefined value. */
112 #elif U_PLATFORM == U_PF_ANDROID
113 # define U_TIMEZONE timezone
114 #elif defined(__UCLIBC__)
115 // uClibc does not have __timezone or _timezone.
116 #elif defined(_NEWLIB_VERSION)
117 # define U_TIMEZONE _timezone
118 #elif defined(__GLIBC__)
119 // glibc
120 # define U_TIMEZONE __timezone
121 #elif U_PLATFORM_IS_LINUX_BASED
122 // not defined
123 #elif U_PLATFORM_USES_ONLY_WIN32_API
124 # define U_TIMEZONE _timezone
125 #elif U_PLATFORM == U_PF_BSD && !defined(__NetBSD__)
126 /* not defined */
127 #elif U_PLATFORM == U_PF_OS400
128 /* not defined */
129 #elif U_PLATFORM == U_PF_IPHONE
130 /* not defined */
131 #else
132 # define U_TIMEZONE timezone
133 #endif
134
135 #ifdef U_TZNAME
136 /* Use the predefined value. */
137 #elif U_PLATFORM_USES_ONLY_WIN32_API
138 /* not usable on all windows platforms */
139 #if U_PLATFORM_HAS_WINUWP_API == 0
140 # define U_TZNAME _tzname
141 #endif
142 #elif U_PLATFORM == U_PF_OS400
143 /* not defined */
144 #else
145 # define U_TZNAME tzname
146 #endif
147
148 #ifdef U_HAVE_MMAP
149 /* Use the predefined value. */
150 #elif U_PLATFORM_USES_ONLY_WIN32_API
151 # define U_HAVE_MMAP 0
152 #else
153 # define U_HAVE_MMAP 1
154 #endif
155
156 #ifdef U_HAVE_POPEN
157 /* Use the predefined value. */
158 #elif U_PLATFORM_USES_ONLY_WIN32_API
159 # define U_HAVE_POPEN 0
160 #elif U_PLATFORM == U_PF_OS400
161 # define U_HAVE_POPEN 0
162 #else
163 # define U_HAVE_POPEN 1
164 #endif
165
166 /**
167 * \def U_HAVE_DIRENT_H
168 * Defines whether dirent.h is available.
169 * @internal
170 */
171 #ifdef U_HAVE_DIRENT_H
172 /* Use the predefined value. */
173 #elif U_PLATFORM_USES_ONLY_WIN32_API
174 # define U_HAVE_DIRENT_H 0
175 #else
176 # define U_HAVE_DIRENT_H 1
177 #endif
178
179 /** @} */
180
181 /*===========================================================================*/
182 /** @{ GCC built in functions for atomic memory operations */
183 /*===========================================================================*/
184
185 /**
186 * \def U_HAVE_GCC_ATOMICS
187 * @internal
188 */
189 #ifdef U_HAVE_GCC_ATOMICS
190 /* Use the predefined value. */
191 #elif U_PLATFORM == U_PF_MINGW
192 #define U_HAVE_GCC_ATOMICS 0
193 #elif U_GCC_MAJOR_MINOR >= 404 || defined(__clang__)
194 /* TODO: Intel icc and IBM xlc on AIX also support gcc atomics. (Intel originated them.)
195 * Add them for these compilers.
196 * Note: Clang sets __GNUC__ defines for version 4.2, so misses the 4.4 test here.
197 */
198 # define U_HAVE_GCC_ATOMICS 1
199 #else
200 # define U_HAVE_GCC_ATOMICS 0
201 #endif
202
203 /** @} */
204
205 /**
206 * \def U_HAVE_STD_ATOMICS
207 * Defines whether the standard C++11 <atomic> is available.
208 * ICU will use this when available,
209 * otherwise will fall back to compiler or platform specific alternatives.
210 * @internal
211 */
212 #ifdef U_HAVE_STD_ATOMICS
213 /* Use the predefined value. */
214 #elif U_CPLUSPLUS_VERSION < 11
215 /* Not C++11, disable use of atomics */
216 # define U_HAVE_STD_ATOMICS 0
217 #elif __clang__ && __clang_major__==3 && __clang_minor__<=1
218 /* Clang 3.1, has atomic variable initializer bug. */
219 # define U_HAVE_STD_ATOMICS 0
220 #else
221 /* U_HAVE_ATOMIC is typically set by an autoconf test of #include <atomic> */
222 /* Can be set manually, or left undefined, on platforms without autoconf. */
223 # if defined(U_HAVE_ATOMIC) && U_HAVE_ATOMIC
224 # define U_HAVE_STD_ATOMICS 1
225 # else
226 # define U_HAVE_STD_ATOMICS 0
227 # endif
228 #endif
229
230
231 /**
232 * \def U_HAVE_CLANG_ATOMICS
233 * Defines whether Clang c11 style built-in atomics are available.
234 * These are used in preference to gcc atomics when both are available.
235 */
236 #ifdef U_HAVE_CLANG_ATOMICS
237 /* Use the predefined value. */
238 #elif __has_builtin(__c11_atomic_load) && \
239 __has_builtin(__c11_atomic_store) && \
240 __has_builtin(__c11_atomic_fetch_add) && \
241 __has_builtin(__c11_atomic_fetch_sub)
242 # define U_HAVE_CLANG_ATOMICS 1
243 #else
244 # define U_HAVE_CLANG_ATOMICS 0
245 #endif
246
247 /*===========================================================================*/
248 /** @{ Programs used by ICU code */
249 /*===========================================================================*/
250
251 /**
252 * \def U_MAKE_IS_NMAKE
253 * Defines whether the "make" program is Windows nmake.
254 */
255 #ifdef U_MAKE_IS_NMAKE
256 /* Use the predefined value. */
257 #elif U_PLATFORM == U_PF_WINDOWS
258 # define U_MAKE_IS_NMAKE 1
259 #else
260 # define U_MAKE_IS_NMAKE 0
261 #endif
262
263 /** @} */
264
265 /*==========================================================================*/
266 /* Platform utilities */
267 /*==========================================================================*/
268
269 /**
270 * Platform utilities isolates the platform dependencies of the
271 * library. For each platform which this code is ported to, these
272 * functions may have to be re-implemented.
273 */
274
275 /**
276 * Floating point utility to determine if a double is Not a Number (NaN).
277 * @internal
278 */
279 U_INTERNAL UBool U_EXPORT2 uprv_isNaN(double d);
280 /**
281 * Floating point utility to determine if a double has an infinite value.
282 * @internal
283 */
284 U_INTERNAL UBool U_EXPORT2 uprv_isInfinite(double d);
285 /**
286 * Floating point utility to determine if a double has a positive infinite value.
287 * @internal
288 */
289 U_INTERNAL UBool U_EXPORT2 uprv_isPositiveInfinity(double d);
290 /**
291 * Floating point utility to determine if a double has a negative infinite value.
292 * @internal
293 */
294 U_INTERNAL UBool U_EXPORT2 uprv_isNegativeInfinity(double d);
295 /**
296 * Floating point utility that returns a Not a Number (NaN) value.
297 * @internal
298 */
299 U_INTERNAL double U_EXPORT2 uprv_getNaN(void);
300 /**
301 * Floating point utility that returns an infinite value.
302 * @internal
303 */
304 U_INTERNAL double U_EXPORT2 uprv_getInfinity(void);
305
306 /**
307 * Floating point utility to truncate a double.
308 * @internal
309 */
310 U_INTERNAL double U_EXPORT2 uprv_trunc(double d);
311 /**
312 * Floating point utility to calculate the floor of a double.
313 * @internal
314 */
315 U_INTERNAL double U_EXPORT2 uprv_floor(double d);
316 /**
317 * Floating point utility to calculate the ceiling of a double.
318 * @internal
319 */
320 U_INTERNAL double U_EXPORT2 uprv_ceil(double d);
321 /**
322 * Floating point utility to calculate the absolute value of a double.
323 * @internal
324 */
325 U_INTERNAL double U_EXPORT2 uprv_fabs(double d);
326 /**
327 * Floating point utility to calculate the fractional and integer parts of a double.
328 * @internal
329 */
330 U_INTERNAL double U_EXPORT2 uprv_modf(double d, double* pinteger);
331 /**
332 * Floating point utility to calculate the remainder of a double divided by another double.
333 * @internal
334 */
335 U_INTERNAL double U_EXPORT2 uprv_fmod(double d, double y);
336 /**
337 * Floating point utility to calculate d to the power of exponent (d^exponent).
338 * @internal
339 */
340 U_INTERNAL double U_EXPORT2 uprv_pow(double d, double exponent);
341 /**
342 * Floating point utility to calculate 10 to the power of exponent (10^exponent).
343 * @internal
344 */
345 U_INTERNAL double U_EXPORT2 uprv_pow10(int32_t exponent);
346 /**
347 * Floating point utility to calculate the maximum value of two doubles.
348 * @internal
349 */
350 U_INTERNAL double U_EXPORT2 uprv_fmax(double d, double y);
351 /**
352 * Floating point utility to calculate the minimum value of two doubles.
353 * @internal
354 */
355 U_INTERNAL double U_EXPORT2 uprv_fmin(double d, double y);
356 /**
357 * Private utility to calculate the maximum value of two integers.
358 * @internal
359 */
360 U_INTERNAL int32_t U_EXPORT2 uprv_max(int32_t d, int32_t y);
361 /**
362 * Private utility to calculate the minimum value of two integers.
363 * @internal
364 */
365 U_INTERNAL int32_t U_EXPORT2 uprv_min(int32_t d, int32_t y);
366
367 #if U_IS_BIG_ENDIAN
368 # define uprv_isNegative(number) (*((signed char *)&(number))<0)
369 #else
370 # define uprv_isNegative(number) (*((signed char *)&(number)+sizeof(number)-1)<0)
371 #endif
372
373 /**
374 * Return the largest positive number that can be represented by an integer
375 * type of arbitrary bit length.
376 * @internal
377 */
378 U_INTERNAL double U_EXPORT2 uprv_maxMantissa(void);
379
380 /**
381 * Floating point utility to calculate the logarithm of a double.
382 * @internal
383 */
384 U_INTERNAL double U_EXPORT2 uprv_log(double d);
385
386 /**
387 * Does common notion of rounding e.g. uprv_floor(x + 0.5);
388 * @param x the double number
389 * @return the rounded double
390 * @internal
391 */
392 U_INTERNAL double U_EXPORT2 uprv_round(double x);
393
394 /**
395 * Adds the signed integers a and b, storing the result in res.
396 * Checks for signed integer overflow.
397 * Similar to the GCC/Clang extension __builtin_add_overflow
398 *
399 * @param a The first operand.
400 * @param b The second operand.
401 * @param res a + b
402 * @return true if overflow occurred; false if no overflow occurred.
403 * @internal
404 */
405 U_INTERNAL UBool U_EXPORT2 uprv_add32_overflow(int32_t a, int32_t b, int32_t* res);
406
407 /**
408 * Multiplies the signed integers a and b, storing the result in res.
409 * Checks for signed integer overflow.
410 * Similar to the GCC/Clang extension __builtin_mul_overflow
411 *
412 * @param a The first multiplicand.
413 * @param b The second multiplicand.
414 * @param res a * b
415 * @return true if overflow occurred; false if no overflow occurred.
416 * @internal
417 */
418 U_INTERNAL UBool U_EXPORT2 uprv_mul32_overflow(int32_t a, int32_t b, int32_t* res);
419
420 #if 0
421 /**
422 * Returns the number of digits after the decimal point in a double number x.
423 *
424 * @param x the double number
425 * @return the number of digits after the decimal point in a double number x.
426 * @internal
427 */
428 /*U_INTERNAL int32_t U_EXPORT2 uprv_digitsAfterDecimal(double x);*/
429 #endif
430
431 #if !U_CHARSET_IS_UTF8
432 /**
433 * Please use ucnv_getDefaultName() instead.
434 * Return the default codepage for this platform and locale.
435 * This function can call setlocale() on Unix platforms. Please read the
436 * platform documentation on setlocale() before calling this function.
437 * @return the default codepage for this platform
438 * @internal
439 */
440 U_INTERNAL const char* U_EXPORT2 uprv_getDefaultCodepage(void);
441 #endif
442
443 /**
444 * Please use uloc_getDefault() instead.
445 * Return the default locale ID string by querying the system, or
446 * zero if one cannot be found.
447 * This function can call setlocale() on Unix platforms. Please read the
448 * platform documentation on setlocale() before calling this function.
449 * @return the default locale ID string
450 * @internal
451 */
452 U_INTERNAL const char* U_EXPORT2 uprv_getDefaultLocaleID(void);
453
454 /**
455 * Time zone utilities
456 *
457 * Wrappers for C runtime library functions relating to timezones.
458 * The t_tzset() function (similar to tzset) uses the current setting
459 * of the environment variable TZ to assign values to three global
460 * variables: daylight, timezone, and tzname. These variables have the
461 * following meanings, and are declared in &lt;time.h&gt;.
462 *
463 * daylight Nonzero if daylight-saving-time zone (DST) is specified
464 * in TZ; otherwise, 0. Default value is 1.
465 * timezone Difference in seconds between coordinated universal
466 * time and local time. E.g., -28,800 for PST (GMT-8hrs)
467 * tzname(0) Three-letter time-zone name derived from TZ environment
468 * variable. E.g., "PST".
469 * tzname(1) Three-letter DST zone name derived from TZ environment
470 * variable. E.g., "PDT". If DST zone is omitted from TZ,
471 * tzname(1) is an empty string.
472 *
473 * Notes: For example, to set the TZ environment variable to correspond
474 * to the current time zone in Germany, you can use one of the
475 * following statements:
476 *
477 * set TZ=GST1GDT
478 * set TZ=GST+1GDT
479 *
480 * If the TZ value is not set, t_tzset() attempts to use the time zone
481 * information specified by the operating system. Under Windows NT
482 * and Windows 95, this information is specified in the Control Panel's
483 * Date/Time application.
484 * @internal
485 */
486 U_INTERNAL void U_EXPORT2 uprv_tzset(void);
487
488 /**
489 * Difference in seconds between coordinated universal
490 * time and local time. E.g., -28,800 for PST (GMT-8hrs)
491 * @return the difference in seconds between coordinated universal time and local time.
492 * @internal
493 */
494 U_INTERNAL int32_t U_EXPORT2 uprv_timezone(void);
495
496 /**
497 * tzname(0) Three-letter time-zone name derived from TZ environment
498 * variable. E.g., "PST".
499 * tzname(1) Three-letter DST zone name derived from TZ environment
500 * variable. E.g., "PDT". If DST zone is omitted from TZ,
501 * tzname(1) is an empty string.
502 * @internal
503 */
504 U_INTERNAL const char* U_EXPORT2 uprv_tzname(int n);
505
506 /**
507 * Reset the global tzname cache.
508 * @internal
509 */
510 U_INTERNAL void uprv_tzname_clear_cache();
511
512 /**
513 * Get UTC (GMT) time measured in milliseconds since 0:00 on 1/1/1970.
514 * This function is affected by 'faketime' and should be the bottleneck for all user-visible ICU time functions.
515 * @return the UTC time measured in milliseconds
516 * @internal
517 */
518 U_INTERNAL UDate U_EXPORT2 uprv_getUTCtime(void);
519
520 /**
521 * Get UTC (GMT) time measured in milliseconds since 0:00 on 1/1/1970.
522 * This function is not affected by 'faketime', so it should only be used by low level test functions- not by anything that
523 * exposes time to the end user.
524 * @return the UTC time measured in milliseconds
525 * @internal
526 */
527 U_INTERNAL UDate U_EXPORT2 uprv_getRawUTCtime(void);
528
529 /**
530 * Determine whether a pathname is absolute or not, as defined by the platform.
531 * @param path Pathname to test
532 * @return TRUE if the path is absolute
533 * @internal (ICU 3.0)
534 */
535 U_INTERNAL UBool U_EXPORT2 uprv_pathIsAbsolute(const char *path);
536
537 /**
538 * Use U_MAX_PTR instead of this function.
539 * @param void pointer to test
540 * @return the largest possible pointer greater than the base
541 * @internal (ICU 3.8)
542 */
543 U_INTERNAL void * U_EXPORT2 uprv_maximumPtr(void *base);
544
545 /**
546 * Maximum value of a (void*) - use to indicate the limit of an 'infinite' buffer.
547 * In fact, buffer sizes must not exceed 2GB so that the difference between
548 * the buffer limit and the buffer start can be expressed in an int32_t.
549 *
550 * The definition of U_MAX_PTR must fulfill the following conditions:
551 * - return the largest possible pointer greater than base
552 * - return a valid pointer according to the machine architecture (AS/400, 64-bit, etc.)
553 * - avoid wrapping around at high addresses
554 * - make sure that the returned pointer is not farther from base than 0x7fffffff bytes
555 *
556 * @param base The beginning of a buffer to find the maximum offset from
557 * @internal
558 */
559 #ifndef U_MAX_PTR
560 # if U_PLATFORM == U_PF_OS390 && !defined(_LP64)
561 /* We have 31-bit pointers. */
562 # define U_MAX_PTR(base) ((void *)0x7fffffff)
563 # elif U_PLATFORM == U_PF_OS400
564 # define U_MAX_PTR(base) uprv_maximumPtr((void *)base)
565 # elif 0
566 /*
567 * For platforms where pointers are scalar values (which is normal, but unlike i5/OS)
568 * but that do not define uintptr_t.
569 *
570 * However, this does not work on modern compilers:
571 * The C++ standard does not define pointer overflow, and allows compilers to
572 * assume that p+u>p for any pointer p and any integer u>0.
573 * Thus, modern compilers optimize away the ">" comparison.
574 * (See ICU tickets #7187 and #8096.)
575 */
576 # define U_MAX_PTR(base) \
577 ((void *)(((char *)(base)+0x7fffffffu) > (char *)(base) \
578 ? ((char *)(base)+0x7fffffffu) \
579 : (char *)-1))
580 # else
581 /* Default version. C++ standard compliant for scalar pointers. */
582 # define U_MAX_PTR(base) \
583 ((void *)(((uintptr_t)(base)+0x7fffffffu) > (uintptr_t)(base) \
584 ? ((uintptr_t)(base)+0x7fffffffu) \
585 : (uintptr_t)-1))
586 # endif
587 #endif
588
589 /* Dynamic Library Functions */
590
591 typedef void (UVoidFunction)(void);
592
593 #if U_ENABLE_DYLOAD
594 /**
595 * Load a library
596 * @internal (ICU 4.4)
597 */
598 U_INTERNAL void * U_EXPORT2 uprv_dl_open(const char *libName, UErrorCode *status);
599
600 /**
601 * Close a library
602 * @internal (ICU 4.4)
603 */
604 U_INTERNAL void U_EXPORT2 uprv_dl_close( void *lib, UErrorCode *status);
605
606 /**
607 * Extract a symbol from a library (function)
608 * @internal (ICU 4.8)
609 */
610 U_INTERNAL UVoidFunction* U_EXPORT2 uprv_dlsym_func( void *lib, const char *symbolName, UErrorCode *status);
611
612 /**
613 * Extract a symbol from a library (function)
614 * Not implemented, no clients.
615 * @internal
616 */
617 /* U_INTERNAL void * U_EXPORT2 uprv_dlsym_data( void *lib, const char *symbolName, UErrorCode *status); */
618
619 #endif
620
621 /**
622 * Define malloc and related functions
623 * @internal
624 */
625 #if U_PLATFORM == U_PF_OS400
626 # define uprv_default_malloc(x) _C_TS_malloc(x)
627 # define uprv_default_realloc(x,y) _C_TS_realloc(x,y)
628 # define uprv_default_free(x) _C_TS_free(x)
629 /* also _C_TS_calloc(x) */
630 #else
631 /* C defaults */
632 # define uprv_default_malloc(x) malloc(x)
633 # define uprv_default_realloc(x,y) realloc(x,y)
634 # define uprv_default_free(x) free(x)
635 #endif
636
637
638 #endif