1 /* GLIB - Library of useful routines for C programming
2  * Copyright (C) 2006-2019 Free Software Foundation, Inc.
3  *
4  * This file is not part of the GNU gettext program, but is used with
5  * GNU gettext.
6  *
7  * The original copyright notice is as follows:
8  */
9 
10 /* GLIB - Library of useful routines for C programming
11  * Copyright (C) 1995-1997  Peter Mattis, Spencer Kimball and Josh MacDonald
12  *
13  * This library is free software; you can redistribute it and/or
14  * modify it under the terms of the GNU General Public
15  * License as published by the Free Software Foundation; either
16  * version 3 of the License, or (at your option) any later version.
17  *
18  * This library is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.	 See the GNU
21  * General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public
24  * License along with this library; if not, write to the
25  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
26  * Boston, MA 02111-1307, USA.
27  */
28 
29 /*
30  * Modified by the GLib Team and others 1997-2000.  See the AUTHORS
31  * file for a list of people on the GLib Team.  See the ChangeLog
32  * files for a list of changes.  These files are distributed with
33  * GLib at ftp://ftp.gtk.org/pub/gtk/.
34  */
35 
36 /*
37  * Modified by Bruno Haible for use as a gnulib module.
38  */
39 
40 #ifndef __G_TYPES_H__
41 #define __G_TYPES_H__
42 
43 #include <glibconfig.h>
44 
45 G_BEGIN_DECLS
46 
47 /* Provide type definitions for commonly used types.
48  *  These are useful because a "gint8" can be adjusted
49  *  to be 1 byte (8 bits) on all platforms. Similarly and
50  *  more importantly, "gint32" can be adjusted to be
51  *  4 bytes (32 bits) on all platforms.
52  */
53 
54 typedef char   gchar;
55 typedef short  gshort;
56 typedef long   glong;
57 typedef int    gint;
58 typedef gint   gboolean;
59 
60 typedef unsigned char   guchar;
61 typedef unsigned short  gushort;
62 typedef unsigned long   gulong;
63 typedef unsigned int    guint;
64 
65 typedef float   gfloat;
66 typedef double  gdouble;
67 
68 /* Define min and max constants for the fixed size numerical types */
69 #define G_MININT8	((gint8)  0x80)
70 #define G_MAXINT8	((gint8)  0x7f)
71 #define G_MAXUINT8	((guint8) 0xff)
72 
73 #define G_MININT16	((gint16)  0x8000)
74 #define G_MAXINT16	((gint16)  0x7fff)
75 #define G_MAXUINT16	((guint16) 0xffff)
76 
77 #define G_MININT32	((gint32)  0x80000000)
78 #define G_MAXINT32	((gint32)  0x7fffffff)
79 #define G_MAXUINT32	((guint32) 0xffffffff)
80 
81 #define G_MININT64	((gint64) G_GINT64_CONSTANT(0x8000000000000000))
82 #define G_MAXINT64	G_GINT64_CONSTANT(0x7fffffffffffffff)
83 #define G_MAXUINT64	G_GINT64_CONSTANT(0xffffffffffffffffU)
84 
85 typedef void* gpointer;
86 typedef const void *gconstpointer;
87 
88 typedef gint            (*GCompareFunc)         (gconstpointer  a,
89                                                  gconstpointer  b);
90 typedef gint            (*GCompareDataFunc)     (gconstpointer  a,
91                                                  gconstpointer  b,
92 						 gpointer       user_data);
93 typedef gboolean        (*GEqualFunc)           (gconstpointer  a,
94                                                  gconstpointer  b);
95 typedef void            (*GDestroyNotify)       (gpointer       data);
96 typedef void            (*GFunc)                (gpointer       data,
97                                                  gpointer       user_data);
98 typedef guint           (*GHashFunc)            (gconstpointer  key);
99 typedef void            (*GHFunc)               (gpointer       key,
100                                                  gpointer       value,
101                                                  gpointer       user_data);
102 typedef void            (*GFreeFunc)            (gpointer       data);
103 typedef const gchar *   (*GTranslateFunc)       (const gchar   *str,
104 						 gpointer       data);
105 
106 
107 /* Define some mathematical constants that aren't available
108  * symbolically in some strict ISO C implementations.
109  */
110 #define G_E     2.7182818284590452353602874713526624977572470937000
111 #define G_LN2   0.69314718055994530941723212145817656807550013436026
112 #define G_LN10  2.3025850929940456840179914546843642076011014886288
113 #define G_PI    3.1415926535897932384626433832795028841971693993751
114 #define G_PI_2  1.5707963267948966192313216916397514420985846996876
115 #define G_PI_4  0.78539816339744830961566084581987572104929234984378
116 #define G_SQRT2 1.4142135623730950488016887242096980785696718753769
117 
118 #if 0
119 
120 /* Portable endian checks and conversions
121  *
122  * glibconfig.h defines G_BYTE_ORDER which expands to one of
123  * the below macros.
124  */
125 #define G_LITTLE_ENDIAN 1234
126 #define G_BIG_ENDIAN    4321
127 #define G_PDP_ENDIAN    3412		/* unused, need specific PDP check */
128 
129 
130 /* Basic bit swapping functions
131  */
132 #define GUINT16_SWAP_LE_BE_CONSTANT(val)	((guint16) ( \
133     (guint16) ((guint16) (val) >> 8) |	\
134     (guint16) ((guint16) (val) << 8)))
135 
136 #define GUINT32_SWAP_LE_BE_CONSTANT(val)	((guint32) ( \
137     (((guint32) (val) & (guint32) 0x000000ffU) << 24) | \
138     (((guint32) (val) & (guint32) 0x0000ff00U) <<  8) | \
139     (((guint32) (val) & (guint32) 0x00ff0000U) >>  8) | \
140     (((guint32) (val) & (guint32) 0xff000000U) >> 24)))
141 
142 #define GUINT64_SWAP_LE_BE_CONSTANT(val)	((guint64) ( \
143       (((guint64) (val) &						\
144 	(guint64) G_GINT64_CONSTANT (0x00000000000000ffU)) << 56) |	\
145       (((guint64) (val) &						\
146 	(guint64) G_GINT64_CONSTANT (0x000000000000ff00U)) << 40) |	\
147       (((guint64) (val) &						\
148 	(guint64) G_GINT64_CONSTANT (0x0000000000ff0000U)) << 24) |	\
149       (((guint64) (val) &						\
150 	(guint64) G_GINT64_CONSTANT (0x00000000ff000000U)) <<  8) |	\
151       (((guint64) (val) &						\
152 	(guint64) G_GINT64_CONSTANT (0x000000ff00000000U)) >>  8) |	\
153       (((guint64) (val) &						\
154 	(guint64) G_GINT64_CONSTANT (0x0000ff0000000000U)) >> 24) |	\
155       (((guint64) (val) &						\
156 	(guint64) G_GINT64_CONSTANT (0x00ff000000000000U)) >> 40) |	\
157       (((guint64) (val) &						\
158 	(guint64) G_GINT64_CONSTANT (0xff00000000000000U)) >> 56)))
159 
160 /* Arch specific stuff for speed
161  */
162 #if defined (__GNUC__) && (__GNUC__ >= 2) && defined (__OPTIMIZE__)
163 #  if defined (__i386__)
164 #    define GUINT16_SWAP_LE_BE_IA32(val) \
165        (__extension__						\
166 	({ register guint16 __v, __x = ((guint16) (val));	\
167 	   if (__builtin_constant_p (__x))			\
168 	     __v = GUINT16_SWAP_LE_BE_CONSTANT (__x);		\
169 	   else							\
170 	     __asm__ ("rorw $8, %w0"				\
171 		      : "=r" (__v)				\
172 		      : "0" (__x)				\
173 		      : "cc");					\
174 	    __v; }))
175 #    if !defined (__i486__) && !defined (__i586__) \
176 	&& !defined (__pentium__) && !defined (__i686__) \
177 	&& !defined (__pentiumpro__) && !defined (__pentium4__)
178 #       define GUINT32_SWAP_LE_BE_IA32(val) \
179 	  (__extension__					\
180 	   ({ register guint32 __v, __x = ((guint32) (val));	\
181 	      if (__builtin_constant_p (__x))			\
182 		__v = GUINT32_SWAP_LE_BE_CONSTANT (__x);	\
183 	      else						\
184 		__asm__ ("rorw $8, %w0\n\t"			\
185 			 "rorl $16, %0\n\t"			\
186 			 "rorw $8, %w0"				\
187 			 : "=r" (__v)				\
188 			 : "0" (__x)				\
189 			 : "cc");				\
190 	      __v; }))
191 #    else /* 486 and higher has bswap */
192 #       define GUINT32_SWAP_LE_BE_IA32(val) \
193 	  (__extension__					\
194 	   ({ register guint32 __v, __x = ((guint32) (val));	\
195 	      if (__builtin_constant_p (__x))			\
196 		__v = GUINT32_SWAP_LE_BE_CONSTANT (__x);	\
197 	      else						\
198 		__asm__ ("bswap %0"				\
199 			 : "=r" (__v)				\
200 			 : "0" (__x));				\
201 	      __v; }))
202 #    endif /* processor specific 32-bit stuff */
203 #    define GUINT64_SWAP_LE_BE_IA32(val) \
204        (__extension__							\
205 	({ union { guint64 __ll;					\
206 		   guint32 __l[2]; } __w, __r;				\
207 	   __w.__ll = ((guint64) (val));				\
208 	   if (__builtin_constant_p (__w.__ll))				\
209 	     __r.__ll = GUINT64_SWAP_LE_BE_CONSTANT (__w.__ll);		\
210 	   else								\
211 	     {								\
212 	       __r.__l[0] = GUINT32_SWAP_LE_BE (__w.__l[1]);		\
213 	       __r.__l[1] = GUINT32_SWAP_LE_BE (__w.__l[0]);		\
214 	     }								\
215 	   __r.__ll; }))
216      /* Possibly just use the constant version and let gcc figure it out? */
217 #    define GUINT16_SWAP_LE_BE(val) (GUINT16_SWAP_LE_BE_IA32 (val))
218 #    define GUINT32_SWAP_LE_BE(val) (GUINT32_SWAP_LE_BE_IA32 (val))
219 #    define GUINT64_SWAP_LE_BE(val) (GUINT64_SWAP_LE_BE_IA32 (val))
220 #  elif defined (__ia64__)
221 #    define GUINT16_SWAP_LE_BE_IA64(val) \
222        (__extension__						\
223 	({ register guint16 __v, __x = ((guint16) (val));	\
224 	   if (__builtin_constant_p (__x))			\
225 	     __v = GUINT16_SWAP_LE_BE_CONSTANT (__x);		\
226 	   else							\
227 	     __asm__ __volatile__ ("shl %0 = %1, 48 ;;"		\
228 				   "mux1 %0 = %0, @rev ;;"	\
229 				    : "=r" (__v)		\
230 				    : "r" (__x));		\
231 	    __v; }))
232 #    define GUINT32_SWAP_LE_BE_IA64(val) \
233        (__extension__						\
234 	 ({ register guint32 __v, __x = ((guint32) (val));	\
235 	    if (__builtin_constant_p (__x))			\
236 	      __v = GUINT32_SWAP_LE_BE_CONSTANT (__x);		\
237 	    else						\
238 	     __asm__ __volatile__ ("shl %0 = %1, 32 ;;"		\
239 				   "mux1 %0 = %0, @rev ;;"	\
240 				    : "=r" (__v)		\
241 				    : "r" (__x));		\
242 	    __v; }))
243 #    define GUINT64_SWAP_LE_BE_IA64(val) \
244        (__extension__						\
245 	({ register guint64 __v, __x = ((guint64) (val));	\
246 	   if (__builtin_constant_p (__x))			\
247 	     __v = GUINT64_SWAP_LE_BE_CONSTANT (__x);		\
248 	   else							\
249 	     __asm__ __volatile__ ("mux1 %0 = %1, @rev ;;"	\
250 				   : "=r" (__v)			\
251 				   : "r" (__x));		\
252 	   __v; }))
253 #    define GUINT16_SWAP_LE_BE(val) (GUINT16_SWAP_LE_BE_IA64 (val))
254 #    define GUINT32_SWAP_LE_BE(val) (GUINT32_SWAP_LE_BE_IA64 (val))
255 #    define GUINT64_SWAP_LE_BE(val) (GUINT64_SWAP_LE_BE_IA64 (val))
256 #  elif defined (__x86_64__)
257 #    define GUINT32_SWAP_LE_BE_X86_64(val) \
258        (__extension__						\
259 	 ({ register guint32 __v, __x = ((guint32) (val));	\
260 	    if (__builtin_constant_p (__x))			\
261 	      __v = GUINT32_SWAP_LE_BE_CONSTANT (__x);		\
262 	    else						\
263 	     __asm__ ("bswapl %0"				\
264 		      : "=r" (__v)				\
265 		      : "0" (__x));				\
266 	    __v; }))
267 #    define GUINT64_SWAP_LE_BE_X86_64(val) \
268        (__extension__						\
269 	({ register guint64 __v, __x = ((guint64) (val));	\
270 	   if (__builtin_constant_p (__x))			\
271 	     __v = GUINT64_SWAP_LE_BE_CONSTANT (__x);		\
272 	   else							\
273 	     __asm__ ("bswapq %0"				\
274 		      : "=r" (__v)				\
275 		      : "0" (__x));				\
276 	   __v; }))
277      /* gcc seems to figure out optimal code for this on its own */
278 #    define GUINT16_SWAP_LE_BE(val) (GUINT16_SWAP_LE_BE_CONSTANT (val))
279 #    define GUINT32_SWAP_LE_BE(val) (GUINT32_SWAP_LE_BE_X86_64 (val))
280 #    define GUINT64_SWAP_LE_BE(val) (GUINT64_SWAP_LE_BE_X86_64 (val))
281 #  else /* generic gcc */
282 #    define GUINT16_SWAP_LE_BE(val) (GUINT16_SWAP_LE_BE_CONSTANT (val))
283 #    define GUINT32_SWAP_LE_BE(val) (GUINT32_SWAP_LE_BE_CONSTANT (val))
284 #    define GUINT64_SWAP_LE_BE(val) (GUINT64_SWAP_LE_BE_CONSTANT (val))
285 #  endif
286 #else /* generic */
287 #  define GUINT16_SWAP_LE_BE(val) (GUINT16_SWAP_LE_BE_CONSTANT (val))
288 #  define GUINT32_SWAP_LE_BE(val) (GUINT32_SWAP_LE_BE_CONSTANT (val))
289 #  define GUINT64_SWAP_LE_BE(val) (GUINT64_SWAP_LE_BE_CONSTANT (val))
290 #endif /* generic */
291 
292 #define GUINT16_SWAP_LE_PDP(val)	((guint16) (val))
293 #define GUINT16_SWAP_BE_PDP(val)	(GUINT16_SWAP_LE_BE (val))
294 #define GUINT32_SWAP_LE_PDP(val)	((guint32) ( \
295     (((guint32) (val) & (guint32) 0x0000ffffU) << 16) | \
296     (((guint32) (val) & (guint32) 0xffff0000U) >> 16)))
297 #define GUINT32_SWAP_BE_PDP(val)	((guint32) ( \
298     (((guint32) (val) & (guint32) 0x00ff00ffU) << 8) | \
299     (((guint32) (val) & (guint32) 0xff00ff00U) >> 8)))
300 
301 /* The G*_TO_?E() macros are defined in glibconfig.h.
302  * The transformation is symmetric, so the FROM just maps to the TO.
303  */
304 #define GINT16_FROM_LE(val)	(GINT16_TO_LE (val))
305 #define GUINT16_FROM_LE(val)	(GUINT16_TO_LE (val))
306 #define GINT16_FROM_BE(val)	(GINT16_TO_BE (val))
307 #define GUINT16_FROM_BE(val)	(GUINT16_TO_BE (val))
308 #define GINT32_FROM_LE(val)	(GINT32_TO_LE (val))
309 #define GUINT32_FROM_LE(val)	(GUINT32_TO_LE (val))
310 #define GINT32_FROM_BE(val)	(GINT32_TO_BE (val))
311 #define GUINT32_FROM_BE(val)	(GUINT32_TO_BE (val))
312 
313 #define GINT64_FROM_LE(val)	(GINT64_TO_LE (val))
314 #define GUINT64_FROM_LE(val)	(GUINT64_TO_LE (val))
315 #define GINT64_FROM_BE(val)	(GINT64_TO_BE (val))
316 #define GUINT64_FROM_BE(val)	(GUINT64_TO_BE (val))
317 
318 #define GLONG_FROM_LE(val)	(GLONG_TO_LE (val))
319 #define GULONG_FROM_LE(val)	(GULONG_TO_LE (val))
320 #define GLONG_FROM_BE(val)	(GLONG_TO_BE (val))
321 #define GULONG_FROM_BE(val)	(GULONG_TO_BE (val))
322 
323 #define GINT_FROM_LE(val)	(GINT_TO_LE (val))
324 #define GUINT_FROM_LE(val)	(GUINT_TO_LE (val))
325 #define GINT_FROM_BE(val)	(GINT_TO_BE (val))
326 #define GUINT_FROM_BE(val)	(GUINT_TO_BE (val))
327 
328 
329 /* Portable versions of host-network order stuff
330  */
331 #define g_ntohl(val) (GUINT32_FROM_BE (val))
332 #define g_ntohs(val) (GUINT16_FROM_BE (val))
333 #define g_htonl(val) (GUINT32_TO_BE (val))
334 #define g_htons(val) (GUINT16_TO_BE (val))
335 
336 /* IEEE Standard 754 Single Precision Storage Format (gfloat):
337  *
338  *        31 30           23 22            0
339  * +--------+---------------+---------------+
340  * | s 1bit | e[30:23] 8bit | f[22:0] 23bit |
341  * +--------+---------------+---------------+
342  * B0------------------->B1------->B2-->B3-->
343  *
344  * IEEE Standard 754 Double Precision Storage Format (gdouble):
345  *
346  *        63 62            52 51            32   31            0
347  * +--------+----------------+----------------+ +---------------+
348  * | s 1bit | e[62:52] 11bit | f[51:32] 20bit | | f[31:0] 32bit |
349  * +--------+----------------+----------------+ +---------------+
350  * B0--------------->B1---------->B2--->B3---->  B4->B5->B6->B7->
351  */
352 /* subtract from biased_exponent to form base2 exponent (normal numbers) */
353 typedef union  _GDoubleIEEE754	GDoubleIEEE754;
354 typedef union  _GFloatIEEE754	GFloatIEEE754;
355 #define G_IEEE754_FLOAT_BIAS	(127)
356 #define G_IEEE754_DOUBLE_BIAS	(1023)
357 /* multiply with base2 exponent to get base10 exponent (normal numbers) */
358 #define G_LOG_2_BASE_10		(0.30102999566398119521)
359 #if G_BYTE_ORDER == G_LITTLE_ENDIAN
360 union _GFloatIEEE754
361 {
362   gfloat v_float;
363   struct {
364     guint mantissa : 23;
365     guint biased_exponent : 8;
366     guint sign : 1;
367   } mpn;
368 };
369 union _GDoubleIEEE754
370 {
371   gdouble v_double;
372   struct {
373     guint mantissa_low : 32;
374     guint mantissa_high : 20;
375     guint biased_exponent : 11;
376     guint sign : 1;
377   } mpn;
378 };
379 #elif G_BYTE_ORDER == G_BIG_ENDIAN
380 union _GFloatIEEE754
381 {
382   gfloat v_float;
383   struct {
384     guint sign : 1;
385     guint biased_exponent : 8;
386     guint mantissa : 23;
387   } mpn;
388 };
389 union _GDoubleIEEE754
390 {
391   gdouble v_double;
392   struct {
393     guint sign : 1;
394     guint biased_exponent : 11;
395     guint mantissa_high : 20;
396     guint mantissa_low : 32;
397   } mpn;
398 };
399 #else /* !G_LITTLE_ENDIAN && !G_BIG_ENDIAN */
400 #error unknown ENDIAN type
401 #endif /* !G_LITTLE_ENDIAN && !G_BIG_ENDIAN */
402 
403 typedef struct _GTimeVal                GTimeVal;
404 
405 struct _GTimeVal
406 {
407   glong tv_sec;
408   glong tv_usec;
409 };
410 
411 #endif
412 
413 G_END_DECLS
414 
415 /* We prefix variable declarations so they can
416  * properly get exported in windows dlls.
417  */
418 #ifndef GLIB_VAR
419 #  ifdef G_PLATFORM_WIN32
420 #    ifdef GLIB_STATIC_COMPILATION
421 #      define GLIB_VAR extern
422 #    else /* !GLIB_STATIC_COMPILATION */
423 #      ifdef GLIB_COMPILATION
424 #        ifdef DLL_EXPORT
425 #          define GLIB_VAR __declspec(dllexport)
426 #        else /* !DLL_EXPORT */
427 #          define GLIB_VAR extern
428 #        endif /* !DLL_EXPORT */
429 #      else /* !GLIB_COMPILATION */
430 #        define GLIB_VAR extern __declspec(dllimport)
431 #      endif /* !GLIB_COMPILATION */
432 #    endif /* !GLIB_STATIC_COMPILATION */
433 #  else /* !G_PLATFORM_WIN32 */
434 #    define GLIB_VAR extern
435 #  endif /* !G_PLATFORM_WIN32 */
436 #endif /* GLIB_VAR */
437 
438 #endif /* __G_TYPES_H__ */
439 
440