hb-private.hh revision 153142dac8dd9abaf164bb88af07c600c17fc3a1
164aef3a54999496fd1de4f5aa5b019e4c03b3836Behdad Esfahbod/*
22409d5f8d7dd8b535ce5ea29e933f7db27d33793Behdad Esfahbod * Copyright © 2007,2008,2009  Red Hat, Inc.
32409d5f8d7dd8b535ce5ea29e933f7db27d33793Behdad Esfahbod * Copyright © 2011  Google, Inc.
464aef3a54999496fd1de4f5aa5b019e4c03b3836Behdad Esfahbod *
5c755cb3e3ac55156d0d2ec05adea7a650b97cc41Behdad Esfahbod *  This is part of HarfBuzz, a text shaping library.
664aef3a54999496fd1de4f5aa5b019e4c03b3836Behdad Esfahbod *
764aef3a54999496fd1de4f5aa5b019e4c03b3836Behdad Esfahbod * Permission is hereby granted, without written agreement and without
864aef3a54999496fd1de4f5aa5b019e4c03b3836Behdad Esfahbod * license or royalty fees, to use, copy, modify, and distribute this
964aef3a54999496fd1de4f5aa5b019e4c03b3836Behdad Esfahbod * software and its documentation for any purpose, provided that the
1064aef3a54999496fd1de4f5aa5b019e4c03b3836Behdad Esfahbod * above copyright notice and the following two paragraphs appear in
1164aef3a54999496fd1de4f5aa5b019e4c03b3836Behdad Esfahbod * all copies of this software.
1264aef3a54999496fd1de4f5aa5b019e4c03b3836Behdad Esfahbod *
1364aef3a54999496fd1de4f5aa5b019e4c03b3836Behdad Esfahbod * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR
1464aef3a54999496fd1de4f5aa5b019e4c03b3836Behdad Esfahbod * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
1564aef3a54999496fd1de4f5aa5b019e4c03b3836Behdad Esfahbod * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN
1664aef3a54999496fd1de4f5aa5b019e4c03b3836Behdad Esfahbod * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
1764aef3a54999496fd1de4f5aa5b019e4c03b3836Behdad Esfahbod * DAMAGE.
1864aef3a54999496fd1de4f5aa5b019e4c03b3836Behdad Esfahbod *
1964aef3a54999496fd1de4f5aa5b019e4c03b3836Behdad Esfahbod * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
2064aef3a54999496fd1de4f5aa5b019e4c03b3836Behdad Esfahbod * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
2164aef3a54999496fd1de4f5aa5b019e4c03b3836Behdad Esfahbod * FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS
2264aef3a54999496fd1de4f5aa5b019e4c03b3836Behdad Esfahbod * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO
2364aef3a54999496fd1de4f5aa5b019e4c03b3836Behdad Esfahbod * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
2464aef3a54999496fd1de4f5aa5b019e4c03b3836Behdad Esfahbod *
2564aef3a54999496fd1de4f5aa5b019e4c03b3836Behdad Esfahbod * Red Hat Author(s): Behdad Esfahbod
262409d5f8d7dd8b535ce5ea29e933f7db27d33793Behdad Esfahbod * Google Author(s): Behdad Esfahbod
2764aef3a54999496fd1de4f5aa5b019e4c03b3836Behdad Esfahbod */
2864aef3a54999496fd1de4f5aa5b019e4c03b3836Behdad Esfahbod
29c57d454accff66e5f2c58006e8fb40bc020b6182Behdad Esfahbod#ifndef HB_PRIVATE_HH
30c57d454accff66e5f2c58006e8fb40bc020b6182Behdad Esfahbod#define HB_PRIVATE_HH
315b3f7702a64fe0513d08a67bdb72704e46fd7cd4Behdad Esfahbod
32df66028781a7609a515980e64396e6f1044d764aBehdad Esfahbod#if HAVE_CONFIG_H
33df66028781a7609a515980e64396e6f1044d764aBehdad Esfahbod#include "config.h"
34df66028781a7609a515980e64396e6f1044d764aBehdad Esfahbod#endif
35c7d457aa3ae7138630f52ca7263f663a3ea284c0Behdad Esfahbod
36bc200457430c083914a64bf4b056153506749610Behdad Esfahbod#include "hb-common.h"
37b28815c1f6e46d38471cacbc31248ca6fda8c4d1Behdad Esfahbod
38c7d457aa3ae7138630f52ca7263f663a3ea284c0Behdad Esfahbod#include <stdlib.h>
39f0954d1e08ae288eda9904e17108fc73f48c0b98Behdad Esfahbod#include <string.h>
40f0954d1e08ae288eda9904e17108fc73f48c0b98Behdad Esfahbod#include <assert.h>
411d52151a19ed8ec776276aac5cc6ce3769e0d947Behdad Esfahbod
421d52151a19ed8ec776276aac5cc6ce3769e0d947Behdad Esfahbod/* We only use these two for debug output.  However, the debug code is
431d52151a19ed8ec776276aac5cc6ce3769e0d947Behdad Esfahbod * always seen by the compiler (and optimized out in non-debug builds.
441d52151a19ed8ec776276aac5cc6ce3769e0d947Behdad Esfahbod * If including these becomes a problem, we can start thinking about
451d52151a19ed8ec776276aac5cc6ce3769e0d947Behdad Esfahbod * someway around that. */
467acb389569cf99c6bae9db31a8ed7c7007fbb566Behdad Esfahbod#include <stdio.h>
477acb389569cf99c6bae9db31a8ed7c7007fbb566Behdad Esfahbod#include <errno.h>
48c7d457aa3ae7138630f52ca7263f663a3ea284c0Behdad Esfahbod
49acdba3f90b232fc12fcb200dca2584481b339118Behdad EsfahbodHB_BEGIN_DECLS
50acdba3f90b232fc12fcb200dca2584481b339118Behdad Esfahbod
51bc200457430c083914a64bf4b056153506749610Behdad Esfahbod
52bc200457430c083914a64bf4b056153506749610Behdad Esfahbod/* Essentials */
53bc200457430c083914a64bf4b056153506749610Behdad Esfahbod
54bc200457430c083914a64bf4b056153506749610Behdad Esfahbod#ifndef NULL
55bc200457430c083914a64bf4b056153506749610Behdad Esfahbod# define NULL ((void *) 0)
56bc200457430c083914a64bf4b056153506749610Behdad Esfahbod#endif
57bc200457430c083914a64bf4b056153506749610Behdad Esfahbod
58bc200457430c083914a64bf4b056153506749610Behdad Esfahbod#undef FALSE
59bc200457430c083914a64bf4b056153506749610Behdad Esfahbod#define FALSE 0
60bc200457430c083914a64bf4b056153506749610Behdad Esfahbod
61bc200457430c083914a64bf4b056153506749610Behdad Esfahbod#undef TRUE
62bc200457430c083914a64bf4b056153506749610Behdad Esfahbod#define TRUE 1
63df66028781a7609a515980e64396e6f1044d764aBehdad Esfahbod
64a794ebf4be9896393f4badf02905a4007981a588Behdad Esfahbod
65c7d457aa3ae7138630f52ca7263f663a3ea284c0Behdad Esfahbod/* Basics */
66c7d457aa3ae7138630f52ca7263f663a3ea284c0Behdad Esfahbod
67153142dac8dd9abaf164bb88af07c600c17fc3a1Behdad EsfahbodHB_END_DECLS
68153142dac8dd9abaf164bb88af07c600c17fc3a1Behdad Esfahbod
69c7d457aa3ae7138630f52ca7263f663a3ea284c0Behdad Esfahbod#undef MIN
70153142dac8dd9abaf164bb88af07c600c17fc3a1Behdad Esfahbodtemplate <typename Type> static inline Type MIN (const Type &a, const Type &b) { return a < b ? a : b; }
717586089c6fa8185cad8387869d3703c637e5cbb1Behdad Esfahbod
728a3511ac6c795226699c2b36e03401ecdf88f5f8Behdad Esfahbod#undef MAX
73153142dac8dd9abaf164bb88af07c600c17fc3a1Behdad Esfahbodtemplate <typename Type> static inline Type MAX (const Type &a, const Type &b) { return a > b ? a : b; }
74153142dac8dd9abaf164bb88af07c600c17fc3a1Behdad Esfahbod
75153142dac8dd9abaf164bb88af07c600c17fc3a1Behdad EsfahbodHB_BEGIN_DECLS
768a3511ac6c795226699c2b36e03401ecdf88f5f8Behdad Esfahbod
774591753ad4b5ec0224e3f1befdfe4fc5f6075562Behdad Esfahbod#undef  ARRAY_LENGTH
784591753ad4b5ec0224e3f1befdfe4fc5f6075562Behdad Esfahbod#define ARRAY_LENGTH(__array) ((signed int) (sizeof (__array) / sizeof (__array[0])))
7912360f7c159826ae72271b34486dee59d96aa8caBehdad Esfahbod
8035a7383c6138fd705560f0d4bb30659cbd1ab64cBehdad Esfahbod#define HB_STMT_START do
8135a7383c6138fd705560f0d4bb30659cbd1ab64cBehdad Esfahbod#define HB_STMT_END   while (0)
825b3f7702a64fe0513d08a67bdb72704e46fd7cd4Behdad Esfahbod
83303fe62824d4e99df554b6bfaacba05d068522fbBehdad Esfahbod#define _ASSERT_STATIC1(_line, _cond) typedef int _static_assert_on_line_##_line##_failed[(_cond)?1:-1]
84303fe62824d4e99df554b6bfaacba05d068522fbBehdad Esfahbod#define _ASSERT_STATIC0(_line, _cond) _ASSERT_STATIC1 (_line, (_cond))
85303fe62824d4e99df554b6bfaacba05d068522fbBehdad Esfahbod#define ASSERT_STATIC(_cond) _ASSERT_STATIC0 (__LINE__, (_cond))
86303fe62824d4e99df554b6bfaacba05d068522fbBehdad Esfahbod
87dcb7026f33cbcdf60e9b7fcdd44c64cc08702c74Behdad Esfahbod#define ASSERT_STATIC_EXPR(_cond) ((void) sizeof (char[(_cond) ? 1 : -1]))
88dcb7026f33cbcdf60e9b7fcdd44c64cc08702c74Behdad Esfahbod
89bc200457430c083914a64bf4b056153506749610Behdad Esfahbod
906fd5364bdc3a2b459175377e9e16c86cff054232Behdad Esfahbod/* Lets assert int types.  Saves trouble down the road. */
916fd5364bdc3a2b459175377e9e16c86cff054232Behdad Esfahbod
926fd5364bdc3a2b459175377e9e16c86cff054232Behdad EsfahbodASSERT_STATIC (sizeof (int8_t) == 1);
936fd5364bdc3a2b459175377e9e16c86cff054232Behdad EsfahbodASSERT_STATIC (sizeof (uint8_t) == 1);
946fd5364bdc3a2b459175377e9e16c86cff054232Behdad EsfahbodASSERT_STATIC (sizeof (int16_t) == 2);
956fd5364bdc3a2b459175377e9e16c86cff054232Behdad EsfahbodASSERT_STATIC (sizeof (uint16_t) == 2);
966fd5364bdc3a2b459175377e9e16c86cff054232Behdad EsfahbodASSERT_STATIC (sizeof (int32_t) == 4);
976fd5364bdc3a2b459175377e9e16c86cff054232Behdad EsfahbodASSERT_STATIC (sizeof (uint32_t) == 4);
986fd5364bdc3a2b459175377e9e16c86cff054232Behdad EsfahbodASSERT_STATIC (sizeof (int64_t) == 8);
996fd5364bdc3a2b459175377e9e16c86cff054232Behdad EsfahbodASSERT_STATIC (sizeof (uint64_t) == 8);
1006fd5364bdc3a2b459175377e9e16c86cff054232Behdad Esfahbod
101b13640de6992de9ee9d07e3581c33b8181b70ff9Behdad EsfahbodASSERT_STATIC (sizeof (hb_codepoint_t) == 4);
102b13640de6992de9ee9d07e3581c33b8181b70ff9Behdad EsfahbodASSERT_STATIC (sizeof (hb_position_t) == 4);
103b13640de6992de9ee9d07e3581c33b8181b70ff9Behdad EsfahbodASSERT_STATIC (sizeof (hb_mask_t) == 4);
104ae9eeaff9300f3bb7bed588c5478e8e5461b3df0Behdad EsfahbodASSERT_STATIC (sizeof (hb_var_int_t) == 4);
1056fd5364bdc3a2b459175377e9e16c86cff054232Behdad Esfahbod
106bc200457430c083914a64bf4b056153506749610Behdad Esfahbod/* Misc */
107bc200457430c083914a64bf4b056153506749610Behdad Esfahbod
108c7d457aa3ae7138630f52ca7263f663a3ea284c0Behdad Esfahbod
109df66028781a7609a515980e64396e6f1044d764aBehdad Esfahbod#if defined(__GNUC__) && (__GNUC__ > 2) && defined(__OPTIMIZE__)
110494d28ad988e505c1c45cc35a7ec6b880cfec5fcBehdad Esfahbod#define _HB_BOOLEAN_EXPR(expr) ((expr) ? 1 : 0)
11164d3fc8d0dada673245cc8c0b1c12cd849b30997Behdad Esfahbod#define likely(expr) (__builtin_expect (_HB_BOOLEAN_EXPR(expr), 1))
11264d3fc8d0dada673245cc8c0b1c12cd849b30997Behdad Esfahbod#define unlikely(expr) (__builtin_expect (_HB_BOOLEAN_EXPR(expr), 0))
113df66028781a7609a515980e64396e6f1044d764aBehdad Esfahbod#else
11464d3fc8d0dada673245cc8c0b1c12cd849b30997Behdad Esfahbod#define likely(expr) (expr)
11564d3fc8d0dada673245cc8c0b1c12cd849b30997Behdad Esfahbod#define unlikely(expr) (expr)
116df66028781a7609a515980e64396e6f1044d764aBehdad Esfahbod#endif
117df66028781a7609a515980e64396e6f1044d764aBehdad Esfahbod
118df66028781a7609a515980e64396e6f1044d764aBehdad Esfahbod#ifndef __GNUC__
119df66028781a7609a515980e64396e6f1044d764aBehdad Esfahbod#undef __attribute__
120df66028781a7609a515980e64396e6f1044d764aBehdad Esfahbod#define __attribute__(x)
121df66028781a7609a515980e64396e6f1044d764aBehdad Esfahbod#endif
122df66028781a7609a515980e64396e6f1044d764aBehdad Esfahbod
123df66028781a7609a515980e64396e6f1044d764aBehdad Esfahbod#if __GNUC__ >= 3
12433d13fdda99acaeffa9600737e8870278d053ebeBehdad Esfahbod#define HB_PURE_FUNC	__attribute__((pure))
12533d13fdda99acaeffa9600737e8870278d053ebeBehdad Esfahbod#define HB_CONST_FUNC	__attribute__((const))
126df66028781a7609a515980e64396e6f1044d764aBehdad Esfahbod#else
12733d13fdda99acaeffa9600737e8870278d053ebeBehdad Esfahbod#define HB_PURE_FUNC
12833d13fdda99acaeffa9600737e8870278d053ebeBehdad Esfahbod#define HB_CONST_FUNC
129df66028781a7609a515980e64396e6f1044d764aBehdad Esfahbod#endif
130bc7830e4259755ce7549025c6f5bf750e78c2ff1Behdad Esfahbod#if __GNUC__ >= 4
13133d13fdda99acaeffa9600737e8870278d053ebeBehdad Esfahbod#define HB_UNUSED	__attribute__((unused))
132bc7830e4259755ce7549025c6f5bf750e78c2ff1Behdad Esfahbod#else
13333d13fdda99acaeffa9600737e8870278d053ebeBehdad Esfahbod#define HB_UNUSED
134bc7830e4259755ce7549025c6f5bf750e78c2ff1Behdad Esfahbod#endif
135df66028781a7609a515980e64396e6f1044d764aBehdad Esfahbod
136eee8598d75d3af692d9ececf7e8ac458e892ba9eBehdad Esfahbod#ifndef HB_INTERNAL
137357d0f29c431b842179ee4b56c21bcc402178ce7Behdad Esfahbod# define HB_INTERNAL __attribute__((__visibility__("hidden")))
138eee8598d75d3af692d9ececf7e8ac458e892ba9eBehdad Esfahbod#endif
139eee8598d75d3af692d9ececf7e8ac458e892ba9eBehdad Esfahbod
140df66028781a7609a515980e64396e6f1044d764aBehdad Esfahbod
141df66028781a7609a515980e64396e6f1044d764aBehdad Esfahbod#if (defined(__WIN32__) && !defined(__WINE__)) || defined(_MSC_VER)
142df66028781a7609a515980e64396e6f1044d764aBehdad Esfahbod#define snprintf _snprintf
143df66028781a7609a515980e64396e6f1044d764aBehdad Esfahbod#endif
144df66028781a7609a515980e64396e6f1044d764aBehdad Esfahbod
145df66028781a7609a515980e64396e6f1044d764aBehdad Esfahbod#ifdef _MSC_VER
146df66028781a7609a515980e64396e6f1044d764aBehdad Esfahbod#undef inline
147df66028781a7609a515980e64396e6f1044d764aBehdad Esfahbod#define inline __inline
148df66028781a7609a515980e64396e6f1044d764aBehdad Esfahbod#endif
149df66028781a7609a515980e64396e6f1044d764aBehdad Esfahbod
150df66028781a7609a515980e64396e6f1044d764aBehdad Esfahbod#ifdef __STRICT_ANSI__
151df66028781a7609a515980e64396e6f1044d764aBehdad Esfahbod#undef inline
152df66028781a7609a515980e64396e6f1044d764aBehdad Esfahbod#define inline __inline__
153df66028781a7609a515980e64396e6f1044d764aBehdad Esfahbod#endif
154df66028781a7609a515980e64396e6f1044d764aBehdad Esfahbod
155df66028781a7609a515980e64396e6f1044d764aBehdad Esfahbod
1567d3a126334f8e6f6441561c1bb592bd3fa7a2c5cBehdad Esfahbod#if __GNUC__ >= 3
1577d3a126334f8e6f6441561c1bb592bd3fa7a2c5cBehdad Esfahbod#define HB_FUNC __PRETTY_FUNCTION__
1587d3a126334f8e6f6441561c1bb592bd3fa7a2c5cBehdad Esfahbod#elif defined(_MSC_VER)
1597d3a126334f8e6f6441561c1bb592bd3fa7a2c5cBehdad Esfahbod#define HB_FUNC __FUNCSIG__
1607d3a126334f8e6f6441561c1bb592bd3fa7a2c5cBehdad Esfahbod#else
1617d3a126334f8e6f6441561c1bb592bd3fa7a2c5cBehdad Esfahbod#define HB_FUNC __func__
1627d3a126334f8e6f6441561c1bb592bd3fa7a2c5cBehdad Esfahbod#endif
1637d3a126334f8e6f6441561c1bb592bd3fa7a2c5cBehdad Esfahbod
1647d3a126334f8e6f6441561c1bb592bd3fa7a2c5cBehdad Esfahbod
1659b6023338530a2dbb8214eb4391ef3e8372f3892Behdad Esfahbod/* Return the number of 1 bits in mask. */
16697e7f8f305c47caf2968a9da3b8407825547286dBehdad Esfahbodstatic inline HB_CONST_FUNC unsigned int
167c7d457aa3ae7138630f52ca7263f663a3ea284c0Behdad Esfahbod_hb_popcount32 (uint32_t mask)
168c7d457aa3ae7138630f52ca7263f663a3ea284c0Behdad Esfahbod{
169c7d457aa3ae7138630f52ca7263f663a3ea284c0Behdad Esfahbod#if __GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 4)
1709b6023338530a2dbb8214eb4391ef3e8372f3892Behdad Esfahbod  return __builtin_popcount (mask);
171c7d457aa3ae7138630f52ca7263f663a3ea284c0Behdad Esfahbod#else
1729b6023338530a2dbb8214eb4391ef3e8372f3892Behdad Esfahbod  /* "HACKMEM 169" */
1739b6023338530a2dbb8214eb4391ef3e8372f3892Behdad Esfahbod  register uint32_t y;
1749b6023338530a2dbb8214eb4391ef3e8372f3892Behdad Esfahbod  y = (mask >> 1) &033333333333;
1759b6023338530a2dbb8214eb4391ef3e8372f3892Behdad Esfahbod  y = mask - y - ((y >>1) & 033333333333);
1769b6023338530a2dbb8214eb4391ef3e8372f3892Behdad Esfahbod  return (((y + (y >> 3)) & 030707070707) % 077);
177c7d457aa3ae7138630f52ca7263f663a3ea284c0Behdad Esfahbod#endif
178c7d457aa3ae7138630f52ca7263f663a3ea284c0Behdad Esfahbod}
179c7d457aa3ae7138630f52ca7263f663a3ea284c0Behdad Esfahbod
1809b6023338530a2dbb8214eb4391ef3e8372f3892Behdad Esfahbod/* Returns the number of bits needed to store number */
1819b6023338530a2dbb8214eb4391ef3e8372f3892Behdad Esfahbodstatic inline HB_CONST_FUNC unsigned int
1829b6023338530a2dbb8214eb4391ef3e8372f3892Behdad Esfahbod_hb_bit_storage (unsigned int number)
1839b6023338530a2dbb8214eb4391ef3e8372f3892Behdad Esfahbod{
1849b6023338530a2dbb8214eb4391ef3e8372f3892Behdad Esfahbod#if defined(__GNUC__) && (__GNUC__ >= 4) && defined(__OPTIMIZE__)
185f7acd8df5146155b51d6f50aeb04f54f3030c1c3Behdad Esfahbod  return likely (number) ? (sizeof (unsigned int) * 8 - __builtin_clz (number)) : 0;
1869b6023338530a2dbb8214eb4391ef3e8372f3892Behdad Esfahbod#else
1879b6023338530a2dbb8214eb4391ef3e8372f3892Behdad Esfahbod  register unsigned int n_bits = 0;
1889b6023338530a2dbb8214eb4391ef3e8372f3892Behdad Esfahbod  while (number) {
1899b6023338530a2dbb8214eb4391ef3e8372f3892Behdad Esfahbod    n_bits++;
1909b6023338530a2dbb8214eb4391ef3e8372f3892Behdad Esfahbod    number >>= 1;
1919b6023338530a2dbb8214eb4391ef3e8372f3892Behdad Esfahbod  }
1929b6023338530a2dbb8214eb4391ef3e8372f3892Behdad Esfahbod  return n_bits;
1939b6023338530a2dbb8214eb4391ef3e8372f3892Behdad Esfahbod#endif
1949b6023338530a2dbb8214eb4391ef3e8372f3892Behdad Esfahbod}
195df66028781a7609a515980e64396e6f1044d764aBehdad Esfahbod
196f7acd8df5146155b51d6f50aeb04f54f3030c1c3Behdad Esfahbod/* Returns the number of zero bits in the least significant side of number */
197f7acd8df5146155b51d6f50aeb04f54f3030c1c3Behdad Esfahbodstatic inline HB_CONST_FUNC unsigned int
198f7acd8df5146155b51d6f50aeb04f54f3030c1c3Behdad Esfahbod_hb_ctz (unsigned int number)
199f7acd8df5146155b51d6f50aeb04f54f3030c1c3Behdad Esfahbod{
200f7acd8df5146155b51d6f50aeb04f54f3030c1c3Behdad Esfahbod#if defined(__GNUC__) && (__GNUC__ >= 4) && defined(__OPTIMIZE__)
201f7acd8df5146155b51d6f50aeb04f54f3030c1c3Behdad Esfahbod  return likely (number) ? __builtin_ctz (number) : 0;
202f7acd8df5146155b51d6f50aeb04f54f3030c1c3Behdad Esfahbod#else
203f7acd8df5146155b51d6f50aeb04f54f3030c1c3Behdad Esfahbod  register unsigned int n_bits = 0;
204f7acd8df5146155b51d6f50aeb04f54f3030c1c3Behdad Esfahbod  if (unlikely (!number)) return 0;
205f7acd8df5146155b51d6f50aeb04f54f3030c1c3Behdad Esfahbod  while (!(number & 1)) {
206f7acd8df5146155b51d6f50aeb04f54f3030c1c3Behdad Esfahbod    n_bits++;
207f7acd8df5146155b51d6f50aeb04f54f3030c1c3Behdad Esfahbod    number >>= 1;
208f7acd8df5146155b51d6f50aeb04f54f3030c1c3Behdad Esfahbod  }
209f7acd8df5146155b51d6f50aeb04f54f3030c1c3Behdad Esfahbod  return n_bits;
210f7acd8df5146155b51d6f50aeb04f54f3030c1c3Behdad Esfahbod#endif
211f7acd8df5146155b51d6f50aeb04f54f3030c1c3Behdad Esfahbod}
212f7acd8df5146155b51d6f50aeb04f54f3030c1c3Behdad Esfahbod
2138f08c3275040870a645ef034a38d30c05c619f21Behdad Esfahbod/* Type of bsearch() / qsort() compare function */
2148f08c3275040870a645ef034a38d30c05c619f21Behdad Esfahbodtypedef int (*hb_compare_func_t) (const void *, const void *);
2158f08c3275040870a645ef034a38d30c05c619f21Behdad Esfahbod
2168f08c3275040870a645ef034a38d30c05c619f21Behdad Esfahbod
217f60f2166c48d07f556ff83f04e95181946eb03dfBehdad Esfahbod/* We need external help for these */
218f60f2166c48d07f556ff83f04e95181946eb03dfBehdad Esfahbod
219f60f2166c48d07f556ff83f04e95181946eb03dfBehdad Esfahbod#ifdef HAVE_GLIB
220f60f2166c48d07f556ff83f04e95181946eb03dfBehdad Esfahbod
221f60f2166c48d07f556ff83f04e95181946eb03dfBehdad Esfahbod#include <glib.h>
222f60f2166c48d07f556ff83f04e95181946eb03dfBehdad Esfahbod
2233e8bdbf9414291da5cf61213d5f4275c1ae23ae5Behdad Esfahbodtypedef volatile int hb_atomic_int_t;
224f60f2166c48d07f556ff83f04e95181946eb03dfBehdad Esfahbod#define hb_atomic_int_fetch_and_add(AI, V)	g_atomic_int_exchange_and_add (&(AI), V)
225f60f2166c48d07f556ff83f04e95181946eb03dfBehdad Esfahbod#define hb_atomic_int_get(AI)			g_atomic_int_get (&(AI))
226f60f2166c48d07f556ff83f04e95181946eb03dfBehdad Esfahbod#define hb_atomic_int_set(AI, V)		g_atomic_int_set (&(AI), V)
227f60f2166c48d07f556ff83f04e95181946eb03dfBehdad Esfahbod
228f60f2166c48d07f556ff83f04e95181946eb03dfBehdad Esfahbodtypedef GStaticMutex hb_mutex_t;
229f60f2166c48d07f556ff83f04e95181946eb03dfBehdad Esfahbod#define HB_MUTEX_INIT			G_STATIC_MUTEX_INIT
230f60f2166c48d07f556ff83f04e95181946eb03dfBehdad Esfahbod#define hb_mutex_init(M)		g_static_mutex_init (&M)
231f60f2166c48d07f556ff83f04e95181946eb03dfBehdad Esfahbod#define hb_mutex_lock(M)		g_static_mutex_lock (&M)
232f60f2166c48d07f556ff83f04e95181946eb03dfBehdad Esfahbod#define hb_mutex_trylock(M)		g_static_mutex_trylock (&M)
233f60f2166c48d07f556ff83f04e95181946eb03dfBehdad Esfahbod#define hb_mutex_unlock(M)		g_static_mutex_unlock (&M)
234f60f2166c48d07f556ff83f04e95181946eb03dfBehdad Esfahbod
235f60f2166c48d07f556ff83f04e95181946eb03dfBehdad Esfahbod#else
236f60f2166c48d07f556ff83f04e95181946eb03dfBehdad Esfahbod
237f60f2166c48d07f556ff83f04e95181946eb03dfBehdad Esfahbod#ifdef _MSC_VER
238ae9eeaff9300f3bb7bed588c5478e8e5461b3df0Behdad Esfahbod#define _HB__STR2__(x) #x
239ae9eeaff9300f3bb7bed588c5478e8e5461b3df0Behdad Esfahbod#define _HB__STR1__(x) _HB__STR2__(x)
240ae9eeaff9300f3bb7bed588c5478e8e5461b3df0Behdad Esfahbod#define _HB__LOC__ __FILE__ "("_HB__STR1__(__LINE__)") : Warning Msg: "
24182438c6ad5f60b6afc75c48cef72e18bef532468Bradley Grainger#pragma message(_HB__LOC__"Could not find any system to define platform macros, library will NOT be thread-safe")
242f60f2166c48d07f556ff83f04e95181946eb03dfBehdad Esfahbod#else
243f60f2166c48d07f556ff83f04e95181946eb03dfBehdad Esfahbod#warning "Could not find any system to define platform macros, library will NOT be thread-safe"
244f60f2166c48d07f556ff83f04e95181946eb03dfBehdad Esfahbod#endif
245f60f2166c48d07f556ff83f04e95181946eb03dfBehdad Esfahbod
2463e8bdbf9414291da5cf61213d5f4275c1ae23ae5Behdad Esfahbodtypedef volatile int hb_atomic_int_t;
247f60f2166c48d07f556ff83f04e95181946eb03dfBehdad Esfahbod#define hb_atomic_int_fetch_and_add(AI, V)	((AI) += (V), (AI) - (V))
248f60f2166c48d07f556ff83f04e95181946eb03dfBehdad Esfahbod#define hb_atomic_int_get(AI)			(AI)
249ffff7dc44cb2a35a60f92831165e9d3c3a61ce19Behdad Esfahbod#define hb_atomic_int_set(AI, V)		HB_STMT_START { (AI) = (V); } HB_STMT_END
250f60f2166c48d07f556ff83f04e95181946eb03dfBehdad Esfahbod
2513e8bdbf9414291da5cf61213d5f4275c1ae23ae5Behdad Esfahbodtypedef volatile int hb_mutex_t;
252ffff7dc44cb2a35a60f92831165e9d3c3a61ce19Behdad Esfahbod#define HB_MUTEX_INIT				0
253ffff7dc44cb2a35a60f92831165e9d3c3a61ce19Behdad Esfahbod#define hb_mutex_init(M)			HB_STMT_START { (M) = 0; } HB_STMT_END
254ffff7dc44cb2a35a60f92831165e9d3c3a61ce19Behdad Esfahbod#define hb_mutex_lock(M)			HB_STMT_START { (M) = 1; } HB_STMT_END
255ffff7dc44cb2a35a60f92831165e9d3c3a61ce19Behdad Esfahbod#define hb_mutex_trylock(M)			((M) = 1, 1)
256ffff7dc44cb2a35a60f92831165e9d3c3a61ce19Behdad Esfahbod#define hb_mutex_unlock(M)			HB_STMT_START { (M) = 0; } HB_STMT_END
257f60f2166c48d07f556ff83f04e95181946eb03dfBehdad Esfahbod
258f60f2166c48d07f556ff83f04e95181946eb03dfBehdad Esfahbod#endif
259f60f2166c48d07f556ff83f04e95181946eb03dfBehdad Esfahbod
260f60f2166c48d07f556ff83f04e95181946eb03dfBehdad Esfahbod
261a9f24c802956d57180d71b83e96a0fb81197df4aBehdad Esfahbod/* A reference count */
262a9f24c802956d57180d71b83e96a0fb81197df4aBehdad Esfahbod
263a9f24c802956d57180d71b83e96a0fb81197df4aBehdad Esfahbodtypedef struct {
264a9f24c802956d57180d71b83e96a0fb81197df4aBehdad Esfahbod  hb_atomic_int_t ref_count;
265a9f24c802956d57180d71b83e96a0fb81197df4aBehdad Esfahbod
266a9f24c802956d57180d71b83e96a0fb81197df4aBehdad Esfahbod#define HB_REFERENCE_COUNT_INVALID_VALUE ((hb_atomic_int_t) -1)
267a9f24c802956d57180d71b83e96a0fb81197df4aBehdad Esfahbod#define HB_REFERENCE_COUNT_INVALID {HB_REFERENCE_COUNT_INVALID_VALUE}
268a9f24c802956d57180d71b83e96a0fb81197df4aBehdad Esfahbod
269a9f24c802956d57180d71b83e96a0fb81197df4aBehdad Esfahbod  inline void init (int v) { ref_count = v; /* non-atomic is fine */ }
270a9f24c802956d57180d71b83e96a0fb81197df4aBehdad Esfahbod  inline int inc (void) { return hb_atomic_int_fetch_and_add (ref_count,  1); }
271a9f24c802956d57180d71b83e96a0fb81197df4aBehdad Esfahbod  inline int dec (void) { return hb_atomic_int_fetch_and_add (ref_count, -1); }
272ebdc83467c31574daa118fc18cd2ef2dc819b503Behdad Esfahbod  inline void set (int v) { hb_atomic_int_set (ref_count, v); }
273a9f24c802956d57180d71b83e96a0fb81197df4aBehdad Esfahbod
274a9f24c802956d57180d71b83e96a0fb81197df4aBehdad Esfahbod  inline int get (void) const { return hb_atomic_int_get (ref_count); }
275a9f24c802956d57180d71b83e96a0fb81197df4aBehdad Esfahbod  inline bool is_invalid (void) const { return get () == HB_REFERENCE_COUNT_INVALID_VALUE; }
276a9f24c802956d57180d71b83e96a0fb81197df4aBehdad Esfahbod
277a9f24c802956d57180d71b83e96a0fb81197df4aBehdad Esfahbod} hb_reference_count_t;
278a9f24c802956d57180d71b83e96a0fb81197df4aBehdad Esfahbod
279a9f24c802956d57180d71b83e96a0fb81197df4aBehdad Esfahbod
280f60f2166c48d07f556ff83f04e95181946eb03dfBehdad Esfahbod/* Big-endian handling */
281f60f2166c48d07f556ff83f04e95181946eb03dfBehdad Esfahbod
282153142dac8dd9abaf164bb88af07c600c17fc3a1Behdad Esfahbodstatic inline uint16_t hb_be_uint16 (const uint16_t v)
283153142dac8dd9abaf164bb88af07c600c17fc3a1Behdad Esfahbod{
284153142dac8dd9abaf164bb88af07c600c17fc3a1Behdad Esfahbod  const uint8_t *V = (const uint8_t *) &v;
285153142dac8dd9abaf164bb88af07c600c17fc3a1Behdad Esfahbod  return (uint16_t) (V[0] << 8) + V[1];
286153142dac8dd9abaf164bb88af07c600c17fc3a1Behdad Esfahbod}
2877a52f281178867379adb6e6c6fb0022102f75d17Behdad Esfahbod
288e032ed9f75d4a0f365649a25706871bbb5ae6651Behdad Esfahbod#define hb_be_uint16_put(v,V)	HB_STMT_START { v[0] = (V>>8); v[1] = (V); } HB_STMT_END
2897a52f281178867379adb6e6c6fb0022102f75d17Behdad Esfahbod#define hb_be_uint16_get(v)	(uint16_t) ((v[0] << 8) + v[1])
290153142dac8dd9abaf164bb88af07c600c17fc3a1Behdad Esfahbod#define hb_be_uint16_eq(a,b)	(a[0] == b[0] && a[1] == b[1])
2917a52f281178867379adb6e6c6fb0022102f75d17Behdad Esfahbod
292e032ed9f75d4a0f365649a25706871bbb5ae6651Behdad Esfahbod#define hb_be_uint32_put(v,V)	HB_STMT_START { v[0] = (V>>24); v[1] = (V>>16); v[2] = (V>>8); v[3] = (V); } HB_STMT_END
2937a52f281178867379adb6e6c6fb0022102f75d17Behdad Esfahbod#define hb_be_uint32_get(v)	(uint32_t) ((v[0] << 24) + (v[1] << 16) + (v[2] << 8) + v[3])
294153142dac8dd9abaf164bb88af07c600c17fc3a1Behdad Esfahbod#define hb_be_uint32_eq(a,b)	(a[0] == b[0] && a[1] == b[1] && a[2] == b[2] && a[3] == b[3])
295f60f2166c48d07f556ff83f04e95181946eb03dfBehdad Esfahbod
296f60f2166c48d07f556ff83f04e95181946eb03dfBehdad Esfahbod
2974188096a7722f09ffa9319986c0286071da10a27Behdad Esfahbod/* ASCII tag/character handling */
298db5227c40e5c35fe2ffb750f32b639cb44424a1dBehdad Esfahbod
299153142dac8dd9abaf164bb88af07c600c17fc3a1Behdad Esfahbodstatic inline unsigned char ISALPHA (unsigned char c)
300153142dac8dd9abaf164bb88af07c600c17fc3a1Behdad Esfahbod{ return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'); }
301153142dac8dd9abaf164bb88af07c600c17fc3a1Behdad Esfahbodstatic inline unsigned char ISALNUM (unsigned char c)
302153142dac8dd9abaf164bb88af07c600c17fc3a1Behdad Esfahbod{ return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9'); }
303153142dac8dd9abaf164bb88af07c600c17fc3a1Behdad Esfahbodstatic inline unsigned char TOUPPER (unsigned char c)
304153142dac8dd9abaf164bb88af07c600c17fc3a1Behdad Esfahbod{ return (c >= 'a' && c <= 'z') ? c - 'a' + 'A' : c; }
305153142dac8dd9abaf164bb88af07c600c17fc3a1Behdad Esfahbodstatic inline unsigned char TOLOWER (unsigned char c)
306153142dac8dd9abaf164bb88af07c600c17fc3a1Behdad Esfahbod{ return (c >= 'A' && c <= 'Z') ? c - 'A' + 'a' : c; }
307db5227c40e5c35fe2ffb750f32b639cb44424a1dBehdad Esfahbod
3084188096a7722f09ffa9319986c0286071da10a27Behdad Esfahbod#define HB_TAG_CHAR4(s)   (HB_TAG(((const char *) s)[0], \
3094188096a7722f09ffa9319986c0286071da10a27Behdad Esfahbod				  ((const char *) s)[1], \
3104188096a7722f09ffa9319986c0286071da10a27Behdad Esfahbod				  ((const char *) s)[2], \
3114188096a7722f09ffa9319986c0286071da10a27Behdad Esfahbod				  ((const char *) s)[3]))
3124188096a7722f09ffa9319986c0286071da10a27Behdad Esfahbod
313db5227c40e5c35fe2ffb750f32b639cb44424a1dBehdad Esfahbod
314bc200457430c083914a64bf4b056153506749610Behdad Esfahbod/* Debug */
315bc200457430c083914a64bf4b056153506749610Behdad Esfahbod
316bc200457430c083914a64bf4b056153506749610Behdad Esfahbod#ifndef HB_DEBUG
317bc200457430c083914a64bf4b056153506749610Behdad Esfahbod#define HB_DEBUG 0
318bc200457430c083914a64bf4b056153506749610Behdad Esfahbod#endif
319bc200457430c083914a64bf4b056153506749610Behdad Esfahbod
320fca368c4682624346a0aaee690e1ad6ed4c0b337Behdad Esfahbodstatic inline bool /* always returns TRUE */
321bc200457430c083914a64bf4b056153506749610Behdad Esfahbod_hb_trace (const char *what,
322bc200457430c083914a64bf4b056153506749610Behdad Esfahbod	   const char *function,
323bc200457430c083914a64bf4b056153506749610Behdad Esfahbod	   const void *obj,
324bc200457430c083914a64bf4b056153506749610Behdad Esfahbod	   unsigned int depth,
325bc200457430c083914a64bf4b056153506749610Behdad Esfahbod	   unsigned int max_depth)
326bc200457430c083914a64bf4b056153506749610Behdad Esfahbod{
32711e3ec444a85fc72541823c2e98cc92c4ceb19afBehdad Esfahbod  (void) ((depth < max_depth) && fprintf (stderr, "%s(%p) %-*d-> %s\n", what, obj, depth, depth, function));
328bc200457430c083914a64bf4b056153506749610Behdad Esfahbod  return TRUE;
329bc200457430c083914a64bf4b056153506749610Behdad Esfahbod}
330bc200457430c083914a64bf4b056153506749610Behdad Esfahbod
331f60f2166c48d07f556ff83f04e95181946eb03dfBehdad Esfahbod
332acdba3f90b232fc12fcb200dca2584481b339118Behdad EsfahbodHB_END_DECLS
333acdba3f90b232fc12fcb200dca2584481b339118Behdad Esfahbod
334c57d454accff66e5f2c58006e8fb40bc020b6182Behdad Esfahbod#endif /* HB_PRIVATE_HH */
335