summaryrefslogtreecommitdiffstats
path: root/Modules
diff options
context:
space:
mode:
authorChristian Heimes <christian@cheimes.de>2012-10-17 21:52:17 (GMT)
committerChristian Heimes <christian@cheimes.de>2012-10-17 21:52:17 (GMT)
commit743e0cd6b5d59767aae2524700857f188ca1e80e (patch)
tree89897c0424a3b361e04d451e2b3a64e5c7c17756 /Modules
parent1e9af84e2ef41115dd07d00b57e5a2a7041bfeed (diff)
downloadcpython-743e0cd6b5d59767aae2524700857f188ca1e80e.zip
cpython-743e0cd6b5d59767aae2524700857f188ca1e80e.tar.gz
cpython-743e0cd6b5d59767aae2524700857f188ca1e80e.tar.bz2
Issue #16166: Add PY_LITTLE_ENDIAN and PY_BIG_ENDIAN macros and unified
endianess detection and handling.
Diffstat (limited to 'Modules')
-rw-r--r--Modules/_io/textio.c18
-rwxr-xr-xModules/_sha3/cleanup.py8
-rw-r--r--Modules/_sha3/keccak/KeccakF-1600-opt32.c2
-rw-r--r--Modules/_sha3/keccak/KeccakF-1600-opt64.c2
-rw-r--r--Modules/_sha3/sha3module.c13
-rw-r--r--Modules/_struct.c21
-rw-r--r--Modules/arraymodule.c7
-rw-r--r--Modules/sha256module.c22
-rw-r--r--Modules/sha512module.c22
9 files changed, 44 insertions, 71 deletions
diff --git a/Modules/_io/textio.c b/Modules/_io/textio.c
index 96434a8..8344d43 100644
--- a/Modules/_io/textio.c
+++ b/Modules/_io/textio.c
@@ -747,7 +747,7 @@ utf16_encode(textio *self, PyObject *text)
{
if (!self->encoding_start_of_stream) {
/* Skip the BOM and use native byte ordering */
-#if defined(WORDS_BIGENDIAN)
+#if PY_BIG_ENDIAN
return utf16be_encode(self, text);
#else
return utf16le_encode(self, text);
@@ -776,7 +776,7 @@ utf32_encode(textio *self, PyObject *text)
{
if (!self->encoding_start_of_stream) {
/* Skip the BOM and use native byte ordering */
-#if defined(WORDS_BIGENDIAN)
+#if PY_BIG_ENDIAN
return utf32be_encode(self, text);
#else
return utf32le_encode(self, text);
@@ -1913,10 +1913,7 @@ typedef struct {
#define COOKIE_BUF_LEN (sizeof(Py_off_t) + 3 * sizeof(int) + sizeof(char))
-#if defined(WORDS_BIGENDIAN)
-
-# define IS_LITTLE_ENDIAN 0
-
+#if PY_BIG_ENDIAN
/* We want the least significant byte of start_pos to also be the least
significant byte of the cookie, which means that in big-endian mode we
must copy the fields in reverse order. */
@@ -1928,9 +1925,6 @@ typedef struct {
# define OFF_NEED_EOF 0
#else
-
-# define IS_LITTLE_ENDIAN 1
-
/* Little-endian mode: the least significant byte of start_pos will
naturally end up the least significant byte of the cookie. */
@@ -1951,7 +1945,7 @@ textiowrapper_parse_cookie(cookie_type *cookie, PyObject *cookieObj)
return -1;
if (_PyLong_AsByteArray(cookieLong, buffer, sizeof(buffer),
- IS_LITTLE_ENDIAN, 0) < 0) {
+ PY_LITTLE_ENDIAN, 0) < 0) {
Py_DECREF(cookieLong);
return -1;
}
@@ -1977,9 +1971,9 @@ textiowrapper_build_cookie(cookie_type *cookie)
memcpy(buffer + OFF_CHARS_TO_SKIP, &cookie->chars_to_skip, sizeof(cookie->chars_to_skip));
memcpy(buffer + OFF_NEED_EOF, &cookie->need_eof, sizeof(cookie->need_eof));
- return _PyLong_FromByteArray(buffer, sizeof(buffer), IS_LITTLE_ENDIAN, 0);
+ return _PyLong_FromByteArray(buffer, sizeof(buffer),
+ PY_LITTLE_ENDIAN, 0);
}
-#undef IS_LITTLE_ENDIAN
static int
_textiowrapper_decoder_setstate(textio *self, cookie_type *cookie)
diff --git a/Modules/_sha3/cleanup.py b/Modules/_sha3/cleanup.py
index 5238ab3..aabcb04 100755
--- a/Modules/_sha3/cleanup.py
+++ b/Modules/_sha3/cleanup.py
@@ -32,10 +32,10 @@ def cleanup(f):
if line.startswith("typedef unsigned long long int"):
buf.append("/* %s */\n" % line.strip())
continue
- ## remove #include "brg_endian.h"
- #if "brg_endian.h" in line:
- # buf.append("/* %s */\n" % line.strip())
- # continue
+ # remove #include "brg_endian.h"
+ if "brg_endian.h" in line:
+ buf.append("/* %s */\n" % line.strip())
+ continue
# transform C++ comments into ANSI C comments
line = CPP1.sub(r"/* \1 */", line)
line = CPP2.sub(r" /* \1 */", line)
diff --git a/Modules/_sha3/keccak/KeccakF-1600-opt32.c b/Modules/_sha3/keccak/KeccakF-1600-opt32.c
index 473dde5..dba6d59 100644
--- a/Modules/_sha3/keccak/KeccakF-1600-opt32.c
+++ b/Modules/_sha3/keccak/KeccakF-1600-opt32.c
@@ -12,7 +12,7 @@ http://creativecommons.org/publicdomain/zero/1.0/
*/
#include <string.h>
-#include "brg_endian.h"
+/* #include "brg_endian.h" */
#include "KeccakF-1600-opt32-settings.h"
#include "KeccakF-1600-interface.h"
diff --git a/Modules/_sha3/keccak/KeccakF-1600-opt64.c b/Modules/_sha3/keccak/KeccakF-1600-opt64.c
index 57e2aa4..f19b18b 100644
--- a/Modules/_sha3/keccak/KeccakF-1600-opt64.c
+++ b/Modules/_sha3/keccak/KeccakF-1600-opt64.c
@@ -12,7 +12,7 @@ http://creativecommons.org/publicdomain/zero/1.0/
*/
#include <string.h>
-#include "brg_endian.h"
+/* #include "brg_endian.h" */
#include "KeccakF-1600-opt64-settings.h"
#include "KeccakF-1600-interface.h"
diff --git a/Modules/_sha3/sha3module.c b/Modules/_sha3/sha3module.c
index 08f6839..4c3c6db 100644
--- a/Modules/_sha3/sha3module.c
+++ b/Modules/_sha3/sha3module.c
@@ -124,11 +124,14 @@
#define UseInterleaveTables
#endif
-/* replacement for brg_endian.h
-#define IS_BIG_ENDIAN BIG_ENDIAN
-#define IS_LITTLE_ENDIAN LITTLE_ENDIAN
-#define PLATFORM_BYTE_ORDER BYTE_ORDER
-*/
+/* replacement for brg_endian.h */
+#define IS_BIG_ENDIAN 4321
+#define IS_LITTLE_ENDIAN 1234
+#if PY_BIG_ENDIAN
+# define PLATFORM_BYTE_ORDER IS_BIG_ENDIAN
+#else
+# define PLATFORM_BYTE_ORDER IS_LITTLE_ENDIAN
+#endif
/* inline all Keccak dependencies */
#include "keccak/KeccakNISTInterface.h"
diff --git a/Modules/_struct.c b/Modules/_struct.c
index 0b20e26..0f50144 100644
--- a/Modules/_struct.c
+++ b/Modules/_struct.c
@@ -1199,12 +1199,11 @@ whichtable(char **pfmt)
case '!': /* Network byte order is big-endian */
return bigendian_table;
case '=': { /* Host byte order -- different from native in alignment! */
- int n = 1;
- char *p = (char *) &n;
- if (*p == 1)
- return lilendian_table;
- else
- return bigendian_table;
+#if PY_LITTLE_ENDIAN
+ return lilendian_table;
+#else
+ return bigendian_table;
+#endif
}
default:
--*pfmt; /* Back out of pointer increment */
@@ -2088,13 +2087,13 @@ PyInit__struct(void)
/* Check endian and swap in faster functions */
{
- int one = 1;
formatdef *native = native_table;
formatdef *other, *ptr;
- if ((int)*(unsigned char*)&one)
- other = lilendian_table;
- else
- other = bigendian_table;
+#if PY_LITTLE_ENDIAN
+ other = lilendian_table;
+#else
+ other = bigendian_table;
+#endif
/* Scan through the native table, find a matching
entry in the endian table and swap in the
native implementations whenever possible
diff --git a/Modules/arraymodule.c b/Modules/arraymodule.c
index 3f5aa8b..4d4c2ae 100644
--- a/Modules/arraymodule.c
+++ b/Modules/arraymodule.c
@@ -1664,11 +1664,8 @@ static const struct mformatdescr {
static enum machine_format_code
typecode_to_mformat_code(char typecode)
{
-#ifdef WORDS_BIGENDIAN
- const int is_big_endian = 1;
-#else
- const int is_big_endian = 0;
-#endif
+ const int is_big_endian = PY_BIG_ENDIAN;
+
size_t intsize;
int is_signed;
diff --git a/Modules/sha256module.c b/Modules/sha256module.c
index 76d91af..4abd1cd 100644
--- a/Modules/sha256module.c
+++ b/Modules/sha256module.c
@@ -21,13 +21,6 @@
#include "hashlib.h"
-/* Endianness testing and definitions */
-#define TestEndianness(variable) {int i=1; variable=PCT_BIG_ENDIAN;\
- if (*((char*)&i)==1) variable=PCT_LITTLE_ENDIAN;}
-
-#define PCT_LITTLE_ENDIAN 1
-#define PCT_BIG_ENDIAN 0
-
/* Some useful types */
typedef unsigned char SHA_BYTE;
@@ -50,7 +43,6 @@ typedef struct {
SHA_INT32 digest[8]; /* Message digest */
SHA_INT32 count_lo, count_hi; /* 64-bit bit count */
SHA_BYTE data[SHA_BLOCKSIZE]; /* SHA data buffer */
- int Endianness;
int local; /* unprocessed amount in data */
int digestsize;
} SHAobject;
@@ -58,13 +50,11 @@ typedef struct {
/* When run on a little-endian CPU we need to perform byte reversal on an
array of longwords. */
-static void longReverse(SHA_INT32 *buffer, int byteCount, int Endianness)
+#if PY_LITTLE_ENDIAN
+static void longReverse(SHA_INT32 *buffer, int byteCount)
{
SHA_INT32 value;
- if ( Endianness == PCT_BIG_ENDIAN )
- return;
-
byteCount /= sizeof(*buffer);
while (byteCount--) {
value = *buffer;
@@ -73,10 +63,10 @@ static void longReverse(SHA_INT32 *buffer, int byteCount, int Endianness)
*buffer++ = ( value << 16 ) | ( value >> 16 );
}
}
+#endif
static void SHAcopy(SHAobject *src, SHAobject *dest)
{
- dest->Endianness = src->Endianness;
dest->local = src->local;
dest->digestsize = src->digestsize;
dest->count_lo = src->count_lo;
@@ -131,7 +121,9 @@ sha_transform(SHAobject *sha_info)
SHA_INT32 S[8], W[64], t0, t1;
memcpy(W, sha_info->data, sizeof(sha_info->data));
- longReverse(W, (int)sizeof(sha_info->data), sha_info->Endianness);
+#if PY_LITTLE_ENDIAN
+ longReverse(W, (int)sizeof(sha_info->data));
+#endif
for (i = 16; i < 64; ++i) {
W[i] = Gamma1(W[i - 2]) + W[i - 7] + Gamma0(W[i - 15]) + W[i - 16];
@@ -228,7 +220,6 @@ sha_transform(SHAobject *sha_info)
static void
sha_init(SHAobject *sha_info)
{
- TestEndianness(sha_info->Endianness)
sha_info->digest[0] = 0x6A09E667L;
sha_info->digest[1] = 0xBB67AE85L;
sha_info->digest[2] = 0x3C6EF372L;
@@ -246,7 +237,6 @@ sha_init(SHAobject *sha_info)
static void
sha224_init(SHAobject *sha_info)
{
- TestEndianness(sha_info->Endianness)
sha_info->digest[0] = 0xc1059ed8L;
sha_info->digest[1] = 0x367cd507L;
sha_info->digest[2] = 0x3070dd17L;
diff --git a/Modules/sha512module.c b/Modules/sha512module.c
index 88f8a64..bfb022c 100644
--- a/Modules/sha512module.c
+++ b/Modules/sha512module.c
@@ -22,13 +22,6 @@
#ifdef PY_LONG_LONG /* If no PY_LONG_LONG, don't compile anything! */
-/* Endianness testing and definitions */
-#define TestEndianness(variable) {int i=1; variable=PCT_BIG_ENDIAN;\
- if (*((char*)&i)==1) variable=PCT_LITTLE_ENDIAN;}
-
-#define PCT_LITTLE_ENDIAN 1
-#define PCT_BIG_ENDIAN 0
-
/* Some useful types */
typedef unsigned char SHA_BYTE;
@@ -52,7 +45,6 @@ typedef struct {
SHA_INT64 digest[8]; /* Message digest */
SHA_INT32 count_lo, count_hi; /* 64-bit bit count */
SHA_BYTE data[SHA_BLOCKSIZE]; /* SHA data buffer */
- int Endianness;
int local; /* unprocessed amount in data */
int digestsize;
} SHAobject;
@@ -60,13 +52,11 @@ typedef struct {
/* When run on a little-endian CPU we need to perform byte reversal on an
array of longwords. */
-static void longReverse(SHA_INT64 *buffer, int byteCount, int Endianness)
+#if PY_LITTLE_ENDIAN
+static void longReverse(SHA_INT64 *buffer, int byteCount)
{
SHA_INT64 value;
- if ( Endianness == PCT_BIG_ENDIAN )
- return;
-
byteCount /= sizeof(*buffer);
while (byteCount--) {
value = *buffer;
@@ -83,10 +73,10 @@ static void longReverse(SHA_INT64 *buffer, int byteCount, int Endianness)
buffer++;
}
}
+#endif
static void SHAcopy(SHAobject *src, SHAobject *dest)
{
- dest->Endianness = src->Endianness;
dest->local = src->local;
dest->digestsize = src->digestsize;
dest->count_lo = src->count_lo;
@@ -141,7 +131,9 @@ sha512_transform(SHAobject *sha_info)
SHA_INT64 S[8], W[80], t0, t1;
memcpy(W, sha_info->data, sizeof(sha_info->data));
- longReverse(W, (int)sizeof(sha_info->data), sha_info->Endianness);
+#if PY_LITTLE_ENDIAN
+ longReverse(W, (int)sizeof(sha_info->data));
+#endif
for (i = 16; i < 80; ++i) {
W[i] = Gamma1(W[i - 2]) + W[i - 7] + Gamma0(W[i - 15]) + W[i - 16];
@@ -254,7 +246,6 @@ sha512_transform(SHAobject *sha_info)
static void
sha512_init(SHAobject *sha_info)
{
- TestEndianness(sha_info->Endianness)
sha_info->digest[0] = Py_ULL(0x6a09e667f3bcc908);
sha_info->digest[1] = Py_ULL(0xbb67ae8584caa73b);
sha_info->digest[2] = Py_ULL(0x3c6ef372fe94f82b);
@@ -272,7 +263,6 @@ sha512_init(SHAobject *sha_info)
static void
sha384_init(SHAobject *sha_info)
{
- TestEndianness(sha_info->Endianness)
sha_info->digest[0] = Py_ULL(0xcbbb9d5dc1059ed8);
sha_info->digest[1] = Py_ULL(0x629a292a367cd507);
sha_info->digest[2] = Py_ULL(0x9159015a3070dd17);