gfx/graphite2/src/inc/bits.h

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /* GRAPHITE2 LICENSING
michael@0 2
michael@0 3 Copyright 2012, SIL International
michael@0 4 All rights reserved.
michael@0 5
michael@0 6 This library is free software; you can redistribute it and/or modify
michael@0 7 it under the terms of the GNU Lesser General Public License as published
michael@0 8 by the Free Software Foundation; either version 2.1 of License, or
michael@0 9 (at your option) any later version.
michael@0 10
michael@0 11 This program is distributed in the hope that it will be useful,
michael@0 12 but WITHOUT ANY WARRANTY; without even the implied warranty of
michael@0 13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
michael@0 14 Lesser General Public License for more details.
michael@0 15
michael@0 16 You should also have received a copy of the GNU Lesser General Public
michael@0 17 License along with this library in the file named "LICENSE".
michael@0 18 If not, write to the Free Software Foundation, 51 Franklin Street,
michael@0 19 Suite 500, Boston, MA 02110-1335, USA or visit their web page on the
michael@0 20 internet at http://www.fsf.org/licenses/lgpl.html.
michael@0 21
michael@0 22 Alternatively, the contents of this file may be used under the terms of the
michael@0 23 Mozilla Public License (http://mozilla.org/MPL) or the GNU General Public
michael@0 24 License, as published by the Free Software Foundation, either version 2
michael@0 25 of the License or (at your option) any later version.
michael@0 26 */
michael@0 27 #pragma once
michael@0 28
michael@0 29 namespace graphite2
michael@0 30 {
michael@0 31
michael@0 32 template<typename T>
michael@0 33 inline unsigned int bit_set_count(T v)
michael@0 34 {
michael@0 35 v = v - ((v >> 1) & T(~T(0)/3)); // temp
michael@0 36 v = (v & T(~T(0)/15*3)) + ((v >> 2) & T(~T(0)/15*3)); // temp
michael@0 37 v = (v + (v >> 4)) & T(~T(0)/255*15); // temp
michael@0 38 return (T)(v * T(~T(0)/255)) >> (sizeof(T)-1)*8; // count
michael@0 39 }
michael@0 40
michael@0 41
michael@0 42 template<int S>
michael@0 43 inline unsigned long _mask_over_val(unsigned long v)
michael@0 44 {
michael@0 45 v = _mask_over_val<S/2>(v);
michael@0 46 v |= v >> S*4;
michael@0 47 return v;
michael@0 48 }
michael@0 49
michael@0 50 template<>
michael@0 51 inline unsigned long _mask_over_val<1>(unsigned long v)
michael@0 52 {
michael@0 53 v |= v >> 1;
michael@0 54 v |= v >> 2;
michael@0 55 v |= v >> 4;
michael@0 56 return v;
michael@0 57 }
michael@0 58
michael@0 59 template<typename T>
michael@0 60 inline T mask_over_val(T v)
michael@0 61 {
michael@0 62 return _mask_over_val<sizeof(T)>(v);
michael@0 63 }
michael@0 64
michael@0 65 template<typename T>
michael@0 66 inline unsigned long next_highest_power2(T v)
michael@0 67 {
michael@0 68 return _mask_over_val<sizeof(T)>(v-1)+1;
michael@0 69 }
michael@0 70
michael@0 71 template<typename T>
michael@0 72 inline unsigned int log_binary(T v)
michael@0 73 {
michael@0 74 return bit_set_count(mask_over_val(v))-1;
michael@0 75 }
michael@0 76
michael@0 77 template<typename T>
michael@0 78 inline T has_zero(const T x)
michael@0 79 {
michael@0 80 return (x - T(~T(0)/255)) & ~x & T(~T(0)/255*128);
michael@0 81 }
michael@0 82
michael@0 83 template<typename T>
michael@0 84 inline T zero_bytes(const T x, unsigned char n)
michael@0 85 {
michael@0 86 const T t = T(~T(0)/255*n);
michael@0 87 return T((has_zero(x^t) >> 7)*n);
michael@0 88 }
michael@0 89
michael@0 90 }

mercurial