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

Comparing libev/ev.c (file contents):
Revision 1.449 by root, Sun Sep 23 21:21:58 2012 UTC vs.
Revision 1.450 by root, Mon Oct 8 15:43:35 2012 UTC

505 505
506#ifndef ECB_H 506#ifndef ECB_H
507#define ECB_H 507#define ECB_H
508 508
509/* 16 bits major, 16 bits minor */ 509/* 16 bits major, 16 bits minor */
510#define ECB_VERSION 0x00010001 510#define ECB_VERSION 0x00010002
511 511
512#ifdef _WIN32 512#ifdef _WIN32
513 typedef signed char int8_t; 513 typedef signed char int8_t;
514 typedef unsigned char uint8_t; 514 typedef unsigned char uint8_t;
515 typedef signed short int16_t; 515 typedef signed short int16_t;
530 #else 530 #else
531 #define ECB_PTRSIZE 4 531 #define ECB_PTRSIZE 4
532 typedef uint32_t uintptr_t; 532 typedef uint32_t uintptr_t;
533 typedef int32_t intptr_t; 533 typedef int32_t intptr_t;
534 #endif 534 #endif
535 typedef intptr_t ptrdiff_t;
536#else 535#else
537 #include <inttypes.h> 536 #include <inttypes.h>
538 #if UINTMAX_MAX > 0xffffffffU 537 #if UINTMAX_MAX > 0xffffffffU
539 #define ECB_PTRSIZE 8 538 #define ECB_PTRSIZE 8
540 #else 539 #else
560#define ECB_C (__STDC__+0) /* this assumes that __STDC__ is either empty or a number */ 559#define ECB_C (__STDC__+0) /* this assumes that __STDC__ is either empty or a number */
561#define ECB_C99 (__STDC_VERSION__ >= 199901L) 560#define ECB_C99 (__STDC_VERSION__ >= 199901L)
562#define ECB_C11 (__STDC_VERSION__ >= 201112L) 561#define ECB_C11 (__STDC_VERSION__ >= 201112L)
563#define ECB_CPP (__cplusplus+0) 562#define ECB_CPP (__cplusplus+0)
564#define ECB_CPP11 (__cplusplus >= 201103L) 563#define ECB_CPP11 (__cplusplus >= 201103L)
564
565#if ECB_CPP
566 #define ECB_EXTERN_C extern "C"
567 #define ECB_EXTERN_C_BEG ECB_EXTERN_C {
568 #define ECB_EXTERN_C_END }
569#else
570 #define ECB_EXTERN_C extern
571 #define ECB_EXTERN_C_BEG
572 #define ECB_EXTERN_C_END
573#endif
565 574
566/*****************************************************************************/ 575/*****************************************************************************/
567 576
568/* ECB_NO_THREADS - ecb is not used by multiple threads, ever */ 577/* ECB_NO_THREADS - ecb is not used by multiple threads, ever */
569/* ECB_NO_SMP - ecb might be used in multiple threads, but only on a single cpu */ 578/* ECB_NO_SMP - ecb might be used in multiple threads, but only on a single cpu */
615 624
616#ifndef ECB_MEMORY_FENCE 625#ifndef ECB_MEMORY_FENCE
617 #if ECB_GCC_VERSION(4,7) 626 #if ECB_GCC_VERSION(4,7)
618 /* see comment below (stdatomic.h) about the C11 memory model. */ 627 /* see comment below (stdatomic.h) about the C11 memory model. */
619 #define ECB_MEMORY_FENCE __atomic_thread_fence (__ATOMIC_SEQ_CST) 628 #define ECB_MEMORY_FENCE __atomic_thread_fence (__ATOMIC_SEQ_CST)
629
630 /* The __has_feature syntax from clang is so misdesigned that we cannot use it
631 * without risking compile time errors with other compilers. We *could*
632 * define our own ecb_clang_has_feature, but I just can't be bothered to work
633 * around this shit time and again.
620 #elif defined __clang && __has_feature (cxx_atomic) 634 * #elif defined __clang && __has_feature (cxx_atomic)
621 /* see comment below (stdatomic.h) about the C11 memory model. */ 635 * // see comment below (stdatomic.h) about the C11 memory model.
622 #define ECB_MEMORY_FENCE __c11_atomic_thread_fence (__ATOMIC_SEQ_CST) 636 * #define ECB_MEMORY_FENCE __c11_atomic_thread_fence (__ATOMIC_SEQ_CST)
637 */
638
623 #elif ECB_GCC_VERSION(4,4) || defined __INTEL_COMPILER || defined __clang__ 639 #elif ECB_GCC_VERSION(4,4) || defined __INTEL_COMPILER || defined __clang__
624 #define ECB_MEMORY_FENCE __sync_synchronize () 640 #define ECB_MEMORY_FENCE __sync_synchronize ()
625 #elif _MSC_VER >= 1400 /* VC++ 2005 */ 641 #elif _MSC_VER >= 1400 /* VC++ 2005 */
626 #pragma intrinsic(_ReadBarrier,_WriteBarrier,_ReadWriteBarrier) 642 #pragma intrinsic(_ReadBarrier,_WriteBarrier,_ReadWriteBarrier)
627 #define ECB_MEMORY_FENCE _ReadWriteBarrier () 643 #define ECB_MEMORY_FENCE _ReadWriteBarrier ()
935 ecb_inline void ecb_unreachable (void) ecb_noreturn; 951 ecb_inline void ecb_unreachable (void) ecb_noreturn;
936 ecb_inline void ecb_unreachable (void) { } 952 ecb_inline void ecb_unreachable (void) { }
937#endif 953#endif
938 954
939/* try to tell the compiler that some condition is definitely true */ 955/* try to tell the compiler that some condition is definitely true */
940#define ecb_assume(cond) do { if (!(cond)) ecb_unreachable (); } while (0) 956#define ecb_assume(cond) if (!(cond)) ecb_unreachable (); else 0
941 957
942ecb_inline unsigned char ecb_byteorder_helper (void) ecb_const; 958ecb_inline unsigned char ecb_byteorder_helper (void) ecb_const;
943ecb_inline unsigned char 959ecb_inline unsigned char
944ecb_byteorder_helper (void) 960ecb_byteorder_helper (void)
945{ 961{
946 const uint32_t u = 0x11223344; 962 /* the union code still generates code under pressure in gcc, */
947 return *(unsigned char *)&u; 963 /* but less than using pointers, and always seems to */
964 /* successfully return a constant. */
965 /* the reason why we have this horrible preprocessor mess */
966 /* is to avoid it in all cases, at least on common architectures */
967 /* or when using a recent enough gcc version (>= 4.6) */
968#if __i386 || __i386__ || _M_X86 || __amd64 || __amd64__ || _M_X64
969 return 0x44;
970#elif __BYTE_ORDER__ && __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
971 return 0x44;
972#elif __BYTE_ORDER__ && __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
973 return 0x11;
974#else
975 union
976 {
977 uint32_t i;
978 uint8_t c;
979 } u = { 0x11223344 };
980 return u.c;
981#endif
948} 982}
949 983
950ecb_inline ecb_bool ecb_big_endian (void) ecb_const; 984ecb_inline ecb_bool ecb_big_endian (void) ecb_const;
951ecb_inline ecb_bool ecb_big_endian (void) { return ecb_byteorder_helper () == 0x11; } 985ecb_inline ecb_bool ecb_big_endian (void) { return ecb_byteorder_helper () == 0x11; }
952ecb_inline ecb_bool ecb_little_endian (void) ecb_const; 986ecb_inline ecb_bool ecb_little_endian (void) ecb_const;
981 { 1015 {
982 return N; 1016 return N;
983 } 1017 }
984#else 1018#else
985 #define ecb_array_length(name) (sizeof (name) / sizeof (name [0])) 1019 #define ecb_array_length(name) (sizeof (name) / sizeof (name [0]))
1020#endif
1021
1022/*******************************************************************************/
1023/* floating point stuff, can be disabled by defining ECB_NO_LIBM */
1024
1025/* basically, everything uses "ieee pure-endian" floating point numbers */
1026/* the only noteworthy exception is ancient armle, which uses order 43218765 */
1027#if 0 \
1028 || __i386 || __i386__ \
1029 || __amd64 || __amd64__ || __x86_64 || __x86_64__ \
1030 || __powerpc__ || __ppc__ || __powerpc64__ || __ppc64__ \
1031 || defined __arm__ && defined __ARM_EABI__ \
1032 || defined __s390__ || defined __s390x__ \
1033 || defined __mips__ \
1034 || defined __alpha__ \
1035 || defined __hppa__ \
1036 || defined __ia64__ \
1037 || defined _M_IX86 || defined _M_AMD64 || defined _M_IA64
1038 #define ECB_STDFP 1
1039 #include <string.h> /* for memcpy */
1040#else
1041 #define ECB_STDFP 0
1042 #include <math.h> /* for frexp*, ldexp* */
1043#endif
1044
1045#ifndef ECB_NO_LIBM
1046
1047 /* convert a float to ieee single/binary32 */
1048 ecb_function_ uint32_t ecb_float_to_binary32 (float x) ecb_const;
1049 ecb_function_ uint32_t
1050 ecb_float_to_binary32 (float x)
1051 {
1052 uint32_t r;
1053
1054 #if ECB_STDFP
1055 memcpy (&r, &x, 4);
1056 #else
1057 /* slow emulation, works for anything but -0 */
1058 uint32_t m;
1059 int e;
1060
1061 if (x == 0e0f ) return 0x00000000U;
1062 if (x > +3.40282346638528860e+38f) return 0x7f800000U;
1063 if (x < -3.40282346638528860e+38f) return 0xff800000U;
1064 if (x != x ) return 0x7fbfffffU;
1065
1066 m = frexpf (x, &e) * 0x1000000U;
1067
1068 r = m & 0x80000000U;
1069
1070 if (r)
1071 m = -m;
1072
1073 if (e <= -126)
1074 {
1075 m &= 0xffffffU;
1076 m >>= (-125 - e);
1077 e = -126;
1078 }
1079
1080 r |= (e + 126) << 23;
1081 r |= m & 0x7fffffU;
1082 #endif
1083
1084 return r;
1085 }
1086
1087 /* converts an ieee single/binary32 to a float */
1088 ecb_function_ float ecb_binary32_to_float (uint32_t x) ecb_const;
1089 ecb_function_ float
1090 ecb_binary32_to_float (uint32_t x)
1091 {
1092 float r;
1093
1094 #if ECB_STDFP
1095 memcpy (&r, &x, 4);
1096 #else
1097 /* emulation, only works for normals and subnormals and +0 */
1098 int neg = x >> 31;
1099 int e = (x >> 23) & 0xffU;
1100
1101 x &= 0x7fffffU;
1102
1103 if (e)
1104 x |= 0x800000U;
1105 else
1106 e = 1;
1107
1108 /* we distrust ldexpf a bit and do the 2**-24 scaling by an extra multiply */
1109 r = ldexpf (x * (0.5f / 0x800000U), e - 126);
1110
1111 r = neg ? -r : r;
1112 #endif
1113
1114 return r;
1115 }
1116
1117 /* convert a double to ieee double/binary64 */
1118 ecb_function_ uint64_t ecb_double_to_binary64 (double x) ecb_const;
1119 ecb_function_ uint64_t
1120 ecb_double_to_binary64 (double x)
1121 {
1122 uint64_t r;
1123
1124 #if ECB_STDFP
1125 memcpy (&r, &x, 8);
1126 #else
1127 /* slow emulation, works for anything but -0 */
1128 uint64_t m;
1129 int e;
1130
1131 if (x == 0e0 ) return 0x0000000000000000U;
1132 if (x > +1.79769313486231470e+308) return 0x7ff0000000000000U;
1133 if (x < -1.79769313486231470e+308) return 0xfff0000000000000U;
1134 if (x != x ) return 0X7ff7ffffffffffffU;
1135
1136 m = frexp (x, &e) * 0x20000000000000U;
1137
1138 r = m & 0x8000000000000000;;
1139
1140 if (r)
1141 m = -m;
1142
1143 if (e <= -1022)
1144 {
1145 m &= 0x1fffffffffffffU;
1146 m >>= (-1021 - e);
1147 e = -1022;
1148 }
1149
1150 r |= ((uint64_t)(e + 1022)) << 52;
1151 r |= m & 0xfffffffffffffU;
1152 #endif
1153
1154 return r;
1155 }
1156
1157 /* converts an ieee double/binary64 to a double */
1158 ecb_function_ double ecb_binary64_to_double (uint64_t x) ecb_const;
1159 ecb_function_ double
1160 ecb_binary64_to_double (uint64_t x)
1161 {
1162 double r;
1163
1164 #if ECB_STDFP
1165 memcpy (&r, &x, 8);
1166 #else
1167 /* emulation, only works for normals and subnormals and +0 */
1168 int neg = x >> 63;
1169 int e = (x >> 52) & 0x7ffU;
1170
1171 x &= 0xfffffffffffffU;
1172
1173 if (e)
1174 x |= 0x10000000000000U;
1175 else
1176 e = 1;
1177
1178 /* we distrust ldexp a bit and do the 2**-53 scaling by an extra multiply */
1179 r = ldexp (x * (0.5 / 0x10000000000000U), e - 1022);
1180
1181 r = neg ? -r : r;
1182 #endif
1183
1184 return r;
1185 }
1186
986#endif 1187#endif
987 1188
988#endif 1189#endif
989 1190
990/* ECB.H END */ 1191/* ECB.H END */

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines