libcss
Loading...
Searching...
No Matches
bloom.h
Go to the documentation of this file.
1/*
2 * This file is part of LibCSS.
3 * Licensed under the MIT License,
4 * http://www.opensource.org/licenses/mit-license.php
5 * Copyright 2013 Michael Drake <tlsa@netsurf-browser.org>
6 */
7
31#ifndef libcss_bloom_h_
32#define libcss_bloom_h_
33
34#include <stdint.h>
35#include <string.h>
36
37/* Size of bloom filter as multiple of 32 bits.
38 * Has to be 4, 8, or 16.
39 * Larger increases optimisation of style selection engine but uses more memory.
40 */
41#define CSS_BLOOM_SIZE 4
42
43
44
45/* Check valid bloom filter size */
46#if !(CSS_BLOOM_SIZE == 4 || CSS_BLOOM_SIZE == 8 || CSS_BLOOM_SIZE == 16)
47# error Unsupported bloom filter size. Size must be {4|8|16}.
48#endif
49
50/* Setup index bit mask */
51#define INDEX_BITS_N (CSS_BLOOM_SIZE - 1)
52
53
54
55/* type for bloom */
56typedef uint32_t css_bloom;
57
58
65static inline void css_bloom_add_hash(css_bloom bloom[CSS_BLOOM_SIZE],
66 lwc_hash hash)
67{
68 unsigned int bit = hash & 0x1f; /* Top 5 bits */
69 unsigned int index = (hash >> 5) & INDEX_BITS_N; /* Next N bits */
70
71 bloom[index] |= (1u << bit);
72}
73
74
82static inline bool css_bloom_has_hash(const css_bloom bloom[CSS_BLOOM_SIZE],
83 lwc_hash hash)
84{
85 unsigned int bit = hash & 0x1f; /* Top 5 bits */
86 unsigned int index = (hash >> 5) & INDEX_BITS_N; /* Next N bits */
87
88 return (bloom[index] & (1u << bit));
89}
90
91
99static inline bool css_bloom_in_bloom(
100 const css_bloom a[CSS_BLOOM_SIZE],
101 const css_bloom b[CSS_BLOOM_SIZE])
102{
103 if ((a[0] & b[0]) != a[0])
104 return false;
105 if ((a[1] & b[1]) != a[1])
106 return false;
107 if ((a[2] & b[2]) != a[2])
108 return false;
109 if ((a[3] & b[3]) != a[3])
110 return false;
111#if (CSS_BLOOM_SIZE > 4)
112 if ((a[4] & b[4]) != a[4])
113 return false;
114 if ((a[5] & b[5]) != a[5])
115 return false;
116 if ((a[6] & b[6]) != a[6])
117 return false;
118 if ((a[7] & b[7]) != a[7])
119 return false;
120#endif
121#if (CSS_BLOOM_SIZE > 8)
122 if ((a[8] & b[8]) != a[8])
123 return false;
124 if ((a[9] & b[9]) != a[9])
125 return false;
126 if ((a[10] & b[10]) != a[10])
127 return false;
128 if ((a[11] & b[11]) != a[11])
129 return false;
130 if ((a[12] & b[12]) != a[12])
131 return false;
132 if ((a[13] & b[13]) != a[13])
133 return false;
134 if ((a[14] & b[14]) != a[14])
135 return false;
136 if ((a[15] & b[15]) != a[15])
137 return false;
138#endif
139 return true;
140}
141
142
149static inline void css_bloom_merge(
150 const css_bloom a[restrict CSS_BLOOM_SIZE],
151 css_bloom b[restrict CSS_BLOOM_SIZE])
152{
153 b[0] |= a[0];
154 b[1] |= a[1];
155 b[2] |= a[2];
156 b[3] |= a[3];
157#if (CSS_BLOOM_SIZE > 4)
158 b[4] |= a[4];
159 b[5] |= a[5];
160 b[6] |= a[6];
161 b[7] |= a[7];
162#endif
163#if (CSS_BLOOM_SIZE > 8)
164 b[8] |= a[8];
165 b[9] |= a[9];
166 b[10] |= a[10];
167 b[11] |= a[11];
168 b[12] |= a[12];
169 b[13] |= a[13];
170 b[14] |= a[14];
171 b[15] |= a[15];
172#endif
173}
174
175
181static inline void css_bloom_init(css_bloom bloom[CSS_BLOOM_SIZE])
182{
183 memset(bloom, 0, sizeof(*bloom) * CSS_BLOOM_SIZE);
184}
185
186#endif
uint32_t css_bloom
Definition bloom.h:56
#define INDEX_BITS_N
Definition bloom.h:51
#define CSS_BLOOM_SIZE
Definition bloom.h:41