@@ -80,6 +80,24 @@ pg_lfind8_le(uint8 key, uint8 *base, uint32 nelem)
80
80
return false;
81
81
}
82
82
83
+ /*
84
+ * pg_lfind32_one_by_one_helper
85
+ *
86
+ * Searches the array of integers one-by-one. The caller is responsible for
87
+ * ensuring that there are at least "nelem" integers in the array.
88
+ */
89
+ static inline bool
90
+ pg_lfind32_one_by_one_helper (uint32 key , const uint32 * base , uint32 nelem )
91
+ {
92
+ for (uint32 i = 0 ; i < nelem ; i ++ )
93
+ {
94
+ if (key == base [i ])
95
+ return true;
96
+ }
97
+
98
+ return false;
99
+ }
100
+
83
101
#ifndef USE_NO_SIMD
84
102
/*
85
103
* pg_lfind32_simd_helper
@@ -88,7 +106,7 @@ pg_lfind8_le(uint8 key, uint8 *base, uint32 nelem)
88
106
* ensuring that there are at least 4-registers-worth of integers remaining.
89
107
*/
90
108
static inline bool
91
- pg_lfind32_simd_helper (const Vector32 keys , uint32 * base )
109
+ pg_lfind32_simd_helper (const Vector32 keys , const uint32 * base )
92
110
{
93
111
const uint32 nelem_per_vector = sizeof (Vector32 ) / sizeof (uint32 );
94
112
Vector32 vals1 ,
@@ -132,11 +150,10 @@ pg_lfind32_simd_helper(const Vector32 keys, uint32 *base)
132
150
* return false.
133
151
*/
134
152
static inline bool
135
- pg_lfind32 (uint32 key , uint32 * base , uint32 nelem )
153
+ pg_lfind32 (uint32 key , const uint32 * base , uint32 nelem )
136
154
{
137
- uint32 i = 0 ;
138
-
139
155
#ifndef USE_NO_SIMD
156
+ uint32 i = 0 ;
140
157
141
158
/*
142
159
* For better instruction-level parallelism, each loop iteration operates
@@ -150,25 +167,15 @@ pg_lfind32(uint32 key, uint32 *base, uint32 nelem)
150
167
const uint32 tail_idx = nelem & ~(nelem_per_iteration - 1 );
151
168
152
169
#if defined(USE_ASSERT_CHECKING )
153
- bool assert_result = false;
154
-
155
- /* pre-compute the result for assert checking */
156
- for (int j = 0 ; j < nelem ; j ++ )
157
- {
158
- if (key == base [j ])
159
- {
160
- assert_result = true;
161
- break ;
162
- }
163
- }
170
+ bool assert_result = pg_lfind32_one_by_one_helper (key , base , nelem );
164
171
#endif
165
172
166
173
/*
167
- * If there aren't enough elements for the SIMD code, jump to the standard
174
+ * If there aren't enough elements for the SIMD code, use the standard
168
175
* one-by-one linear search code.
169
176
*/
170
177
if (nelem < nelem_per_iteration )
171
- goto one_by_one ;
178
+ return pg_lfind32_one_by_one_helper ( key , base , nelem ) ;
172
179
173
180
/*
174
181
* Process as many elements as possible with a block of 4 registers.
@@ -193,27 +200,10 @@ pg_lfind32(uint32 key, uint32 *base, uint32 nelem)
193
200
*/
194
201
Assert (assert_result == pg_lfind32_simd_helper (keys , & base [nelem - nelem_per_iteration ]));
195
202
return pg_lfind32_simd_helper (keys , & base [nelem - nelem_per_iteration ]);
196
-
197
- one_by_one :
198
-
199
- #endif /* ! USE_NO_SIMD */
200
-
203
+ #else
201
204
/* Process the elements one at a time. */
202
- for (; i < nelem ; i ++ )
203
- {
204
- if (key == base [i ])
205
- {
206
- #ifndef USE_NO_SIMD
207
- Assert (assert_result == true);
205
+ return pg_lfind32_one_by_one_helper (key , base , nelem );
208
206
#endif
209
- return true;
210
- }
211
- }
212
-
213
- #ifndef USE_NO_SIMD
214
- Assert (assert_result == false);
215
- #endif
216
- return false;
217
207
}
218
208
219
209
#endif /* PG_LFIND_H */
0 commit comments