1 /* *****************************************************************************
2 * Copyright (c) 2007-2014 Alexis Naveros.
4 * Ecere Corporation has unlimited/unrestricted rights.
5 * *****************************************************************************/
10 #if defined(__linux__) || defined(__gnu_linux__) || defined(__linux) || defined(__linux)
13 #elif defined(__APPLE__)
16 #elif defined(__unix__) || defined(__unix) || defined(unix)
18 #elif defined(_WIN64) || defined(__WIN64__) || defined(WIN64)
21 #define CC_WINDOWS (1)
22 #elif defined(_WIN32) || defined(__WIN32__) || defined(WIN32)
24 #define CC_WINDOWS (1)
29 #define ADDRESS(p,o) ((void *)(((char *)p)+(o)))
33 #define ADDRESSDIFF(a,b) (((char *)a)-((char *)b))
37 #define CC_SIZEOF_ALIGN4(x) ((sizeof(x)+0x3)&~0x3)
38 #define CC_SIZEOF_ALIGN8(x) ((sizeof(x)+0x7)&~0x7)
39 #define CC_SIZEOF_ALIGN16(x) ((sizeof(x)+0xF)&~0xF)
40 #define CC_SIZEOF_ALIGN32(x) ((sizeof(x)+0x1F)&~0x1F)
41 #define CC_SIZEOF_ALIGN64(x) ((sizeof(x)+0x3F)&~0x3F)
44 #define CC_MIN(x,y) ((x)>(y)?(y):(x))
45 #define CC_MAX(x,y) ((x)<(y)?(y):(x))
46 #define CC_CLAMP(x,min,max) ((x)<(min)?(min):((x)>(max)?(max):(x)))
49 #define CC_MAX_INT8(x,y) (((int8_t)x)-((((int8_t)x)-((int8_t)y))&((((int8_t)x)-((int8_t)y))>>7)))
50 #define CC_MAX_INT16(x,y) (((int16_t)x)-((((int16_t)x)-((int16_t)y))&((((int16_t)x)-((int16_t)y))>>15)))
51 #define CC_MAX_INT32(x,y) (((int32_t)x)-((((int32_t)x)-((int32_t)y))&((((int32_t)x)-((int32_t)y))>>31)))
52 #define CC_MAX_INT64(x,y) (((int64_t)x)-((((int64_t)x)-((int64_t)y))&((((int64_t)x)-((int64_t)y))>>63)))
54 #define CC_MIN_INT8(x,y) (((int8_t)y)+((((int8_t)x)-((int8_t)y))&((((int8_t)x)-((int8_t)y))>>7)))
55 #define CC_MIN_INT16(x,y) (((int16_t)y)+((((int16_t)x)-((int16_t)y))&((((int16_t)x)-((int16_t)y))>>15)))
56 #define CC_MIN_INT32(x,y) (((int32_t)y)+((((int32_t)x)-((int32_t)y))&((((int32_t)x)-((int32_t)y))>>31)))
57 #define CC_MIN_INT64(x,y) (((int64_t)y)+((((int64_t)x)-((int64_t)y))&((((int64_t)x)-((int64_t)y))>>63)))
59 #define CC_SHIFTDIV_INT8(value,shift) ({uint8_t _s=((uint8_t)value)>>7;((int8_t)((value)+(_s<<shift)-_s))>>shift;})
60 #define CC_SHIFTDIV_INT16(value,shift) ({uint16_t _s=((uint16_t)value)>>15;((int16_t)((value)+(_s<<shift)-_s))>>shift;})
61 #define CC_SHIFTDIV_INT32(value,shift) ({uint32_t _s=((uint32_t)value)>>31;((int32_t)((value)+(_s<<shift)-_s))>>shift;})
62 #define CC_SHIFTDIV_INT64(value,shift) ({uint64_t _s=((uint64_t)value)>>63;((int64_t)((value)+(_s<<shift)-_s))>>shift;})
64 #define CC_SHIFTDIVROUND(value,shift) ((value>>shift)+(((value&((1<<shift)-1))<<1)>=(1<<shift)))
66 #define CC_SHIFTDIVROUND_INT8(value,shift) ((value>>shift)+((((value&((1<<shift)-1))-((uint8_t)value>>7))<<1)>=(1<<shift)))
67 #define CC_SHIFTDIVROUND_INT16(value,shift) ((value>>shift)+((((value&((1<<shift)-1))-((uint16_t)value>>15))<<1)>=(1<<shift)))
68 #define CC_SHIFTDIVROUND_INT32(value,shift) ((value>>shift)+((((value&((1<<shift)-1))-((uint32_t)value>>31))<<1)>=(1<<shift)))
69 #define CC_SHIFTDIVROUND_INT64(value,shift) ((value>>shift)+((((value&((1<<shift)-1))-((uint64_t)value>>63))<<1)>=(1<<shift)))
72 #define CC_NUMBITS2(n) ((n&2)?1:0)
73 #define CC_NUMBITS4(n) ((n&(0xC))?(2+CC_NUMBITS2(n>>2)):(CC_NUMBITS2(n)))
74 #define CC_NUMBITS8(n) ((n&0xF0)?(4+CC_NUMBITS4(n>>4)):(CC_NUMBITS4(n)))
75 #define CC_NUMBITS16(n) ((n&0xFF00)?(8+CC_NUMBITS8(n>>8)):(CC_NUMBITS8(n)))
76 #define CC_NUMBITS32(n) ((n&0xFFFF0000)?(16+CC_NUMBITS16(n>>16)):(CC_NUMBITS16(n)))
77 #define CC_NUMBITS(n) (n==0?0:CC_NUMBITS32(n)+1)
83 #define CC_STRINGIFY(s) CC_STRINGIFY_IN(s)
84 #define CC_STRINGIFY_IN(s) #s
86 #define CC_CONCATENATE(s,n) CC_CONCATENATE_IN(s,n)
87 #define CC_CONCATENATE_IN(s,n) s ## n
109 extern const size_t ccTypeSize[CC_TYPE_COUNT];
116 #define CC_DIR_SEPARATOR_CHAR '/'
117 #define CC_DIR_SEPARATOR_STRING "/"
119 #define CC_DIR_SEPARATOR_CHAR '\\'
120 #define CC_DIR_SEPARATOR_STRING "\\"
122 #define CC_DIR_SEPARATOR_CHAR '/'
123 #define CC_DIR_SEPARATOR_STRING "/"
128 #define CC_LLD "%I64d"
129 #define CC_LLU "%I64u"
130 #define CC_LLX "%I64x"
133 #define CC_LLD "%lld"
134 #define CC_LLU "%llu"
135 #define CC_LLX "%llx"
142 uint32_t ccHash32Data( void *data, int size );
143 uint32_t ccHash32Int32( uint32_t data );
144 uint32_t ccHash32Int64( uint64_t data );
145 uint32_t ccHash32Array32( uint32_t *data, int count );
146 uint32_t ccHash32Array64( uint64_t *data, int count );
148 static inline uint32_t ccHash32Int16Inline( uint32_t i )
151 hash = ( i << 16 ) ^ i;
162 static inline uint32_t ccHash32Int32Inline( uint32_t i )
166 hash = ( ( hash << 16 ) ^ hash ) ^ ( ( i & 0xFFFF0000 ) >> 5 );
177 static inline uint32_t ccHash32Int64Inline( uint64_t i )
180 hash = (uint32_t)(i & 0xFFFF);
181 hash = ( ( hash << 16 ) ^ hash ) ^ ( ( (uint32_t)( i >> 16 ) & 0xFFFF ) << 11 );
182 hash += ( hash >> 11 ) + ( (uint32_t)( i >> 32 ) & 0xFFFF );
183 hash = ( ( hash << 16 ) ^ hash ) ^ (uint32_t)( ( i & 0xFFFF000000000000LL ) >> 37 );
194 static inline uint32_t ccHash32Data3Inline( uint8_t *data )
198 hash += ( (uint32_t)data[1] << 8 ) | (uint32_t)data[0];
200 hash ^= (uint32_t)data[2] << 18;
211 static inline uint32_t ccHash32Data4Inline( uint8_t *data )
215 hash += ( (uint32_t)data[1] << 8 ) | (uint32_t)data[0];
216 hash = ( hash << 16 ) ^ ( ( ( (uint32_t)data[3] << 19 ) | ( (uint32_t)data[2] << 11 ) ) ^ hash );
227 static inline uint32_t ccHash32Data5Inline( uint8_t *data )
231 hash += ( (uint32_t)data[1] << 8 ) | (uint32_t)data[0];
232 hash = ( hash << 16 ) ^ ( ( ( (uint32_t)data[3] << 19 ) | ( (uint32_t)data[2] << 11 ) ) ^ hash );
234 hash += (uint32_t)data[4];
246 static inline uint32_t ccHash32Data6Inline( uint8_t *data )
250 hash += ( (uint32_t)data[1] << 8 ) | (uint32_t)data[0];
251 hash = ( hash << 16 ) ^ ( ( ( (uint32_t)data[3] << 19 ) | ( (uint32_t)data[2] << 11 ) ) ^ hash );
253 hash += ( (uint32_t)data[5] << 8 ) | (uint32_t)data[4];
265 static inline uint32_t ccHash32Data7Inline( uint8_t *data )
269 hash += ( (uint32_t)data[1] << 8 ) | (uint32_t)data[0];
270 hash = ( hash << 16 ) ^ ( ( ( (uint32_t)data[3] << 19 ) | ( (uint32_t)data[2] << 11 ) ) ^ hash );
272 data = ADDRESS( data, 4 );
273 hash += ( (uint32_t)data[5] << 8 ) | (uint32_t)data[4];
275 hash ^= (uint32_t)data[6] << 18;
286 static inline uint32_t ccHash32Data8Inline( uint8_t *data )
290 hash += ( (uint32_t)data[1] << 8 ) | (uint32_t)data[0];
291 hash = ( hash << 16 ) ^ ( ( ( (uint32_t)data[3] << 19 ) | ( (uint32_t)data[2] << 11 ) ) ^ hash );
293 hash += ( (uint32_t)data[5] << 8 ) | (uint32_t)data[4];
294 hash = ( hash << 16 ) ^ ( ( ( (uint32_t)data[7] << 19 ) | ( (uint32_t)data[6] << 11 ) ) ^ hash );
317 } ccQuickRandState32;
319 static inline uint32_t ccQuickRand32( ccQuickRandState32 *randstate )
322 e = randstate->a - ( ( randstate->b << 27 ) | ( randstate->b >> (32-27) ) );
323 randstate->a = randstate->b ^ ( ( randstate->c << 17 ) | ( randstate->c >> (32-17) ) );
324 randstate->b = randstate->c + randstate->d;
325 randstate->c = randstate->d + e;
326 randstate->d = e + randstate->a;
330 static inline void ccQuickRand32Seed( ccQuickRandState32 *randstate, uint32_t seed )
333 randstate->a = 0xf1ea5eed;
337 for( i = 0 ; i < 20 ; i++ )
338 ccQuickRand32( randstate );
342 static inline void ccQuickRand32SeedFast( ccQuickRandState32 *randstate, uint32_t seed0, uint32_t seed1, uint32_t seed2 )
345 randstate->a = 0xf1ea5eed;
346 randstate->b = seed0;
347 randstate->c = seed1;
348 randstate->d = seed2;
349 for( i = 0 ; i < 4 ; i++ )
350 ccQuickRand32( randstate );
361 } ccQuickRandState64;
363 static inline uint64_t ccQuickRand64( ccQuickRandState64 *randstate )
366 e = randstate->a - ( ( randstate->b << 7 ) | ( randstate->b >> (64-7) ) );
367 randstate->a = randstate->b ^ ( ( randstate->c << 13 ) | ( randstate->c >> (64-13) ) );
368 randstate->b = randstate->c + ( ( randstate->d << 37 ) | ( randstate->d >> (64-37) ) );
369 randstate->c = randstate->d + e;
370 randstate->d = e + randstate->a;
374 static inline void ccQuickRand64Seed( ccQuickRandState64 *randstate, uint64_t seed )
377 randstate->a = 0xf1ea5eed;
381 for( i = 0 ; i < 20 ; i++ )
382 ccQuickRand64( randstate );
392 int ccMemCmp( void *s0, void *s1, int size );
393 int ccMemCmp32( uint32_t *s0, uint32_t *s1, int count );
394 int ccMemCmp64( uint64_t *s0, uint64_t *s1, int count );
395 int ccMemCmpRetSize( void *s0, void *s1, int size );
397 static inline int ccMemCmpInline( void *s0, void *s1, int size )
403 for( i = 0 ; i < size ; i++ )
411 static inline int ccMemCmpSizeInline( void *s0, void *s1, int size )
417 for( i = 0 ; i < size ; i++ )
429 uint8_t ccLog2Int8( uint8_t i );
430 uint16_t ccLog2Int16( uint16_t i );
431 uint32_t ccLog2Int32( uint32_t i );
432 uint64_t ccLog2Int64( uint64_t i );
433 #if CPUCONF_LONG_SIZE == 8
434 #define ccLog2IntL(v) ccLog2Int64(v)
436 #define ccLog2IntL(v) ccLog2Int32(v)
443 static inline int8_t ccPowInt8( int8_t base, int exp )
454 return (int8_t)result;
457 static inline int16_t ccPowInt16( int16_t base, int exp )
468 return (int16_t)result;
471 static inline int32_t ccPowInt32( int32_t base, int exp )
485 static inline int64_t ccPowInt64( int64_t base, int exp )
503 static inline uint8_t ccMergeIntMask8( uint8_t i0, uint8_t i1, uint8_t mask )
505 return (uint8_t)(i0 ^ ( ( i0 ^ i1 ) & mask ));
508 static inline uint16_t ccMergeIntMask16( uint16_t i0, uint16_t i1, uint16_t mask )
510 return (uint16_t)(i0 ^ ( ( i0 ^ i1 ) & mask ));
513 static inline uint32_t ccMergeIntMask32( uint32_t i0, uint32_t i1, uint32_t mask )
515 return i0 ^ ( ( i0 ^ i1 ) & mask );
518 static inline uint64_t ccMergeIntMask64( uint64_t i0, uint64_t i1, uint64_t mask )
520 return i0 ^ ( ( i0 ^ i1 ) & mask );
523 #if CPUCONF_LONG_SIZE == 8
524 #define ccMergeIntMaskL(v) ccMergeIntMask64(v)
526 #define ccMergeIntMaskL(v) ccMergeIntMask32(v)
535 static inline int ccCountBits64( uint64_t i )
538 for( r = 0 ; i ; r++ )
544 static inline int ccCountBits32( uint32_t v )
547 v = v - ( ( v >> 1 ) & 0x55555555 );
548 v = ( v & 0x33333333 ) + ( ( v >> 2 ) & 0x33333333 );
549 c = ( ( ( v + ( v >> 4 ) ) & 0xF0F0F0F ) * 0x1010101 ) >> 24;
557 static inline int ccTrailingCount32( uint32_t v )
565 if( !( v & 0xffff ) )
591 static inline int ccTrailingCount64( uint64_t v )
599 if( !( v & 0xffffffff ) )
604 if( !( v & 0xffff ) )
633 static inline uint32_t ccReverseBits32( uint32_t value )
640 for( value >>= 1 ; value ; value >>= 1 )
651 static inline uint64_t ccReverseBits64( uint64_t value )
658 for( value >>= 1 ; value ; value >>= 1 )
669 static inline uint32_t ccReverseBitsVar32( uint32_t value, int numbits )
674 value &= ( ((uint32_t)1) << numbits ) - 1;
677 for( value >>= 1 ; value ; value >>= 1 )
684 result >>= 32 - numbits;
689 static inline uint64_t ccReverseBitsVar64( uint64_t value, int numbits )
694 value &= ( ((uint64_t)1) << numbits ) - 1;
697 for( value >>= 1 ; value ; value >>= 1 )
704 result >>= 64 - numbits;
715 static inline uint8_t ccIsPow2Int8( uint8_t v )
717 return ( ( v & ( v - 1 ) ) == 0 );
720 static inline uint16_t ccIsPow2Int16( uint16_t v )
722 return ( ( v & ( v - 1 ) ) == 0 );
725 static inline uint32_t ccIsPow2Int32( uint32_t v )
727 return ( ( v & ( v - 1 ) ) == 0 );
730 static inline uint64_t ccIsPow2Int64( uint64_t v )
732 return ( ( v & ( v - 1 ) ) == 0 );
736 static inline uint8_t ccPow2Round8( uint8_t v )
746 static inline uint16_t ccPow2Round16( uint16_t v )
757 static inline uint32_t ccPow2Round32( uint32_t v )
769 static inline uint64_t ccPow2Round64( uint64_t v )
782 #if CPUCONF_LONG_SIZE == 8
783 #define ccPow2RoundL(v) ccPow2Round64(v)
785 #define ccPow2RoundL(v) ccPow2Round32(v)
794 static inline uint32_t ccTestNullByte32( uint32_t v )
796 return ( v - 0x01010101 ) & ~v & 0x80808080;
799 static inline uint64_t ccTestNullByte64( uint64_t v )
801 return ( v - 0x0101010101010101ULL ) & ~v & 0x8080808080808080ULL;
804 static inline uint32_t ccSignBit32( uint32_t v )
809 static inline uint64_t ccSignBit64( uint64_t v )
814 static inline uint32_t ccAbs32( int32_t v )
818 return ( v ^ mask ) - mask;
821 static inline uint64_t ccAbs64( int64_t v )
824 mask = (int32_t)(v >> 63);
825 return ( v ^ mask ) - mask;
834 static inline int32_t ccMortonNumber32( int32_t x, int32_t y )
839 for( i = 0 ; i < 16 ; i++ )
841 z |= ( x & ( ((uint32_t)1) << i ) ) << i;
842 z |= ( y & ( ((uint32_t)1) << i ) ) << ( i + 1 );
847 static inline int64_t ccMortonNumber64( int32_t x, int32_t y )
852 for( i = 0 ; i < 16 ; i++ )
854 z |= ( x & ( ((uint64_t)1) << i ) ) << i;
855 z |= ( y & ( ((uint64_t)1) << i ) ) << ( i + 1 );
866 static inline int ccStrCmpEqualInline( char *s0, char *s1 )
879 static inline int ccIsAlphaNum( char c )
881 if( ( c >= 'a' ) && ( c <= 'z' ) )
883 if( ( c >= 'A' ) && ( c <= 'Z' ) )
885 if( ( c >= '0' ) && ( c <= '9' ) )
890 static inline int ccIsAlphaNumExtended( unsigned char c )
892 if( ( c >= 'a' ) && ( c <= 'z' ) )
894 if( ( c >= 'A' ) && ( c <= 'Z' ) )
896 if( ( c >= '0' ) && ( c <= '9' ) )
903 static inline int ccCharHexBase( char c )
906 if( ( c >= '0' ) && ( c <= '9' ) )
908 else if( ( c >= 'A' ) && ( c <= 'F' ) )
910 else if( ( c >= 'a' ) && ( c <= 'f' ) )
918 void ccStrLowCase( char *str, int length );
919 void ccStrLowCopy( char *dst, char *src, int length );
920 int ccStrCmpEqual( char *s0, char *s1 );
921 int ccStrCmpEqualTest( char *s0, char *s1 );
922 int ccStrCmpStdTest( char *s0, char *s1 );
923 char *ccStrCmpWord( char *str, char *word );
924 char *ccStrCmpSeq( char *str, char *seq, int seqlength );
925 char *ccStrMatchSeq( char *str, char *seq, int seqlength );
926 char *ccSeqCmpSeq( char *s1, char *s2, int s1length, int s2length );
927 int ccStrWordCmpWord( char *s1, char *s2 );
928 char *ccStrLowCmpWord( char *str, char *word );
929 char *ccStrLowCmpSeq( char *str, char *seq, int seqlength );
930 char *ccStrFindStr( char *str0, char *str1 );
931 char *ccStrFindStrSkip( char *str0, char *str1 );
932 char *ccStrFindSeq( char *str, char *seq, int seqlength );
933 char *ccStrFindWord( char *str, char *word, int wordlength );
934 int ccStrWordLength( char *str );
935 int ccStrFindChar( char *str, char c );
936 int ccSeqFindChar( char *seq, int seqlen, char c );
937 int ccStrFindCharLast( char *str, char c );
938 int ccSeqFindCharLast( char *seq, int seqlen, char c );
939 char *ccSeqFindStr( char *seq, int seqlen, char *str );
940 char *ccSeqFindStrSkip( char *seq, int seqlen, char *str );
941 char *ccStrParam( char *str, int *retparamlen, int *retskiplen );
942 int ccParseParameters( char *str, char **argv, int argcountmax );
943 int ccParseParametersCut( char *str, char **argv, int argcountmax );
944 char *ccStrNextWord( char *str );
945 char *ccStrSkipWord( char *str );
946 char *ccStrEndWord( char *str );
947 char *ccStrNextWordSameLine( char *str );
948 char *ccStrNextParam( char *str );
949 char *ccStrNextLine( char *str );
950 char *ccStrPassLine( char *str );
951 int ccStrParseInt32( char *str, int32_t *retint );
952 int ccSeqParseInt32( char *seq, int seqlength, int32_t *retint );
953 int ccStrParseInt64( char *str, int64_t *retint );
954 int ccSeqParseInt64( char *seq, int seqlength, int64_t *retint );
955 int ccStrParseFloat( char *str, float *retfloat );
956 int ccSeqParseFloat( char *seq, int seqlength, float *retfloat );
957 int ccStrParseDouble( char *str, double *retdouble );
958 int ccSeqParseDouble( char *seq, int seqlength, double *retdouble );
959 int ccStrParseHex( char *str, int hexchars );
960 char *ccStrAllocPrintf( char *format, ... );
961 char *ccStrDup( char *str );
962 int ccUnicodeToUtf8( char *s, uint32_t unicode );
963 /* Returns 1 when data is insufficient, send more bytes ; state must be initialized to zero */
964 uint32_t ccUtf8ToUnicode( uint32_t byte, uint32_t *state, uint32_t *retunicode );
970 #define CC_FLT_INT_MAPPING
972 #if CPUCONF_FLOAT_SIZE == 4
973 typedef uint32_t ccuintf;
974 #elif CPUCONF_FLOAT_SIZE == 8
975 typedef uint64_t ccuintf;
977 #undef CC_FLT_INT_MAPPING
980 #if CPUCONF_DOUBLE_SIZE == 4
981 typedef uint32_t ccuintd;
982 #elif CPUCONF_DOUBLE_SIZE == 8
983 typedef uint64_t ccuintd;
985 #undef CC_FLT_INT_MAPPING
989 #ifdef CC_FLT_INT_MAPPING
991 static inline ccuintf ccFloatToUint( float f )
994 return *((ccuintf *)p);
997 static inline float ccUintToFloat( ccuintf f )
1000 return *((float *)p);
1004 static inline ccuintd ccDoubleToUint( double d )
1007 return *((ccuintd *)p);
1010 static inline double ccUintToDouble( ccuintd d )
1013 return *((double *)p);
1024 #define CC_LOG2_E 1.4426950408889634073599246810018921
1026 static inline float ccFastExpFloat( float x )
1035 else if( x < -80.0 )
1037 u.i = (int32_t)( x * ( (float)0x800000 * (float)CC_LOG2_E ) ) + ( 0x3f800000 - 486408 );
1041 static inline float ccFastExpFloatNearZero( float x )
1050 else if( x < -80.0 )
1052 u.i = (int32_t)( x * ( (float)0x800000 * (float)CC_LOG2_E ) ) + 0x3f800000;
1056 static inline double ccFastExpDouble( double x )
1058 #if CPUCONF_WORD_SIZE >= 64
1066 else if( x < -80.0 )
1068 u.i = (int64_t)( x * ( (double)0x10000000000000 * CC_LOG2_E ) ) + ( (uint64_t)0x3ff0000000000000 - (uint64_t)261138306564096 );
1077 return expf( (float)x );
1078 else if( x < -80.0 )
1080 #ifdef CPUCONF_LITTLE_ENDIAN
1081 u.i[1] = (int32_t)( x * ( (double)0x100000 * CC_LOG2_E ) ) + ( 0x3ff00000 - 60801 );
1084 u.i[0] = (int32_t)( x * ( (double)0x100000 * CC_LOG2_E ) ) + ( 0x3ff00000 - 60801 );
1091 static inline double ccFastExpDoubleNearZero( double x )
1093 #if CPUCONF_WORD_SIZE >= 64
1101 else if( x < -80.0 )
1103 u.i = (int64_t)( x * ( (double)0x10000000000000 * CC_LOG2_E ) ) + (uint64_t)0x3ff0000000000000;
1112 return expf( (float)x );
1113 else if( x < -80.0 )
1115 #ifdef CPUCONF_LITTLE_ENDIAN
1116 u.i[1] = (int32_t)( x * ( (double)0x100000 * CC_LOG2_E ) ) + 0x3ff00000;
1119 u.i[0] = (int32_t)( x * ( (double)0x100000 * CC_LOG2_E ) ) + 0x3ff00000;
1132 static inline float ccFastLog2Float( float x )
1141 base = ( ( u.i >> 23 ) & 0xff ) - 0x80;
1142 u.i &= ~( (uint32_t)0xff << 23 );
1143 u.i += (uint32_t)0x7f << 23;
1144 return (float)base + ( u.f * ( 2.0f + u.f * ( -1.0f/3.0f ) ) ) - ( 2.0f/3.0f );
1147 static inline float ccFastLog2Double( double x )
1149 #if CPUCONF_WORD_SIZE >= 64
1157 base = ( ( u.i >> 52 ) & 0x7ff ) - 0x400;
1158 u.i &= ~( (uint64_t)0x7ff << 52 );
1159 u.i += (uint64_t)0x3ff << 52;
1168 base = ( ( u.i[1] >> 20 ) & 0x7ff ) - 0x400;
1169 u.i[1] &= ~( (uint32_t)0x7ff << 20 );
1170 u.i[1] += (uint32_t)0x3ff << 20;
1172 return (float)(base + ( u.f * ( 2.0f + u.f * ( -1.0f/3.0f ) ) ) - ( 2.0f/3.0f ));
1180 /* Only valid between -M_PI and M_PI */
1181 static inline float ccFastSinFloat( float x )
1184 s = (float)(( 1.27323954474 * x ) + ( -0.405284734569 * x * fabsf( x ) ));
1188 /* Only valid between -M_PI and M_PI */
1189 static inline double ccFastSinDouble( double x )
1192 s = ( 1.27323954474 * x ) + ( -0.405284734569 * x * fabs( x ) );
1202 #define CC_INT16_BSWAP(i) (__extension__({uint16_t bsw=(i);((bsw&0xff)<<8)|(bsw>>8);}))
1203 #define CC_INT32_BSWAP(i) (__extension__({uint32_t bsw=(i);(bsw<<24)|((bsw&0xff00)<<8)|((bsw>>8)&0xff00)|(bsw>>24);}))
1204 #define CC_INT64_BSWAP(i) (__extension__({uint64_t bsw=(i);(bsw>>56)|((bsw&0x00ff000000000000LL)>>40)|((bsw&0x0000ff0000000000LL)>>24)|((bsw&0x000000ff00000000LL)>>8)|((bsw&0x00000000ff000000LL)<<8)|((bsw&0x0000000000ff0000LL)<<24)|((bsw&0x000000000000ff00LL)<<40)|(bsw<<56);}))
1207 static inline uint16_t ccByteSwap16( uint16_t i )
1209 return (uint16_t)(CC_INT16_BSWAP( i ));
1212 #if defined(__GNUC__) && defined(__i386__)
1214 static inline uint32_t ccByteSwap32( uint32_t i )
1216 __asm__( "bswap %0" : "=r" (i) : "0" (i) );
1220 static inline uint64_t ccByteSwap64( uint64_t i )
1227 __asm__( "bswapl %0 ; bswapl %1 ; xchgl %0,%1" : "=r" (u.s[0]), "=r" (u.s[1]) : "0" (u.s[0]), "1" (u.s[1]) );
1231 #elif defined(__GNUC__) && defined(__x86_64__)
1233 static inline uint32_t ccByteSwap32( uint32_t i )
1235 __asm__( "bswapl %0" : "=r" (i) : "0" (i) );
1239 static inline uint64_t ccByteSwap64( uint64_t i )
1241 __asm__( "bswapq %0" : "=r" (i) : "0" (i) );
1247 static inline uint32_t ccByteSwap32( uint32_t i )
1249 return CC_INT32_BSWAP( i );
1252 static inline uint64_t ccByteSwap64( uint64_t i )
1254 return CC_INT64_BSWAP( i );
1259 static inline float ccByteSwapf( float f )
1264 i = ccByteSwap32( *((uint32_t *)p) );
1266 return *((float *)p);
1269 static inline double ccByteSwapd( double f )
1274 i = ccByteSwap64( *((uint64_t *)p) );
1276 return *((double *)p);
1280 static inline uint32_t ccAlignInt32( uint32_t i )
1291 static inline uint64_t ccAlignInt64( uint64_t i )
1303 static inline uintptr_t ccAlignIntPtr( uintptr_t i )
1311 #if CPUCONF_INTPTR_BITS > 32
1323 static inline uint8_t ccRotateLeft8( uint8_t x, int bits )
1325 return ( x << bits ) | ( x >> ( 8 - bits ) );
1328 static inline uint16_t ccRotateLeft16( uint16_t x, int bits )
1330 return ( x << bits ) | ( x >> ( 16 - bits ) );
1333 static inline uint32_t ccRotateLeft32( uint32_t x, int bits )
1335 return ( x << bits ) | ( x >> ( 32 - bits ) );
1338 static inline uint64_t ccRotateLeft64( uint64_t x, int bits )
1340 return ( x << bits ) | ( x >> ( 64 - bits ) );
1344 static inline uint8_t ccRotateRight8( uint8_t x, int bits )
1346 return ( x >> bits ) | ( x << ( 8 - bits ) );
1349 static inline uint16_t ccRotateRight16( uint16_t x, int bits )
1351 return ( x >> bits ) | ( x << ( 16 - bits ) );
1354 static inline uint32_t ccRotateRight32( uint32_t x, int bits )
1356 return ( x >> bits ) | ( x << ( 32 - bits ) );
1359 static inline uint64_t ccRotateRight64( uint64_t x, int bits )
1361 return ( x >> bits ) | ( x << ( 64 - bits ) );
1368 #define CC_INT32_MAX ((((uint32_t)1)<<31)-1)
1370 static inline int32_t ccFloatToInt32Sat( float f )
1372 if( f >= (float)CC_INT32_MAX )
1373 return CC_INT32_MAX;
1374 else if( f <= -(float)CC_INT32_MAX )
1375 return -CC_INT32_MAX;
1381 static inline int32_t ccDoubleToInt32Sat( double f )
1383 if( f >= (double)CC_INT32_MAX )
1384 return CC_INT32_MAX;
1385 else if( f <= -(double)CC_INT32_MAX )
1386 return -CC_INT32_MAX;
1392 #define CC_INT64_MAX ((((uint64_t)1)<<63)-1)
1394 static inline int64_t ccFloatToInt64Sat( float f )
1396 if( f >= (float)CC_INT64_MAX )
1397 return CC_INT64_MAX;
1398 else if( f <= -(float)CC_INT64_MAX )
1399 return -CC_INT64_MAX;
1405 static inline int64_t ccDoubleToInt64Sat( double f )
1407 if( f >= (double)CC_INT64_MAX )
1408 return CC_INT64_MAX;
1409 else if( f <= -(double)CC_INT64_MAX )
1410 return -CC_INT64_MAX;
1420 void ccQuickSort( void **table, int count, int (*sortfunc)( void *t0, void *t1 ), uint32_t randmask );
1421 void ccQuickSortContext( void **table, int count, int (*sortfunc)( void *context, void *t0, void *t1 ), void *context, uint32_t randmask );
1423 int ccMergeSort( void **src, void **tmp, int count, int (*sortfunc)( void *t0, void *t1 ) );
1424 int ccMergeSortContext( void **src, void **tmp, int count, int (*sortfunc)( void *context, void *t0, void *t1 ), void *context );
1426 void ccHybridSort( void **table, void **tmp, int count, int (*sortfunc)( void *t0, void *t1 ), uint32_t randmask );
1427 void ccHybridSortContext( void **table, void **tmp, int count, int (*sortfunc)( void *context, void *t0, void *t1 ), void *context, uint32_t randmask );
1435 void ccDebugLog( char *filename, char *string, ... );
1448 void ccGrowthInit( ccGrowth *growth, int defaultsize );
1449 int ccGrowthPrintf( ccGrowth *growth, char *format, ... );
1450 int ccGrowthData( ccGrowth *growth, void *data, size_t size );
1451 int ccGrowthSeek( ccGrowth *growth, int offset );
1452 void ccGrowthFree( ccGrowth *growth );
1453 void ccGrowthElapsedTimeString( ccGrowth *growth, int64_t timecount, int maxfieldcount );
1459 void *ccFileLoad( const char *path, size_t maxsize, size_t *retsize );
1460 size_t ccFileLoadDirect( const char *path, void *data, size_t minsize, size_t maxsize );
1461 int ccFileStore( const char *path, void *data, size_t datasize, int fsyncflag );
1462 int ccFileExists( char *path );
1463 int ccFileStat( char *path, size_t *retfilesize, time_t *retfiletime );
1464 int ccRenameFile( char *oldpath, char *newpath );
1470 typedef struct _ccDir ccDir;
1472 ccDir *ccOpenDir( char *path );
1473 char *ccReadDir( ccDir *dir );
1474 void ccCloseDir( ccDir *dir );
1480 int64_t ccGetFreeDiskSpace( char *dirpath );
1486 int ccGetTimeOfDay( struct timeval *tv );
1488 void ccSleep( int milliseconds );
1490 static inline uint64_t ccGetMillisecondsTime()
1492 struct timeval lntime;
1493 ccGetTimeOfDay( &lntime );
1494 return ( (uint64_t)lntime.tv_sec * 1000 ) + ( (uint64_t)lntime.tv_usec / 1000 );
1497 static inline uint64_t ccGetMicrosecondsTime()
1499 struct timeval lntime;
1500 ccGetTimeOfDay( &lntime );
1501 return ( (uint64_t)lntime.tv_sec * 1000000 ) + (uint64_t)lntime.tv_usec;
1504 static inline uint64_t ccGetNanosecondsTime()
1506 struct timeval lntime;
1507 ccGetTimeOfDay( &lntime );
1508 return ( (uint64_t)lntime.tv_sec * 1000000000 ) + ( (uint64_t)lntime.tv_usec * 1000 );
1515 /* Returned string must be free()d */
1516 char *ccGetSystemName();