Skip to content
Snippets Groups Projects
Commit 0574bcd7 authored by xleroy's avatar xleroy
Browse files

Determine endianness at run-time

git-svn-id: https://yquem.inria.fr/compcert/svn/compcert/trunk@1303 fca1b0fc-160b-0410-b1d3-a4f43f01ea2e
parent 66cb2a32
No related branches found
No related tags found
No related merge requests found
...@@ -5,14 +5,6 @@ ...@@ -5,14 +5,6 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#if defined(__ppc__) || defined(__PPC__)
#define ARCH_BIG_ENDIAN
#elif defined(__i386__) || defined(__x86_64__) || defined(__ARMEL__)
#undef ARCH_BIG_ENDIAN
#else
#error "unknown endianness"
#endif
typedef unsigned int u32; typedef unsigned int u32;
struct SHA1Context { struct SHA1Context {
...@@ -26,22 +18,24 @@ struct SHA1Context { ...@@ -26,22 +18,24 @@ struct SHA1Context {
#define rol5(x) (((x) << 5) | ((x) >> 27)) #define rol5(x) (((x) << 5) | ((x) >> 27))
#define rol30(x) (((x) << 30) | ((x) >> 2)) #define rol30(x) (((x) << 30) | ((x) >> 2))
static int arch_big_endian;
static void SHA1_copy_and_swap(void * src, void * dst, int numwords) static void SHA1_copy_and_swap(void * src, void * dst, int numwords)
{ {
#ifdef ARCH_BIG_ENDIAN if (arch_big_endian) {
memcpy(dst, src, numwords * sizeof(u32)); memcpy(dst, src, numwords * sizeof(u32));
#else } else {
unsigned char * s, * d; unsigned char * s, * d;
unsigned char a, b; unsigned char a, b;
for (s = src, d = dst; numwords > 0; s += 4, d += 4, numwords--) { for (s = src, d = dst; numwords > 0; s += 4, d += 4, numwords--) {
a = s[0]; a = s[0];
b = s[1]; b = s[1];
d[0] = s[3]; d[0] = s[3];
d[1] = s[2]; d[1] = s[2];
d[2] = b; d[2] = b;
d[3] = a; d[3] = a;
}
} }
#endif
} }
#define F(x,y,z) ( z ^ (x & (y ^ z) ) ) #define F(x,y,z) ( z ^ (x & (y ^ z) ) )
...@@ -225,6 +219,14 @@ static void do_bench(int nblocks) ...@@ -225,6 +219,14 @@ static void do_bench(int nblocks)
int main(int argc, char ** argv) int main(int argc, char ** argv)
{ {
/* Determine endianness */
union { int i; unsigned char b[4]; } u;
u.i = 0x12345678;
switch (u.b[0]) {
case 0x12: arch_big_endian = 1; break;
case 0x78: arch_big_endian = 0; break;
default: printf("Cannot determine endianness\n"); return 2;
}
do_test(test_input_1, test_output_1); do_test(test_input_1, test_output_1);
do_test(test_input_2, test_output_2); do_test(test_input_2, test_output_2);
do_bench(1000000); do_bench(1000000);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment