Skip to content

Commit 6687430

Browse files
Refactor code for setting pg_popcount* function pointers.
Presently, there are three copies of this code, and a proposed follow-up patch would add more code to each copy. This commit introduces a new inline function for this code and makes use of it in the pg_popcount*_choose functions, thereby reducing code duplication. Author: Paul Amonson Discussion: https://postgr.es/m/BL1PR11MB5304097DF7EA81D04C33F3D1DCA6A%40BL1PR11MB5304.namprd11.prod.outlook.com
1 parent 38698dd commit 6687430

File tree

1 file changed

+9
-28
lines changed

1 file changed

+9
-28
lines changed

src/port/pg_bitutils.c

Lines changed: 9 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -148,8 +148,8 @@ pg_popcount_available(void)
148148
* the function pointers so that subsequent calls are routed directly to
149149
* the chosen implementation.
150150
*/
151-
static int
152-
pg_popcount32_choose(uint32 word)
151+
static inline void
152+
choose_popcount_functions(void)
153153
{
154154
if (pg_popcount_available())
155155
{
@@ -163,45 +163,26 @@ pg_popcount32_choose(uint32 word)
163163
pg_popcount64 = pg_popcount64_slow;
164164
pg_popcount = pg_popcount_slow;
165165
}
166+
}
166167

168+
static int
169+
pg_popcount32_choose(uint32 word)
170+
{
171+
choose_popcount_functions();
167172
return pg_popcount32(word);
168173
}
169174

170175
static int
171176
pg_popcount64_choose(uint64 word)
172177
{
173-
if (pg_popcount_available())
174-
{
175-
pg_popcount32 = pg_popcount32_fast;
176-
pg_popcount64 = pg_popcount64_fast;
177-
pg_popcount = pg_popcount_fast;
178-
}
179-
else
180-
{
181-
pg_popcount32 = pg_popcount32_slow;
182-
pg_popcount64 = pg_popcount64_slow;
183-
pg_popcount = pg_popcount_slow;
184-
}
185-
178+
choose_popcount_functions();
186179
return pg_popcount64(word);
187180
}
188181

189182
static uint64
190183
pg_popcount_choose(const char *buf, int bytes)
191184
{
192-
if (pg_popcount_available())
193-
{
194-
pg_popcount32 = pg_popcount32_fast;
195-
pg_popcount64 = pg_popcount64_fast;
196-
pg_popcount = pg_popcount_fast;
197-
}
198-
else
199-
{
200-
pg_popcount32 = pg_popcount32_slow;
201-
pg_popcount64 = pg_popcount64_slow;
202-
pg_popcount = pg_popcount_slow;
203-
}
204-
185+
choose_popcount_functions();
205186
return pg_popcount(buf, bytes);
206187
}
207188

0 commit comments

Comments
 (0)