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