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

Commit4b35408

Browse files
committed
Use bitwise rotate functions in more places
There were a number of places in the code that used bespoke bit-twiddlingexpressions to do bitwise rotation. While we've had pg_rotate_right32()for a while now, we hadn't gotten around to standardizing on that. Do sonow. Since many potential call sites look more natural with the "left"equivalent, add that function too.Reviewed by Tom Lane and Yugo NagataDiscussion:https://www.postgresql.org/message-id/CAFBsxsH7c1LC0CGZ0ADCBXLHU5-%3DKNXx-r7tHYPAW51b2HK4Qw%40mail.gmail.com
1 parentd7a9786 commit4b35408

File tree

9 files changed

+28
-25
lines changed

9 files changed

+28
-25
lines changed

‎src/backend/executor/execGrouping.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -459,8 +459,8 @@ TupleHashTableHash_internal(struct tuplehash_hash *tb,
459459
Datumattr;
460460
boolisNull;
461461

462-
/*rotate hashkey left 1 bit at each step */
463-
hashkey= (hashkey <<1) | ((hashkey&0x80000000) ?1 :0);
462+
/*combine successive hashkeys by rotating */
463+
hashkey=pg_rotate_left32(hashkey,1);
464464

465465
attr=slot_getattr(slot,att,&isNull);
466466

‎src/backend/executor/nodeHash.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1840,8 +1840,8 @@ ExecHashGetHashValue(HashJoinTable hashtable,
18401840
Datumkeyval;
18411841
boolisNull;
18421842

1843-
/*rotate hashkey left 1 bit at each step */
1844-
hashkey= (hashkey <<1) | ((hashkey&0x80000000) ?1 :0);
1843+
/*combine successive hashkeys by rotating */
1844+
hashkey=pg_rotate_left32(hashkey,1);
18451845

18461846
/*
18471847
* Get the join attribute value of the tuple

‎src/backend/executor/nodeMemoize.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -166,8 +166,8 @@ MemoizeHash_hash(struct memoize_hash *tb, const MemoizeKey *key)
166166
{
167167
for (inti=0;i<numkeys;i++)
168168
{
169-
/*rotate hashkey left 1 bit at each step */
170-
hashkey= (hashkey <<1) | ((hashkey&0x80000000) ?1 :0);
169+
/*combine successive hashkeys by rotating */
170+
hashkey=pg_rotate_left32(hashkey,1);
171171

172172
if (!pslot->tts_isnull[i])/* treat nulls as having hash key 0 */
173173
{
@@ -189,8 +189,8 @@ MemoizeHash_hash(struct memoize_hash *tb, const MemoizeKey *key)
189189

190190
for (inti=0;i<numkeys;i++)
191191
{
192-
/*rotate hashkey left 1 bit at each step */
193-
hashkey= (hashkey <<1) | ((hashkey&0x80000000) ?1 :0);
192+
/*combine successive hashkeys by rotating */
193+
hashkey=pg_rotate_left32(hashkey,1);
194194

195195
if (!pslot->tts_isnull[i])/* treat nulls as having hash key 0 */
196196
{

‎src/backend/utils/adt/jsonb_util.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include"common/hashfn.h"
1919
#include"common/jsonapi.h"
2020
#include"miscadmin.h"
21+
#include"port/pg_bitutils.h"
2122
#include"utils/builtins.h"
2223
#include"utils/datetime.h"
2324
#include"utils/json.h"
@@ -1342,7 +1343,7 @@ JsonbHashScalarValue(const JsonbValue *scalarVal, uint32 *hash)
13421343
* the previous value left 1 bit, then XOR'ing in the new
13431344
* key/value/element's hash value.
13441345
*/
1345-
*hash= (*hash <<1) | (*hash >>31);
1346+
*hash=pg_rotate_left32(*hash,1);
13461347
*hash ^=tmp;
13471348
}
13481349

‎src/backend/utils/adt/multirangetypes.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
#include"lib/stringinfo.h"
3939
#include"libpq/pqformat.h"
4040
#include"miscadmin.h"
41+
#include"port/pg_bitutils.h"
4142
#include"utils/builtins.h"
4243
#include"utils/lsyscache.h"
4344
#include"utils/rangetypes.h"
@@ -2772,7 +2773,7 @@ hash_multirange(PG_FUNCTION_ARGS)
27722773
/* Merge hashes of flags and bounds */
27732774
range_hash=hash_uint32((uint32)flags);
27742775
range_hash ^=lower_hash;
2775-
range_hash= (range_hash <<1) | (range_hash >>31);
2776+
range_hash=pg_rotate_left32(range_hash,1);
27762777
range_hash ^=upper_hash;
27772778

27782779
/*

‎src/backend/utils/adt/rangetypes.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
#include"lib/stringinfo.h"
3636
#include"libpq/pqformat.h"
3737
#include"miscadmin.h"
38+
#include"port/pg_bitutils.h"
3839
#include"utils/builtins.h"
3940
#include"utils/date.h"
4041
#include"utils/lsyscache.h"
@@ -1363,7 +1364,7 @@ hash_range(PG_FUNCTION_ARGS)
13631364
/* Merge hashes of flags and bounds */
13641365
result=hash_uint32((uint32)flags);
13651366
result ^=lower_hash;
1366-
result= (result <<1) | (result >>31);
1367+
result=pg_rotate_left32(result,1);
13671368
result ^=upper_hash;
13681369

13691370
PG_RETURN_INT32(result);

‎src/backend/utils/cache/catcache.c

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#include"catalog/pg_type.h"
2727
#include"common/hashfn.h"
2828
#include"miscadmin.h"
29+
#include"port/pg_bitutils.h"
2930
#ifdefCATCACHE_STATS
3031
#include"storage/ipc.h"/* for on_proc_exit */
3132
#endif
@@ -281,25 +282,18 @@ CatalogCacheComputeHashValue(CatCache *cache, int nkeys,
281282
{
282283
case4:
283284
oneHash= (cc_hashfunc[3]) (v4);
284-
285-
hashValue ^=oneHash <<24;
286-
hashValue ^=oneHash >>8;
285+
hashValue ^=pg_rotate_left32(oneHash,24);
287286
/* FALLTHROUGH */
288287
case3:
289288
oneHash= (cc_hashfunc[2]) (v3);
290-
291-
hashValue ^=oneHash <<16;
292-
hashValue ^=oneHash >>16;
289+
hashValue ^=pg_rotate_left32(oneHash,16);
293290
/* FALLTHROUGH */
294291
case2:
295292
oneHash= (cc_hashfunc[1]) (v2);
296-
297-
hashValue ^=oneHash <<8;
298-
hashValue ^=oneHash >>24;
293+
hashValue ^=pg_rotate_left32(oneHash,8);
299294
/* FALLTHROUGH */
300295
case1:
301296
oneHash= (cc_hashfunc[0]) (v1);
302-
303297
hashValue ^=oneHash;
304298
break;
305299
default:

‎src/common/hashfn.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include"postgres.h"
2525

2626
#include"common/hashfn.h"
27+
#include"port/pg_bitutils.h"
2728

2829

2930
/*
@@ -44,8 +45,7 @@
4445
/* Get a bit mask of the bits set in non-uint32 aligned addresses */
4546
#defineUINT32_ALIGN_MASK (sizeof(uint32) - 1)
4647

47-
/* Rotate a uint32 value left by k bits - note multiple evaluation! */
48-
#definerot(x,k) (((x)<<(k)) | ((x)>>(32-(k))))
48+
#definerot(x,k) pg_rotate_left32(x, k)
4949

5050
/*----------
5151
* mix -- mix 3 32-bit values reversibly.

‎src/include/port/pg_bitutils.h

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -285,12 +285,18 @@ extern intpg_popcount64(uint64 word);
285285
externuint64pg_popcount(constchar*buf,intbytes);
286286

287287
/*
288-
* Rotate the bits of "word" to the right by n bits.
288+
* Rotate the bits of "word" to the right/left by n bits.
289289
*/
290290
staticinlineuint32
291291
pg_rotate_right32(uint32word,intn)
292292
{
293-
return (word >>n) | (word << (sizeof(word)*BITS_PER_BYTE-n));
293+
return (word >>n) | (word << (32-n));
294+
}
295+
296+
staticinlineuint32
297+
pg_rotate_left32(uint32word,intn)
298+
{
299+
return (word <<n) | (word >> (32-n));
294300
}
295301

296302
#endif/* PG_BITUTILS_H */

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp