1 /* 2 ****************************************************************************** 3 * 4 * Copyright (C) 1997-2014, 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 !defined(__cplusplus) || __cplusplus<201103L 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 /** @{ Code alignment */ 234 /*===========================================================================*/ 235 236 /** 237 * \def U_ALIGN_CODE 238 * This is used to align code fragments to a specific byte boundary. 239 * This is useful for getting consistent performance test results. 240 * @internal 241 */ 242 #ifdef U_ALIGN_CODE 243 /* Use the predefined value. */ 244 #elif defined(_MSC_VER) && defined(_M_IX86) && !defined(_MANAGED) 245 # define U_ALIGN_CODE(boundarySize) __asm align boundarySize 246 #else 247 # define U_ALIGN_CODE(boundarySize) 248 #endif 249 250 /** @} */ 251 252 /*===========================================================================*/ 253 /** @{ Programs used by ICU code */ 254 /*===========================================================================*/ 255 256 /** 257 * \def U_MAKE_IS_NMAKE 258 * Defines whether the "make" program is Windows nmake. 259 */ 260 #ifdef U_MAKE_IS_NMAKE 261 /* Use the predefined value. */ 262 #elif U_PLATFORM == U_PF_WINDOWS 263 # define U_MAKE_IS_NMAKE 1 264 #else 265 # define U_MAKE_IS_NMAKE 0 266 #endif 267 268 /** @} */ 269 270 /*==========================================================================*/ 271 /* Platform utilities */ 272 /*==========================================================================*/ 273 274 /** 275 * Platform utilities isolates the platform dependencies of the 276 * libarary. For each platform which this code is ported to, these 277 * functions may have to be re-implemented. 278 */ 279 280 /** 281 * Floating point utility to determine if a double is Not a Number (NaN). 282 * @internal 283 */ 284 U_INTERNAL UBool U_EXPORT2 uprv_isNaN(double d); 285 /** 286 * Floating point utility to determine if a double has an infinite value. 287 * @internal 288 */ 289 U_INTERNAL UBool U_EXPORT2 uprv_isInfinite(double d); 290 /** 291 * Floating point utility to determine if a double has a positive infinite value. 292 * @internal 293 */ 294 U_INTERNAL UBool U_EXPORT2 uprv_isPositiveInfinity(double d); 295 /** 296 * Floating point utility to determine if a double has a negative infinite value. 297 * @internal 298 */ 299 U_INTERNAL UBool U_EXPORT2 uprv_isNegativeInfinity(double d); 300 /** 301 * Floating point utility that returns a Not a Number (NaN) value. 302 * @internal 303 */ 304 U_INTERNAL double U_EXPORT2 uprv_getNaN(void); 305 /** 306 * Floating point utility that returns an infinite value. 307 * @internal 308 */ 309 U_INTERNAL double U_EXPORT2 uprv_getInfinity(void); 310 311 /** 312 * Floating point utility to truncate a double. 313 * @internal 314 */ 315 U_INTERNAL double U_EXPORT2 uprv_trunc(double d); 316 /** 317 * Floating point utility to calculate the floor of a double. 318 * @internal 319 */ 320 U_INTERNAL double U_EXPORT2 uprv_floor(double d); 321 /** 322 * Floating point utility to calculate the ceiling of a double. 323 * @internal 324 */ 325 U_INTERNAL double U_EXPORT2 uprv_ceil(double d); 326 /** 327 * Floating point utility to calculate the absolute value of a double. 328 * @internal 329 */ 330 U_INTERNAL double U_EXPORT2 uprv_fabs(double d); 331 /** 332 * Floating point utility to calculate the fractional and integer parts of a double. 333 * @internal 334 */ 335 U_INTERNAL double U_EXPORT2 uprv_modf(double d, double* pinteger); 336 /** 337 * Floating point utility to calculate the remainder of a double divided by another double. 338 * @internal 339 */ 340 U_INTERNAL double U_EXPORT2 uprv_fmod(double d, double y); 341 /** 342 * Floating point utility to calculate d to the power of exponent (d^exponent). 343 * @internal 344 */ 345 U_INTERNAL double U_EXPORT2 uprv_pow(double d, double exponent); 346 /** 347 * Floating point utility to calculate 10 to the power of exponent (10^exponent). 348 * @internal 349 */ 350 U_INTERNAL double U_EXPORT2 uprv_pow10(int32_t exponent); 351 /** 352 * Floating point utility to calculate the maximum value of two doubles. 353 * @internal 354 */ 355 U_INTERNAL double U_EXPORT2 uprv_fmax(double d, double y); 356 /** 357 * Floating point utility to calculate the minimum value of two doubles. 358 * @internal 359 */ 360 U_INTERNAL double U_EXPORT2 uprv_fmin(double d, double y); 361 /** 362 * Private utility to calculate the maximum value of two integers. 363 * @internal 364 */ 365 U_INTERNAL int32_t U_EXPORT2 uprv_max(int32_t d, int32_t y); 366 /** 367 * Private utility to calculate the minimum value of two integers. 368 * @internal 369 */ 370 U_INTERNAL int32_t U_EXPORT2 uprv_min(int32_t d, int32_t y); 371 372 #if U_IS_BIG_ENDIAN 373 # define uprv_isNegative(number) (*((signed char *)&(number))<0) 374 #else 375 # define uprv_isNegative(number) (*((signed char *)&(number)+sizeof(number)-1)<0) 376 #endif 377 378 /** 379 * Return the largest positive number that can be represented by an integer 380 * type of arbitrary bit length. 381 * @internal 382 */ 383 U_INTERNAL double U_EXPORT2 uprv_maxMantissa(void); 384 385 /** 386 * Floating point utility to calculate the logarithm of a double. 387 * @internal 388 */ 389 U_INTERNAL double U_EXPORT2 uprv_log(double d); 390 391 /** 392 * Does common notion of rounding e.g. uprv_floor(x + 0.5); 393 * @param x the double number 394 * @return the rounded double 395 * @internal 396 */ 397 U_INTERNAL double U_EXPORT2 uprv_round(double x); 398 399 #if 0 400 /** 401 * Returns the number of digits after the decimal point in a double number x. 402 * 403 * @param x the double number 404 * @return the number of digits after the decimal point in a double number x. 405 * @internal 406 */ 407 /*U_INTERNAL int32_t U_EXPORT2 uprv_digitsAfterDecimal(double x);*/ 408 #endif 409 410 #if !U_CHARSET_IS_UTF8 411 /** 412 * Please use ucnv_getDefaultName() instead. 413 * Return the default codepage for this platform and locale. 414 * This function can call setlocale() on Unix platforms. Please read the 415 * platform documentation on setlocale() before calling this function. 416 * @return the default codepage for this platform 417 * @internal 418 */ 419 U_INTERNAL const char* U_EXPORT2 uprv_getDefaultCodepage(void); 420 #endif 421 422 /** 423 * Please use uloc_getDefault() instead. 424 * Return the default locale ID string by querying ths system, or 425 * zero if one cannot be found. 426 * This function can call setlocale() on Unix platforms. Please read the 427 * platform documentation on setlocale() before calling this function. 428 * @return the default locale ID string 429 * @internal 430 */ 431 U_INTERNAL const char* U_EXPORT2 uprv_getDefaultLocaleID(void); 432 433 /** 434 * Time zone utilities 435 * 436 * Wrappers for C runtime library functions relating to timezones. 437 * The t_tzset() function (similar to tzset) uses the current setting 438 * of the environment variable TZ to assign values to three global 439 * variables: daylight, timezone, and tzname. These variables have the 440 * following meanings, and are declared in <time.h>. 441 * 442 * daylight Nonzero if daylight-saving-time zone (DST) is specified 443 * in TZ; otherwise, 0. Default value is 1. 444 * timezone Difference in seconds between coordinated universal 445 * time and local time. E.g., -28,800 for PST (GMT-8hrs) 446 * tzname(0) Three-letter time-zone name derived from TZ environment 447 * variable. E.g., "PST". 448 * tzname(1) Three-letter DST zone name derived from TZ environment 449 * variable. E.g., "PDT". If DST zone is omitted from TZ, 450 * tzname(1) is an empty string. 451 * 452 * Notes: For example, to set the TZ environment variable to correspond 453 * to the current time zone in Germany, you can use one of the 454 * following statements: 455 * 456 * set TZ=GST1GDT 457 * set TZ=GST+1GDT 458 * 459 * If the TZ value is not set, t_tzset() attempts to use the time zone 460 * information specified by the operating system. Under Windows NT 461 * and Windows 95, this information is specified in the Control Panel's 462 * Date/Time application. 463 * @internal 464 */ 465 U_INTERNAL void U_EXPORT2 uprv_tzset(void); 466 467 /** 468 * Difference in seconds between coordinated universal 469 * time and local time. E.g., -28,800 for PST (GMT-8hrs) 470 * @return the difference in seconds between coordinated universal time and local time. 471 * @internal 472 */ 473 U_INTERNAL int32_t U_EXPORT2 uprv_timezone(void); 474 475 /** 476 * tzname(0) Three-letter time-zone name derived from TZ environment 477 * variable. E.g., "PST". 478 * tzname(1) Three-letter DST zone name derived from TZ environment 479 * variable. E.g., "PDT". If DST zone is omitted from TZ, 480 * tzname(1) is an empty string. 481 * @internal 482 */ 483 U_INTERNAL const char* U_EXPORT2 uprv_tzname(int n); 484 485 /** 486 * Get UTC (GMT) time measured in milliseconds since 0:00 on 1/1/1970. 487 * This function is affected by 'faketime' and should be the bottleneck for all user-visible ICU time functions. 488 * @return the UTC time measured in milliseconds 489 * @internal 490 */ 491 U_INTERNAL UDate U_EXPORT2 uprv_getUTCtime(void); 492 493 /** 494 * Get UTC (GMT) time measured in milliseconds since 0:00 on 1/1/1970. 495 * This function is not affected by 'faketime', so it should only be used by low level test functions- not by anything that 496 * exposes time to the end user. 497 * @return the UTC time measured in milliseconds 498 * @internal 499 */ 500 U_INTERNAL UDate U_EXPORT2 uprv_getRawUTCtime(void); 501 502 /** 503 * Determine whether a pathname is absolute or not, as defined by the platform. 504 * @param path Pathname to test 505 * @return TRUE if the path is absolute 506 * @internal (ICU 3.0) 507 */ 508 U_INTERNAL UBool U_EXPORT2 uprv_pathIsAbsolute(const char *path); 509 510 /** 511 * Use U_MAX_PTR instead of this function. 512 * @param void pointer to test 513 * @return the largest possible pointer greater than the base 514 * @internal (ICU 3.8) 515 */ 516 U_INTERNAL void * U_EXPORT2 uprv_maximumPtr(void *base); 517 518 /** 519 * Maximum value of a (void*) - use to indicate the limit of an 'infinite' buffer. 520 * In fact, buffer sizes must not exceed 2GB so that the difference between 521 * the buffer limit and the buffer start can be expressed in an int32_t. 522 * 523 * The definition of U_MAX_PTR must fulfill the following conditions: 524 * - return the largest possible pointer greater than base 525 * - return a valid pointer according to the machine architecture (AS/400, 64-bit, etc.) 526 * - avoid wrapping around at high addresses 527 * - make sure that the returned pointer is not farther from base than 0x7fffffff bytes 528 * 529 * @param base The beginning of a buffer to find the maximum offset from 530 * @internal 531 */ 532 #ifndef U_MAX_PTR 533 # if U_PLATFORM == U_PF_OS390 && !defined(_LP64) 534 /* We have 31-bit pointers. */ 535 # define U_MAX_PTR(base) ((void *)0x7fffffff) 536 # elif U_PLATFORM == U_PF_OS400 537 # define U_MAX_PTR(base) uprv_maximumPtr((void *)base) 538 # elif 0 539 /* 540 * For platforms where pointers are scalar values (which is normal, but unlike i5/OS) 541 * but that do not define uintptr_t. 542 * 543 * However, this does not work on modern compilers: 544 * The C++ standard does not define pointer overflow, and allows compilers to 545 * assume that p+u>p for any pointer p and any integer u>0. 546 * Thus, modern compilers optimize away the ">" comparison. 547 * (See ICU tickets #7187 and #8096.) 548 */ 549 # define U_MAX_PTR(base) \ 550 ((void *)(((char *)(base)+0x7fffffffu) > (char *)(base) \ 551 ? ((char *)(base)+0x7fffffffu) \ 552 : (char *)-1)) 553 # else 554 /* Default version. C++ standard compliant for scalar pointers. */ 555 # define U_MAX_PTR(base) \ 556 ((void *)(((uintptr_t)(base)+0x7fffffffu) > (uintptr_t)(base) \ 557 ? ((uintptr_t)(base)+0x7fffffffu) \ 558 : (uintptr_t)-1)) 559 # endif 560 #endif 561 562 /* Dynamic Library Functions */ 563 564 typedef void (UVoidFunction)(void); 565 566 #if U_ENABLE_DYLOAD 567 /** 568 * Load a library 569 * @internal (ICU 4.4) 570 */ 571 U_INTERNAL void * U_EXPORT2 uprv_dl_open(const char *libName, UErrorCode *status); 572 573 /** 574 * Close a library 575 * @internal (ICU 4.4) 576 */ 577 U_INTERNAL void U_EXPORT2 uprv_dl_close( void *lib, UErrorCode *status); 578 579 /** 580 * Extract a symbol from a library (function) 581 * @internal (ICU 4.8) 582 */ 583 U_INTERNAL UVoidFunction* U_EXPORT2 uprv_dlsym_func( void *lib, const char *symbolName, UErrorCode *status); 584 585 /** 586 * Extract a symbol from a library (function) 587 * Not implemented, no clients. 588 * @internal 589 */ 590 /* U_INTERNAL void * U_EXPORT2 uprv_dlsym_data( void *lib, const char *symbolName, UErrorCode *status); */ 591 592 #endif 593 594 /** 595 * Define malloc and related functions 596 * @internal 597 */ 598 #if U_PLATFORM == U_PF_OS400 599 # define uprv_default_malloc(x) _C_TS_malloc(x) 600 # define uprv_default_realloc(x,y) _C_TS_realloc(x,y) 601 # define uprv_default_free(x) _C_TS_free(x) 602 /* also _C_TS_calloc(x) */ 603 #else 604 /* C defaults */ 605 # define uprv_default_malloc(x) malloc(x) 606 # define uprv_default_realloc(x,y) realloc(x,y) 607 # define uprv_default_free(x) free(x) 608 #endif 609 610 611 #endif 612