Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit7188a78

Browse files
Improve style of pg_lfind32().
This commit simplifies pg_lfind32() a bit by moving the standardone-by-one linear search code to an inline helper function.Reviewed-by: Tom LaneDiscussion:https://postgr.es/m/20240327013616.GA3940109%40nathanxps13
1 parent2d8f56d commit7188a78

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+
staticinlinebool
90+
pg_lfind32_one_by_one_helper(uint32key,constuint32*base,uint32nelem)
91+
{
92+
for (uint32i=0;i<nelem;i++)
93+
{
94+
if (key==base[i])
95+
return true;
96+
}
97+
98+
return false;
99+
}
100+
83101
#ifndefUSE_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
staticinlinebool
91-
pg_lfind32_simd_helper(constVector32keys,uint32*base)
109+
pg_lfind32_simd_helper(constVector32keys,constuint32*base)
92110
{
93111
constuint32nelem_per_vector=sizeof(Vector32) /sizeof(uint32);
94112
Vector32vals1,
@@ -132,11 +150,10 @@ pg_lfind32_simd_helper(const Vector32 keys, uint32 *base)
132150
* return false.
133151
*/
134152
staticinlinebool
135-
pg_lfind32(uint32key,uint32*base,uint32nelem)
153+
pg_lfind32(uint32key,constuint32*base,uint32nelem)
136154
{
137-
uint32i=0;
138-
139155
#ifndefUSE_NO_SIMD
156+
uint32i=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
constuint32tail_idx=nelem& ~(nelem_per_iteration-1);
151168

152169
#if defined(USE_ASSERT_CHECKING)
153-
boolassert_result= false;
154-
155-
/* pre-compute the result for assert checking */
156-
for (intj=0;j<nelem;j++)
157-
{
158-
if (key==base[j])
159-
{
160-
assert_result= true;
161-
break;
162-
}
163-
}
170+
boolassert_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-
gotoone_by_one;
178+
returnpg_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
returnpg_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-
#ifndefUSE_NO_SIMD
207-
Assert(assert_result== true);
205+
returnpg_lfind32_one_by_one_helper(key,base,nelem);
208206
#endif
209-
return true;
210-
}
211-
}
212-
213-
#ifndefUSE_NO_SIMD
214-
Assert(assert_result== false);
215-
#endif
216-
return false;
217207
}
218208

219209
#endif/* PG_LFIND_H */

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp