Consistently use unsigned arithmetic for alignment calculations.

This avoids an assumption about the signed number representation.  It is
anticipated to have no functional changes on supported configurations;
many two's complement assumptions remain elsewhere.

Per a suggestion from Andres Freund.
This commit is contained in:
Noah Misch 2013-10-20 21:04:52 -04:00
parent 713a9f210d
commit 709170b790
3 changed files with 10 additions and 10 deletions

View File

@ -314,7 +314,7 @@ hash_any(register const unsigned char *k, register int keylen)
a = b = c = 0x9e3779b9 + len + 3923095; a = b = c = 0x9e3779b9 + len + 3923095;
/* If the source pointer is word-aligned, we use word-wide fetches */ /* If the source pointer is word-aligned, we use word-wide fetches */
if (((intptr_t) k & UINT32_ALIGN_MASK) == 0) if (((uintptr_t) k & UINT32_ALIGN_MASK) == 0)
{ {
/* Code path for aligned source data */ /* Code path for aligned source data */
register const uint32 *ka = (const uint32 *) k; register const uint32 *ka = (const uint32 *) k;

View File

@ -101,7 +101,7 @@
#define att_align_datum(cur_offset, attalign, attlen, attdatum) \ #define att_align_datum(cur_offset, attalign, attlen, attdatum) \
( \ ( \
((attlen) == -1 && VARATT_IS_SHORT(DatumGetPointer(attdatum))) ? \ ((attlen) == -1 && VARATT_IS_SHORT(DatumGetPointer(attdatum))) ? \
(intptr_t) (cur_offset) : \ (uintptr_t) (cur_offset) : \
att_align_nominal(cur_offset, attalign) \ att_align_nominal(cur_offset, attalign) \
) )
@ -116,13 +116,13 @@
* aligned 4-byte length word; in either case we need not align.) * aligned 4-byte length word; in either case we need not align.)
* *
* Note: some callers pass a "char *" pointer for cur_offset. This is * Note: some callers pass a "char *" pointer for cur_offset. This is
* a bit of a hack but should work all right as long as intptr_t is the * a bit of a hack but should work all right as long as uintptr_t is the
* correct width. * correct width.
*/ */
#define att_align_pointer(cur_offset, attalign, attlen, attptr) \ #define att_align_pointer(cur_offset, attalign, attlen, attptr) \
( \ ( \
((attlen) == -1 && VARATT_NOT_PAD_BYTE(attptr)) ? \ ((attlen) == -1 && VARATT_NOT_PAD_BYTE(attptr)) ? \
(intptr_t) (cur_offset) : \ (uintptr_t) (cur_offset) : \
att_align_nominal(cur_offset, attalign) \ att_align_nominal(cur_offset, attalign) \
) )
@ -144,7 +144,7 @@
#define att_align_nominal(cur_offset, attalign) \ #define att_align_nominal(cur_offset, attalign) \
( \ ( \
((attalign) == 'i') ? INTALIGN(cur_offset) : \ ((attalign) == 'i') ? INTALIGN(cur_offset) : \
(((attalign) == 'c') ? (intptr_t) (cur_offset) : \ (((attalign) == 'c') ? (uintptr_t) (cur_offset) : \
(((attalign) == 'd') ? DOUBLEALIGN(cur_offset) : \ (((attalign) == 'd') ? DOUBLEALIGN(cur_offset) : \
( \ ( \
AssertMacro((attalign) == 's'), \ AssertMacro((attalign) == 's'), \

View File

@ -486,7 +486,7 @@ typedef NameData *Name;
* True iff pointer is properly aligned to point to the given type. * True iff pointer is properly aligned to point to the given type.
*/ */
#define PointerIsAligned(pointer, type) \ #define PointerIsAligned(pointer, type) \
(((intptr_t)(pointer) % (sizeof (type))) == 0) (((uintptr_t)(pointer) % (sizeof (type))) == 0)
#define OidIsValid(objectId) ((bool) ((objectId) != InvalidOid)) #define OidIsValid(objectId) ((bool) ((objectId) != InvalidOid))
@ -532,7 +532,7 @@ typedef NameData *Name;
*/ */
#define TYPEALIGN(ALIGNVAL,LEN) \ #define TYPEALIGN(ALIGNVAL,LEN) \
(((intptr_t) (LEN) + ((ALIGNVAL) - 1)) & ~((intptr_t) ((ALIGNVAL) - 1))) (((uintptr_t) (LEN) + ((ALIGNVAL) - 1)) & ~((uintptr_t) ((ALIGNVAL) - 1)))
#define SHORTALIGN(LEN) TYPEALIGN(ALIGNOF_SHORT, (LEN)) #define SHORTALIGN(LEN) TYPEALIGN(ALIGNOF_SHORT, (LEN))
#define INTALIGN(LEN) TYPEALIGN(ALIGNOF_INT, (LEN)) #define INTALIGN(LEN) TYPEALIGN(ALIGNOF_INT, (LEN))
@ -543,7 +543,7 @@ typedef NameData *Name;
#define BUFFERALIGN(LEN) TYPEALIGN(ALIGNOF_BUFFER, (LEN)) #define BUFFERALIGN(LEN) TYPEALIGN(ALIGNOF_BUFFER, (LEN))
#define TYPEALIGN_DOWN(ALIGNVAL,LEN) \ #define TYPEALIGN_DOWN(ALIGNVAL,LEN) \
(((intptr_t) (LEN)) & ~((intptr_t) ((ALIGNVAL) - 1))) (((uintptr_t) (LEN)) & ~((uintptr_t) ((ALIGNVAL) - 1)))
#define SHORTALIGN_DOWN(LEN) TYPEALIGN_DOWN(ALIGNOF_SHORT, (LEN)) #define SHORTALIGN_DOWN(LEN) TYPEALIGN_DOWN(ALIGNOF_SHORT, (LEN))
#define INTALIGN_DOWN(LEN) TYPEALIGN_DOWN(ALIGNOF_INT, (LEN)) #define INTALIGN_DOWN(LEN) TYPEALIGN_DOWN(ALIGNOF_INT, (LEN))
@ -552,7 +552,7 @@ typedef NameData *Name;
#define MAXALIGN_DOWN(LEN) TYPEALIGN_DOWN(MAXIMUM_ALIGNOF, (LEN)) #define MAXALIGN_DOWN(LEN) TYPEALIGN_DOWN(MAXIMUM_ALIGNOF, (LEN))
/* /*
* The above macros will not work with types wider than intptr_t, like with * The above macros will not work with types wider than uintptr_t, like with
* uint64 on 32-bit platforms. That's not problem for the usual use where a * uint64 on 32-bit platforms. That's not problem for the usual use where a
* pointer or a length is aligned, but for the odd case that you need to * pointer or a length is aligned, but for the odd case that you need to
* align something (potentially) wider, use TYPEALIGN64. * align something (potentially) wider, use TYPEALIGN64.
@ -763,7 +763,7 @@ typedef NameData *Name;
int _val = (val); \ int _val = (val); \
Size _len = (len); \ Size _len = (len); \
\ \
if ((((intptr_t) _vstart) & LONG_ALIGN_MASK) == 0 && \ if ((((uintptr_t) _vstart) & LONG_ALIGN_MASK) == 0 && \
(_len & LONG_ALIGN_MASK) == 0 && \ (_len & LONG_ALIGN_MASK) == 0 && \
_val == 0 && \ _val == 0 && \
_len <= MEMSET_LOOP_LIMIT && \ _len <= MEMSET_LOOP_LIMIT && \