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