From eae68c81ad7089b92e79c1ebf526a978a541de65 Mon Sep 17 00:00:00 2001 From: Ankith Date: Fri, 29 Mar 2024 17:42:39 +0530 Subject: [PATCH] Fix arm32 compiler warnings --- src_c/bitmask.c | 80 ++++++++++++++++++----------------------- src_c/color.c | 7 +++- src_c/include/bitmask.h | 13 ++++++- src_c/mixer.c | 3 +- 4 files changed, 55 insertions(+), 48 deletions(-) diff --git a/src_c/bitmask.c b/src_c/bitmask.c index b434936f31..903f43efc0 100644 --- a/src_c/bitmask.c +++ b/src_c/bitmask.c @@ -39,54 +39,44 @@ static INLINE unsigned int bitcount(BITMASK_W n) { - const int bitmask_len = BITMASK_W_LEN; - if (bitmask_len == 32) { +#if BITMASK_W_LEN == 32 #ifdef GILLIES - /* (C) Donald W. Gillies, 1992. All rights reserved. You may reuse - this bitcount() function anywhere you please as long as you retain - this Copyright Notice. */ - register unsigned long tmp; - return (tmp = (n) - (((n) >> 1) & 033333333333) - - (((n) >> 2) & 011111111111), - tmp = ((tmp + (tmp >> 3)) & 030707070707), - tmp = (tmp + (tmp >> 6)), - tmp = (tmp + (tmp >> 12) + (tmp >> 24)) & 077); + /* (C) Donald W. Gillies, 1992. All rights reserved. You may reuse + this bitcount() function anywhere you please as long as you retain + this Copyright Notice. */ + register unsigned long tmp; + return ( + tmp = (n) - (((n) >> 1) & 033333333333) - (((n) >> 2) & 011111111111), + tmp = ((tmp + (tmp >> 3)) & 030707070707), tmp = (tmp + (tmp >> 6)), + tmp = (tmp + (tmp >> 12) + (tmp >> 24)) & 077); /* End of Donald W. Gillies bitcount code */ -#else - /* This piece taken from Jorg Arndt's "Algorithms for Programmers" */ - n = ((n >> 1) & 0x55555555) + (n & 0x55555555); // 0-2 in 2 bits - n = ((n >> 2) & 0x33333333) + (n & 0x33333333); // 0-4 in 4 bits - n = ((n >> 4) + n) & 0x0f0f0f0f; // 0-8 in 4 bits - n += n >> 8; // 0-16 in 8 bits - n += n >> 16; // 0-32 in 8 bits - return n & 0xff; -#endif - } - else if (bitmask_len == 64) { - n = ((n >> 1) & 0x5555555555555555) + (n & 0x5555555555555555); - n = ((n >> 2) & 0x3333333333333333) + (n & 0x3333333333333333); - n = ((n >> 4) + n) & 0x0f0f0f0f0f0f0f0f; - n += n >> 8; - n += n >> 16; -#ifdef _WIN32 - /* Use explicit typecast to silence MSVC warning about large bitshift, - * even though this part code does not run on windows */ - n += (long long)n >> 32; -#else - n += n >> 32; -#endif - return n & 0xff; - } - else { - /* Handle non-32 or 64 bit case the slow way */ - unsigned int nbits = 0; - while (n) { - if (n & 1) - nbits++; - n = n >> 1; - } - return nbits; +#else /* ~GILLIES */ + /* This piece taken from Jorg Arndt's "Algorithms for Programmers" */ + n = ((n >> 1) & 0x55555555) + (n & 0x55555555); // 0-2 in 2 bits + n = ((n >> 2) & 0x33333333) + (n & 0x33333333); // 0-4 in 4 bits + n = ((n >> 4) + n) & 0x0f0f0f0f; // 0-8 in 4 bits + n += n >> 8; // 0-16 in 8 bits + n += n >> 16; // 0-32 in 8 bits + return n & 0xff; +#endif /* ~GILLIES */ +#elif BITMASK_W_LEN == 64 + n = ((n >> 1) & 0x5555555555555555) + (n & 0x5555555555555555); + n = ((n >> 2) & 0x3333333333333333) + (n & 0x3333333333333333); + n = ((n >> 4) + n) & 0x0f0f0f0f0f0f0f0f; + n += n >> 8; + n += n >> 16; + n += n >> 32; + return n & 0xff; +#else /* BITMASK_W_LEN */ + /* Handle non-32 or 64 bit case the slow way */ + unsigned int nbits = 0; + while (n) { + if (n & 1) + nbits++; + n = n >> 1; } + return nbits; +#endif /* BITMASK_W_LEN */ } /* Positive modulo of the given dividend and divisor (dividend % divisor). diff --git a/src_c/color.c b/src_c/color.c index cb025bed68..0273c999e4 100644 --- a/src_c/color.c +++ b/src_c/color.c @@ -2356,9 +2356,14 @@ _pg_pylong_to_uint32(PyObject *val, Uint32 *color, int handle_negative) * with most significant bit set could be incorrectly exposed as * negative */ - if (longval > 0xFFFFFFFF || (longval < 0 && !handle_negative)) { + if (longval < 0 && !handle_negative) { goto error; } +#if LONG_BIT > 32 + if (longval > 0xFFFFFFFF) { + goto error; + } +#endif *color = (Uint32)longval; return 1; diff --git a/src_c/include/bitmask.h b/src_c/include/bitmask.h index eee09b707e..edbd16d96d 100644 --- a/src_c/include/bitmask.h +++ b/src_c/include/bitmask.h @@ -26,6 +26,8 @@ extern "C" { #endif #include +#include + /* Define INLINE for different compilers. If your compiler does not support inlining then there might be a performance hit in bitmask_overlap_area(). @@ -42,8 +44,17 @@ extern "C" { #endif #endif +#ifndef LONG_BIT +/* Compat code for when LONG_BIT isn't defined */ +#if LONG_MAX == 2147483647 +#define LONG_BIT 32 +#else +#define LONG_BIT 64 +#endif +#endif + #define BITMASK_W unsigned long int -#define BITMASK_W_LEN (sizeof(BITMASK_W) * CHAR_BIT) +#define BITMASK_W_LEN LONG_BIT #define BITMASK_W_MASK (BITMASK_W_LEN - 1) #define BITMASK_N(n) ((BITMASK_W)1 << (n)) diff --git a/src_c/mixer.c b/src_c/mixer.c index f2f4ecb1f7..e3a26aef1b 100644 --- a/src_c/mixer.c +++ b/src_c/mixer.c @@ -255,7 +255,8 @@ _format_view_to_audio(Py_buffer *view) view->format); return 0; } - if (view->itemsize && PG_SAMPLE_SIZE(format) != view->itemsize) { + if (view->itemsize && + (Py_ssize_t)PG_SAMPLE_SIZE(format) != view->itemsize) { PyErr_Format(PyExc_ValueError, "Array item size %d does not match format '%s'", (int)view->itemsize, view->format);