… | |
… | |
29 | |
29 | |
30 | #ifndef ECB_H |
30 | #ifndef ECB_H |
31 | #define ECB_H |
31 | #define ECB_H |
32 | |
32 | |
33 | /* 16 bits major, 16 bits minor */ |
33 | /* 16 bits major, 16 bits minor */ |
34 | #define ECB_VERSION 0x00010001 |
34 | #define ECB_VERSION 0x00010002 |
35 | |
35 | |
36 | #ifdef _WIN32 |
36 | #ifdef _WIN32 |
37 | typedef signed char int8_t; |
37 | typedef signed char int8_t; |
38 | typedef unsigned char uint8_t; |
38 | typedef unsigned char uint8_t; |
39 | typedef signed short int16_t; |
39 | typedef signed short int16_t; |
… | |
… | |
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 */ |
… | |
… | |
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 (stdatomic.h) about the C11 memory model. */ |
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) |
|
|
153 | |
|
|
154 | /* The __has_feature syntax from clang is so misdesigned that we cannot use it |
|
|
155 | * without risking compile time errors with other compilers. We *could* |
|
|
156 | * define our own ecb_clang_has_feature, but I just can't be bothered to work |
|
|
157 | * around * this shit time and again. |
144 | #elif defined __clang && __has_feature (cxx_atomic) |
158 | * #elif defined __clang && __has_feature (cxx_atomic) |
145 | /* see comment below (stdatomic.h) about the C11 memory model. */ |
159 | * // see comment below (stdatomic.h) about the C11 memory model. |
146 | #define ECB_MEMORY_FENCE __c11_atomic_thread_fence (__ATOMIC_SEQ_CST) |
160 | * #define ECB_MEMORY_FENCE __c11_atomic_thread_fence (__ATOMIC_SEQ_CST) |
|
|
161 | */ |
|
|
162 | |
147 | #elif ECB_GCC_VERSION(4,4) || defined __INTEL_COMPILER || defined __clang__ |
163 | #elif ECB_GCC_VERSION(4,4) || defined __INTEL_COMPILER || defined __clang__ |
148 | #define ECB_MEMORY_FENCE __sync_synchronize () |
164 | #define ECB_MEMORY_FENCE __sync_synchronize () |
149 | #elif _MSC_VER >= 1400 /* VC++ 2005 */ |
165 | #elif _MSC_VER >= 1400 /* VC++ 2005 */ |
150 | #pragma intrinsic(_ReadBarrier,_WriteBarrier,_ReadWriteBarrier) |
166 | #pragma intrinsic(_ReadBarrier,_WriteBarrier,_ReadWriteBarrier) |
151 | #define ECB_MEMORY_FENCE _ReadWriteBarrier () |
167 | #define ECB_MEMORY_FENCE _ReadWriteBarrier () |
… | |
… | |
525 | } |
541 | } |
526 | #else |
542 | #else |
527 | #define ecb_array_length(name) (sizeof (name) / sizeof (name [0])) |
543 | #define ecb_array_length(name) (sizeof (name) / sizeof (name [0])) |
528 | #endif |
544 | #endif |
529 | |
545 | |
|
|
546 | /*******************************************************************************/ |
|
|
547 | /* floating point stuff, can be disabled by defining ECB_NO_LIBM */ |
|
|
548 | |
|
|
549 | /* basically, everything uses "ieee pure-endian" floating point numbers */ |
|
|
550 | /* the only noteworthy exception is ancient armle, which uses order 43218765 */ |
|
|
551 | #if 0 \ |
|
|
552 | || __i386 || __i386__ \ |
|
|
553 | || __amd64 || __amd64__ || __x86_64 || __x86_64__ \ |
|
|
554 | || __powerpc__ || __ppc__ || __powerpc64__ || __ppc64__ \ |
|
|
555 | || defined __arm__ && defined __ARM_EABI__ \ |
|
|
556 | || defined __s390__ || defined __s390x__ \ |
|
|
557 | || defined __mips__ \ |
|
|
558 | || defined __alpha__ \ |
|
|
559 | || defined __hppa__ \ |
|
|
560 | || defined __ia64__ \ |
|
|
561 | || defined _M_IX86 || defined _M_AMD64 || defined _M_IA64 |
|
|
562 | #define ECB_STDFP 1 |
|
|
563 | #include <string.h> /* for memcpy */ |
|
|
564 | #else |
|
|
565 | #define ECB_STDFP 0 |
|
|
566 | #include <math.h> /* for frexp*, ldexp* */ |
|
|
567 | #endif |
|
|
568 | |
|
|
569 | #ifndef ECB_NO_LIBM |
|
|
570 | |
|
|
571 | /* convert a float to ieee single/binary32 */ |
|
|
572 | ecb_function_ uint32_t ecb_float_to_binary32 (float x) ecb_const; |
|
|
573 | ecb_function_ uint32_t |
|
|
574 | ecb_float_to_binary32 (float x) |
|
|
575 | { |
|
|
576 | uint32_t r; |
|
|
577 | |
|
|
578 | #if ECB_STDFP |
|
|
579 | memcpy (&r, &x, 4); |
|
|
580 | #else |
|
|
581 | /* slow emulation, works for anything but -0 */ |
|
|
582 | uint32_t m; |
|
|
583 | int e; |
|
|
584 | |
|
|
585 | if (x == 0e0f ) return 0x00000000U; |
|
|
586 | if (x > +3.40282346638528860e+38f) return 0x7f800000U; |
|
|
587 | if (x < -3.40282346638528860e+38f) return 0xff800000U; |
|
|
588 | if (x != x ) return 0x7fbfffffU; |
|
|
589 | |
|
|
590 | m = frexpf (x, &e) * 0x1000000U; |
|
|
591 | |
|
|
592 | r = m & 0x80000000U; |
|
|
593 | |
|
|
594 | if (r) |
|
|
595 | m = -m; |
|
|
596 | |
|
|
597 | if (e <= -126) |
|
|
598 | { |
|
|
599 | m &= 0xffffffU; |
|
|
600 | m >>= (-125 - e); |
|
|
601 | e = -126; |
|
|
602 | } |
|
|
603 | |
|
|
604 | r |= (e + 126) << 23; |
|
|
605 | r |= m & 0x7fffffU; |
530 | #endif |
606 | #endif |
531 | |
607 | |
|
|
608 | return r; |
|
|
609 | } |
|
|
610 | |
|
|
611 | /* converts an ieee single/binary32 to a float */ |
|
|
612 | ecb_function_ float ecb_binary32_to_float (uint32_t x) ecb_const; |
|
|
613 | ecb_function_ float |
|
|
614 | ecb_binary32_to_float (uint32_t x) |
|
|
615 | { |
|
|
616 | float r; |
|
|
617 | |
|
|
618 | #if ECB_STDFP |
|
|
619 | memcpy (&r, &x, 4); |
|
|
620 | #else |
|
|
621 | /* emulation, only works for normals and subnormals and +0 */ |
|
|
622 | int neg = x >> 31; |
|
|
623 | int e = (x >> 23) & 0xffU; |
|
|
624 | |
|
|
625 | x &= 0x7fffffU; |
|
|
626 | |
|
|
627 | if (e) |
|
|
628 | x |= 0x800000U; |
|
|
629 | else |
|
|
630 | e = 1; |
|
|
631 | |
|
|
632 | /* we distrust ldexpf a bit and do the 2**-24 scaling by an extra multiply */ |
|
|
633 | r = ldexpf (x * (0.5f / 0x800000U), e - 126); |
|
|
634 | |
|
|
635 | r = neg ? -r : r; |
|
|
636 | #endif |
|
|
637 | |
|
|
638 | return r; |
|
|
639 | } |
|
|
640 | |
|
|
641 | /* convert a double to ieee double/binary64 */ |
|
|
642 | ecb_function_ uint64_t ecb_double_to_binary64 (double x) ecb_const; |
|
|
643 | ecb_function_ uint64_t |
|
|
644 | ecb_double_to_binary64 (double x) |
|
|
645 | { |
|
|
646 | uint64_t r; |
|
|
647 | |
|
|
648 | #if ECB_STDFP |
|
|
649 | memcpy (&r, &x, 8); |
|
|
650 | #else |
|
|
651 | /* slow emulation, works for anything but -0 */ |
|
|
652 | uint64_t m; |
|
|
653 | int e; |
|
|
654 | |
|
|
655 | if (x == 0e0 ) return 0x0000000000000000U; |
|
|
656 | if (x > +1.79769313486231470e+308) return 0x7ff0000000000000U; |
|
|
657 | if (x < -1.79769313486231470e+308) return 0xfff0000000000000U; |
|
|
658 | if (x != x ) return 0X7ff7ffffffffffffU; |
|
|
659 | |
|
|
660 | m = frexp (x, &e) * 0x20000000000000U; |
|
|
661 | |
|
|
662 | r = m & 0x8000000000000000;; |
|
|
663 | |
|
|
664 | if (r) |
|
|
665 | m = -m; |
|
|
666 | |
|
|
667 | if (e <= -1022) |
|
|
668 | { |
|
|
669 | m &= 0x1fffffffffffffU; |
|
|
670 | m >>= (-1021 - e); |
|
|
671 | e = -1022; |
|
|
672 | } |
|
|
673 | |
|
|
674 | r |= ((uint64_t)(e + 1022)) << 52; |
|
|
675 | r |= m & 0xfffffffffffffU; |
|
|
676 | #endif |
|
|
677 | |
|
|
678 | return r; |
|
|
679 | } |
|
|
680 | |
|
|
681 | /* converts an ieee double/binary64 to a double */ |
|
|
682 | ecb_function_ double ecb_binary64_to_double (uint64_t x) ecb_const; |
|
|
683 | ecb_function_ double |
|
|
684 | ecb_binary64_to_double (uint64_t x) |
|
|
685 | { |
|
|
686 | double r; |
|
|
687 | |
|
|
688 | #if ECB_STDFP |
|
|
689 | memcpy (&r, &x, 8); |
|
|
690 | #else |
|
|
691 | /* emulation, only works for normals and subnormals and +0 */ |
|
|
692 | int neg = x >> 63; |
|
|
693 | int e = (x >> 52) & 0x7ffU; |
|
|
694 | |
|
|
695 | x &= 0xfffffffffffffU; |
|
|
696 | |
|
|
697 | if (e) |
|
|
698 | x |= 0x10000000000000U; |
|
|
699 | else |
|
|
700 | e = 1; |
|
|
701 | |
|
|
702 | /* we distrust ldexp a bit and do the 2**-53 scaling by an extra multiply */ |
|
|
703 | r = ldexp (x * (0.5 / 0x10000000000000U), e - 1022); |
|
|
704 | |
|
|
705 | r = neg ? -r : r; |
|
|
706 | #endif |
|
|
707 | |
|
|
708 | return r; |
|
|
709 | } |
|
|
710 | |
|
|
711 | #endif |
|
|
712 | |
|
|
713 | #endif |
|
|
714 | |