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