2 * YASM utility functions.
4 * Includes standard headers and defines prototypes for replacement functions
7 * Copyright (C) 2001-2007 Peter Johnson
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND OTHER CONTRIBUTORS ``AS IS''
19 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR OTHER CONTRIBUTORS BE
22 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 * POSSIBILITY OF SUCH DAMAGE.
37 #if defined(HAVE_GNU_C_LIBRARY) || defined(__MINGW32__) || defined(__DJGPP__)
39 /* Work around glibc's non-defining of certain things when using gcc -ansi */
40 # ifdef __STRICT_ANSI__
41 # undef __STRICT_ANSI__
44 /* Work around glibc's string inlines (in bits/string2.h) if needed */
45 # ifdef NO_STRING_INLINES
46 # define __NO_STRING_INLINES
51 #if !defined(lint) && !defined(NDEBUG)
67 #include <libyasm-stdint.h>
68 #include <libyasm/coretype.h>
71 # define _(String) String
79 # define _(String) gettext(String)
81 # define gettext(Msgid) (Msgid)
82 # define dgettext(Domainname, Msgid) (Msgid)
83 # define dcgettext(Domainname, Msgid, Category) (Msgid)
84 # define textdomain(Domainname) while (0) /* nothing */
85 # define bindtextdomain(Domainname, Dirname) while (0) /* nothing */
86 # define _(String) (String)
91 # define N_(String) gettext_noop(String)
93 # define N_(String) (String)
97 #define yasm__mergesort(a, b, c, d) mergesort(a, b, c, d)
101 #define yasm__strsep(a, b) strsep(a, b)
104 #ifdef HAVE_STRCASECMP
105 # define yasm__strcasecmp(x, y) strcasecmp(x, y)
106 # define yasm__strncasecmp(x, y, n) strncasecmp(x, y, n)
108 # define yasm__strcasecmp(x, y) stricmp(x, y)
109 # define yasm__strncasecmp(x, y, n) strnicmp(x, y, n)
111 # define yasm__strcasecmp(x, y) _stricmp(x, y)
112 # define yasm__strncasecmp(x, y, n) _strnicmp(x, y, n)
114 # define yasm__strcasecmp(x, y) strcmpi(x, y)
115 # define yasm__strncasecmp(x, y, n) strncmpi(x, y, n)
117 # define USE_OUR_OWN_STRCASECMP
120 #include <libyasm/compat-queue.h>
123 # include <dmalloc.h>
124 # define yasm__xstrdup(str) xstrdup(str)
125 # define yasm_xmalloc(size) xmalloc(size)
126 # define yasm_xcalloc(count, size) xcalloc(count, size)
127 # define yasm_xrealloc(ptr, size) xrealloc(ptr, size)
128 # define yasm_xfree(ptr) xfree(ptr)
131 /* Bit-counting: used primarily by HAMT but also in a few other places. */
132 #define BC_TWO(c) (0x1ul << (c))
133 #define BC_MSK(c) (((unsigned long)(-1)) / (BC_TWO(BC_TWO(c)) + 1ul))
134 #define BC_COUNT(x,c) ((x) & BC_MSK(c)) + (((x) >> (BC_TWO(c))) & BC_MSK(c))
135 #define BitCount(d, s) do { \
136 d = BC_COUNT(s, 0); \
137 d = BC_COUNT(d, 1); \
138 d = BC_COUNT(d, 2); \
139 d = BC_COUNT(d, 3); \
140 d = BC_COUNT(d, 4); \
143 /** Determine if a value is exactly a power of 2. Zero is treated as a power
146 * \return Nonzero if x is a power of 2.
148 #define is_exp2(x) ((x & (x - 1)) == 0)
151 /** Get the number of elements in an array.
154 * \return Number of elements.
156 #define NELEMS(array) (sizeof(array) / sizeof(array[0]))