replace Python aliases for standard integer types with the standard integer types (#17884)

This commit is contained in:
Benjamin Peterson 2016-09-06 13:24:00 -07:00
parent 88bd3edb3e
commit 9b3d77052f
8 changed files with 72 additions and 87 deletions

View File

@ -42,10 +42,10 @@ extern "C" {
*/
#if PYLONG_BITS_IN_DIGIT == 30
typedef PY_UINT32_T digit;
typedef PY_INT32_T sdigit; /* signed variant of digit */
typedef PY_UINT64_T twodigits;
typedef PY_INT64_T stwodigits; /* signed variant of twodigits */
typedef uint32_t digit;
typedef int32_t sdigit; /* signed variant of digit */
typedef uint64_t twodigits;
typedef int64_t stwodigits; /* signed variant of twodigits */
#define PyLong_SHIFT 30
#define _PyLong_DECIMAL_SHIFT 9 /* max(e such that 10**e fits in a digit) */
#define _PyLong_DECIMAL_BASE ((digit)1000000000) /* 10 ** DECIMAL_SHIFT */

View File

@ -36,14 +36,14 @@ PyAPI_FUNC(Py_hash_t) _Py_HashBytes(const void*, Py_ssize_t);
* memory layout on 64 bit systems
* cccccccc cccccccc cccccccc uc -- unsigned char[24]
* pppppppp ssssssss ........ fnv -- two Py_hash_t
* k0k0k0k0 k1k1k1k1 ........ siphash -- two PY_UINT64_T
* k0k0k0k0 k1k1k1k1 ........ siphash -- two uint64_t
* ........ ........ ssssssss djbx33a -- 16 bytes padding + one Py_hash_t
* ........ ........ eeeeeeee pyexpat XML hash salt
*
* memory layout on 32 bit systems
* cccccccc cccccccc cccccccc uc
* ppppssss ........ ........ fnv -- two Py_hash_t
* k0k0k0k0 k1k1k1k1 ........ siphash -- two PY_UINT64_T (*)
* k0k0k0k0 k1k1k1k1 ........ siphash -- two uint64_t (*)
* ........ ........ ssss.... djbx33a -- 16 bytes padding + one Py_hash_t
* ........ ........ eeee.... pyexpat XML hash salt
*
@ -59,13 +59,11 @@ typedef union {
Py_hash_t prefix;
Py_hash_t suffix;
} fnv;
#ifdef PY_UINT64_T
/* two uint64 for SipHash24 */
struct {
PY_UINT64_T k0;
PY_UINT64_T k1;
uint64_t k0;
uint64_t k1;
} siphash;
#endif
/* a different (!) Py_hash_t for small string optimization */
struct {
unsigned char padding[16];
@ -121,8 +119,7 @@ PyAPI_FUNC(PyHash_FuncDef*) PyHash_GetFuncDef(void);
* configure script.
*
* - FNV is available on all platforms and architectures.
* - SIPHASH24 only works on plaforms that provide PY_UINT64_T and doesn't
* require aligned memory for integers.
* - SIPHASH24 only works on plaforms that don't require aligned memory for integers.
* - With EXTERNAL embedders can provide an alternative implementation with::
*
* PyHash_FuncDef PyHash_Func = {...};
@ -134,8 +131,7 @@ PyAPI_FUNC(PyHash_FuncDef*) PyHash_GetFuncDef(void);
#define Py_HASH_FNV 2
#ifndef Py_HASH_ALGORITHM
# if (defined(PY_UINT64_T) && defined(PY_UINT32_T) \
&& !defined(HAVE_ALIGNED_REQUIRED))
# ifndef HAVE_ALIGNED_REQUIRED
# define Py_HASH_ALGORITHM Py_HASH_SIPHASH24
# else
# define Py_HASH_ALGORITHM Py_HASH_FNV

View File

@ -13,16 +13,12 @@ functions and constants
extern "C" {
#endif
#ifdef PY_INT64_T
/* _PyTime_t: Python timestamp with subsecond precision. It can be used to
store a duration, and so indirectly a date (related to another date, like
UNIX epoch). */
typedef PY_INT64_T _PyTime_t;
typedef int64_t _PyTime_t;
#define _PyTime_MIN PY_LLONG_MIN
#define _PyTime_MAX PY_LLONG_MAX
#else
# error "_PyTime_t need signed 64-bit integer type"
#endif
typedef enum {
/* Round towards minus infinity (-inf).

View File

@ -69,10 +69,6 @@
#include "Python.h"
#include <time.h> /* for seeding to current time */
#ifndef PY_UINT32_T
# error "Failed to find an exact-width 32-bit integer type"
#endif
/* Period parameters -- These are all magic. Don't change. */
#define N 624
#define M 397
@ -83,7 +79,7 @@
typedef struct {
PyObject_HEAD
int index;
PY_UINT32_T state[N];
uint32_t state[N];
} RandomObject;
static PyTypeObject Random_Type;
@ -95,13 +91,13 @@ static PyTypeObject Random_Type;
/* generates a random number on [0,0xffffffff]-interval */
static PY_UINT32_T
static uint32_t
genrand_int32(RandomObject *self)
{
PY_UINT32_T y;
static const PY_UINT32_T mag01[2] = {0x0U, MATRIX_A};
uint32_t y;
static const uint32_t mag01[2] = {0x0U, MATRIX_A};
/* mag01[x] = x * MATRIX_A for x=0,1 */
PY_UINT32_T *mt;
uint32_t *mt;
mt = self->state;
if (self->index >= N) { /* generate N words at one time */
@ -141,16 +137,16 @@ genrand_int32(RandomObject *self)
static PyObject *
random_random(RandomObject *self)
{
PY_UINT32_T a=genrand_int32(self)>>5, b=genrand_int32(self)>>6;
uint32_t a=genrand_int32(self)>>5, b=genrand_int32(self)>>6;
return PyFloat_FromDouble((a*67108864.0+b)*(1.0/9007199254740992.0));
}
/* initializes mt[N] with a seed */
static void
init_genrand(RandomObject *self, PY_UINT32_T s)
init_genrand(RandomObject *self, uint32_t s)
{
int mti;
PY_UINT32_T *mt;
uint32_t *mt;
mt = self->state;
mt[0]= s;
@ -170,10 +166,10 @@ init_genrand(RandomObject *self, PY_UINT32_T s)
/* init_key is the array for initializing keys */
/* key_length is its length */
static PyObject *
init_by_array(RandomObject *self, PY_UINT32_T init_key[], size_t key_length)
init_by_array(RandomObject *self, uint32_t init_key[], size_t key_length)
{
size_t i, j, k; /* was signed in the original code. RDH 12/16/2002 */
PY_UINT32_T *mt;
uint32_t *mt;
mt = self->state;
init_genrand(self, 19650218U);
@ -181,14 +177,14 @@ init_by_array(RandomObject *self, PY_UINT32_T init_key[], size_t key_length)
k = (N>key_length ? N : key_length);
for (; k; k--) {
mt[i] = (mt[i] ^ ((mt[i-1] ^ (mt[i-1] >> 30)) * 1664525U))
+ init_key[j] + (PY_UINT32_T)j; /* non linear */
+ init_key[j] + (uint32_t)j; /* non linear */
i++; j++;
if (i>=N) { mt[0] = mt[N-1]; i=1; }
if (j>=key_length) j=0;
}
for (k=N-1; k; k--) {
mt[i] = (mt[i] ^ ((mt[i-1] ^ (mt[i-1] >> 30)) * 1566083941U))
- (PY_UINT32_T)i; /* non linear */
- (uint32_t)i; /* non linear */
i++;
if (i>=N) { mt[0] = mt[N-1]; i=1; }
}
@ -208,7 +204,7 @@ random_seed(RandomObject *self, PyObject *args)
{
PyObject *result = NULL; /* guilty until proved innocent */
PyObject *n = NULL;
PY_UINT32_T *key = NULL;
uint32_t *key = NULL;
size_t bits, keyused;
int res;
PyObject *arg = NULL;
@ -220,7 +216,7 @@ random_seed(RandomObject *self, PyObject *args)
time_t now;
time(&now);
init_genrand(self, (PY_UINT32_T)now);
init_genrand(self, (uint32_t)now);
Py_INCREF(Py_None);
return Py_None;
}
@ -248,7 +244,7 @@ random_seed(RandomObject *self, PyObject *args)
keyused = bits == 0 ? 1 : (bits - 1) / 32 + 1;
/* Convert seed to byte sequence. */
key = (PY_UINT32_T *)PyMem_Malloc((size_t)4 * keyused);
key = (uint32_t *)PyMem_Malloc((size_t)4 * keyused);
if (key == NULL) {
PyErr_NoMemory();
goto Done;
@ -267,7 +263,7 @@ random_seed(RandomObject *self, PyObject *args)
size_t i, j;
/* Reverse an array. */
for (i = 0, j = keyused - 1; i < j; i++, j--) {
PY_UINT32_T tmp = key[i];
uint32_t tmp = key[i];
key[i] = key[j];
key[j] = tmp;
}
@ -329,7 +325,7 @@ random_setstate(RandomObject *self, PyObject *state)
element = PyLong_AsUnsignedLong(PyTuple_GET_ITEM(state, i));
if (element == (unsigned long)-1 && PyErr_Occurred())
return NULL;
self->state[i] = (PY_UINT32_T)element;
self->state[i] = (uint32_t)element;
}
index = PyLong_AsLong(PyTuple_GET_ITEM(state, i));
@ -349,8 +345,8 @@ static PyObject *
random_getrandbits(RandomObject *self, PyObject *args)
{
int k, i, words;
PY_UINT32_T r;
PY_UINT32_T *wordarray;
uint32_t r;
uint32_t *wordarray;
PyObject *result;
if (!PyArg_ParseTuple(args, "i:getrandbits", &k))
@ -366,7 +362,7 @@ random_getrandbits(RandomObject *self, PyObject *args)
return PyLong_FromUnsignedLong(genrand_int32(self) >> (32 - k));
words = (k - 1) / 32 + 1;
wordarray = (PY_UINT32_T *)PyMem_Malloc(words * 4);
wordarray = (uint32_t *)PyMem_Malloc(words * 4);
if (wordarray == NULL) {
PyErr_NoMemory();
return NULL;

View File

@ -98,14 +98,14 @@ test_sizeof_c_types(PyObject *self)
CHECK_SIGNNESS(Py_UCS1, 0);
CHECK_SIGNNESS(Py_UCS2, 0);
CHECK_SIGNNESS(Py_UCS4, 0);
CHECK_SIZEOF(PY_INT32_T, 4);
CHECK_SIGNNESS(PY_INT32_T, 1);
CHECK_SIZEOF(PY_UINT32_T, 4);
CHECK_SIGNNESS(PY_UINT32_T, 0);
CHECK_SIZEOF(PY_INT64_T, 8);
CHECK_SIGNNESS(PY_INT64_T, 1);
CHECK_SIZEOF(PY_UINT64_T, 8);
CHECK_SIGNNESS(PY_UINT64_T, 0);
CHECK_SIZEOF(int32_t, 4);
CHECK_SIGNNESS(int32_t, 1);
CHECK_SIZEOF(uint32_t, 4);
CHECK_SIGNNESS(uint32_t, 0);
CHECK_SIZEOF(int64_t, 8);
CHECK_SIGNNESS(int64_t, 1);
CHECK_SIZEOF(uint64_t, 8);
CHECK_SIGNNESS(uint64_t, 0);
/* pointer/size types */
CHECK_SIZEOF(size_t, sizeof(void *));

View File

@ -297,7 +297,7 @@ static const int stepsizeTable[89] = {
#define GETINT8(cp, i) GETINTX(signed char, (cp), (i))
#define GETINT16(cp, i) GETINTX(short, (cp), (i))
#define GETINT32(cp, i) GETINTX(PY_INT32_T, (cp), (i))
#define GETINT32(cp, i) GETINTX(int32_t, (cp), (i))
#if WORDS_BIGENDIAN
#define GETINT24(cp, i) ( \
@ -314,7 +314,7 @@ static const int stepsizeTable[89] = {
#define SETINT8(cp, i, val) SETINTX(signed char, (cp), (i), (val))
#define SETINT16(cp, i, val) SETINTX(short, (cp), (i), (val))
#define SETINT32(cp, i, val) SETINTX(PY_INT32_T, (cp), (i), (val))
#define SETINT32(cp, i, val) SETINTX(int32_t, (cp), (i), (val))
#if WORDS_BIGENDIAN
#define SETINT24(cp, i, val) do { \
@ -1129,7 +1129,7 @@ audioop_bias_impl(PyObject *module, Py_buffer *fragment, int width, int bias)
val = ((unsigned int)GETINT24(fragment->buf, i)) & 0xffffffu;
else {
assert(width == 4);
val = GETINTX(PY_UINT32_T, fragment->buf, i);
val = GETINTX(uint32_t, fragment->buf, i);
}
val += (unsigned int)bias;
@ -1144,7 +1144,7 @@ audioop_bias_impl(PyObject *module, Py_buffer *fragment, int width, int bias)
SETINT24(ncp, i, (int)val);
else {
assert(width == 4);
SETINTX(PY_UINT32_T, ncp, i, val);
SETINTX(uint32_t, ncp, i, val);
}
}
return rv;

View File

@ -5330,7 +5330,7 @@ _PyUnicode_EncodeUTF32(PyObject *str,
const void *data;
Py_ssize_t len;
PyObject *v;
PY_UINT32_T *out;
uint32_t *out;
#if PY_LITTLE_ENDIAN
int native_ordering = byteorder <= 0;
#else
@ -5361,7 +5361,7 @@ _PyUnicode_EncodeUTF32(PyObject *str,
/* output buffer is 4-bytes aligned */
assert(_Py_IS_ALIGNED(PyBytes_AS_STRING(v), 4));
out = (PY_UINT32_T *)PyBytes_AS_STRING(v);
out = (uint32_t *)PyBytes_AS_STRING(v);
if (byteorder == 0)
*out++ = 0xFEFF;
if (len == 0)
@ -5427,7 +5427,7 @@ _PyUnicode_EncodeUTF32(PyObject *str,
/* four bytes are reserved for each surrogate */
if (moreunits > 1) {
Py_ssize_t outpos = out - (PY_UINT32_T*) PyBytes_AS_STRING(v);
Py_ssize_t outpos = out - (uint32_t*) PyBytes_AS_STRING(v);
Py_ssize_t morebytes = 4 * (moreunits - 1);
if (PyBytes_GET_SIZE(v) > PY_SSIZE_T_MAX - morebytes) {
/* integer overflow */
@ -5436,7 +5436,7 @@ _PyUnicode_EncodeUTF32(PyObject *str,
}
if (_PyBytes_Resize(&v, PyBytes_GET_SIZE(v) + morebytes) < 0)
goto error;
out = (PY_UINT32_T*) PyBytes_AS_STRING(v) + outpos;
out = (uint32_t*) PyBytes_AS_STRING(v) + outpos;
}
if (PyBytes_Check(rep)) {

View File

@ -318,40 +318,37 @@ static PyHash_FuncDef PyHash_Func = {fnv, "fnv", 8 * SIZEOF_PY_HASH_T,
Modified for Python by Christian Heimes:
- C89 / MSVC compatibility
- PY_UINT64_T, PY_UINT32_T and PY_UINT8_T
- _rotl64() on Windows
- letoh64() fallback
*/
typedef unsigned char PY_UINT8_T;
/* byte swap little endian to host endian
* Endian conversion not only ensures that the hash function returns the same
* value on all platforms. It is also required to for a good dispersion of
* the hash values' least significant bits.
*/
#if PY_LITTLE_ENDIAN
# define _le64toh(x) ((PY_UINT64_T)(x))
# define _le64toh(x) ((uint64_t)(x))
#elif defined(__APPLE__)
# define _le64toh(x) OSSwapLittleToHostInt64(x)
#elif defined(HAVE_LETOH64)
# define _le64toh(x) le64toh(x)
#else
# define _le64toh(x) (((PY_UINT64_T)(x) << 56) | \
(((PY_UINT64_T)(x) << 40) & 0xff000000000000ULL) | \
(((PY_UINT64_T)(x) << 24) & 0xff0000000000ULL) | \
(((PY_UINT64_T)(x) << 8) & 0xff00000000ULL) | \
(((PY_UINT64_T)(x) >> 8) & 0xff000000ULL) | \
(((PY_UINT64_T)(x) >> 24) & 0xff0000ULL) | \
(((PY_UINT64_T)(x) >> 40) & 0xff00ULL) | \
((PY_UINT64_T)(x) >> 56))
# define _le64toh(x) (((uint64_t)(x) << 56) | \
(((uint64_t)(x) << 40) & 0xff000000000000ULL) | \
(((uint64_t)(x) << 24) & 0xff0000000000ULL) | \
(((uint64_t)(x) << 8) & 0xff00000000ULL) | \
(((uint64_t)(x) >> 8) & 0xff000000ULL) | \
(((uint64_t)(x) >> 24) & 0xff0000ULL) | \
(((uint64_t)(x) >> 40) & 0xff00ULL) | \
((uint64_t)(x) >> 56))
#endif
#ifdef _MSC_VER
# define ROTATE(x, b) _rotl64(x, b)
#else
# define ROTATE(x, b) (PY_UINT64_T)( ((x) << (b)) | ( (x) >> (64 - (b))) )
# define ROTATE(x, b) (uint64_t)( ((x) << (b)) | ( (x) >> (64 - (b))) )
#endif
#define HALF_ROUND(a,b,c,d,s,t) \
@ -369,22 +366,22 @@ typedef unsigned char PY_UINT8_T;
static Py_hash_t
siphash24(const void *src, Py_ssize_t src_sz) {
PY_UINT64_T k0 = _le64toh(_Py_HashSecret.siphash.k0);
PY_UINT64_T k1 = _le64toh(_Py_HashSecret.siphash.k1);
PY_UINT64_T b = (PY_UINT64_T)src_sz << 56;
const PY_UINT64_T *in = (PY_UINT64_T*)src;
uint64_t k0 = _le64toh(_Py_HashSecret.siphash.k0);
uint64_t k1 = _le64toh(_Py_HashSecret.siphash.k1);
uint64_t b = (uint64_t)src_sz << 56;
const uint64_t *in = (uint64_t*)src;
PY_UINT64_T v0 = k0 ^ 0x736f6d6570736575ULL;
PY_UINT64_T v1 = k1 ^ 0x646f72616e646f6dULL;
PY_UINT64_T v2 = k0 ^ 0x6c7967656e657261ULL;
PY_UINT64_T v3 = k1 ^ 0x7465646279746573ULL;
uint64_t v0 = k0 ^ 0x736f6d6570736575ULL;
uint64_t v1 = k1 ^ 0x646f72616e646f6dULL;
uint64_t v2 = k0 ^ 0x6c7967656e657261ULL;
uint64_t v3 = k1 ^ 0x7465646279746573ULL;
PY_UINT64_T t;
PY_UINT8_T *pt;
PY_UINT8_T *m;
uint64_t t;
uint8_t *pt;
uint8_t *m;
while (src_sz >= 8) {
PY_UINT64_T mi = _le64toh(*in);
uint64_t mi = _le64toh(*in);
in += 1;
src_sz -= 8;
v3 ^= mi;
@ -393,13 +390,13 @@ siphash24(const void *src, Py_ssize_t src_sz) {
}
t = 0;
pt = (PY_UINT8_T *)&t;
m = (PY_UINT8_T *)in;
pt = (uint8_t *)&t;
m = (uint8_t *)in;
switch (src_sz) {
case 7: pt[6] = m[6];
case 6: pt[5] = m[5];
case 5: pt[4] = m[4];
case 4: Py_MEMCPY(pt, m, sizeof(PY_UINT32_T)); break;
case 4: Py_MEMCPY(pt, m, sizeof(uint32_t)); break;
case 3: pt[2] = m[2];
case 2: pt[1] = m[1];
case 1: pt[0] = m[0];