diff -r b9a6592c6250 Modules/_ctypes/_ctypes.c --- a/Modules/_ctypes/_ctypes.c Fri Jun 22 14:11:58 2012 -0400 +++ b/Modules/_ctypes/_ctypes.c Fri Jun 22 22:35:38 2012 +0300 @@ -3042,7 +3042,7 @@ address = (PPROC)GetProcAddress(handle, name); if (address) return address; - if (((size_t)name & ~0xFFFF) == 0) { + if (((Py_uintptr_t)name & ~(Py_uintptr_t)0xFFFF) == 0) { return NULL; } diff -r b9a6592c6250 Modules/_elementtree.c --- a/Modules/_elementtree.c Fri Jun 22 14:11:58 2012 -0400 +++ b/Modules/_elementtree.c Fri Jun 22 22:35:38 2012 +0300 @@ -98,7 +98,7 @@ info. */ #define JOIN_GET(p) ((Py_uintptr_t) (p) & 1) #define JOIN_SET(p, flag) ((void*) ((Py_uintptr_t) (JOIN_OBJ(p)) | (flag))) -#define JOIN_OBJ(p) ((PyObject*) ((Py_uintptr_t) (p) & ~1)) +#define JOIN_OBJ(p) ((PyObject*) ((Py_uintptr_t) (p) & ~(Py_uintptr_t)1)) /* glue functions (see the init function for details) */ static PyObject* elementtree_parseerror_obj; diff -r b9a6592c6250 Objects/stringlib/codecs.h --- a/Objects/stringlib/codecs.h Fri Jun 22 14:11:58 2012 -0400 +++ b/Objects/stringlib/codecs.h Fri Jun 22 22:35:38 2012 +0300 @@ -2,9 +2,6 @@ #if STRINGLIB_IS_UNICODE -/* Mask to check or force alignment of a pointer to C 'long' boundaries */ -#define LONG_PTR_MASK (size_t) (SIZEOF_LONG - 1) - /* Mask to quickly check whether a C 'long' contains a non-ASCII, UTF8-encoded char. */ #if (SIZEOF_LONG == 8) @@ -22,7 +19,7 @@ { Py_UCS4 ch; const char *s = *inptr; - const char *aligned_end = (const char *) ((size_t) end & ~LONG_PTR_MASK); + const char *aligned_end = (const char *) Py_ALIGN_DOWN(end, SIZEOF_LONG); STRINGLIB_CHAR *p = dest + *outpos; while (s < end) { @@ -36,7 +33,7 @@ First, check if we can do an aligned read, as most CPUs have a penalty for unaligned reads. */ - if (!((size_t) s & LONG_PTR_MASK)) { + if (Py_IS_ALIGNED(s, SIZEOF_LONG)) { /* Help register allocation */ register const char *_s = s; register STRINGLIB_CHAR *_p = p; @@ -449,7 +446,7 @@ { Py_UCS4 ch; const unsigned char *aligned_end = - (const unsigned char *) ((size_t) e & ~LONG_PTR_MASK); + (const unsigned char *) Py_ALIGN_DOWN(e, SIZEOF_LONG); const unsigned char *q = *inptr; STRINGLIB_CHAR *p = dest + *outpos; /* Offsets from q for retrieving byte pairs in the right order. */ @@ -464,7 +461,7 @@ Py_UCS4 ch2; /* First check for possible aligned read of a C 'long'. Unaligned reads are more expensive, better to defer to another iteration. */ - if (!((size_t) q & LONG_PTR_MASK)) { + if (Py_IS_ALIGNED(q, SIZEOF_LONG)) { /* Fast path for runs of in-range non-surrogate chars. */ register const unsigned char *_q = q; while (_q < aligned_end) { @@ -561,7 +558,6 @@ #undef FAST_CHAR_MASK #undef STRIPPED_MASK #undef SWAB -#undef LONG_PTR_MASK Py_LOCAL_INLINE(void) @@ -584,7 +580,7 @@ _PyUnicode_CONVERT_BYTES(STRINGLIB_CHAR, unsigned short, in, end, out); # endif } else { - const STRINGLIB_CHAR *unrolled_end = in + (len & ~ (Py_ssize_t) 3); + const STRINGLIB_CHAR *unrolled_end = in + Py_SIZE_ROUND_DOWN(len, 4); while (in < unrolled_end) { out[0] = SWAB2(in[0]); out[1] = SWAB2(in[1]); diff -r b9a6592c6250 Objects/stringlib/fastsearch.h --- a/Objects/stringlib/fastsearch.h Fri Jun 22 14:11:58 2012 -0400 +++ b/Objects/stringlib/fastsearch.h Fri Jun 22 22:35:38 2012 +0300 @@ -44,7 +44,7 @@ #define DO_MEMCHR(memchr, s, needle, nchars) do { \ candidate = memchr((const void *) (s), (needle), (nchars) * sizeof(STRINGLIB_CHAR)); \ found = (const STRINGLIB_CHAR *) \ - ((Py_ssize_t) candidate & (~ ((Py_ssize_t) sizeof(STRINGLIB_CHAR) - 1))); \ + ((Py_uintptr_t) candidate & ~ (Py_uintptr_t) (sizeof(STRINGLIB_CHAR) - 1)); \ } while (0) if (mode == FAST_SEARCH) { diff -r b9a6592c6250 Objects/stringlib/find_max_char.h --- a/Objects/stringlib/find_max_char.h Fri Jun 22 14:11:58 2012 -0400 +++ b/Objects/stringlib/find_max_char.h Fri Jun 22 22:35:38 2012 +0300 @@ -2,9 +2,6 @@ #if STRINGLIB_IS_UNICODE -/* Mask to check or force alignment of a pointer to C 'long' boundaries */ -#define LONG_PTR_MASK (size_t) (SIZEOF_LONG - 1) - /* Mask to quickly check whether a C 'long' contains a non-ASCII, UTF8-encoded char. */ #if (SIZEOF_LONG == 8) @@ -21,10 +18,11 @@ STRINGLIB(find_max_char)(const STRINGLIB_CHAR *begin, const STRINGLIB_CHAR *end) { const unsigned char *p = (const unsigned char *) begin; - const unsigned char *aligned_end = (const unsigned char *) ((size_t) end & ~LONG_PTR_MASK); + const unsigned char *aligned_end = + (const unsigned char *) Py_ALIGN_DOWN(end, SIZEOF_LONG); while (p < end) { - if (!((size_t) p & LONG_PTR_MASK)) { + if (Py_IS_ALIGNED(p, SIZEOF_LONG)) { /* Help register allocation */ register const unsigned char *_p = p; while (_p < aligned_end) { @@ -43,7 +41,6 @@ return 127; } -#undef LONG_PTR_MASK #undef ASCII_CHAR_MASK #else /* STRINGLIB_SIZEOF_CHAR == 1 */ @@ -72,7 +69,7 @@ register Py_UCS4 mask; Py_ssize_t n = end - begin; const STRINGLIB_CHAR *p = begin; - const STRINGLIB_CHAR *unrolled_end = begin + (n & ~ (Py_ssize_t) 3); + const STRINGLIB_CHAR *unrolled_end = begin + Py_SIZE_ROUND_DOWN(n, 4); Py_UCS4 max_char; max_char = MAX_CHAR_ASCII; diff -r b9a6592c6250 Objects/unicodeobject.c --- a/Objects/unicodeobject.c Fri Jun 22 14:11:58 2012 -0400 +++ b/Objects/unicodeobject.c Fri Jun 22 22:35:38 2012 +0300 @@ -55,6 +55,10 @@ # define BYTEORDER_IS_LITTLE_ENDIAN #endif +#define Py_SIZE_ROUND_DOWN(n, a) ((size_t)(n) & ~(size_t)((a) - 1)) +#define Py_ALIGN_DOWN(p, a) ((void *)((Py_uintptr_t)(p) & ~(Py_uintptr_t)((a) - 1))) +#define Py_IS_ALIGNED(p, a) (!((Py_uintptr_t)(p) & (Py_uintptr_t)((a) - 1))) + /* --- Globals ------------------------------------------------------------ The globals are initialized by the _PyUnicode_Init() API and should @@ -159,7 +163,7 @@ const from_type *_end = (end); \ Py_ssize_t n = (_end) - (_iter); \ const from_type *_unrolled_end = \ - _iter + (n & ~ (Py_ssize_t) 3); \ + _iter + Py_SIZE_ROUND_DOWN(n, 4); \ while (_iter < (_unrolled_end)) { \ _to[0] = (to_type) _iter[0]; \ _to[1] = (to_type) _iter[1]; \ @@ -4633,9 +4637,6 @@ #include "stringlib/codecs.h" #include "stringlib/undef.h" -/* Mask to check or force alignment of a pointer to C 'long' boundaries */ -#define LONG_PTR_MASK (size_t) (SIZEOF_LONG - 1) - /* Mask to quickly check whether a C 'long' contains a non-ASCII, UTF8-encoded char. */ #if (SIZEOF_LONG == 8) @@ -4650,11 +4651,11 @@ ascii_decode(const char *start, const char *end, Py_UCS1 *dest) { const char *p = start; - const char *aligned_end = (const char *) ((size_t) end & ~LONG_PTR_MASK); + const char *aligned_end = (const char *) Py_ALIGN_DOWN(end, SIZEOF_LONG); #if SIZEOF_LONG <= SIZEOF_VOID_P - assert(!((size_t) dest & LONG_PTR_MASK)); - if (!((size_t) p & LONG_PTR_MASK)) { + assert(Py_IS_ALIGNED(dest, SIZEOF_LONG)); + if (Py_IS_ALIGNED(p, SIZEOF_LONG)) { /* Fast path, see in STRINGLIB(utf8_decode) for an explanation. */ /* Help register allocation */ @@ -4680,7 +4681,7 @@ while (p < end) { /* Fast path, see in STRINGLIB(utf8_decode) in stringlib/codecs.h for an explanation. */ - if (!((size_t) p & LONG_PTR_MASK)) { + if (Py_IS_ALIGNED(p, SIZEOF_LONG)) { /* Help register allocation */ register const char *_p = p; while (_p < aligned_end) { @@ -5388,7 +5389,7 @@ return NULL; /* output buffer is 2-bytes aligned */ - assert(((Py_uintptr_t)PyBytes_AS_STRING(v) & 1) == 0); + assert(Py_IS_ALIGNED(PyBytes_AS_STRING(v), 2)); out = (unsigned short *)PyBytes_AS_STRING(v); if (byteorder == 0) *out++ = 0xFEFF;