ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/libecb/ecb.h
(Generate patch)

Comparing libecb/ecb.h (file contents):
Revision 1.96 by root, Wed May 30 16:56:06 2012 UTC vs.
Revision 1.102 by root, Thu Jun 28 17:23:58 2012 UTC

54 #else 54 #else
55 #define ECB_PTRSIZE 4 55 #define ECB_PTRSIZE 4
56 typedef uint32_t uintptr_t; 56 typedef uint32_t uintptr_t;
57 typedef int32_t intptr_t; 57 typedef int32_t intptr_t;
58 #endif 58 #endif
59 typedef intptr_t ptrdiff_t;
60#else 59#else
61 #include <inttypes.h> 60 #include <inttypes.h>
62 #if UINTMAX_MAX > 0xffffffffU 61 #if UINTMAX_MAX > 0xffffffffU
63 #define ECB_PTRSIZE 8 62 #define ECB_PTRSIZE 8
64 #else 63 #else
84#define ECB_C (__STDC__+0) /* this assumes that __STDC__ is either empty or a number */ 83#define ECB_C (__STDC__+0) /* this assumes that __STDC__ is either empty or a number */
85#define ECB_C99 (__STDC_VERSION__ >= 199901L) 84#define ECB_C99 (__STDC_VERSION__ >= 199901L)
86#define ECB_C11 (__STDC_VERSION__ >= 201112L) 85#define ECB_C11 (__STDC_VERSION__ >= 201112L)
87#define ECB_CPP (__cplusplus+0) 86#define ECB_CPP (__cplusplus+0)
88#define ECB_CPP11 (__cplusplus >= 201103L) 87#define ECB_CPP11 (__cplusplus >= 201103L)
88
89#if ECB_CPP
90 #define ECB_EXTERN_C extern "C"
91 #define ECB_EXTERN_C_BEG ECB_EXTERN_C {
92 #define ECB_EXTERN_C_END }
93#else
94 #define ECB_EXTERN_C extern
95 #define ECB_EXTERN_C_BEG
96 #define ECB_EXTERN_C_END
97#endif
89 98
90/*****************************************************************************/ 99/*****************************************************************************/
91 100
92/* ECB_NO_THREADS - ecb is not used by multiple threads, ever */ 101/* ECB_NO_THREADS - ecb is not used by multiple threads, ever */
93/* ECB_NO_SMP - ecb might be used in multiple threads, but only on a single cpu */ 102/* ECB_NO_SMP - ecb might be used in multiple threads, but only on a single cpu */
137 #endif 146 #endif
138#endif 147#endif
139 148
140#ifndef ECB_MEMORY_FENCE 149#ifndef ECB_MEMORY_FENCE
141 #if ECB_GCC_VERSION(4,7) 150 #if ECB_GCC_VERSION(4,7)
142 /* see comment below about the C11 memory model. in short - avoid */ 151 /* see comment below (stdatomic.h) about the C11 memory model. */
143 #define ECB_MEMORY_FENCE __atomic_thread_fence (__ATOMIC_SEQ_CST) 152 #define ECB_MEMORY_FENCE __atomic_thread_fence (__ATOMIC_SEQ_CST)
144 #elif defined __clang && __has_feature (cxx_atomic) 153 /*#elif defined __clang && __has_feature (cxx_atomic)*/
145 /* see above */ 154 /* see comment below (stdatomic.h) about the C11 memory model. */
146 #define ECB_MEMORY_FENCE __c11_atomic_thread_fence (__ATOMIC_SEQ_CST) 155 /*#define ECB_MEMORY_FENCE __c11_atomic_thread_fence (__ATOMIC_SEQ_CST)*/
147 #elif ECB_GCC_VERSION(4,4) || defined __INTEL_COMPILER || defined __clang__ 156 #elif ECB_GCC_VERSION(4,4) || defined __INTEL_COMPILER || defined __clang__
148 #define ECB_MEMORY_FENCE __sync_synchronize () 157 #define ECB_MEMORY_FENCE __sync_synchronize ()
149 #elif _MSC_VER >= 1400 /* VC++ 2005 */ 158 #elif _MSC_VER >= 1400 /* VC++ 2005 */
150 #pragma intrinsic(_ReadBarrier,_WriteBarrier,_ReadWriteBarrier) 159 #pragma intrinsic(_ReadBarrier,_WriteBarrier,_ReadWriteBarrier)
151 #define ECB_MEMORY_FENCE _ReadWriteBarrier () 160 #define ECB_MEMORY_FENCE _ReadWriteBarrier ()
167#ifndef ECB_MEMORY_FENCE 176#ifndef ECB_MEMORY_FENCE
168 #if ECB_C11 && !defined __STDC_NO_ATOMICS__ 177 #if ECB_C11 && !defined __STDC_NO_ATOMICS__
169 /* we assume that these memory fences work on all variables/all memory accesses, */ 178 /* we assume that these memory fences work on all variables/all memory accesses, */
170 /* not just C11 atomics and atomic accesses */ 179 /* not just C11 atomics and atomic accesses */
171 #include <stdatomic.h> 180 #include <stdatomic.h>
172 /* unfortunately, the C11 memory model seems to be very limited, and unable to express */ 181 /* Unfortunately, neither gcc 4.7 nor clang 3.1 generate any instructions for */
173 /* simple barrier semantics. That means we need to take out thor's hammer. */ 182 /* any fence other than seq_cst, which isn't very efficient for us. */
183 /* Why that is, we don't know - either the C11 memory model is quite useless */
184 /* for most usages, or gcc and clang have a bug */
185 /* I *currently* lean towards the latter, and inefficiently implement */
186 /* all three of ecb's fences as a seq_cst fence */
174 #define ECB_MEMORY_FENCE atomic_thread_fence (memory_order_seq_cst) 187 #define ECB_MEMORY_FENCE atomic_thread_fence (memory_order_seq_cst)
175 #endif 188 #endif
176#endif 189#endif
177 190
178#ifndef ECB_MEMORY_FENCE 191#ifndef ECB_MEMORY_FENCE
455 ecb_inline void ecb_unreachable (void) ecb_noreturn; 468 ecb_inline void ecb_unreachable (void) ecb_noreturn;
456 ecb_inline void ecb_unreachable (void) { } 469 ecb_inline void ecb_unreachable (void) { }
457#endif 470#endif
458 471
459/* try to tell the compiler that some condition is definitely true */ 472/* try to tell the compiler that some condition is definitely true */
460#define ecb_assume(cond) do { if (!(cond)) ecb_unreachable (); } while (0) 473#define ecb_assume(cond) if (!(cond)) ecb_unreachable (); else 0
461 474
462ecb_inline unsigned char ecb_byteorder_helper (void) ecb_const; 475ecb_inline unsigned char ecb_byteorder_helper (void) ecb_const;
463ecb_inline unsigned char 476ecb_inline unsigned char
464ecb_byteorder_helper (void) 477ecb_byteorder_helper (void)
465{ 478{
466 const uint32_t u = 0x11223344; 479 /* the union code still generates code under pressure in gcc, */
467 return *(unsigned char *)&u; 480 /* but less than using pointers, and always seem to */
481 /* successfully return a constant. */
482 /* the reason why we have this horrible preprocessor mess */
483 /* is to avoid it in all cases, at least on common architectures */
484 /* and yes, gcc defines __BYTE_ORDER__, g++ does not */
485#if __i386 || __i386__ || _M_X86 || __amd64 || __amd64__ || _M_X64
486 return 0x44;
487#elif __BYTE_ORDER__ && __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
488 return 0x44;
489#elif __BYTE_ORDER__ && __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
490 retrurn 0x11;
491#else
492 union
493 {
494 uint32_t i;
495 uint8_t c;
496 } u = { 0x11223344 };
497 return u.c;
498#endif
468} 499}
469 500
470ecb_inline ecb_bool ecb_big_endian (void) ecb_const; 501ecb_inline ecb_bool ecb_big_endian (void) ecb_const;
471ecb_inline ecb_bool ecb_big_endian (void) { return ecb_byteorder_helper () == 0x11; } 502ecb_inline ecb_bool ecb_big_endian (void) { return ecb_byteorder_helper () == 0x11; }
472ecb_inline ecb_bool ecb_little_endian (void) ecb_const; 503ecb_inline ecb_bool ecb_little_endian (void) ecb_const;
503 } 534 }
504#else 535#else
505 #define ecb_array_length(name) (sizeof (name) / sizeof (name [0])) 536 #define ecb_array_length(name) (sizeof (name) / sizeof (name [0]))
506#endif 537#endif
507 538
539#if __STDC_IEC_559__
540 // we assume this is defined for most C and many C++ compilers
541 ecb_inline ecb_bool ecb_float_ieee (void) ecb_const;
542 ecb_inline ecb_bool ecb_float_ieee (void) { return 1; }
543 ecb_inline ecb_bool ecb_double_ieee (void) ecb_const;
544 ecb_inline ecb_bool ecb_double_ieee (void) { return 1; }
545#elif ECB_CPP
546 #include <limits>
547 ecb_inline ecb_bool ecb_float_ieee (void) ecb_const;
548 ecb_inline ecb_bool ecb_float_ieee (void) { return std::numeric_limits<float >::is_iec559; }
549 ecb_inline ecb_bool ecb_double_ieee (void) ecb_const;
550 ecb_inline ecb_bool ecb_double_ieee (void) { return std::numeric_limits<double>::is_iec559; }
551#else
552 ecb_inline ecb_bool ecb_float_ieee (void) ecb_const;
553 ecb_inline ecb_bool ecb_float_ieee (void) { return 0; }
554 ecb_inline ecb_bool ecb_double_ieee (void) ecb_const;
555 ecb_inline ecb_bool ecb_double_ieee (void) { return 0; }
508#endif 556#endif
509 557
558// convert a float to ieee single/binary32
559ecb_function_ uint32_t ecb_float_to_binary32 (float x) ecb_const;
560ecb_function_ uint32_t
561ecb_float_to_binary32 (float x)
562{
563 /* slow emulation, works for anything but nan's and -0 */
564 ECB_EXTERN_C float frexpf (float v, int *e);
565 uint32_t r, m;
566 int e;
567
568 if (x == 0e0f ) return 0;
569 if (x > +3.40282346638528860e+38f) return 0x7f800000U;
570 if (x < -3.40282346638528860e+38f) return 0xff800000U;
571
572 m = frexpf (x, &e) * 0x1000000U;
573
574 r = m & 0x80000000U;
575
576 if (r)
577 m = -m;
578
579 if (e < -125)
580 {
581 m &= 0xffffffU;
582 m >>= (-125 - e);
583 e = -126;
584 }
585
586 r |= (e + 126) << 23;
587 r |= m & 0x7fffffU;
588
589 return r;
590}
591
592// converts a ieee single/binary32 to a float
593ecb_function_ float ecb_binary32_to_float (uint32_t x) ecb_const;
594ecb_function_ float
595ecb_binary32_to_float (uint32_t x)
596{
597 /* emulation, only works for normals and subnormals and +0 */
598 ECB_EXTERN_C float ldexpf (float x, int e);
599
600 int neg = x >> 31;
601 int e = (x >> 23) & 0xffU;
602 float r;
603
604 x &= 0x7fffffU;
605
606 if (e)
607 x |= 0x800000U;
608
609 /* we distrust ldexpf a bit and do the 2**-24 scaling by an extra multiply */
610 r = ldexpf (x * (1.f / 0x1000000U), e - 126);
611
612 return neg ? -r : r;
613}
614
615ecb_function_ uint64_t ecb_double_to_binary64 (double x) ecb_const;
616ecb_function_ uint64_t
617ecb_double_to_binary64 (double x)
618{
619}
620
621#endif
622

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines