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

Comparing libecb/ecb.h (file contents):
Revision 1.160 by sf-exg, Thu Mar 19 15:43:51 2015 UTC vs.
Revision 1.170 by root, Sat Nov 21 21:14:55 2015 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;
148 #define ECB_NO_SMP 1 148 #define ECB_NO_SMP 1
149#endif 149#endif
150 150
151#if ECB_NO_SMP 151#if ECB_NO_SMP
152 #define ECB_MEMORY_FENCE do { } while (0) 152 #define ECB_MEMORY_FENCE do { } while (0)
153#endif
154
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
157 #include <builtins.h>
153#endif 158#endif
154 159
155#ifndef ECB_MEMORY_FENCE 160#ifndef ECB_MEMORY_FENCE
156 #if ECB_GCC_VERSION(2,5) || defined __INTEL_COMPILER || (__llvm__ && __GNUC__) || __SUNPRO_C >= 0x5110 || __SUNPRO_CC >= 0x5110 161 #if ECB_GCC_VERSION(2,5) || defined __INTEL_COMPILER || (__llvm__ && __GNUC__) || __SUNPRO_C >= 0x5110 || __SUNPRO_CC >= 0x5110
157 #if __i386 || __i386__ 162 #if __i386 || __i386__
170 #elif defined __ARM_ARCH_7__ || defined __ARM_ARCH_7A__ \ 175 #elif defined __ARM_ARCH_7__ || defined __ARM_ARCH_7A__ \
171 || defined __ARM_ARCH_7M__ || defined __ARM_ARCH_7R__ 176 || defined __ARM_ARCH_7M__ || defined __ARM_ARCH_7R__
172 #define ECB_MEMORY_FENCE __asm__ __volatile__ ("dmb" : : : "memory") 177 #define ECB_MEMORY_FENCE __asm__ __volatile__ ("dmb" : : : "memory")
173 #elif __aarch64__ 178 #elif __aarch64__
174 #define ECB_MEMORY_FENCE __asm__ __volatile__ ("dmb ish" : : : "memory") 179 #define ECB_MEMORY_FENCE __asm__ __volatile__ ("dmb ish" : : : "memory")
175 #elif (__sparc || __sparc__) && !__sparcv8 180 #elif (__sparc || __sparc__) && !(__sparc_v8__ || defined __sparcv8)
176 #define ECB_MEMORY_FENCE __asm__ __volatile__ ("membar #LoadStore | #LoadLoad | #StoreStore | #StoreLoad" : : : "memory") 181 #define ECB_MEMORY_FENCE __asm__ __volatile__ ("membar #LoadStore | #LoadLoad | #StoreStore | #StoreLoad" : : : "memory")
177 #define ECB_MEMORY_FENCE_ACQUIRE __asm__ __volatile__ ("membar #LoadStore | #LoadLoad" : : : "memory") 182 #define ECB_MEMORY_FENCE_ACQUIRE __asm__ __volatile__ ("membar #LoadStore | #LoadLoad" : : : "memory")
178 #define ECB_MEMORY_FENCE_RELEASE __asm__ __volatile__ ("membar #LoadStore | #StoreStore") 183 #define ECB_MEMORY_FENCE_RELEASE __asm__ __volatile__ ("membar #LoadStore | #StoreStore")
179 #elif defined __s390__ || defined __s390x__ 184 #elif defined __s390__ || defined __s390x__
180 #define ECB_MEMORY_FENCE __asm__ __volatile__ ("bcr 15,0" : : : "memory") 185 #define ECB_MEMORY_FENCE __asm__ __volatile__ ("bcr 15,0" : : : "memory")
352 #define ecb_deprecated __declspec (deprecated) 357 #define ecb_deprecated __declspec (deprecated)
353#else 358#else
354 #define ecb_deprecated ecb_attribute ((__deprecated__)) 359 #define ecb_deprecated ecb_attribute ((__deprecated__))
355#endif 360#endif
356 361
357#if __MSC_VER >= 1500 362#if _MSC_VER >= 1500
358 #define ecb_deprecated_message(msg) __declspec (deprecated (msg)) 363 #define ecb_deprecated_message(msg) __declspec (deprecated (msg))
359#elif ECB_GCC_VERSION(4,5) 364#elif ECB_GCC_VERSION(4,5)
360 #define ecb_deprecated_message(msg) ecb_attribute ((__deprecated__ (msg)) 365 #define ecb_deprecated_message(msg) ecb_attribute ((__deprecated__ (msg))
361#else 366#else
362 #define ecb_deprecated_message(msg) ecb_deprecated 367 #define ecb_deprecated_message(msg) ecb_deprecated
371#define ecb_unused ecb_attribute ((__unused__)) 376#define ecb_unused ecb_attribute ((__unused__))
372#define ecb_const ecb_attribute ((__const__)) 377#define ecb_const ecb_attribute ((__const__))
373#define ecb_pure ecb_attribute ((__pure__)) 378#define ecb_pure ecb_attribute ((__pure__))
374 379
375#if ECB_C11 || __IBMC_NORETURN 380#if ECB_C11 || __IBMC_NORETURN
376 /* http://pic.dhe.ibm.com/infocenter/compbg/v121v141/topic/com.ibm.xlcpp121.bg.doc/language_ref/noreturn.html */ 381 /* http://www-01.ibm.com/support/knowledgecenter/SSGH3R_13.1.0/com.ibm.xlcpp131.aix.doc/language_ref/noreturn.html */
377 #define ecb_noreturn _Noreturn 382 #define ecb_noreturn _Noreturn
378#elif ECB_CPP11 383#elif ECB_CPP11
379 #define ecb_noreturn [[noreturn]] 384 #define ecb_noreturn [[noreturn]]
380#elif _MSC_VER >= 1200 385#elif _MSC_VER >= 1200
381 /* http://msdn.microsoft.com/en-us/library/k6ktzx3s.aspx */ 386 /* http://msdn.microsoft.com/en-us/library/k6ktzx3s.aspx */
443 448
444 ecb_function_ ecb_const int ecb_ctz64 (uint64_t x); 449 ecb_function_ ecb_const int ecb_ctz64 (uint64_t x);
445 ecb_function_ ecb_const int 450 ecb_function_ ecb_const int
446 ecb_ctz64 (uint64_t x) 451 ecb_ctz64 (uint64_t x)
447 { 452 {
448 int shift = x & 0xffffffffU ? 0 : 32; 453 int shift = x & 0xffffffff ? 0 : 32;
449 return ecb_ctz32 (x >> shift) + shift; 454 return ecb_ctz32 (x >> shift) + shift;
450 } 455 }
451 456
452 ecb_function_ ecb_const int ecb_popcount32 (uint32_t x); 457 ecb_function_ ecb_const int ecb_popcount32 (uint32_t x);
453 ecb_function_ ecb_const int 458 ecb_function_ ecb_const int
547ecb_inline ecb_const uint32_t ecb_rotr32 (uint32_t x, unsigned int count) { return (x << (32 - count)) | (x >> count); } 552ecb_inline ecb_const uint32_t ecb_rotr32 (uint32_t x, unsigned int count) { return (x << (32 - count)) | (x >> count); }
548ecb_inline ecb_const uint64_t ecb_rotl64 (uint64_t x, unsigned int count) { return (x >> (64 - count)) | (x << count); } 553ecb_inline ecb_const uint64_t ecb_rotl64 (uint64_t x, unsigned int count) { return (x >> (64 - count)) | (x << count); }
549ecb_inline ecb_const uint64_t ecb_rotr64 (uint64_t x, unsigned int count) { return (x << (64 - count)) | (x >> count); } 554ecb_inline ecb_const uint64_t ecb_rotr64 (uint64_t x, unsigned int count) { return (x << (64 - count)) | (x >> count); }
550 555
551#if ECB_GCC_VERSION(4,3) || (ECB_CLANG_BUILTIN(__builtin_bswap32) && ECB_CLANG_BUILTIN(__builtin_bswap64)) 556#if ECB_GCC_VERSION(4,3) || (ECB_CLANG_BUILTIN(__builtin_bswap32) && ECB_CLANG_BUILTIN(__builtin_bswap64))
557 #if ECB_GCC_VERSION(4,8) || ECB_CLANG_BUILTIN(__builtin_bswap16)
558 #define ecb_bswap16(x) __builtin_bswap16 (x)
559 #else
552 #define ecb_bswap16(x) (__builtin_bswap32 (x) >> 16) 560 #define ecb_bswap16(x) (__builtin_bswap32 (x) >> 16)
561 #endif
553 #define ecb_bswap32(x) __builtin_bswap32 (x) 562 #define ecb_bswap32(x) __builtin_bswap32 (x)
554 #define ecb_bswap64(x) __builtin_bswap64 (x) 563 #define ecb_bswap64(x) __builtin_bswap64 (x)
564#elif _MSC_VER
565 #include <stdlib.h>
566 #define ecb_bswap16(x) ((uint16_t)_byteswap_ushort ((uint16_t)(x)))
567 #define ecb_bswap32(x) ((uint32_t)_byteswap_ulong ((uint32_t)(x)))
568 #define ecb_bswap64(x) ((uint64_t)_byteswap_uint64 ((uint64_t)(x)))
555#else 569#else
556 ecb_function_ ecb_const uint16_t ecb_bswap16 (uint16_t x); 570 ecb_function_ ecb_const uint16_t ecb_bswap16 (uint16_t x);
557 ecb_function_ ecb_const uint16_t 571 ecb_function_ ecb_const uint16_t
558 ecb_bswap16 (uint16_t x) 572 ecb_bswap16 (uint16_t x)
559 { 573 {
647 return N; 661 return N;
648 } 662 }
649#else 663#else
650 #define ecb_array_length(name) (sizeof (name) / sizeof (name [0])) 664 #define ecb_array_length(name) (sizeof (name) / sizeof (name [0]))
651#endif 665#endif
666
667ecb_function_ ecb_const uint32_t ecb_binary16_to_binary32 (uint32_t x);
668ecb_function_ ecb_const uint32_t
669ecb_binary16_to_binary32 (uint32_t x)
670{
671 unsigned int s = (x & 0x8000) << (31 - 15);
672 int e = (x >> 10) & 0x001f;
673 unsigned int m = x & 0x03ff;
674
675 if (ecb_expect_false (e == 31))
676 /* infinity or NaN */
677 e = 255 - (127 - 15);
678 else if (ecb_expect_false (!e))
679 {
680 if (ecb_expect_true (!m))
681 /* zero, handled by code below by forcing e to 0 */
682 e = 0 - (127 - 15);
683 else
684 {
685 /* subnormal, renormalise */
686 unsigned int s = 10 - ecb_ld32 (m);
687
688 m = (m << s) & 0x3ff; /* mask implicit bit */
689 e -= s - 1;
690 }
691 }
692
693 /* e and m now are normalised, or zero, (or inf or nan) */
694 e += 127 - 15;
695
696 return s | (e << 23) | (m << (23 - 10));
697}
698
699ecb_function_ ecb_const uint16_t ecb_binary32_to_binary16 (uint32_t x);
700ecb_function_ ecb_const uint16_t
701ecb_binary32_to_binary16 (uint32_t x)
702{
703 unsigned int s = (x >> 16) & 0x00008000; /* sign bit, the easy part */
704 unsigned int e = ((x >> 23) & 0x000000ff) - (127 - 15); /* the desired exponent */
705 unsigned int m = x & 0x007fffff;
706
707 x &= 0x7fffffff;
708
709 /* if it's within range of binary16 normals, use fast path */
710 if (ecb_expect_true (0x38800000 <= x && x <= 0x477fefff))
711 {
712 /* mantissa round-to-even */
713 m += 0x00000fff + ((m >> (23 - 10)) & 1);
714
715 /* handle overflow */
716 if (ecb_expect_false (m >= 0x00800000))
717 {
718 m >>= 1;
719 e += 1;
720 }
721
722 return s | (e << 10) | (m >> (23 - 10));
723 }
724
725 /* handle large numbers and infinity */
726 if (ecb_expect_true (0x477fefff < x && x <= 0x7f800000))
727 return s | 0x7c00;
728
729 /* handle zero, subnormals and small numbers */
730 if (ecb_expect_true (x < 0x38800000))
731 {
732 /* zero */
733 if (ecb_expect_true (!x))
734 return s;
735
736 /* handle subnormals */
737
738 /* too small, will be zero */
739 if (e < (14 - 24)) /* might not be sharp, but is good enough */
740 return s;
741
742 m |= 0x00800000; /* make implicit bit explicit */
743
744 /* very tricky - we need to round to the nearest e (+10) bit value */
745 {
746 unsigned int bits = 14 - e;
747 unsigned int half = (1 << (bits - 1)) - 1;
748 unsigned int even = (m >> bits) & 1;
749
750 /* if this overflows, we will end up with a normalised number */
751 m = (m + half + even) >> bits;
752 }
753
754 return s | m;
755 }
756
757 /* handle NaNs, preserve leftmost nan bits, but make sure we don't turn them into infinities */
758 m >>= 13;
759
760 return s | 0x7c00 | m | !m;
761}
652 762
653/*******************************************************************************/ 763/*******************************************************************************/
654/* floating point stuff, can be disabled by defining ECB_NO_LIBM */ 764/* floating point stuff, can be disabled by defining ECB_NO_LIBM */
655 765
656/* basically, everything uses "ieee pure-endian" floating point numbers */ 766/* basically, everything uses "ieee pure-endian" floating point numbers */
693 #define ECB_NAN ECB_INFINITY 803 #define ECB_NAN ECB_INFINITY
694 #endif 804 #endif
695 805
696 #if ECB_C99 || _XOPEN_VERSION >= 600 || _POSIX_VERSION >= 200112L 806 #if ECB_C99 || _XOPEN_VERSION >= 600 || _POSIX_VERSION >= 200112L
697 #define ecb_ldexpf(x,e) ldexpf ((x), (e)) 807 #define ecb_ldexpf(x,e) ldexpf ((x), (e))
808 #define ecb_frexpf(x,e) frexpf ((x), (e))
698 #else 809 #else
699 #define ecb_ldexpf(x,e) (float) ldexp ((float) (x), (e)) 810 #define ecb_ldexpf(x,e) (float) ldexp ((double) (x), (e))
811 #define ecb_frexpf(x,e) (float) frexp ((double) (x), (e))
700 #endif 812 #endif
701
702 /* converts an ieee half/binary16 to a float */
703 ecb_function_ ecb_const float ecb_binary16_to_float (uint16_t x);
704 ecb_function_ ecb_const float
705 ecb_binary16_to_float (uint16_t x)
706 {
707 int e = (x >> 10) & 0x1f;
708 int m = x & 0x3ff;
709 float r;
710
711 if (!e ) r = ecb_ldexpf (m , -24);
712 else if (e != 31) r = ecb_ldexpf (m + 0x400, e - 25);
713 else if (m ) r = ECB_NAN;
714 else r = ECB_INFINITY;
715
716 return x & 0x8000 ? -r : r;
717 }
718 813
719 /* convert a float to ieee single/binary32 */ 814 /* convert a float to ieee single/binary32 */
720 ecb_function_ ecb_const uint32_t ecb_float_to_binary32 (float x); 815 ecb_function_ ecb_const uint32_t ecb_float_to_binary32 (float x);
721 ecb_function_ ecb_const uint32_t 816 ecb_function_ ecb_const uint32_t
722 ecb_float_to_binary32 (float x) 817 ecb_float_to_binary32 (float x)
733 if (x == 0e0f ) return 0x00000000U; 828 if (x == 0e0f ) return 0x00000000U;
734 if (x > +3.40282346638528860e+38f) return 0x7f800000U; 829 if (x > +3.40282346638528860e+38f) return 0x7f800000U;
735 if (x < -3.40282346638528860e+38f) return 0xff800000U; 830 if (x < -3.40282346638528860e+38f) return 0xff800000U;
736 if (x != x ) return 0x7fbfffffU; 831 if (x != x ) return 0x7fbfffffU;
737 832
738 m = frexpf (x, &e) * 0x1000000U; 833 m = ecb_frexpf (x, &e) * 0x1000000U;
739 834
740 r = m & 0x80000000U; 835 r = m & 0x80000000U;
741 836
742 if (r) 837 if (r)
743 m = -m; 838 m = -m;
854 #endif 949 #endif
855 950
856 return r; 951 return r;
857 } 952 }
858 953
859#endif 954 /* convert a float to ieee half/binary16 */
955 ecb_function_ ecb_const uint16_t ecb_float_to_binary16 (float x);
956 ecb_function_ ecb_const uint16_t
957 ecb_float_to_binary16 (float x)
958 {
959 return ecb_binary32_to_binary16 (ecb_float_to_binary32 (x));
960 }
860 961
861#endif 962 /* convert an ieee half/binary16 to float */
963 ecb_function_ ecb_const float ecb_binary16_to_float (uint16_t x);
964 ecb_function_ ecb_const float
965 ecb_binary16_to_float (uint16_t x)
966 {
967 return ecb_binary32_to_float (ecb_binary16_to_binary32 (x));
968 }
862 969
970#endif
971
972#endif
973

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines