1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/gfx/graphite2/src/inc/bits.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,90 @@ 1.4 +/* GRAPHITE2 LICENSING 1.5 + 1.6 + Copyright 2012, SIL International 1.7 + All rights reserved. 1.8 + 1.9 + This library is free software; you can redistribute it and/or modify 1.10 + it under the terms of the GNU Lesser General Public License as published 1.11 + by the Free Software Foundation; either version 2.1 of License, or 1.12 + (at your option) any later version. 1.13 + 1.14 + This program is distributed in the hope that it will be useful, 1.15 + but WITHOUT ANY WARRANTY; without even the implied warranty of 1.16 + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 1.17 + Lesser General Public License for more details. 1.18 + 1.19 + You should also have received a copy of the GNU Lesser General Public 1.20 + License along with this library in the file named "LICENSE". 1.21 + If not, write to the Free Software Foundation, 51 Franklin Street, 1.22 + Suite 500, Boston, MA 02110-1335, USA or visit their web page on the 1.23 + internet at http://www.fsf.org/licenses/lgpl.html. 1.24 + 1.25 +Alternatively, the contents of this file may be used under the terms of the 1.26 +Mozilla Public License (http://mozilla.org/MPL) or the GNU General Public 1.27 +License, as published by the Free Software Foundation, either version 2 1.28 +of the License or (at your option) any later version. 1.29 +*/ 1.30 +#pragma once 1.31 + 1.32 +namespace graphite2 1.33 +{ 1.34 + 1.35 +template<typename T> 1.36 +inline unsigned int bit_set_count(T v) 1.37 +{ 1.38 + v = v - ((v >> 1) & T(~T(0)/3)); // temp 1.39 + v = (v & T(~T(0)/15*3)) + ((v >> 2) & T(~T(0)/15*3)); // temp 1.40 + v = (v + (v >> 4)) & T(~T(0)/255*15); // temp 1.41 + return (T)(v * T(~T(0)/255)) >> (sizeof(T)-1)*8; // count 1.42 +} 1.43 + 1.44 + 1.45 +template<int S> 1.46 +inline unsigned long _mask_over_val(unsigned long v) 1.47 +{ 1.48 + v = _mask_over_val<S/2>(v); 1.49 + v |= v >> S*4; 1.50 + return v; 1.51 +} 1.52 + 1.53 +template<> 1.54 +inline unsigned long _mask_over_val<1>(unsigned long v) 1.55 +{ 1.56 + v |= v >> 1; 1.57 + v |= v >> 2; 1.58 + v |= v >> 4; 1.59 + return v; 1.60 +} 1.61 + 1.62 +template<typename T> 1.63 +inline T mask_over_val(T v) 1.64 +{ 1.65 + return _mask_over_val<sizeof(T)>(v); 1.66 +} 1.67 + 1.68 +template<typename T> 1.69 +inline unsigned long next_highest_power2(T v) 1.70 +{ 1.71 + return _mask_over_val<sizeof(T)>(v-1)+1; 1.72 +} 1.73 + 1.74 +template<typename T> 1.75 +inline unsigned int log_binary(T v) 1.76 +{ 1.77 + return bit_set_count(mask_over_val(v))-1; 1.78 +} 1.79 + 1.80 +template<typename T> 1.81 +inline T has_zero(const T x) 1.82 +{ 1.83 + return (x - T(~T(0)/255)) & ~x & T(~T(0)/255*128); 1.84 +} 1.85 + 1.86 +template<typename T> 1.87 +inline T zero_bytes(const T x, unsigned char n) 1.88 +{ 1.89 + const T t = T(~T(0)/255*n); 1.90 + return T((has_zero(x^t) >> 7)*n); 1.91 +} 1.92 + 1.93 +}