[HB] Assorted compiler macros
[framework/uifw/harfbuzz.git] / src / hb-private.h
1 /*
2  * Copyright (C) 2007,2008,2009  Red Hat, Inc.
3  *
4  *  This is part of HarfBuzz, an OpenType Layout engine library.
5  *
6  * Permission is hereby granted, without written agreement and without
7  * license or royalty fees, to use, copy, modify, and distribute this
8  * software and its documentation for any purpose, provided that the
9  * above copyright notice and the following two paragraphs appear in
10  * all copies of this software.
11  *
12  * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR
13  * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
14  * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN
15  * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
16  * DAMAGE.
17  *
18  * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
19  * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
20  * FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS
21  * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO
22  * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
23  *
24  * Red Hat Author(s): Behdad Esfahbod
25  */
26
27 #ifndef HB_PRIVATE_H
28 #define HB_PRIVATE_H
29
30 #if HAVE_CONFIG_H
31 #include "config.h"
32 #endif
33
34 #include <stdlib.h>
35 #include <stdio.h> /* XXX */
36 #include <string.h>
37 #include <assert.h>
38
39 #include "hb-common.h"
40
41 #include <glib.h>
42
43 /* Macros to convert to/from BigEndian */
44 #define hb_be_uint8
45 #define hb_be_int8
46 #define hb_be_uint16    GUINT16_TO_BE
47 #define hb_be_int16     GINT16_TO_BE
48 #define hb_be_uint32    GUINT32_TO_BE
49 #define hb_be_int32     GINT32_TO_BE
50 #define hb_be_uint64    GUINT64_TO_BE
51 #define hb_be_int64     GINT64_TO_BE
52
53 /* Basics */
54
55 #undef MIN
56 #define MIN(a,b) ((a) < (b) ? (a) : (b))
57
58 #ifndef HB_INTERNAL
59 # define HB_INTERNAL
60 #endif
61
62 #ifndef NULL
63 # define NULL ((void *)0)
64 #endif
65
66 #ifndef FALSE
67 # define FALSE 0
68 #endif
69
70 #ifndef TRUE
71 # define TRUE 1
72 #endif
73
74 #define HB_STMT_START do
75 #define HB_STMT_END   while (0)
76
77 #define _ASSERT_STATIC1(_line, _cond) typedef int _static_assert_on_line_##_line##_failed[(_cond)?1:-1]
78 #define _ASSERT_STATIC0(_line, _cond) _ASSERT_STATIC1 (_line, (_cond))
79 #define ASSERT_STATIC(_cond) _ASSERT_STATIC0 (__LINE__, (_cond))
80
81 #define ASSERT_SIZE(_type, _size) ASSERT_STATIC (sizeof (_type) == (_size))
82
83
84 #if defined(__GNUC__) && (__GNUC__ > 2) && defined(__OPTIMIZE__)
85 #define _CAIRO_BOOLEAN_EXPR(expr)                   \
86  __extension__ ({                               \
87    int _cairo_boolean_var_;                         \
88    if (expr)                                    \
89       _cairo_boolean_var_ = 1;                      \
90    else                                         \
91       _cairo_boolean_var_ = 0;                      \
92    _cairo_boolean_var_;                             \
93 })
94 #define HB_LIKELY(expr) (__builtin_expect (_CAIRO_BOOLEAN_EXPR(expr), 1))
95 #define HB_UNLIKELY(expr) (__builtin_expect (_CAIRO_BOOLEAN_EXPR(expr), 0))
96 #else
97 #define HB_LIKELY(expr) (expr)
98 #define HB_UNLIKELY(expr) (expr)
99 #endif
100
101 #ifndef __GNUC__
102 #undef __attribute__
103 #define __attribute__(x)
104 #endif
105
106 #if __GNUC__ >= 3
107 #define HB_GNUC_UNUSED  __attribute__((unused))
108 #define HB_GNUC_PURE    __attribute__((pure))
109 #define HB_GNUC_CONST   __attribute__((const))
110 #else
111 #define HB_GNUC_UNUSED
112 #define HB_GNUC_PURE
113 #define HB_GNUC_CONST
114 #endif
115
116
117 #if (defined(__WIN32__) && !defined(__WINE__)) || defined(_MSC_VER)
118 #define snprintf _snprintf
119 #endif
120
121 #ifdef _MSC_VER
122 #undef inline
123 #define inline __inline
124 #endif
125
126 #ifdef __STRICT_ANSI__
127 #undef inline
128 #define inline __inline__
129 #endif
130
131
132 /* Return the number of 1 bits in mask.
133  *
134  * GCC 3.4 supports a "population count" builtin, which on many targets is
135  * implemented with a single instruction. There is a fallback definition
136  * in libgcc in case a target does not have one, which should be just as
137  * good as the open-coded solution below, (which is "HACKMEM 169").
138  */
139 static HB_GNUC_UNUSED inline unsigned int
140 _hb_popcount32 (uint32_t mask)
141 {
142 #if __GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 4)
143     return __builtin_popcount (mask);
144 #else
145     register int y;
146
147     y = (mask >> 1) &033333333333;
148     y = mask - y - ((y >>1) & 033333333333);
149     return (((y + (y >> 3)) & 030707070707) % 077);
150 #endif
151 }
152
153
154
155 #include "hb-object-private.h"
156
157 #endif /* HB_PRIVATE_H */