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