1 | /* |
1 | /* |
2 | * libecb - http://software.schmorp.de/pkg/libecb |
2 | * libecb - http://software.schmorp.de/pkg/libecb |
3 | * |
3 | * |
4 | * Copyright (©) 2009-2011 Marc Alexander Lehmann <libecb@schmorp.de> |
4 | * Copyright (©) 2009-2012 Marc Alexander Lehmann <libecb@schmorp.de> |
5 | * Copyright (©) 2011 Emanuele Giaquinta |
5 | * Copyright (©) 2011 Emanuele Giaquinta |
6 | * All rights reserved. |
6 | * All rights reserved. |
7 | * |
7 | * |
8 | * Redistribution and use in source and binary forms, with or without modifica- |
8 | * Redistribution and use in source and binary forms, with or without modifica- |
9 | * tion, are permitted provided that the following conditions are met: |
9 | * tion, are permitted provided that the following conditions are met: |
… | |
… | |
66 | /*****************************************************************************/ |
66 | /*****************************************************************************/ |
67 | |
67 | |
68 | /* ECB_NO_THREADS - ecb is not used by multiple threads, ever */ |
68 | /* ECB_NO_THREADS - ecb is not used by multiple threads, ever */ |
69 | /* ECB_NO_SMP - ecb might be used in multiple threads, but only on a single cpu */ |
69 | /* ECB_NO_SMP - ecb might be used in multiple threads, but only on a single cpu */ |
70 | |
70 | |
|
|
71 | #if ECB_NO_THREADS |
|
|
72 | # define ECB_NO_SMP 1 |
|
|
73 | #endif |
|
|
74 | |
71 | #if ECB_NO_THREADS || ECB_NO_SMP |
75 | #if ECB_NO_THREADS || ECB_NO_SMP |
72 | #define ECB_MEMORY_FENCE do { } while (0) |
76 | #define ECB_MEMORY_FENCE do { } while (0) |
73 | #endif |
77 | #endif |
74 | |
78 | |
75 | #ifndef ECB_MEMORY_FENCE |
79 | #ifndef ECB_MEMORY_FENCE |
76 | #if ECB_GCC_VERSION(2,5) || defined(__INTEL_COMPILER) || defined(__clang__) |
80 | #if ECB_GCC_VERSION(2,5) || defined(__INTEL_COMPILER) || (__llvm__ && __GNUC__) || __SUNPRO_C >= 0x5110 || __SUNPRO_CC >= 0x5110 |
77 | #if __i386__ |
81 | #if __i386 || __i386__ |
78 | #define ECB_MEMORY_FENCE __asm__ __volatile__ ("lock; orb $0, -1(%%esp)" : : : "memory") |
82 | #define ECB_MEMORY_FENCE __asm__ __volatile__ ("lock; orb $0, -1(%%esp)" : : : "memory") |
79 | #define ECB_MEMORY_FENCE_ACQUIRE ECB_MEMORY_FENCE /* non-lock xchg might be enough */ |
83 | #define ECB_MEMORY_FENCE_ACQUIRE ECB_MEMORY_FENCE /* non-lock xchg might be enough */ |
80 | #define ECB_MEMORY_FENCE_RELEASE do { } while (0) /* unlikely to change in future cpus */ |
84 | #define ECB_MEMORY_FENCE_RELEASE do { } while (0) /* unlikely to change in future cpus */ |
81 | #elif __amd64 |
85 | #elif __amd64 || __amd64__ || __x86_64 || __x86_64__ |
82 | #define ECB_MEMORY_FENCE __asm__ __volatile__ ("mfence" : : : "memory") |
86 | #define ECB_MEMORY_FENCE __asm__ __volatile__ ("mfence" : : : "memory") |
83 | #define ECB_MEMORY_FENCE_ACQUIRE __asm__ __volatile__ ("lfence" : : : "memory") |
87 | #define ECB_MEMORY_FENCE_ACQUIRE __asm__ __volatile__ ("lfence" : : : "memory") |
84 | #define ECB_MEMORY_FENCE_RELEASE __asm__ __volatile__ ("sfence") /* play safe - not needed in any current cpu */ |
88 | #define ECB_MEMORY_FENCE_RELEASE __asm__ __volatile__ ("sfence") /* play safe - not needed in any current cpu */ |
85 | #elif __powerpc__ || __ppc__ || __powerpc64__ || __ppc64__ |
89 | #elif __powerpc__ || __ppc__ || __powerpc64__ || __ppc64__ |
86 | #define ECB_MEMORY_FENCE __asm__ __volatile__ ("sync" : : : "memory") |
90 | #define ECB_MEMORY_FENCE __asm__ __volatile__ ("sync" : : : "memory") |
… | |
… | |
88 | || defined(__ARM_ARCH_6K__) || defined(__ARM_ARCH_6ZK__) |
92 | || defined(__ARM_ARCH_6K__) || defined(__ARM_ARCH_6ZK__) |
89 | #define ECB_MEMORY_FENCE __asm__ __volatile__ ("mcr p15,0,%0,c7,c10,5" : : "r" (0) : "memory") |
93 | #define ECB_MEMORY_FENCE __asm__ __volatile__ ("mcr p15,0,%0,c7,c10,5" : : "r" (0) : "memory") |
90 | #elif defined(__ARM_ARCH_7__ ) || defined(__ARM_ARCH_7A__ ) \ |
94 | #elif defined(__ARM_ARCH_7__ ) || defined(__ARM_ARCH_7A__ ) \ |
91 | || defined(__ARM_ARCH_7M__) || defined(__ARM_ARCH_7R__ ) |
95 | || defined(__ARM_ARCH_7M__) || defined(__ARM_ARCH_7R__ ) |
92 | #define ECB_MEMORY_FENCE __asm__ __volatile__ ("dmb" : : : "memory") |
96 | #define ECB_MEMORY_FENCE __asm__ __volatile__ ("dmb" : : : "memory") |
|
|
97 | #elif __sparc || __sparc__ |
|
|
98 | #define ECB_MEMORY_FENCE __asm__ __volatile__ ("membar #LoadStore | #LoadLoad | #StoreStore | #StoreLoad | " : : : "memory") |
|
|
99 | #define ECB_MEMORY_FENCE_ACQUIRE __asm__ __volatile__ ("membar #LoadStore | #LoadLoad" : : : "memory") |
|
|
100 | #define ECB_MEMORY_FENCE_RELEASE __asm__ __volatile__ ("membar #LoadStore | #StoreStore") |
|
|
101 | #elif defined(__s390__) || defined(__s390x__) |
|
|
102 | #define ECB_MEMORY_FENCE __asm__ __volatile__ ("bcr 15,0" : : : "memory") |
|
|
103 | #elif defined(__mips__) |
|
|
104 | #define ECB_MEMORY_FENCE __asm__ __volatile__ ("sync" : : : "memory") |
93 | #endif |
105 | #endif |
94 | #endif |
106 | #endif |
95 | #endif |
107 | #endif |
96 | |
108 | |
97 | #ifndef ECB_MEMORY_FENCE |
109 | #ifndef ECB_MEMORY_FENCE |
… | |
… | |
105 | #define ECB_MEMORY_FENCE_ACQUIRE _ReadWriteBarrier () /* according to msdn, _ReadBarrier is not a load fence */ |
117 | #define ECB_MEMORY_FENCE_ACQUIRE _ReadWriteBarrier () /* according to msdn, _ReadBarrier is not a load fence */ |
106 | #define ECB_MEMORY_FENCE_RELEASE _WriteBarrier () |
118 | #define ECB_MEMORY_FENCE_RELEASE _WriteBarrier () |
107 | #elif defined(_WIN32) |
119 | #elif defined(_WIN32) |
108 | #include <WinNT.h> |
120 | #include <WinNT.h> |
109 | #define ECB_MEMORY_FENCE MemoryBarrier () /* actually just xchg on x86... scary */ |
121 | #define ECB_MEMORY_FENCE MemoryBarrier () /* actually just xchg on x86... scary */ |
|
|
122 | #elif __SUNPRO_C >= 0x5110 || __SUNPRO_CC >= 0x5110 |
|
|
123 | #include <mbarrier.h> |
|
|
124 | #define ECB_MEMORY_FENCE __machine_rw_barrier () |
|
|
125 | #define ECB_MEMORY_FENCE_ACQUIRE __machine_r_barrier () |
|
|
126 | #define ECB_MEMORY_FENCE_RELEASE __machine_w_barrier () |
|
|
127 | #elif __xlC__ |
|
|
128 | #define ECB_MEMORY_FENCE __lwsync () |
110 | #endif |
129 | #endif |
111 | #endif |
130 | #endif |
112 | |
131 | |
113 | #ifndef ECB_MEMORY_FENCE |
132 | #ifndef ECB_MEMORY_FENCE |
114 | #if !ECB_AVOID_PTHREADS |
133 | #if !ECB_AVOID_PTHREADS |
… | |
… | |
377 | |
396 | |
378 | #if ECB_GCC_VERSION(4,5) |
397 | #if ECB_GCC_VERSION(4,5) |
379 | #define ecb_unreachable() __builtin_unreachable () |
398 | #define ecb_unreachable() __builtin_unreachable () |
380 | #else |
399 | #else |
381 | /* this seems to work fine, but gcc always emits a warning for it :/ */ |
400 | /* this seems to work fine, but gcc always emits a warning for it :/ */ |
382 | ecb_function_ void ecb_unreachable (void) ecb_noreturn; |
401 | ecb_inline void ecb_unreachable (void) ecb_noreturn; |
383 | ecb_function_ void ecb_unreachable (void) { } |
402 | ecb_inline void ecb_unreachable (void) { } |
384 | #endif |
403 | #endif |
385 | |
404 | |
386 | /* try to tell the compiler that some condition is definitely true */ |
405 | /* try to tell the compiler that some condition is definitely true */ |
387 | #define ecb_assume(cond) do { if (!(cond)) ecb_unreachable (); } while (0) |
406 | #define ecb_assume(cond) do { if (!(cond)) ecb_unreachable (); } while (0) |
388 | |
407 | |
389 | ecb_function_ unsigned char ecb_byteorder_helper (void) ecb_const; |
408 | ecb_inline unsigned char ecb_byteorder_helper (void) ecb_const; |
390 | ecb_function_ unsigned char |
409 | ecb_inline unsigned char |
391 | ecb_byteorder_helper (void) |
410 | ecb_byteorder_helper (void) |
392 | { |
411 | { |
393 | const uint32_t u = 0x11223344; |
412 | const uint32_t u = 0x11223344; |
394 | return *(unsigned char *)&u; |
413 | return *(unsigned char *)&u; |
395 | } |
414 | } |
396 | |
415 | |
397 | ecb_function_ ecb_bool ecb_big_endian (void) ecb_const; |
416 | ecb_inline ecb_bool ecb_big_endian (void) ecb_const; |
398 | ecb_function_ ecb_bool ecb_big_endian (void) { return ecb_byteorder_helper () == 0x11; } |
417 | ecb_inline ecb_bool ecb_big_endian (void) { return ecb_byteorder_helper () == 0x11; } |
399 | ecb_function_ ecb_bool ecb_little_endian (void) ecb_const; |
418 | ecb_inline ecb_bool ecb_little_endian (void) ecb_const; |
400 | ecb_function_ ecb_bool ecb_little_endian (void) { return ecb_byteorder_helper () == 0x44; } |
419 | ecb_inline ecb_bool ecb_little_endian (void) { return ecb_byteorder_helper () == 0x44; } |
401 | |
420 | |
402 | #if ECB_GCC_VERSION(3,0) || ECB_C99 |
421 | #if ECB_GCC_VERSION(3,0) || ECB_C99 |
403 | #define ecb_mod(m,n) ((m) % (n) + ((m) % (n) < 0 ? (n) : 0)) |
422 | #define ecb_mod(m,n) ((m) % (n) + ((m) % (n) < 0 ? (n) : 0)) |
404 | #else |
423 | #else |
405 | #define ecb_mod(m,n) ((m) < 0 ? ((n) - 1 - ((-1 - (m)) % (n))) : ((m) % (n))) |
424 | #define ecb_mod(m,n) ((m) < 0 ? ((n) - 1 - ((-1 - (m)) % (n))) : ((m) % (n))) |