hb-private.hh revision 7acb389569cf99c6bae9db31a8ed7c7007fbb566
164aef3a54999496fd1de4f5aa5b019e4c03b3836Behdad Esfahbod/* 26c78683c042250a7b5a6fc6ebae4717b03fadf9eBehdad Esfahbod * Copyright (C) 2007,2008,2009 Red Hat, Inc. 364aef3a54999496fd1de4f5aa5b019e4c03b3836Behdad Esfahbod * 464aef3a54999496fd1de4f5aa5b019e4c03b3836Behdad Esfahbod * This is part of HarfBuzz, an OpenType Layout engine library. 564aef3a54999496fd1de4f5aa5b019e4c03b3836Behdad Esfahbod * 664aef3a54999496fd1de4f5aa5b019e4c03b3836Behdad Esfahbod * Permission is hereby granted, without written agreement and without 764aef3a54999496fd1de4f5aa5b019e4c03b3836Behdad Esfahbod * license or royalty fees, to use, copy, modify, and distribute this 864aef3a54999496fd1de4f5aa5b019e4c03b3836Behdad Esfahbod * software and its documentation for any purpose, provided that the 964aef3a54999496fd1de4f5aa5b019e4c03b3836Behdad Esfahbod * above copyright notice and the following two paragraphs appear in 1064aef3a54999496fd1de4f5aa5b019e4c03b3836Behdad Esfahbod * all copies of this software. 1164aef3a54999496fd1de4f5aa5b019e4c03b3836Behdad Esfahbod * 1264aef3a54999496fd1de4f5aa5b019e4c03b3836Behdad Esfahbod * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR 1364aef3a54999496fd1de4f5aa5b019e4c03b3836Behdad Esfahbod * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES 1464aef3a54999496fd1de4f5aa5b019e4c03b3836Behdad Esfahbod * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN 1564aef3a54999496fd1de4f5aa5b019e4c03b3836Behdad Esfahbod * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH 1664aef3a54999496fd1de4f5aa5b019e4c03b3836Behdad Esfahbod * DAMAGE. 1764aef3a54999496fd1de4f5aa5b019e4c03b3836Behdad Esfahbod * 1864aef3a54999496fd1de4f5aa5b019e4c03b3836Behdad Esfahbod * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, 1964aef3a54999496fd1de4f5aa5b019e4c03b3836Behdad Esfahbod * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND 2064aef3a54999496fd1de4f5aa5b019e4c03b3836Behdad Esfahbod * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS 2164aef3a54999496fd1de4f5aa5b019e4c03b3836Behdad Esfahbod * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO 2264aef3a54999496fd1de4f5aa5b019e4c03b3836Behdad Esfahbod * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS. 2364aef3a54999496fd1de4f5aa5b019e4c03b3836Behdad Esfahbod * 2464aef3a54999496fd1de4f5aa5b019e4c03b3836Behdad Esfahbod * Red Hat Author(s): Behdad Esfahbod 2564aef3a54999496fd1de4f5aa5b019e4c03b3836Behdad Esfahbod */ 2664aef3a54999496fd1de4f5aa5b019e4c03b3836Behdad Esfahbod 278dd1c8b8d6797d899d0f5b0a8015886bf6520ca2Behdad Esfahbod#ifndef HB_PRIVATE_H 288dd1c8b8d6797d899d0f5b0a8015886bf6520ca2Behdad Esfahbod#define HB_PRIVATE_H 295b3f7702a64fe0513d08a67bdb72704e46fd7cd4Behdad Esfahbod 30df66028781a7609a515980e64396e6f1044d764aBehdad Esfahbod#if HAVE_CONFIG_H 31df66028781a7609a515980e64396e6f1044d764aBehdad Esfahbod#include "config.h" 32df66028781a7609a515980e64396e6f1044d764aBehdad Esfahbod#endif 33c7d457aa3ae7138630f52ca7263f663a3ea284c0Behdad Esfahbod 348f3be25e7ec88ea451ee8097bf32e5f729f4fef9Behdad Esfahbod/* #define HB_DEBUG 1 */ 35ec9f8db0d3cad146801b7dc5c59e517de3b6fa34Behdad Esfahbod#ifndef HB_DEBUG 36ec9f8db0d3cad146801b7dc5c59e517de3b6fa34Behdad Esfahbod#define HB_DEBUG 0 37ec9f8db0d3cad146801b7dc5c59e517de3b6fa34Behdad Esfahbod#endif 38b28815c1f6e46d38471cacbc31248ca6fda8c4d1Behdad Esfahbod 39c7d457aa3ae7138630f52ca7263f663a3ea284c0Behdad Esfahbod#include <stdlib.h> 40f0954d1e08ae288eda9904e17108fc73f48c0b98Behdad Esfahbod#include <string.h> 41f0954d1e08ae288eda9904e17108fc73f48c0b98Behdad Esfahbod#include <assert.h> 42b28815c1f6e46d38471cacbc31248ca6fda8c4d1Behdad Esfahbod#if HB_DEBUG 437acb389569cf99c6bae9db31a8ed7c7007fbb566Behdad Esfahbod#include <stdio.h> 447acb389569cf99c6bae9db31a8ed7c7007fbb566Behdad Esfahbod#include <errno.h> 45b28815c1f6e46d38471cacbc31248ca6fda8c4d1Behdad Esfahbod#endif 46c7d457aa3ae7138630f52ca7263f663a3ea284c0Behdad Esfahbod 47df66028781a7609a515980e64396e6f1044d764aBehdad Esfahbod#include "hb-common.h" 48df66028781a7609a515980e64396e6f1044d764aBehdad Esfahbod 49c7d457aa3ae7138630f52ca7263f663a3ea284c0Behdad Esfahbod/* Basics */ 50c7d457aa3ae7138630f52ca7263f663a3ea284c0Behdad Esfahbod 51c7d457aa3ae7138630f52ca7263f663a3ea284c0Behdad Esfahbod#undef MIN 52c7d457aa3ae7138630f52ca7263f663a3ea284c0Behdad Esfahbod#define MIN(a,b) ((a) < (b) ? (a) : (b)) 537586089c6fa8185cad8387869d3703c637e5cbb1Behdad Esfahbod 54c7d457aa3ae7138630f52ca7263f663a3ea284c0Behdad Esfahbod#ifndef HB_INTERNAL 55c7d457aa3ae7138630f52ca7263f663a3ea284c0Behdad Esfahbod# define HB_INTERNAL 56c7d457aa3ae7138630f52ca7263f663a3ea284c0Behdad Esfahbod#endif 57c7d457aa3ae7138630f52ca7263f663a3ea284c0Behdad Esfahbod 58c7d457aa3ae7138630f52ca7263f663a3ea284c0Behdad Esfahbod#ifndef NULL 59c7d457aa3ae7138630f52ca7263f663a3ea284c0Behdad Esfahbod# define NULL ((void *)0) 60c7d457aa3ae7138630f52ca7263f663a3ea284c0Behdad Esfahbod#endif 61c7d457aa3ae7138630f52ca7263f663a3ea284c0Behdad Esfahbod 62c7d457aa3ae7138630f52ca7263f663a3ea284c0Behdad Esfahbod#ifndef FALSE 63c7d457aa3ae7138630f52ca7263f663a3ea284c0Behdad Esfahbod# define FALSE 0 64c7d457aa3ae7138630f52ca7263f663a3ea284c0Behdad Esfahbod#endif 65c7d457aa3ae7138630f52ca7263f663a3ea284c0Behdad Esfahbod 66c7d457aa3ae7138630f52ca7263f663a3ea284c0Behdad Esfahbod#ifndef TRUE 67c7d457aa3ae7138630f52ca7263f663a3ea284c0Behdad Esfahbod# define TRUE 1 68c7d457aa3ae7138630f52ca7263f663a3ea284c0Behdad Esfahbod#endif 6912360f7c159826ae72271b34486dee59d96aa8caBehdad Esfahbod 7035a7383c6138fd705560f0d4bb30659cbd1ab64cBehdad Esfahbod#define HB_STMT_START do 7135a7383c6138fd705560f0d4bb30659cbd1ab64cBehdad Esfahbod#define HB_STMT_END while (0) 725b3f7702a64fe0513d08a67bdb72704e46fd7cd4Behdad Esfahbod 73303fe62824d4e99df554b6bfaacba05d068522fbBehdad Esfahbod#define _ASSERT_STATIC1(_line, _cond) typedef int _static_assert_on_line_##_line##_failed[(_cond)?1:-1] 74303fe62824d4e99df554b6bfaacba05d068522fbBehdad Esfahbod#define _ASSERT_STATIC0(_line, _cond) _ASSERT_STATIC1 (_line, (_cond)) 75303fe62824d4e99df554b6bfaacba05d068522fbBehdad Esfahbod#define ASSERT_STATIC(_cond) _ASSERT_STATIC0 (__LINE__, (_cond)) 76303fe62824d4e99df554b6bfaacba05d068522fbBehdad Esfahbod 77303fe62824d4e99df554b6bfaacba05d068522fbBehdad Esfahbod#define ASSERT_SIZE(_type, _size) ASSERT_STATIC (sizeof (_type) == (_size)) 78303fe62824d4e99df554b6bfaacba05d068522fbBehdad Esfahbod 79c7d457aa3ae7138630f52ca7263f663a3ea284c0Behdad Esfahbod 80df66028781a7609a515980e64396e6f1044d764aBehdad Esfahbod#if defined(__GNUC__) && (__GNUC__ > 2) && defined(__OPTIMIZE__) 81df66028781a7609a515980e64396e6f1044d764aBehdad Esfahbod#define _CAIRO_BOOLEAN_EXPR(expr) \ 82df66028781a7609a515980e64396e6f1044d764aBehdad Esfahbod __extension__ ({ \ 83df66028781a7609a515980e64396e6f1044d764aBehdad Esfahbod int _cairo_boolean_var_; \ 84df66028781a7609a515980e64396e6f1044d764aBehdad Esfahbod if (expr) \ 85df66028781a7609a515980e64396e6f1044d764aBehdad Esfahbod _cairo_boolean_var_ = 1; \ 86df66028781a7609a515980e64396e6f1044d764aBehdad Esfahbod else \ 87df66028781a7609a515980e64396e6f1044d764aBehdad Esfahbod _cairo_boolean_var_ = 0; \ 88df66028781a7609a515980e64396e6f1044d764aBehdad Esfahbod _cairo_boolean_var_; \ 89df66028781a7609a515980e64396e6f1044d764aBehdad Esfahbod}) 90df66028781a7609a515980e64396e6f1044d764aBehdad Esfahbod#define HB_LIKELY(expr) (__builtin_expect (_CAIRO_BOOLEAN_EXPR(expr), 1)) 91df66028781a7609a515980e64396e6f1044d764aBehdad Esfahbod#define HB_UNLIKELY(expr) (__builtin_expect (_CAIRO_BOOLEAN_EXPR(expr), 0)) 92df66028781a7609a515980e64396e6f1044d764aBehdad Esfahbod#else 93df66028781a7609a515980e64396e6f1044d764aBehdad Esfahbod#define HB_LIKELY(expr) (expr) 94df66028781a7609a515980e64396e6f1044d764aBehdad Esfahbod#define HB_UNLIKELY(expr) (expr) 95df66028781a7609a515980e64396e6f1044d764aBehdad Esfahbod#endif 96df66028781a7609a515980e64396e6f1044d764aBehdad Esfahbod 97df66028781a7609a515980e64396e6f1044d764aBehdad Esfahbod#ifndef __GNUC__ 98df66028781a7609a515980e64396e6f1044d764aBehdad Esfahbod#undef __attribute__ 99df66028781a7609a515980e64396e6f1044d764aBehdad Esfahbod#define __attribute__(x) 100df66028781a7609a515980e64396e6f1044d764aBehdad Esfahbod#endif 101df66028781a7609a515980e64396e6f1044d764aBehdad Esfahbod 102df66028781a7609a515980e64396e6f1044d764aBehdad Esfahbod#if __GNUC__ >= 3 103df66028781a7609a515980e64396e6f1044d764aBehdad Esfahbod#define HB_GNUC_UNUSED __attribute__((unused)) 104df66028781a7609a515980e64396e6f1044d764aBehdad Esfahbod#define HB_GNUC_PURE __attribute__((pure)) 105df66028781a7609a515980e64396e6f1044d764aBehdad Esfahbod#define HB_GNUC_CONST __attribute__((const)) 106df66028781a7609a515980e64396e6f1044d764aBehdad Esfahbod#else 107df66028781a7609a515980e64396e6f1044d764aBehdad Esfahbod#define HB_GNUC_UNUSED 108df66028781a7609a515980e64396e6f1044d764aBehdad Esfahbod#define HB_GNUC_PURE 109df66028781a7609a515980e64396e6f1044d764aBehdad Esfahbod#define HB_GNUC_CONST 110df66028781a7609a515980e64396e6f1044d764aBehdad Esfahbod#endif 111df66028781a7609a515980e64396e6f1044d764aBehdad Esfahbod 112df66028781a7609a515980e64396e6f1044d764aBehdad Esfahbod 113df66028781a7609a515980e64396e6f1044d764aBehdad Esfahbod#if (defined(__WIN32__) && !defined(__WINE__)) || defined(_MSC_VER) 114df66028781a7609a515980e64396e6f1044d764aBehdad Esfahbod#define snprintf _snprintf 115df66028781a7609a515980e64396e6f1044d764aBehdad Esfahbod#endif 116df66028781a7609a515980e64396e6f1044d764aBehdad Esfahbod 117df66028781a7609a515980e64396e6f1044d764aBehdad Esfahbod#ifdef _MSC_VER 118df66028781a7609a515980e64396e6f1044d764aBehdad Esfahbod#undef inline 119df66028781a7609a515980e64396e6f1044d764aBehdad Esfahbod#define inline __inline 120df66028781a7609a515980e64396e6f1044d764aBehdad Esfahbod#endif 121df66028781a7609a515980e64396e6f1044d764aBehdad Esfahbod 122df66028781a7609a515980e64396e6f1044d764aBehdad Esfahbod#ifdef __STRICT_ANSI__ 123df66028781a7609a515980e64396e6f1044d764aBehdad Esfahbod#undef inline 124df66028781a7609a515980e64396e6f1044d764aBehdad Esfahbod#define inline __inline__ 125df66028781a7609a515980e64396e6f1044d764aBehdad Esfahbod#endif 126df66028781a7609a515980e64396e6f1044d764aBehdad Esfahbod 127df66028781a7609a515980e64396e6f1044d764aBehdad Esfahbod 128c7d457aa3ae7138630f52ca7263f663a3ea284c0Behdad Esfahbod/* Return the number of 1 bits in mask. 129c7d457aa3ae7138630f52ca7263f663a3ea284c0Behdad Esfahbod * 130c7d457aa3ae7138630f52ca7263f663a3ea284c0Behdad Esfahbod * GCC 3.4 supports a "population count" builtin, which on many targets is 131c7d457aa3ae7138630f52ca7263f663a3ea284c0Behdad Esfahbod * implemented with a single instruction. There is a fallback definition 132c7d457aa3ae7138630f52ca7263f663a3ea284c0Behdad Esfahbod * in libgcc in case a target does not have one, which should be just as 133c7d457aa3ae7138630f52ca7263f663a3ea284c0Behdad Esfahbod * good as the open-coded solution below, (which is "HACKMEM 169"). 134c7d457aa3ae7138630f52ca7263f663a3ea284c0Behdad Esfahbod */ 135df66028781a7609a515980e64396e6f1044d764aBehdad Esfahbodstatic HB_GNUC_UNUSED inline unsigned int 136c7d457aa3ae7138630f52ca7263f663a3ea284c0Behdad Esfahbod_hb_popcount32 (uint32_t mask) 137c7d457aa3ae7138630f52ca7263f663a3ea284c0Behdad Esfahbod{ 138c7d457aa3ae7138630f52ca7263f663a3ea284c0Behdad Esfahbod#if __GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 4) 139c7d457aa3ae7138630f52ca7263f663a3ea284c0Behdad Esfahbod return __builtin_popcount (mask); 140c7d457aa3ae7138630f52ca7263f663a3ea284c0Behdad Esfahbod#else 141c7d457aa3ae7138630f52ca7263f663a3ea284c0Behdad Esfahbod register int y; 142c7d457aa3ae7138630f52ca7263f663a3ea284c0Behdad Esfahbod 143c7d457aa3ae7138630f52ca7263f663a3ea284c0Behdad Esfahbod y = (mask >> 1) &033333333333; 144c7d457aa3ae7138630f52ca7263f663a3ea284c0Behdad Esfahbod y = mask - y - ((y >>1) & 033333333333); 145c7d457aa3ae7138630f52ca7263f663a3ea284c0Behdad Esfahbod return (((y + (y >> 3)) & 030707070707) % 077); 146c7d457aa3ae7138630f52ca7263f663a3ea284c0Behdad Esfahbod#endif 147c7d457aa3ae7138630f52ca7263f663a3ea284c0Behdad Esfahbod} 148c7d457aa3ae7138630f52ca7263f663a3ea284c0Behdad Esfahbod 149baec684fde2303edb16341bfcf1022cd72acf129Behdad Esfahbodstatic HB_GNUC_UNUSED inline uint16_t 150baec684fde2303edb16341bfcf1022cd72acf129Behdad Esfahbod_hb_be_uint16 (uint16_t v) 151baec684fde2303edb16341bfcf1022cd72acf129Behdad Esfahbod{ 152baec684fde2303edb16341bfcf1022cd72acf129Behdad Esfahbod return (v>>8) + (v<<8); 153baec684fde2303edb16341bfcf1022cd72acf129Behdad Esfahbod} 154baec684fde2303edb16341bfcf1022cd72acf129Behdad Esfahbodstatic HB_GNUC_UNUSED inline uint32_t 155baec684fde2303edb16341bfcf1022cd72acf129Behdad Esfahbod_hb_be_uint32 (uint32_t v) 156baec684fde2303edb16341bfcf1022cd72acf129Behdad Esfahbod{ 157baec684fde2303edb16341bfcf1022cd72acf129Behdad Esfahbod return _hb_be_uint16 (v>>16) + (_hb_be_uint16 (v) <<16); 158baec684fde2303edb16341bfcf1022cd72acf129Behdad Esfahbod} 159baec684fde2303edb16341bfcf1022cd72acf129Behdad Esfahbod 160baec684fde2303edb16341bfcf1022cd72acf129Behdad Esfahbod/* Macros to convert to/from BigEndian */ 161baec684fde2303edb16341bfcf1022cd72acf129Behdad Esfahbod#define hb_be_uint8 162baec684fde2303edb16341bfcf1022cd72acf129Behdad Esfahbod#define hb_be_int8 163baec684fde2303edb16341bfcf1022cd72acf129Behdad Esfahbod#define hb_be_uint16(v) _hb_be_uint16 (v) 164baec684fde2303edb16341bfcf1022cd72acf129Behdad Esfahbod#define hb_be_int16(v) ((int16_t) hb_be_uint16 (v)) 165baec684fde2303edb16341bfcf1022cd72acf129Behdad Esfahbod#define hb_be_uint32(v) _hb_be_uint32 (v) 166baec684fde2303edb16341bfcf1022cd72acf129Behdad Esfahbod#define hb_be_int32(v) ((int32_t) hb_be_uint32 (v)) 167df66028781a7609a515980e64396e6f1044d764aBehdad Esfahbod 168df66028781a7609a515980e64396e6f1044d764aBehdad Esfahbod 169ba8d94ce3360bfaf2c530d01f2af69237018a531Behdad Esfahbod#include "hb-object-private.h" 170ba8d94ce3360bfaf2c530d01f2af69237018a531Behdad Esfahbod 1718dd1c8b8d6797d899d0f5b0a8015886bf6520ca2Behdad Esfahbod#endif /* HB_PRIVATE_H */ 172