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