ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/libev/ev.c
(Generate patch)

Comparing libev/ev.c (file contents):
Revision 1.478 by root, Sun Oct 11 13:38:44 2015 UTC vs.
Revision 1.479 by root, Sun Dec 20 01:31:17 2015 UTC

532 532
533#ifndef ECB_H 533#ifndef ECB_H
534#define ECB_H 534#define ECB_H
535 535
536/* 16 bits major, 16 bits minor */ 536/* 16 bits major, 16 bits minor */
537#define ECB_VERSION 0x00010004 537#define ECB_VERSION 0x00010005
538 538
539#ifdef _WIN32 539#ifdef _WIN32
540 typedef signed char int8_t; 540 typedef signed char int8_t;
541 typedef unsigned char uint8_t; 541 typedef unsigned char uint8_t;
542 typedef signed short int16_t; 542 typedef signed short int16_t;
559 typedef uint32_t uintptr_t; 559 typedef uint32_t uintptr_t;
560 typedef int32_t intptr_t; 560 typedef int32_t intptr_t;
561 #endif 561 #endif
562#else 562#else
563 #include <inttypes.h> 563 #include <inttypes.h>
564 #if UINTMAX_MAX > 0xffffffffU 564 #if (defined INTPTR_MAX ? INTPTR_MAX : ULONG_MAX) > 0xffffffffU
565 #define ECB_PTRSIZE 8 565 #define ECB_PTRSIZE 8
566 #else 566 #else
567 #define ECB_PTRSIZE 4 567 #define ECB_PTRSIZE 4
568 #endif 568 #endif
569#endif 569#endif
645#endif 645#endif
646 646
647/* http://www-01.ibm.com/support/knowledgecenter/SSGH3R_13.1.0/com.ibm.xlcpp131.aix.doc/compiler_ref/compiler_builtins.html */ 647/* http://www-01.ibm.com/support/knowledgecenter/SSGH3R_13.1.0/com.ibm.xlcpp131.aix.doc/compiler_ref/compiler_builtins.html */
648#if __xlC__ && ECB_CPP 648#if __xlC__ && ECB_CPP
649 #include <builtins.h> 649 #include <builtins.h>
650#endif
651
652#if 1400 <= _MSC_VER
653 #include <intrin.h> /* fence functions _ReadBarrier, also bit search functions _BitScanReverse */
650#endif 654#endif
651 655
652#ifndef ECB_MEMORY_FENCE 656#ifndef ECB_MEMORY_FENCE
653 #if ECB_GCC_VERSION(2,5) || defined __INTEL_COMPILER || (__llvm__ && __GNUC__) || __SUNPRO_C >= 0x5110 || __SUNPRO_CC >= 0x5110 657 #if ECB_GCC_VERSION(2,5) || defined __INTEL_COMPILER || (__llvm__ && __GNUC__) || __SUNPRO_C >= 0x5110 || __SUNPRO_CC >= 0x5110
654 #if __i386 || __i386__ 658 #if __i386 || __i386__
659 #define ECB_MEMORY_FENCE __asm__ __volatile__ ("mfence" : : : "memory") 663 #define ECB_MEMORY_FENCE __asm__ __volatile__ ("mfence" : : : "memory")
660 #define ECB_MEMORY_FENCE_ACQUIRE __asm__ __volatile__ ("" : : : "memory") 664 #define ECB_MEMORY_FENCE_ACQUIRE __asm__ __volatile__ ("" : : : "memory")
661 #define ECB_MEMORY_FENCE_RELEASE __asm__ __volatile__ ("") 665 #define ECB_MEMORY_FENCE_RELEASE __asm__ __volatile__ ("")
662 #elif __powerpc__ || __ppc__ || __powerpc64__ || __ppc64__ 666 #elif __powerpc__ || __ppc__ || __powerpc64__ || __ppc64__
663 #define ECB_MEMORY_FENCE __asm__ __volatile__ ("sync" : : : "memory") 667 #define ECB_MEMORY_FENCE __asm__ __volatile__ ("sync" : : : "memory")
668 #elif defined __ARM_ARCH_2__ \
669 || defined __ARM_ARCH_3__ || defined __ARM_ARCH_3M__ \
670 || defined __ARM_ARCH_4__ || defined __ARM_ARCH_4T__ \
671 || defined __ARM_ARCH_5__ || defined __ARM_ARCH_5E__ \
672 || defined __ARM_ARCH_5T__ || defined __ARM_ARCH_5TE__ \
673 || defined __ARM_ARCH_5TEJ__
674 /* should not need any, unless running old code on newer cpu - arm doesn't support that */
664 #elif defined __ARM_ARCH_6__ || defined __ARM_ARCH_6J__ \ 675 #elif defined __ARM_ARCH_6__ || defined __ARM_ARCH_6J__ \
665 || defined __ARM_ARCH_6K__ || defined __ARM_ARCH_6ZK__ 676 || defined __ARM_ARCH_6K__ || defined __ARM_ARCH_6ZK__ \
677 || defined __ARM_ARCH_6T2__
666 #define ECB_MEMORY_FENCE __asm__ __volatile__ ("mcr p15,0,%0,c7,c10,5" : : "r" (0) : "memory") 678 #define ECB_MEMORY_FENCE __asm__ __volatile__ ("mcr p15,0,%0,c7,c10,5" : : "r" (0) : "memory")
667 #elif defined __ARM_ARCH_7__ || defined __ARM_ARCH_7A__ \ 679 #elif defined __ARM_ARCH_7__ || defined __ARM_ARCH_7A__ \
668 || defined __ARM_ARCH_7M__ || defined __ARM_ARCH_7R__ 680 || defined __ARM_ARCH_7R__ || defined __ARM_ARCH_7M__
669 #define ECB_MEMORY_FENCE __asm__ __volatile__ ("dmb" : : : "memory") 681 #define ECB_MEMORY_FENCE __asm__ __volatile__ ("dmb" : : : "memory")
670 #elif __aarch64__ 682 #elif __aarch64__
671 #define ECB_MEMORY_FENCE __asm__ __volatile__ ("dmb ish" : : : "memory") 683 #define ECB_MEMORY_FENCE __asm__ __volatile__ ("dmb ish" : : : "memory")
672 #elif (__sparc || __sparc__) && !(__sparc_v8__ || defined __sparcv8) 684 #elif (__sparc || __sparc__) && !(__sparc_v8__ || defined __sparcv8)
673 #define ECB_MEMORY_FENCE __asm__ __volatile__ ("membar #LoadStore | #LoadLoad | #StoreStore | #StoreLoad" : : : "memory") 685 #define ECB_MEMORY_FENCE __asm__ __volatile__ ("membar #LoadStore | #LoadLoad | #StoreStore | #StoreLoad" : : : "memory")
915#else 927#else
916 ecb_function_ ecb_const int ecb_ctz32 (uint32_t x); 928 ecb_function_ ecb_const int ecb_ctz32 (uint32_t x);
917 ecb_function_ ecb_const int 929 ecb_function_ ecb_const int
918 ecb_ctz32 (uint32_t x) 930 ecb_ctz32 (uint32_t x)
919 { 931 {
932#if 1400 <= _MSC_VER && (_M_IX86 || _M_X64 || _M_IA64 || _M_ARM)
933 unsigned long r;
934 _BitScanForward (&r, x);
935 return (int)r;
936#else
920 int r = 0; 937 int r = 0;
921 938
922 x &= ~x + 1; /* this isolates the lowest bit */ 939 x &= ~x + 1; /* this isolates the lowest bit */
923 940
924#if ECB_branchless_on_i386 941#if ECB_branchless_on_i386
934 if (x & 0xff00ff00) r += 8; 951 if (x & 0xff00ff00) r += 8;
935 if (x & 0xffff0000) r += 16; 952 if (x & 0xffff0000) r += 16;
936#endif 953#endif
937 954
938 return r; 955 return r;
956#endif
939 } 957 }
940 958
941 ecb_function_ ecb_const int ecb_ctz64 (uint64_t x); 959 ecb_function_ ecb_const int ecb_ctz64 (uint64_t x);
942 ecb_function_ ecb_const int 960 ecb_function_ ecb_const int
943 ecb_ctz64 (uint64_t x) 961 ecb_ctz64 (uint64_t x)
944 { 962 {
963#if 1400 <= _MSC_VER && (_M_X64 || _M_IA64 || _M_ARM)
964 unsigned long r;
965 _BitScanForward64 (&r, x);
966 return (int)r;
967#else
945 int shift = x & 0xffffffffU ? 0 : 32; 968 int shift = x & 0xffffffff ? 0 : 32;
946 return ecb_ctz32 (x >> shift) + shift; 969 return ecb_ctz32 (x >> shift) + shift;
970#endif
947 } 971 }
948 972
949 ecb_function_ ecb_const int ecb_popcount32 (uint32_t x); 973 ecb_function_ ecb_const int ecb_popcount32 (uint32_t x);
950 ecb_function_ ecb_const int 974 ecb_function_ ecb_const int
951 ecb_popcount32 (uint32_t x) 975 ecb_popcount32 (uint32_t x)
959 } 983 }
960 984
961 ecb_function_ ecb_const int ecb_ld32 (uint32_t x); 985 ecb_function_ ecb_const int ecb_ld32 (uint32_t x);
962 ecb_function_ ecb_const int ecb_ld32 (uint32_t x) 986 ecb_function_ ecb_const int ecb_ld32 (uint32_t x)
963 { 987 {
988#if 1400 <= _MSC_VER && (_M_IX86 || _M_X64 || _M_IA64 || _M_ARM)
989 unsigned long r;
990 _BitScanReverse (&r, x);
991 return (int)r;
992#else
964 int r = 0; 993 int r = 0;
965 994
966 if (x >> 16) { x >>= 16; r += 16; } 995 if (x >> 16) { x >>= 16; r += 16; }
967 if (x >> 8) { x >>= 8; r += 8; } 996 if (x >> 8) { x >>= 8; r += 8; }
968 if (x >> 4) { x >>= 4; r += 4; } 997 if (x >> 4) { x >>= 4; r += 4; }
969 if (x >> 2) { x >>= 2; r += 2; } 998 if (x >> 2) { x >>= 2; r += 2; }
970 if (x >> 1) { r += 1; } 999 if (x >> 1) { r += 1; }
971 1000
972 return r; 1001 return r;
1002#endif
973 } 1003 }
974 1004
975 ecb_function_ ecb_const int ecb_ld64 (uint64_t x); 1005 ecb_function_ ecb_const int ecb_ld64 (uint64_t x);
976 ecb_function_ ecb_const int ecb_ld64 (uint64_t x) 1006 ecb_function_ ecb_const int ecb_ld64 (uint64_t x)
977 { 1007 {
1008#if 1400 <= _MSC_VER && (_M_X64 || _M_IA64 || _M_ARM)
1009 unsigned long r;
1010 _BitScanReverse64 (&r, x);
1011 return (int)r;
1012#else
978 int r = 0; 1013 int r = 0;
979 1014
980 if (x >> 32) { x >>= 32; r += 32; } 1015 if (x >> 32) { x >>= 32; r += 32; }
981 1016
982 return r + ecb_ld32 (x); 1017 return r + ecb_ld32 (x);
1018#endif
983 } 1019 }
984#endif 1020#endif
985 1021
986ecb_function_ ecb_const ecb_bool ecb_is_pot32 (uint32_t x); 1022ecb_function_ ecb_const ecb_bool ecb_is_pot32 (uint32_t x);
987ecb_function_ ecb_const ecb_bool ecb_is_pot32 (uint32_t x) { return !(x & (x - 1)); } 1023ecb_function_ ecb_const ecb_bool ecb_is_pot32 (uint32_t x) { return !(x & (x - 1)); }
1090#endif 1126#endif
1091 1127
1092/* try to tell the compiler that some condition is definitely true */ 1128/* try to tell the compiler that some condition is definitely true */
1093#define ecb_assume(cond) if (!(cond)) ecb_unreachable (); else 0 1129#define ecb_assume(cond) if (!(cond)) ecb_unreachable (); else 0
1094 1130
1095ecb_inline ecb_const unsigned char ecb_byteorder_helper (void); 1131ecb_inline ecb_const uint32_t ecb_byteorder_helper (void);
1096ecb_inline ecb_const unsigned char 1132ecb_inline ecb_const uint32_t
1097ecb_byteorder_helper (void) 1133ecb_byteorder_helper (void)
1098{ 1134{
1099 /* the union code still generates code under pressure in gcc, */ 1135 /* the union code still generates code under pressure in gcc, */
1100 /* but less than using pointers, and always seems to */ 1136 /* but less than using pointers, and always seems to */
1101 /* successfully return a constant. */ 1137 /* successfully return a constant. */
1102 /* the reason why we have this horrible preprocessor mess */ 1138 /* the reason why we have this horrible preprocessor mess */
1103 /* is to avoid it in all cases, at least on common architectures */ 1139 /* is to avoid it in all cases, at least on common architectures */
1104 /* or when using a recent enough gcc version (>= 4.6) */ 1140 /* or when using a recent enough gcc version (>= 4.6) */
1105#if ((__i386 || __i386__) && !__VOS__) || _M_IX86 || ECB_GCC_AMD64 || ECB_MSVC_AMD64
1106 return 0x44;
1107#elif __BYTE_ORDER__ && __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ 1141#if (defined __BYTE_ORDER__ && __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__) \
1142 || ((__i386 || __i386__ || _M_IX86 || ECB_GCC_AMD64 || ECB_MSVC_AMD64) && !__VOS__)
1143 #define ECB_LITTLE_ENDIAN 1
1108 return 0x44; 1144 return 0x44332211;
1109#elif __BYTE_ORDER__ && __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ 1145#elif (defined __BYTE_ORDER__ && __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__) \
1146 || ((__AARCH64EB__ || __MIPSEB__ || __ARMEB__) && !__VOS__)
1147 #define ECB_BIG_ENDIAN 1
1110 return 0x11; 1148 return 0x11223344;
1111#else 1149#else
1112 union 1150 union
1113 { 1151 {
1152 uint8_t c[4];
1114 uint32_t i; 1153 uint32_t u;
1115 uint8_t c;
1116 } u = { 0x11223344 }; 1154 } u = { 0x11, 0x22, 0x33, 0x44 };
1117 return u.c; 1155 return u.u;
1118#endif 1156#endif
1119} 1157}
1120 1158
1121ecb_inline ecb_const ecb_bool ecb_big_endian (void); 1159ecb_inline ecb_const ecb_bool ecb_big_endian (void);
1122ecb_inline ecb_const ecb_bool ecb_big_endian (void) { return ecb_byteorder_helper () == 0x11; } 1160ecb_inline ecb_const ecb_bool ecb_big_endian (void) { return ecb_byteorder_helper () == 0x11223344; }
1123ecb_inline ecb_const ecb_bool ecb_little_endian (void); 1161ecb_inline ecb_const ecb_bool ecb_little_endian (void);
1124ecb_inline ecb_const ecb_bool ecb_little_endian (void) { return ecb_byteorder_helper () == 0x44; } 1162ecb_inline ecb_const ecb_bool ecb_little_endian (void) { return ecb_byteorder_helper () == 0x44332211; }
1125 1163
1126#if ECB_GCC_VERSION(3,0) || ECB_C99 1164#if ECB_GCC_VERSION(3,0) || ECB_C99
1127 #define ecb_mod(m,n) ((m) % (n) + ((m) % (n) < 0 ? (n) : 0)) 1165 #define ecb_mod(m,n) ((m) % (n) + ((m) % (n) < 0 ? (n) : 0))
1128#else 1166#else
1129 #define ecb_mod(m,n) ((m) < 0 ? ((n) - 1 - ((-1 - (m)) % (n))) : ((m) % (n))) 1167 #define ecb_mod(m,n) ((m) < 0 ? ((n) - 1 - ((-1 - (m)) % (n))) : ((m) % (n)))
1153 return N; 1191 return N;
1154 } 1192 }
1155#else 1193#else
1156 #define ecb_array_length(name) (sizeof (name) / sizeof (name [0])) 1194 #define ecb_array_length(name) (sizeof (name) / sizeof (name [0]))
1157#endif 1195#endif
1196
1197ecb_function_ ecb_const uint32_t ecb_binary16_to_binary32 (uint32_t x);
1198ecb_function_ ecb_const uint32_t
1199ecb_binary16_to_binary32 (uint32_t x)
1200{
1201 unsigned int s = (x & 0x8000) << (31 - 15);
1202 int e = (x >> 10) & 0x001f;
1203 unsigned int m = x & 0x03ff;
1204
1205 if (ecb_expect_false (e == 31))
1206 /* infinity or NaN */
1207 e = 255 - (127 - 15);
1208 else if (ecb_expect_false (!e))
1209 {
1210 if (ecb_expect_true (!m))
1211 /* zero, handled by code below by forcing e to 0 */
1212 e = 0 - (127 - 15);
1213 else
1214 {
1215 /* subnormal, renormalise */
1216 unsigned int s = 10 - ecb_ld32 (m);
1217
1218 m = (m << s) & 0x3ff; /* mask implicit bit */
1219 e -= s - 1;
1220 }
1221 }
1222
1223 /* e and m now are normalised, or zero, (or inf or nan) */
1224 e += 127 - 15;
1225
1226 return s | (e << 23) | (m << (23 - 10));
1227}
1228
1229ecb_function_ ecb_const uint16_t ecb_binary32_to_binary16 (uint32_t x);
1230ecb_function_ ecb_const uint16_t
1231ecb_binary32_to_binary16 (uint32_t x)
1232{
1233 unsigned int s = (x >> 16) & 0x00008000; /* sign bit, the easy part */
1234 unsigned int e = ((x >> 23) & 0x000000ff) - (127 - 15); /* the desired exponent */
1235 unsigned int m = x & 0x007fffff;
1236
1237 x &= 0x7fffffff;
1238
1239 /* if it's within range of binary16 normals, use fast path */
1240 if (ecb_expect_true (0x38800000 <= x && x <= 0x477fefff))
1241 {
1242 /* mantissa round-to-even */
1243 m += 0x00000fff + ((m >> (23 - 10)) & 1);
1244
1245 /* handle overflow */
1246 if (ecb_expect_false (m >= 0x00800000))
1247 {
1248 m >>= 1;
1249 e += 1;
1250 }
1251
1252 return s | (e << 10) | (m >> (23 - 10));
1253 }
1254
1255 /* handle large numbers and infinity */
1256 if (ecb_expect_true (0x477fefff < x && x <= 0x7f800000))
1257 return s | 0x7c00;
1258
1259 /* handle zero, subnormals and small numbers */
1260 if (ecb_expect_true (x < 0x38800000))
1261 {
1262 /* zero */
1263 if (ecb_expect_true (!x))
1264 return s;
1265
1266 /* handle subnormals */
1267
1268 /* too small, will be zero */
1269 if (e < (14 - 24)) /* might not be sharp, but is good enough */
1270 return s;
1271
1272 m |= 0x00800000; /* make implicit bit explicit */
1273
1274 /* very tricky - we need to round to the nearest e (+10) bit value */
1275 {
1276 unsigned int bits = 14 - e;
1277 unsigned int half = (1 << (bits - 1)) - 1;
1278 unsigned int even = (m >> bits) & 1;
1279
1280 /* if this overflows, we will end up with a normalised number */
1281 m = (m + half + even) >> bits;
1282 }
1283
1284 return s | m;
1285 }
1286
1287 /* handle NaNs, preserve leftmost nan bits, but make sure we don't turn them into infinities */
1288 m >>= 13;
1289
1290 return s | 0x7c00 | m | !m;
1291}
1158 1292
1159/*******************************************************************************/ 1293/*******************************************************************************/
1160/* floating point stuff, can be disabled by defining ECB_NO_LIBM */ 1294/* floating point stuff, can be disabled by defining ECB_NO_LIBM */
1161 1295
1162/* basically, everything uses "ieee pure-endian" floating point numbers */ 1296/* basically, everything uses "ieee pure-endian" floating point numbers */
1205 #else 1339 #else
1206 #define ecb_ldexpf(x,e) (float) ldexp ((double) (x), (e)) 1340 #define ecb_ldexpf(x,e) (float) ldexp ((double) (x), (e))
1207 #define ecb_frexpf(x,e) (float) frexp ((double) (x), (e)) 1341 #define ecb_frexpf(x,e) (float) frexp ((double) (x), (e))
1208 #endif 1342 #endif
1209 1343
1210 /* converts an ieee half/binary16 to a float */
1211 ecb_function_ ecb_const float ecb_binary16_to_float (uint16_t x);
1212 ecb_function_ ecb_const float
1213 ecb_binary16_to_float (uint16_t x)
1214 {
1215 int e = (x >> 10) & 0x1f;
1216 int m = x & 0x3ff;
1217 float r;
1218
1219 if (!e ) r = ecb_ldexpf (m , -24);
1220 else if (e != 31) r = ecb_ldexpf (m + 0x400, e - 25);
1221 else if (m ) r = ECB_NAN;
1222 else r = ECB_INFINITY;
1223
1224 return x & 0x8000 ? -r : r;
1225 }
1226
1227 /* convert a float to ieee single/binary32 */ 1344 /* convert a float to ieee single/binary32 */
1228 ecb_function_ ecb_const uint32_t ecb_float_to_binary32 (float x); 1345 ecb_function_ ecb_const uint32_t ecb_float_to_binary32 (float x);
1229 ecb_function_ ecb_const uint32_t 1346 ecb_function_ ecb_const uint32_t
1230 ecb_float_to_binary32 (float x) 1347 ecb_float_to_binary32 (float x)
1231 { 1348 {
1360 1477
1361 r = neg ? -r : r; 1478 r = neg ? -r : r;
1362 #endif 1479 #endif
1363 1480
1364 return r; 1481 return r;
1482 }
1483
1484 /* convert a float to ieee half/binary16 */
1485 ecb_function_ ecb_const uint16_t ecb_float_to_binary16 (float x);
1486 ecb_function_ ecb_const uint16_t
1487 ecb_float_to_binary16 (float x)
1488 {
1489 return ecb_binary32_to_binary16 (ecb_float_to_binary32 (x));
1490 }
1491
1492 /* convert an ieee half/binary16 to float */
1493 ecb_function_ ecb_const float ecb_binary16_to_float (uint16_t x);
1494 ecb_function_ ecb_const float
1495 ecb_binary16_to_float (uint16_t x)
1496 {
1497 return ecb_binary32_to_float (ecb_binary16_to_binary32 (x));
1365 } 1498 }
1366 1499
1367#endif 1500#endif
1368 1501
1369#endif 1502#endif

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines