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

Comparing libeio/ecb.h (file contents):
Revision 1.26 by root, Mon Sep 21 08:01:00 2015 UTC vs.
Revision 1.27 by root, Mon Jan 18 11:53:09 2016 UTC

40 40
41#ifndef ECB_H 41#ifndef ECB_H
42#define ECB_H 42#define ECB_H
43 43
44/* 16 bits major, 16 bits minor */ 44/* 16 bits major, 16 bits minor */
45#define ECB_VERSION 0x00010004 45#define ECB_VERSION 0x00010005
46 46
47#ifdef _WIN32 47#ifdef _WIN32
48 typedef signed char int8_t; 48 typedef signed char int8_t;
49 typedef unsigned char uint8_t; 49 typedef unsigned char uint8_t;
50 typedef signed short int16_t; 50 typedef signed short int16_t;
67 typedef uint32_t uintptr_t; 67 typedef uint32_t uintptr_t;
68 typedef int32_t intptr_t; 68 typedef int32_t intptr_t;
69 #endif 69 #endif
70#else 70#else
71 #include <inttypes.h> 71 #include <inttypes.h>
72 #if UINTMAX_MAX > 0xffffffffU 72 #if (defined INTPTR_MAX ? INTPTR_MAX : ULONG_MAX) > 0xffffffffU
73 #define ECB_PTRSIZE 8 73 #define ECB_PTRSIZE 8
74 #else 74 #else
75 #define ECB_PTRSIZE 4 75 #define ECB_PTRSIZE 4
76 #endif 76 #endif
77#endif 77#endif
153#endif 153#endif
154 154
155/* http://www-01.ibm.com/support/knowledgecenter/SSGH3R_13.1.0/com.ibm.xlcpp131.aix.doc/compiler_ref/compiler_builtins.html */ 155/* http://www-01.ibm.com/support/knowledgecenter/SSGH3R_13.1.0/com.ibm.xlcpp131.aix.doc/compiler_ref/compiler_builtins.html */
156#if __xlC__ && ECB_CPP 156#if __xlC__ && ECB_CPP
157 #include <builtins.h> 157 #include <builtins.h>
158#endif
159
160#if 1400 <= _MSC_VER
161 #include <intrin.h> /* fence functions _ReadBarrier, also bit search functions _BitScanReverse */
158#endif 162#endif
159 163
160#ifndef ECB_MEMORY_FENCE 164#ifndef ECB_MEMORY_FENCE
161 #if ECB_GCC_VERSION(2,5) || defined __INTEL_COMPILER || (__llvm__ && __GNUC__) || __SUNPRO_C >= 0x5110 || __SUNPRO_CC >= 0x5110 165 #if ECB_GCC_VERSION(2,5) || defined __INTEL_COMPILER || (__llvm__ && __GNUC__) || __SUNPRO_C >= 0x5110 || __SUNPRO_CC >= 0x5110
162 #if __i386 || __i386__ 166 #if __i386 || __i386__
167 #define ECB_MEMORY_FENCE __asm__ __volatile__ ("mfence" : : : "memory") 171 #define ECB_MEMORY_FENCE __asm__ __volatile__ ("mfence" : : : "memory")
168 #define ECB_MEMORY_FENCE_ACQUIRE __asm__ __volatile__ ("" : : : "memory") 172 #define ECB_MEMORY_FENCE_ACQUIRE __asm__ __volatile__ ("" : : : "memory")
169 #define ECB_MEMORY_FENCE_RELEASE __asm__ __volatile__ ("") 173 #define ECB_MEMORY_FENCE_RELEASE __asm__ __volatile__ ("")
170 #elif __powerpc__ || __ppc__ || __powerpc64__ || __ppc64__ 174 #elif __powerpc__ || __ppc__ || __powerpc64__ || __ppc64__
171 #define ECB_MEMORY_FENCE __asm__ __volatile__ ("sync" : : : "memory") 175 #define ECB_MEMORY_FENCE __asm__ __volatile__ ("sync" : : : "memory")
176 #elif defined __ARM_ARCH_2__ \
177 || defined __ARM_ARCH_3__ || defined __ARM_ARCH_3M__ \
178 || defined __ARM_ARCH_4__ || defined __ARM_ARCH_4T__ \
179 || defined __ARM_ARCH_5__ || defined __ARM_ARCH_5E__ \
180 || defined __ARM_ARCH_5T__ || defined __ARM_ARCH_5TE__ \
181 || defined __ARM_ARCH_5TEJ__
182 /* should not need any, unless running old code on newer cpu - arm doesn't support that */
172 #elif defined __ARM_ARCH_6__ || defined __ARM_ARCH_6J__ \ 183 #elif defined __ARM_ARCH_6__ || defined __ARM_ARCH_6J__ \
173 || defined __ARM_ARCH_6K__ || defined __ARM_ARCH_6ZK__ 184 || defined __ARM_ARCH_6K__ || defined __ARM_ARCH_6ZK__ \
185 || defined __ARM_ARCH_6T2__
174 #define ECB_MEMORY_FENCE __asm__ __volatile__ ("mcr p15,0,%0,c7,c10,5" : : "r" (0) : "memory") 186 #define ECB_MEMORY_FENCE __asm__ __volatile__ ("mcr p15,0,%0,c7,c10,5" : : "r" (0) : "memory")
175 #elif defined __ARM_ARCH_7__ || defined __ARM_ARCH_7A__ \ 187 #elif defined __ARM_ARCH_7__ || defined __ARM_ARCH_7A__ \
176 || defined __ARM_ARCH_7M__ || defined __ARM_ARCH_7R__ 188 || defined __ARM_ARCH_7R__ || defined __ARM_ARCH_7M__
177 #define ECB_MEMORY_FENCE __asm__ __volatile__ ("dmb" : : : "memory") 189 #define ECB_MEMORY_FENCE __asm__ __volatile__ ("dmb" : : : "memory")
178 #elif __aarch64__ 190 #elif __aarch64__
179 #define ECB_MEMORY_FENCE __asm__ __volatile__ ("dmb ish" : : : "memory") 191 #define ECB_MEMORY_FENCE __asm__ __volatile__ ("dmb ish" : : : "memory")
180 #elif (__sparc || __sparc__) && !(__sparc_v8__ || defined __sparcv8) 192 #elif (__sparc || __sparc__) && !(__sparc_v8__ || defined __sparcv8)
181 #define ECB_MEMORY_FENCE __asm__ __volatile__ ("membar #LoadStore | #LoadLoad | #StoreStore | #StoreLoad" : : : "memory") 193 #define ECB_MEMORY_FENCE __asm__ __volatile__ ("membar #LoadStore | #LoadLoad | #StoreStore | #StoreLoad" : : : "memory")
423#else 435#else
424 ecb_function_ ecb_const int ecb_ctz32 (uint32_t x); 436 ecb_function_ ecb_const int ecb_ctz32 (uint32_t x);
425 ecb_function_ ecb_const int 437 ecb_function_ ecb_const int
426 ecb_ctz32 (uint32_t x) 438 ecb_ctz32 (uint32_t x)
427 { 439 {
440#if 1400 <= _MSC_VER && (_M_IX86 || _M_X64 || _M_IA64 || _M_ARM)
441 unsigned long r;
442 _BitScanForward (&r, x);
443 return (int)r;
444#else
428 int r = 0; 445 int r = 0;
429 446
430 x &= ~x + 1; /* this isolates the lowest bit */ 447 x &= ~x + 1; /* this isolates the lowest bit */
431 448
432#if ECB_branchless_on_i386 449#if ECB_branchless_on_i386
442 if (x & 0xff00ff00) r += 8; 459 if (x & 0xff00ff00) r += 8;
443 if (x & 0xffff0000) r += 16; 460 if (x & 0xffff0000) r += 16;
444#endif 461#endif
445 462
446 return r; 463 return r;
464#endif
447 } 465 }
448 466
449 ecb_function_ ecb_const int ecb_ctz64 (uint64_t x); 467 ecb_function_ ecb_const int ecb_ctz64 (uint64_t x);
450 ecb_function_ ecb_const int 468 ecb_function_ ecb_const int
451 ecb_ctz64 (uint64_t x) 469 ecb_ctz64 (uint64_t x)
452 { 470 {
471#if 1400 <= _MSC_VER && (_M_X64 || _M_IA64 || _M_ARM)
472 unsigned long r;
473 _BitScanForward64 (&r, x);
474 return (int)r;
475#else
453 int shift = x & 0xffffffffU ? 0 : 32; 476 int shift = x & 0xffffffff ? 0 : 32;
454 return ecb_ctz32 (x >> shift) + shift; 477 return ecb_ctz32 (x >> shift) + shift;
478#endif
455 } 479 }
456 480
457 ecb_function_ ecb_const int ecb_popcount32 (uint32_t x); 481 ecb_function_ ecb_const int ecb_popcount32 (uint32_t x);
458 ecb_function_ ecb_const int 482 ecb_function_ ecb_const int
459 ecb_popcount32 (uint32_t x) 483 ecb_popcount32 (uint32_t x)
467 } 491 }
468 492
469 ecb_function_ ecb_const int ecb_ld32 (uint32_t x); 493 ecb_function_ ecb_const int ecb_ld32 (uint32_t x);
470 ecb_function_ ecb_const int ecb_ld32 (uint32_t x) 494 ecb_function_ ecb_const int ecb_ld32 (uint32_t x)
471 { 495 {
496#if 1400 <= _MSC_VER && (_M_IX86 || _M_X64 || _M_IA64 || _M_ARM)
497 unsigned long r;
498 _BitScanReverse (&r, x);
499 return (int)r;
500#else
472 int r = 0; 501 int r = 0;
473 502
474 if (x >> 16) { x >>= 16; r += 16; } 503 if (x >> 16) { x >>= 16; r += 16; }
475 if (x >> 8) { x >>= 8; r += 8; } 504 if (x >> 8) { x >>= 8; r += 8; }
476 if (x >> 4) { x >>= 4; r += 4; } 505 if (x >> 4) { x >>= 4; r += 4; }
477 if (x >> 2) { x >>= 2; r += 2; } 506 if (x >> 2) { x >>= 2; r += 2; }
478 if (x >> 1) { r += 1; } 507 if (x >> 1) { r += 1; }
479 508
480 return r; 509 return r;
510#endif
481 } 511 }
482 512
483 ecb_function_ ecb_const int ecb_ld64 (uint64_t x); 513 ecb_function_ ecb_const int ecb_ld64 (uint64_t x);
484 ecb_function_ ecb_const int ecb_ld64 (uint64_t x) 514 ecb_function_ ecb_const int ecb_ld64 (uint64_t x)
485 { 515 {
516#if 1400 <= _MSC_VER && (_M_X64 || _M_IA64 || _M_ARM)
517 unsigned long r;
518 _BitScanReverse64 (&r, x);
519 return (int)r;
520#else
486 int r = 0; 521 int r = 0;
487 522
488 if (x >> 32) { x >>= 32; r += 32; } 523 if (x >> 32) { x >>= 32; r += 32; }
489 524
490 return r + ecb_ld32 (x); 525 return r + ecb_ld32 (x);
526#endif
491 } 527 }
492#endif 528#endif
493 529
494ecb_function_ ecb_const ecb_bool ecb_is_pot32 (uint32_t x); 530ecb_function_ ecb_const ecb_bool ecb_is_pot32 (uint32_t x);
495ecb_function_ ecb_const ecb_bool ecb_is_pot32 (uint32_t x) { return !(x & (x - 1)); } 531ecb_function_ ecb_const ecb_bool ecb_is_pot32 (uint32_t x) { return !(x & (x - 1)); }
598#endif 634#endif
599 635
600/* try to tell the compiler that some condition is definitely true */ 636/* try to tell the compiler that some condition is definitely true */
601#define ecb_assume(cond) if (!(cond)) ecb_unreachable (); else 0 637#define ecb_assume(cond) if (!(cond)) ecb_unreachable (); else 0
602 638
603ecb_inline ecb_const unsigned char ecb_byteorder_helper (void); 639ecb_inline ecb_const uint32_t ecb_byteorder_helper (void);
604ecb_inline ecb_const unsigned char 640ecb_inline ecb_const uint32_t
605ecb_byteorder_helper (void) 641ecb_byteorder_helper (void)
606{ 642{
607 /* the union code still generates code under pressure in gcc, */ 643 /* the union code still generates code under pressure in gcc, */
608 /* but less than using pointers, and always seems to */ 644 /* but less than using pointers, and always seems to */
609 /* successfully return a constant. */ 645 /* successfully return a constant. */
610 /* the reason why we have this horrible preprocessor mess */ 646 /* the reason why we have this horrible preprocessor mess */
611 /* is to avoid it in all cases, at least on common architectures */ 647 /* is to avoid it in all cases, at least on common architectures */
612 /* or when using a recent enough gcc version (>= 4.6) */ 648 /* or when using a recent enough gcc version (>= 4.6) */
613#if ((__i386 || __i386__) && !__VOS__) || _M_IX86 || ECB_GCC_AMD64 || ECB_MSVC_AMD64
614 return 0x44;
615#elif __BYTE_ORDER__ && __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ 649#if (defined __BYTE_ORDER__ && __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__) \
650 || ((__i386 || __i386__ || _M_IX86 || ECB_GCC_AMD64 || ECB_MSVC_AMD64) && !__VOS__)
651 #define ECB_LITTLE_ENDIAN 1
616 return 0x44; 652 return 0x44332211;
617#elif __BYTE_ORDER__ && __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ 653#elif (defined __BYTE_ORDER__ && __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__) \
654 || ((__AARCH64EB__ || __MIPSEB__ || __ARMEB__) && !__VOS__)
655 #define ECB_BIG_ENDIAN 1
618 return 0x11; 656 return 0x11223344;
619#else 657#else
620 union 658 union
621 { 659 {
660 uint8_t c[4];
622 uint32_t i; 661 uint32_t u;
623 uint8_t c;
624 } u = { 0x11223344 }; 662 } u = { 0x11, 0x22, 0x33, 0x44 };
625 return u.c; 663 return u.u;
626#endif 664#endif
627} 665}
628 666
629ecb_inline ecb_const ecb_bool ecb_big_endian (void); 667ecb_inline ecb_const ecb_bool ecb_big_endian (void);
630ecb_inline ecb_const ecb_bool ecb_big_endian (void) { return ecb_byteorder_helper () == 0x11; } 668ecb_inline ecb_const ecb_bool ecb_big_endian (void) { return ecb_byteorder_helper () == 0x11223344; }
631ecb_inline ecb_const ecb_bool ecb_little_endian (void); 669ecb_inline ecb_const ecb_bool ecb_little_endian (void);
632ecb_inline ecb_const ecb_bool ecb_little_endian (void) { return ecb_byteorder_helper () == 0x44; } 670ecb_inline ecb_const ecb_bool ecb_little_endian (void) { return ecb_byteorder_helper () == 0x44332211; }
633 671
634#if ECB_GCC_VERSION(3,0) || ECB_C99 672#if ECB_GCC_VERSION(3,0) || ECB_C99
635 #define ecb_mod(m,n) ((m) % (n) + ((m) % (n) < 0 ? (n) : 0)) 673 #define ecb_mod(m,n) ((m) % (n) + ((m) % (n) < 0 ? (n) : 0))
636#else 674#else
637 #define ecb_mod(m,n) ((m) < 0 ? ((n) - 1 - ((-1 - (m)) % (n))) : ((m) % (n))) 675 #define ecb_mod(m,n) ((m) < 0 ? ((n) - 1 - ((-1 - (m)) % (n))) : ((m) % (n)))
661 return N; 699 return N;
662 } 700 }
663#else 701#else
664 #define ecb_array_length(name) (sizeof (name) / sizeof (name [0])) 702 #define ecb_array_length(name) (sizeof (name) / sizeof (name [0]))
665#endif 703#endif
704
705ecb_function_ ecb_const uint32_t ecb_binary16_to_binary32 (uint32_t x);
706ecb_function_ ecb_const uint32_t
707ecb_binary16_to_binary32 (uint32_t x)
708{
709 unsigned int s = (x & 0x8000) << (31 - 15);
710 int e = (x >> 10) & 0x001f;
711 unsigned int m = x & 0x03ff;
712
713 if (ecb_expect_false (e == 31))
714 /* infinity or NaN */
715 e = 255 - (127 - 15);
716 else if (ecb_expect_false (!e))
717 {
718 if (ecb_expect_true (!m))
719 /* zero, handled by code below by forcing e to 0 */
720 e = 0 - (127 - 15);
721 else
722 {
723 /* subnormal, renormalise */
724 unsigned int s = 10 - ecb_ld32 (m);
725
726 m = (m << s) & 0x3ff; /* mask implicit bit */
727 e -= s - 1;
728 }
729 }
730
731 /* e and m now are normalised, or zero, (or inf or nan) */
732 e += 127 - 15;
733
734 return s | (e << 23) | (m << (23 - 10));
735}
736
737ecb_function_ ecb_const uint16_t ecb_binary32_to_binary16 (uint32_t x);
738ecb_function_ ecb_const uint16_t
739ecb_binary32_to_binary16 (uint32_t x)
740{
741 unsigned int s = (x >> 16) & 0x00008000; /* sign bit, the easy part */
742 unsigned int e = ((x >> 23) & 0x000000ff) - (127 - 15); /* the desired exponent */
743 unsigned int m = x & 0x007fffff;
744
745 x &= 0x7fffffff;
746
747 /* if it's within range of binary16 normals, use fast path */
748 if (ecb_expect_true (0x38800000 <= x && x <= 0x477fefff))
749 {
750 /* mantissa round-to-even */
751 m += 0x00000fff + ((m >> (23 - 10)) & 1);
752
753 /* handle overflow */
754 if (ecb_expect_false (m >= 0x00800000))
755 {
756 m >>= 1;
757 e += 1;
758 }
759
760 return s | (e << 10) | (m >> (23 - 10));
761 }
762
763 /* handle large numbers and infinity */
764 if (ecb_expect_true (0x477fefff < x && x <= 0x7f800000))
765 return s | 0x7c00;
766
767 /* handle zero, subnormals and small numbers */
768 if (ecb_expect_true (x < 0x38800000))
769 {
770 /* zero */
771 if (ecb_expect_true (!x))
772 return s;
773
774 /* handle subnormals */
775
776 /* too small, will be zero */
777 if (e < (14 - 24)) /* might not be sharp, but is good enough */
778 return s;
779
780 m |= 0x00800000; /* make implicit bit explicit */
781
782 /* very tricky - we need to round to the nearest e (+10) bit value */
783 {
784 unsigned int bits = 14 - e;
785 unsigned int half = (1 << (bits - 1)) - 1;
786 unsigned int even = (m >> bits) & 1;
787
788 /* if this overflows, we will end up with a normalised number */
789 m = (m + half + even) >> bits;
790 }
791
792 return s | m;
793 }
794
795 /* handle NaNs, preserve leftmost nan bits, but make sure we don't turn them into infinities */
796 m >>= 13;
797
798 return s | 0x7c00 | m | !m;
799}
666 800
667/*******************************************************************************/ 801/*******************************************************************************/
668/* floating point stuff, can be disabled by defining ECB_NO_LIBM */ 802/* floating point stuff, can be disabled by defining ECB_NO_LIBM */
669 803
670/* basically, everything uses "ieee pure-endian" floating point numbers */ 804/* basically, everything uses "ieee pure-endian" floating point numbers */
713 #else 847 #else
714 #define ecb_ldexpf(x,e) (float) ldexp ((double) (x), (e)) 848 #define ecb_ldexpf(x,e) (float) ldexp ((double) (x), (e))
715 #define ecb_frexpf(x,e) (float) frexp ((double) (x), (e)) 849 #define ecb_frexpf(x,e) (float) frexp ((double) (x), (e))
716 #endif 850 #endif
717 851
718 /* converts an ieee half/binary16 to a float */
719 ecb_function_ ecb_const float ecb_binary16_to_float (uint16_t x);
720 ecb_function_ ecb_const float
721 ecb_binary16_to_float (uint16_t x)
722 {
723 int e = (x >> 10) & 0x1f;
724 int m = x & 0x3ff;
725 float r;
726
727 if (!e ) r = ecb_ldexpf (m , -24);
728 else if (e != 31) r = ecb_ldexpf (m + 0x400, e - 25);
729 else if (m ) r = ECB_NAN;
730 else r = ECB_INFINITY;
731
732 return x & 0x8000 ? -r : r;
733 }
734
735 /* convert a float to ieee single/binary32 */ 852 /* convert a float to ieee single/binary32 */
736 ecb_function_ ecb_const uint32_t ecb_float_to_binary32 (float x); 853 ecb_function_ ecb_const uint32_t ecb_float_to_binary32 (float x);
737 ecb_function_ ecb_const uint32_t 854 ecb_function_ ecb_const uint32_t
738 ecb_float_to_binary32 (float x) 855 ecb_float_to_binary32 (float x)
739 { 856 {
870 #endif 987 #endif
871 988
872 return r; 989 return r;
873 } 990 }
874 991
875#endif 992 /* convert a float to ieee half/binary16 */
993 ecb_function_ ecb_const uint16_t ecb_float_to_binary16 (float x);
994 ecb_function_ ecb_const uint16_t
995 ecb_float_to_binary16 (float x)
996 {
997 return ecb_binary32_to_binary16 (ecb_float_to_binary32 (x));
998 }
876 999
877#endif 1000 /* convert an ieee half/binary16 to float */
1001 ecb_function_ ecb_const float ecb_binary16_to_float (uint16_t x);
1002 ecb_function_ ecb_const float
1003 ecb_binary16_to_float (uint16_t x)
1004 {
1005 return ecb_binary32_to_float (ecb_binary16_to_binary32 (x));
1006 }
878 1007
1008#endif
1009
1010#endif
1011

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines