Skip to content

Commit 7188a78

Browse files
Improve style of pg_lfind32().
This commit simplifies pg_lfind32() a bit by moving the standard one-by-one linear search code to an inline helper function. Reviewed-by: Tom Lane Discussion: https://postgr.es/m/20240327013616.GA3940109%40nathanxps13
1 parent 2d8f56d commit 7188a78

File tree

1 file changed

+26
-36
lines changed

1 file changed

+26
-36
lines changed

src/include/port/pg_lfind.h

Lines changed: 26 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,24 @@ pg_lfind8_le(uint8 key, uint8 *base, uint32 nelem)
8080
return false;
8181
}
8282

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+
83101
#ifndef USE_NO_SIMD
84102
/*
85103
* pg_lfind32_simd_helper
@@ -88,7 +106,7 @@ pg_lfind8_le(uint8 key, uint8 *base, uint32 nelem)
88106
* ensuring that there are at least 4-registers-worth of integers remaining.
89107
*/
90108
static inline bool
91-
pg_lfind32_simd_helper(const Vector32 keys, uint32 *base)
109+
pg_lfind32_simd_helper(const Vector32 keys, const uint32 *base)
92110
{
93111
const uint32 nelem_per_vector = sizeof(Vector32) / sizeof(uint32);
94112
Vector32 vals1,
@@ -132,11 +150,10 @@ pg_lfind32_simd_helper(const Vector32 keys, uint32 *base)
132150
* return false.
133151
*/
134152
static inline bool
135-
pg_lfind32(uint32 key, uint32 *base, uint32 nelem)
153+
pg_lfind32(uint32 key, const uint32 *base, uint32 nelem)
136154
{
137-
uint32 i = 0;
138-
139155
#ifndef USE_NO_SIMD
156+
uint32 i = 0;
140157

141158
/*
142159
* For better instruction-level parallelism, each loop iteration operates
@@ -150,25 +167,15 @@ pg_lfind32(uint32 key, uint32 *base, uint32 nelem)
150167
const uint32 tail_idx = nelem & ~(nelem_per_iteration - 1);
151168

152169
#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);
164171
#endif
165172

166173
/*
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
168175
* one-by-one linear search code.
169176
*/
170177
if (nelem < nelem_per_iteration)
171-
goto one_by_one;
178+
return pg_lfind32_one_by_one_helper(key, base, nelem);
172179

173180
/*
174181
* Process as many elements as possible with a block of 4 registers.
@@ -193,27 +200,10 @@ pg_lfind32(uint32 key, uint32 *base, uint32 nelem)
193200
*/
194201
Assert(assert_result == pg_lfind32_simd_helper(keys, &base[nelem - nelem_per_iteration]));
195202
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
201204
/* 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);
208206
#endif
209-
return true;
210-
}
211-
}
212-
213-
#ifndef USE_NO_SIMD
214-
Assert(assert_result == false);
215-
#endif
216-
return false;
217207
}
218208

219209
#endif /* PG_LFIND_H */

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy