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