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