ecere/gfx/NewFonts/FontManager: Exporting alignment flags
[sdk] / ecere / src / gfx / newFonts / cc / cc.h
1 /* *****************************************************************************
2  * Copyright (c) 2007-2014 Alexis Naveros.
3  *
4  * Ecere Corporation has unlimited/unrestricted rights.
5  * *****************************************************************************/
6 #include <sys/time.h>
7 #include <stdarg.h>
8
9
10 #if defined(__linux__) || defined(__gnu_linux__) || defined(__linux) || defined(__linux)
11  #define CC_LINUX (1)
12  #define CC_UNIX (1)
13 #elif defined(__APPLE__)
14  #define CC_OSX (1)
15  #define CC_UNIX (1)
16 #elif defined(__unix__) || defined(__unix) || defined(unix)
17  #define CC_UNIX (1)
18 #elif defined(_WIN64) || defined(__WIN64__) || defined(WIN64)
19  #define CC_WIN64 (1)
20  #define CC_WIN32 (1)
21  #define CC_WINDOWS (1)
22 #elif defined(_WIN32) || defined(__WIN32__) || defined(WIN32)
23  #define CC_WIN32 (1)
24  #define CC_WINDOWS (1)
25 #endif
26
27
28 #ifndef ADDRESS
29  #define ADDRESS(p,o) ((void *)(((char *)p)+(o)))
30 #endif
31
32 #ifndef ADDRESSDIFF
33  #define ADDRESSDIFF(a,b) (((char *)a)-((char *)b))
34 #endif
35
36
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)
42
43
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)))
47
48
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)))
53
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)))
58
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;})
63
64 #define CC_SHIFTDIVROUND(value,shift) ((value>>shift)+(((value&((1<<shift)-1))<<1)>=(1<<shift)))
65
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)))
70
71
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)
78
79
80 ////
81
82
83 #define CC_STRINGIFY(s) CC_STRINGIFY_IN(s)
84 #define CC_STRINGIFY_IN(s) #s
85
86 #define CC_CONCATENATE(s,n) CC_CONCATENATE_IN(s,n)
87 #define CC_CONCATENATE_IN(s,n) s ## n
88
89
90 ////
91
92
93 enum
94 {
95   CC_TYPE_UINT8,
96   CC_TYPE_INT8,
97   CC_TYPE_UINT16,
98   CC_TYPE_INT16,
99   CC_TYPE_UINT32,
100   CC_TYPE_INT32,
101   CC_TYPE_UINT64,
102   CC_TYPE_INT64,
103   CC_TYPE_FLOAT,
104   CC_TYPE_DOUBLE,
105
106   CC_TYPE_COUNT
107 };
108
109 extern const size_t ccTypeSize[CC_TYPE_COUNT];
110
111
112 ////
113
114
115 #if CC_UNIX
116  #define CC_DIR_SEPARATOR_CHAR '/'
117  #define CC_DIR_SEPARATOR_STRING "/"
118 #elif CC_WINDOWS
119  #define CC_DIR_SEPARATOR_CHAR '\\'
120  #define CC_DIR_SEPARATOR_STRING "\\"
121 #else
122  #define CC_DIR_SEPARATOR_CHAR '/'
123  #define CC_DIR_SEPARATOR_STRING "/"
124 #endif
125
126 #if CC_WINDOWS
127  #define CC_LL "I64"
128  #define CC_LLD "%I64d"
129  #define CC_LLU "%I64u"
130  #define CC_LLX "%I64x"
131 #else
132  #define CC_LL "ll"
133  #define CC_LLD "%lld"
134  #define CC_LLU "%llu"
135  #define CC_LLX "%llx"
136 #endif
137
138
139 ////
140
141
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 );
147
148 static inline uint32_t ccHash32Int16Inline( uint32_t i )
149 {
150   uint32_t hash;
151   hash = ( i << 16 ) ^ i;
152   hash += hash >> 11;
153   hash ^= hash << 3;
154   hash += hash >> 5;
155   hash ^= hash << 4;
156   hash += hash >> 17;
157   hash ^= hash << 25;
158   hash += hash >> 6;
159   return hash;
160 }
161
162 static inline uint32_t ccHash32Int32Inline( uint32_t i )
163 {
164   uint32_t hash;
165   hash = i & 0xFFFF;
166   hash = ( ( hash << 16 ) ^ hash ) ^ ( ( i & 0xFFFF0000 ) >> 5 );
167   hash += hash >> 11;
168   hash ^= hash << 3;
169   hash += hash >> 5;
170   hash ^= hash << 4;
171   hash += hash >> 17;
172   hash ^= hash << 25;
173   hash += hash >> 6;
174   return hash;
175 }
176
177 static inline uint32_t ccHash32Int64Inline( uint64_t i )
178 {
179   uint32_t hash;
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 );
184   hash += hash >> 11;
185   hash ^= hash << 3;
186   hash += hash >> 5;
187   hash ^= hash << 4;
188   hash += hash >> 17;
189   hash ^= hash << 25;
190   hash += hash >> 6;
191   return hash;
192 }
193
194 static inline uint32_t ccHash32Data3Inline( uint8_t *data )
195 {
196   uint32_t hash;
197   hash = 0;
198   hash += ( (uint32_t)data[1] << 8 ) | (uint32_t)data[0];
199   hash ^= hash << 16;
200   hash ^= (uint32_t)data[2] << 18;
201   hash += hash >> 11;
202   hash ^= hash << 3;
203   hash += hash >> 5;
204   hash ^= hash << 4;
205   hash += hash >> 17;
206   hash ^= hash << 25;
207   hash += hash >> 6;
208   return hash;
209 }
210
211 static inline uint32_t ccHash32Data4Inline( uint8_t *data )
212 {
213   uint32_t hash;
214   hash = 0;
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 );
217   hash += hash >> 11;
218   hash ^= hash << 3;
219   hash += hash >> 5;
220   hash ^= hash << 4;
221   hash += hash >> 17;
222   hash ^= hash << 25;
223   hash += hash >> 6;
224   return hash;
225 }
226
227 static inline uint32_t ccHash32Data5Inline( uint8_t *data )
228 {
229   uint32_t hash;
230   hash = 0;
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 );
233   hash += hash >> 11;
234   hash += (uint32_t)data[4];
235   hash ^= hash << 10;
236   hash += hash >> 1;
237   hash ^= hash << 3;
238   hash += hash >> 5;
239   hash ^= hash << 4;
240   hash += hash >> 17;
241   hash ^= hash << 25;
242   hash += hash >> 6;
243   return hash;
244 }
245
246 static inline uint32_t ccHash32Data6Inline( uint8_t *data )
247 {
248   uint32_t hash;
249   hash = 0;
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 );
252   hash += hash >> 11;
253   hash += ( (uint32_t)data[5] << 8 ) | (uint32_t)data[4];
254   hash ^= hash << 11;
255   hash += hash >> 17;
256   hash ^= hash << 3;
257   hash += hash >> 5;
258   hash ^= hash << 4;
259   hash += hash >> 17;
260   hash ^= hash << 25;
261   hash += hash >> 6;
262   return hash;
263 }
264
265 static inline uint32_t ccHash32Data7Inline( uint8_t *data )
266 {
267   uint32_t hash;
268   hash = 0;
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 );
271   hash += hash >> 11;
272   data = ADDRESS( data, 4 );
273   hash += ( (uint32_t)data[5] << 8 ) | (uint32_t)data[4];
274   hash ^= hash << 16;
275   hash ^= (uint32_t)data[6] << 18;
276   hash += hash >> 11;
277   hash ^= hash << 3;
278   hash += hash >> 5;
279   hash ^= hash << 4;
280   hash += hash >> 17;
281   hash ^= hash << 25;
282   hash += hash >> 6;
283   return hash;
284 }
285
286 static inline uint32_t ccHash32Data8Inline( uint8_t *data )
287 {
288   uint32_t hash;
289   hash = 0;
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 );
292   hash += hash >> 11;
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 );
295   hash += hash >> 11;
296   hash ^= hash << 3;
297   hash += hash >> 5;
298   hash ^= hash << 4;
299   hash += hash >> 17;
300   hash ^= hash << 25;
301   hash += hash >> 6;
302   return hash;
303 }
304
305
306
307 ////
308
309
310
311 typedef struct
312 {
313   uint32_t a;
314   uint32_t b;
315   uint32_t c;
316   uint32_t d;
317 } ccQuickRandState32;
318
319 static inline uint32_t ccQuickRand32( ccQuickRandState32 *randstate )
320 {
321   uint32_t e;
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;
327   return randstate->d;
328 }
329
330 static inline void ccQuickRand32Seed( ccQuickRandState32 *randstate, uint32_t seed )
331 {
332   uint32_t i;
333   randstate->a = 0xf1ea5eed;
334   randstate->b = seed;
335   randstate->c = seed;
336   randstate->d = seed;
337   for( i = 0 ; i < 20 ; i++ )
338     ccQuickRand32( randstate );
339   return;
340 }
341
342 static inline void ccQuickRand32SeedFast( ccQuickRandState32 *randstate, uint32_t seed0, uint32_t seed1, uint32_t seed2 )
343 {
344   uint32_t i;
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 );
351   return;
352 }
353
354
355 typedef struct
356 {
357   uint64_t a;
358   uint64_t b;
359   uint64_t c;
360   uint64_t d;
361 } ccQuickRandState64;
362
363 static inline uint64_t ccQuickRand64( ccQuickRandState64 *randstate )
364 {
365   uint64_t e;
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;
371   return randstate->d;
372 }
373
374 static inline void ccQuickRand64Seed( ccQuickRandState64 *randstate, uint64_t seed )
375 {
376   uint64_t i;
377   randstate->a = 0xf1ea5eed;
378   randstate->b = seed;
379   randstate->c = seed;
380   randstate->d = seed;
381   for( i = 0 ; i < 20 ; i++ )
382     ccQuickRand64( randstate );
383   return;
384 }
385
386
387
388 ////
389
390
391
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 );
396
397 static inline int ccMemCmpInline( void *s0, void *s1, int size )
398 {
399   int i;
400   uint8_t *t0, *t1;
401   t0 = s0;
402   t1 = s1;
403   for( i = 0 ; i < size ; i++ )
404   {
405     if( t0[i] != t1[i] )
406       return 0;
407   }
408   return 1;
409 }
410
411 static inline int ccMemCmpSizeInline( void *s0, void *s1, int size )
412 {
413   int i;
414   uint8_t *t0, *t1;
415   t0 = s0;
416   t1 = s1;
417   for( i = 0 ; i < size ; i++ )
418   {
419     if( t0[i] != t1[i] )
420       break;
421   }
422   return i;
423 }
424
425
426 ////
427
428
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)
435 #else
436  #define ccLog2IntL(v) ccLog2Int32(v)
437 #endif
438
439
440 ////
441
442
443 static inline int8_t ccPowInt8( int8_t base, int exp )
444 {
445   int result;
446   result = 1;
447   while( exp )
448   {
449     if( exp & 1 )
450       result *= base;
451     exp >>= 1;
452     base *= base;
453   }
454   return (int8_t)result;
455 }
456
457 static inline int16_t ccPowInt16( int16_t base, int exp )
458 {
459   int result;
460   result = 1;
461   while( exp )
462   {
463     if( exp & 1 )
464       result *= base;
465     exp >>= 1;
466     base *= base;
467   }
468   return (int16_t)result;
469 }
470
471 static inline int32_t ccPowInt32( int32_t base, int exp )
472 {
473   int result;
474   result = 1;
475   while( exp )
476   {
477     if( exp & 1 )
478       result *= base;
479     exp >>= 1;
480     base *= base;
481   }
482   return result;
483 }
484
485 static inline int64_t ccPowInt64( int64_t base, int exp )
486 {
487   int result;
488   result = 1;
489   while( exp )
490   {
491     if( exp & 1 )
492       result *= base;
493     exp >>= 1;
494     base *= base;
495   }
496   return result;
497 }
498
499
500 ////
501
502
503 static inline uint8_t ccMergeIntMask8( uint8_t i0, uint8_t i1, uint8_t mask )
504 {
505   return (uint8_t)(i0 ^ ( ( i0 ^ i1 ) & mask ));
506 }
507
508 static inline uint16_t ccMergeIntMask16( uint16_t i0, uint16_t i1, uint16_t mask )
509 {
510   return (uint16_t)(i0 ^ ( ( i0 ^ i1 ) & mask ));
511 }
512
513 static inline uint32_t ccMergeIntMask32( uint32_t i0, uint32_t i1, uint32_t mask )
514 {
515   return i0 ^ ( ( i0 ^ i1 ) & mask );
516 }
517
518 static inline uint64_t ccMergeIntMask64( uint64_t i0, uint64_t i1, uint64_t mask )
519 {
520   return i0 ^ ( ( i0 ^ i1 ) & mask );
521 }
522
523 #if CPUCONF_LONG_SIZE == 8
524  #define ccMergeIntMaskL(v) ccMergeIntMask64(v)
525 #else
526  #define ccMergeIntMaskL(v) ccMergeIntMask32(v)
527 #endif
528
529
530
531 ////
532
533
534
535 static inline int ccCountBits64( uint64_t i )
536 {
537   int r;
538   for( r = 0 ; i ; r++ )
539     i &= i - 1;
540   return r;
541 }
542
543
544 static inline int ccCountBits32( uint32_t v )
545 {
546   int c;
547   v = v - ( ( v >> 1 ) & 0x55555555 );
548   v = ( v & 0x33333333 ) + ( ( v >> 2 ) & 0x33333333 );
549   c = ( ( ( v + ( v >> 4 ) ) & 0xF0F0F0F ) * 0x1010101 ) >> 24;
550   return c;
551 }
552
553
554 ////
555
556
557 static inline int ccTrailingCount32( uint32_t v )
558 {
559   int c;
560   if( v & 0x1 )
561     c = 0;
562   else
563   {
564     c = 1;
565     if( !( v & 0xffff ) )
566     {
567       v >>= 16;
568       c += 16;
569     }
570     if( !( v & 0xff ) )
571     {
572       v >>= 8;
573       c += 8;
574     }
575     if( !( v & 0xf ) )
576     {
577       v >>= 4;
578       c += 4;
579     }
580     if( !( v & 0x3 ) )
581     {
582       v >>= 2;
583       c += 2;
584     }
585     c -= v & 0x1;
586   }
587   return c;
588 }
589
590
591 static inline int ccTrailingCount64( uint64_t v )
592 {
593   int c;
594   if( v & 0x1 )
595     c = 0;
596   else
597   {
598     c = 1;
599     if( !( v & 0xffffffff ) )
600     {
601       v >>= 32;
602       c += 32;
603     }
604     if( !( v & 0xffff ) )
605     {
606       v >>= 16;
607       c += 16;
608     }
609     if( !( v & 0xff ) )
610     {
611       v >>= 8;
612       c += 8;
613     }
614     if( !( v & 0xf ) )
615     {
616       v >>= 4;
617       c += 4;
618     }
619     if( !( v & 0x3 ) )
620     {
621       v >>= 2;
622       c += 2;
623     }
624     c -= v & 0x1;
625   }
626   return c;
627 }
628
629
630 ////
631
632
633 static inline uint32_t ccReverseBits32( uint32_t value )
634 {
635   uint32_t result;
636   int shift;
637
638   result = value;
639   shift = 32-1;
640   for( value >>= 1 ; value ; value >>= 1 )
641   {
642     result <<= 1;
643     result |= value & 1;
644     shift--;
645   }
646   result <<= shift;
647
648   return result;
649 }
650
651 static inline uint64_t ccReverseBits64( uint64_t value )
652 {
653   uint64_t result;
654   int shift;
655
656   result = value;
657   shift = 64-1;
658   for( value >>= 1 ; value ; value >>= 1 )
659   {
660     result <<= 1;
661     result |= value & 1;
662     shift--;
663   }
664   result <<= shift;
665
666   return result;
667 }
668
669 static inline uint32_t ccReverseBitsVar32( uint32_t value, int numbits )
670 {
671   uint32_t result;
672   int shift;
673
674   value &= ( ((uint32_t)1) << numbits ) - 1;
675   result = value;
676   shift = 32-1;
677   for( value >>= 1 ; value ; value >>= 1 )
678   {
679     result <<= 1;
680     result |= value & 1;
681     shift--;
682   }
683   result <<= shift;
684   result >>= 32 - numbits;
685
686   return result;
687 }
688
689 static inline uint64_t ccReverseBitsVar64( uint64_t value, int numbits )
690 {
691   uint64_t result;
692   int shift;
693
694   value &= ( ((uint64_t)1) << numbits ) - 1;
695   result = value;
696   shift = 64-1;
697   for( value >>= 1 ; value ; value >>= 1 )
698   {
699     result <<= 1;
700     result |= value & 1;
701     shift--;
702   }
703   result <<= shift;
704   result >>= 64 - numbits;
705
706   return result;
707 }
708
709
710
711 ////
712
713
714
715 static inline uint8_t ccIsPow2Int8( uint8_t v )
716 {
717   return ( ( v & ( v - 1 ) ) == 0 );
718 }
719
720 static inline uint16_t ccIsPow2Int16( uint16_t v )
721 {
722   return ( ( v & ( v - 1 ) ) == 0 );
723 }
724
725 static inline uint32_t ccIsPow2Int32( uint32_t v )
726 {
727   return ( ( v & ( v - 1 ) ) == 0 );
728 }
729
730 static inline uint64_t ccIsPow2Int64( uint64_t v )
731 {
732   return ( ( v & ( v - 1 ) ) == 0 );
733 }
734
735
736 static inline uint8_t ccPow2Round8( uint8_t v )
737 {
738   v--;
739   v |= v >> 1;
740   v |= v >> 2;
741   v |= v >> 4;
742   v++;
743   return v;
744 }
745
746 static inline uint16_t ccPow2Round16( uint16_t v )
747 {
748   v--;
749   v |= v >> 1;
750   v |= v >> 2;
751   v |= v >> 4;
752   v |= v >> 8;
753   v++;
754   return v;
755 }
756
757 static inline uint32_t ccPow2Round32( uint32_t v )
758 {
759   v--;
760   v |= v >> 1;
761   v |= v >> 2;
762   v |= v >> 4;
763   v |= v >> 8;
764   v |= v >> 16;
765   v++;
766   return v;
767 }
768
769 static inline uint64_t ccPow2Round64( uint64_t v )
770 {
771   v--;
772   v |= v >> 1;
773   v |= v >> 2;
774   v |= v >> 4;
775   v |= v >> 8;
776   v |= v >> 16;
777   v |= v >> 32;
778   v++;
779   return v;
780 }
781
782 #if CPUCONF_LONG_SIZE == 8
783  #define ccPow2RoundL(v) ccPow2Round64(v)
784 #else
785  #define ccPow2RoundL(v) ccPow2Round32(v)
786 #endif
787
788
789
790 ////
791
792
793
794 static inline uint32_t ccTestNullByte32( uint32_t v )
795 {
796   return ( v - 0x01010101 ) & ~v & 0x80808080;
797 }
798
799 static inline uint64_t ccTestNullByte64( uint64_t v )
800 {
801   return ( v - 0x0101010101010101ULL ) & ~v & 0x8080808080808080ULL;
802 }
803
804 static inline uint32_t ccSignBit32( uint32_t v )
805 {
806   return v >> 31;
807 }
808
809 static inline uint64_t ccSignBit64( uint64_t v )
810 {
811   return v >> 63;
812 }
813
814 static inline uint32_t ccAbs32( int32_t v )
815 {
816   int32_t mask;
817   mask = v >> 31;
818   return ( v ^ mask ) - mask;
819 }
820
821 static inline uint64_t ccAbs64( int64_t v )
822 {
823   int64_t mask;
824   mask = (int32_t)(v >> 63);
825   return ( v ^ mask ) - mask;
826 }
827
828
829
830 ////
831
832
833
834 static inline int32_t ccMortonNumber32( int32_t x, int32_t y )
835 {
836   int i;
837   uint32_t z;
838   z = 0;
839   for( i = 0 ; i < 16 ; i++ )
840   {
841     z |= ( x & ( ((uint32_t)1) << i ) ) << i;
842     z |= ( y & ( ((uint32_t)1) << i ) ) << ( i + 1 );
843   }
844   return z;
845 }
846
847 static inline int64_t ccMortonNumber64( int32_t x, int32_t y )
848 {
849   int i;
850   uint64_t z;
851   z = 0;
852   for( i = 0 ; i < 16 ; i++ )
853   {
854     z |= ( x & ( ((uint64_t)1) << i ) ) << i;
855     z |= ( y & ( ((uint64_t)1) << i ) ) << ( i + 1 );
856   }
857   return z;
858 }
859
860
861
862 ////
863
864
865
866 static inline int ccStrCmpEqualInline( char *s0, char *s1 )
867 {
868   int i;
869   for( i = 0 ; ; i++ )
870   {
871     if( s0[i] != s1[i] )
872       return 0;
873     if( !( s0[i] ) )
874       break;
875   }
876   return 1;
877 }
878
879 static inline int ccIsAlphaNum( char c )
880 {
881   if( ( c >= 'a' ) && ( c <= 'z' ) )
882     return 1;
883   if( ( c >= 'A' ) && ( c <= 'Z' ) )
884     return 1;
885   if( ( c >= '0' ) && ( c <= '9' ) )
886     return 1;
887   return 0;
888 }
889
890 static inline int ccIsAlphaNumExtended( unsigned char c )
891 {
892   if( ( c >= 'a' ) && ( c <= 'z' ) )
893     return 1;
894   if( ( c >= 'A' ) && ( c <= 'Z' ) )
895     return 1;
896   if( ( c >= '0' ) && ( c <= '9' ) )
897     return 1;
898   if( c >= 128 )
899     return 1;
900   return 0;
901 }
902
903 static inline int ccCharHexBase( char c )
904 {
905   int hex;
906   if( ( c >= '0' ) && ( c <= '9' ) )
907     hex = c - '0';
908   else if( ( c >= 'A' ) && ( c <= 'F' ) )
909     hex = c - ('A'-10);
910   else if( ( c >= 'a' ) && ( c <= 'f' ) )
911     hex = c - ('a'-10);
912   else
913     hex = -1;
914   return hex;
915 }
916
917
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 );
965
966
967 ////
968
969
970 #define CC_FLT_INT_MAPPING
971
972 #if CPUCONF_FLOAT_SIZE == 4
973 typedef uint32_t ccuintf;
974 #elif CPUCONF_FLOAT_SIZE == 8
975 typedef uint64_t ccuintf;
976 #else
977  #undef CC_FLT_INT_MAPPING
978 #endif
979
980 #if CPUCONF_DOUBLE_SIZE == 4
981 typedef uint32_t ccuintd;
982 #elif CPUCONF_DOUBLE_SIZE == 8
983 typedef uint64_t ccuintd;
984 #else
985  #undef CC_FLT_INT_MAPPING
986 #endif
987
988
989 #ifdef CC_FLT_INT_MAPPING
990
991 static inline ccuintf ccFloatToUint( float f )
992 {
993   void *p = &f;
994   return *((ccuintf *)p);
995 }
996
997 static inline float ccUintToFloat( ccuintf f )
998 {
999   void *p = &f;
1000   return *((float *)p);
1001 }
1002
1003
1004 static inline ccuintd ccDoubleToUint( double d )
1005 {
1006   void *p = &d;
1007   return *((ccuintd *)p);
1008 }
1009
1010 static inline double ccUintToDouble( ccuintd d )
1011 {
1012   void *p = &d;
1013   return *((double *)p);
1014 }
1015
1016 #endif
1017
1018
1019
1020 ////
1021
1022
1023
1024 #define CC_LOG2_E 1.4426950408889634073599246810018921
1025
1026 static inline float ccFastExpFloat( float x )
1027 {
1028   union
1029   {
1030     uint32_t i;
1031     float f;
1032   } u;
1033   if( x > 88.0 )
1034     return expf( x );
1035   else if( x < -80.0 )
1036     return 0.0;
1037   u.i = (int32_t)( x * ( (float)0x800000 * (float)CC_LOG2_E ) ) + ( 0x3f800000 - 486408 );
1038   return u.f;
1039 }
1040
1041 static inline float ccFastExpFloatNearZero( float x )
1042 {
1043   union
1044   {
1045     uint32_t i;
1046     float f;
1047   } u;
1048   if( x > 88.0 )
1049     return expf( x );
1050   else if( x < -80.0 )
1051     return 0.0;
1052   u.i = (int32_t)( x * ( (float)0x800000 * (float)CC_LOG2_E ) ) + 0x3f800000;
1053   return u.f;
1054 }
1055
1056 static inline double ccFastExpDouble( double x )
1057 {
1058 #if CPUCONF_WORD_SIZE >= 64
1059   union
1060   {
1061     uint64_t i;
1062     double d;
1063   } u;
1064   if( x > 88.0 )
1065     return exp( x );
1066   else if( x < -80.0 )
1067     return 0.0;
1068   u.i = (int64_t)( x * ( (double)0x10000000000000 * CC_LOG2_E ) ) + ( (uint64_t)0x3ff0000000000000 - (uint64_t)261138306564096 );
1069   return u.d;
1070 #else
1071   union
1072   {
1073     uint32_t i[2];
1074     double d;
1075   } u;
1076   if( x > 88.0 )
1077     return expf( (float)x );
1078   else if( x < -80.0 )
1079     return 0.0;
1080  #ifdef CPUCONF_LITTLE_ENDIAN
1081   u.i[1] = (int32_t)( x * ( (double)0x100000 * CC_LOG2_E ) ) + ( 0x3ff00000 - 60801 );
1082   u.i[0] = 0;
1083  #else
1084   u.i[0] = (int32_t)( x * ( (double)0x100000 * CC_LOG2_E ) ) + ( 0x3ff00000 - 60801 );
1085   u.i[1] = 0;
1086  #endif
1087   return u.d;
1088 #endif
1089 }
1090
1091 static inline double ccFastExpDoubleNearZero( double x )
1092 {
1093 #if CPUCONF_WORD_SIZE >= 64
1094   union
1095   {
1096     uint64_t i;
1097     double d;
1098   } u;
1099   if( x > 88.0 )
1100     return expf( x );
1101   else if( x < -80.0 )
1102     return 0.0;
1103   u.i = (int64_t)( x * ( (double)0x10000000000000 * CC_LOG2_E ) ) + (uint64_t)0x3ff0000000000000;
1104   return u.d;
1105 #else
1106   union
1107   {
1108     uint32_t i[2];
1109     double d;
1110   } u;
1111   if( x > 88.0 )
1112     return expf( (float)x );
1113   else if( x < -80.0 )
1114     return 0.0;
1115  #ifdef CPUCONF_LITTLE_ENDIAN
1116   u.i[1] = (int32_t)( x * ( (double)0x100000 * CC_LOG2_E ) ) + 0x3ff00000;
1117   u.i[0] = 0;
1118  #else
1119   u.i[0] = (int32_t)( x * ( (double)0x100000 * CC_LOG2_E ) ) + 0x3ff00000;
1120   u.i[1] = 0;
1121  #endif
1122   return u.d;
1123 #endif
1124 }
1125
1126
1127
1128 ////
1129
1130
1131
1132 static inline float ccFastLog2Float( float x )
1133 {
1134   int base;
1135   union
1136   {
1137     uint32_t i;
1138     float f;
1139   } u;
1140   u.f = 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 );
1145 }
1146
1147 static inline float ccFastLog2Double( double x )
1148 {
1149 #if CPUCONF_WORD_SIZE >= 64
1150   int base;
1151   union
1152   {
1153     uint64_t i;
1154     double f;
1155   } u;
1156   u.f = x;
1157   base = ( ( u.i >> 52 ) & 0x7ff ) - 0x400;
1158   u.i &= ~( (uint64_t)0x7ff << 52 );
1159   u.i += (uint64_t)0x3ff << 52;
1160 #else
1161   int base;
1162   union
1163   {
1164     uint32_t i[2];
1165     double f;
1166   } u;
1167   u.f = x;
1168   base = ( ( u.i[1] >> 20 ) & 0x7ff ) - 0x400;
1169   u.i[1] &= ~( (uint32_t)0x7ff << 20 );
1170   u.i[1] += (uint32_t)0x3ff << 20;
1171 #endif
1172   return (float)(base + ( u.f * ( 2.0f + u.f * ( -1.0f/3.0f ) ) ) - ( 2.0f/3.0f ));
1173 }
1174
1175
1176
1177 ////
1178
1179
1180 /* Only valid between -M_PI and M_PI */
1181 static inline float ccFastSinFloat( float x )
1182 {
1183   float s;
1184   s = (float)(( 1.27323954474 * x ) + ( -0.405284734569 * x * fabsf( x ) ));
1185   return s;
1186 }
1187
1188 /* Only valid between -M_PI and M_PI */
1189 static inline double ccFastSinDouble( double x )
1190 {
1191   double s;
1192   s = ( 1.27323954474 * x ) + ( -0.405284734569 * x * fabs( x ) );
1193   return s;
1194 }
1195
1196
1197
1198 ////
1199
1200
1201
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);}))
1205
1206
1207 static inline uint16_t ccByteSwap16( uint16_t i )
1208 {
1209   return (uint16_t)(CC_INT16_BSWAP( i ));
1210 }
1211
1212 #if defined(__GNUC__) && defined(__i386__)
1213
1214 static inline uint32_t ccByteSwap32( uint32_t i )
1215 {
1216   __asm__( "bswap %0" : "=r" (i) : "0" (i) );
1217   return i;
1218 }
1219
1220 static inline uint64_t ccByteSwap64( uint64_t i )
1221 {
1222   union {
1223     uint32_t s[2];
1224     uint64_t i;
1225   } u;
1226   u.i = 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]) );
1228   return u.i;
1229 }
1230
1231 #elif defined(__GNUC__) && defined(__x86_64__)
1232
1233 static inline uint32_t ccByteSwap32( uint32_t i )
1234 {
1235   __asm__( "bswapl %0" : "=r" (i) : "0" (i) );
1236   return i;
1237 }
1238
1239 static inline uint64_t ccByteSwap64( uint64_t i )
1240 {
1241   __asm__( "bswapq %0" : "=r" (i) : "0" (i) );
1242   return i;
1243 }
1244
1245 #else
1246
1247 static inline uint32_t ccByteSwap32( uint32_t i )
1248 {
1249   return CC_INT32_BSWAP( i );
1250 }
1251
1252 static inline uint64_t ccByteSwap64( uint64_t i )
1253 {
1254   return CC_INT64_BSWAP( i );
1255 }
1256
1257 #endif
1258
1259 static inline float ccByteSwapf( float f )
1260 {
1261   uint32_t i;
1262   void *p;
1263   p = &f;
1264   i = ccByteSwap32( *((uint32_t *)p) );
1265   p = &i;
1266   return *((float *)p);
1267 }
1268
1269 static inline double ccByteSwapd( double f )
1270 {
1271   uint64_t i;
1272   void *p;
1273   p = &f;
1274   i = ccByteSwap64( *((uint64_t *)p) );
1275   p = &i;
1276   return *((double *)p);
1277 }
1278
1279
1280 static inline uint32_t ccAlignInt32( uint32_t i )
1281 {
1282   i--;
1283   i |= i >> 1;
1284   i |= i >> 2;
1285   i |= i >> 4;
1286   i |= i >> 8;
1287   i |= i >> 16;
1288   return i + 1;
1289 }
1290
1291 static inline uint64_t ccAlignInt64( uint64_t i )
1292 {
1293   i--;
1294   i |= i >> 1;
1295   i |= i >> 2;
1296   i |= i >> 4;
1297   i |= i >> 8;
1298   i |= i >> 16;
1299   i |= i >> 32;
1300   return i + 1;
1301 }
1302
1303 static inline uintptr_t ccAlignIntPtr( uintptr_t i )
1304 {
1305   i--;
1306   i |= i >> 1;
1307   i |= i >> 2;
1308   i |= i >> 4;
1309   i |= i >> 8;
1310   i |= i >> 16;
1311 #if CPUCONF_INTPTR_BITS > 32
1312   i |= i >> 32;
1313 #endif
1314   return i + 1;
1315 }
1316
1317
1318
1319 ////
1320
1321
1322
1323 static inline uint8_t ccRotateLeft8( uint8_t x, int bits )
1324 {
1325   return ( x << bits ) | ( x >> ( 8 - bits ) );
1326 }
1327
1328 static inline uint16_t ccRotateLeft16( uint16_t x, int bits )
1329 {
1330   return ( x << bits ) | ( x >> ( 16 - bits ) );
1331 }
1332
1333 static inline uint32_t ccRotateLeft32( uint32_t x, int bits )
1334 {
1335   return ( x << bits ) | ( x >> ( 32 - bits ) );
1336 }
1337
1338 static inline uint64_t ccRotateLeft64( uint64_t x, int bits )
1339 {
1340   return ( x << bits ) | ( x >> ( 64 - bits ) );
1341 }
1342
1343
1344 static inline uint8_t ccRotateRight8( uint8_t x, int bits )
1345 {
1346   return ( x >> bits ) | ( x << ( 8 - bits ) );
1347 }
1348
1349 static inline uint16_t ccRotateRight16( uint16_t x, int bits )
1350 {
1351   return ( x >> bits ) | ( x << ( 16 - bits ) );
1352 }
1353
1354 static inline uint32_t ccRotateRight32( uint32_t x, int bits )
1355 {
1356   return ( x >> bits ) | ( x << ( 32 - bits ) );
1357 }
1358
1359 static inline uint64_t ccRotateRight64( uint64_t x, int bits )
1360 {
1361   return ( x >> bits ) | ( x << ( 64 - bits ) );
1362 }
1363
1364
1365 ////
1366
1367
1368 #define CC_INT32_MAX ((((uint32_t)1)<<31)-1)
1369
1370 static inline int32_t ccFloatToInt32Sat( float f )
1371 {
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;
1376   else
1377     return (int32_t)f;
1378 }
1379
1380
1381 static inline int32_t ccDoubleToInt32Sat( double f )
1382 {
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;
1387   else
1388     return (int32_t)f;
1389 }
1390
1391
1392 #define CC_INT64_MAX ((((uint64_t)1)<<63)-1)
1393
1394 static inline int64_t ccFloatToInt64Sat( float f )
1395 {
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;
1400   else
1401     return (int64_t)f;
1402 }
1403
1404
1405 static inline int64_t ccDoubleToInt64Sat( double f )
1406 {
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;
1411   else
1412     return (int64_t)f;
1413 }
1414
1415
1416 ////
1417
1418
1419
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 );
1422
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 );
1425
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 );
1428
1429
1430
1431 ////
1432
1433
1434
1435 void ccDebugLog( char *filename, char *string, ... );
1436
1437
1438 ////
1439
1440
1441 typedef struct
1442 {
1443   size_t allocsize;
1444   size_t offset;
1445   char *data;
1446 } ccGrowth;
1447
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 );
1454
1455
1456 ////
1457
1458
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 );
1465
1466
1467 ////
1468
1469
1470 typedef struct _ccDir ccDir;
1471
1472 ccDir *ccOpenDir( char *path );
1473 char *ccReadDir( ccDir *dir );
1474 void ccCloseDir( ccDir *dir );
1475
1476
1477 ////
1478
1479
1480 int64_t ccGetFreeDiskSpace( char *dirpath );
1481
1482
1483 ////
1484
1485
1486 int ccGetTimeOfDay( struct timeval *tv );
1487
1488 void ccSleep( int milliseconds );
1489
1490 static inline uint64_t ccGetMillisecondsTime()
1491 {
1492   struct timeval lntime;
1493   ccGetTimeOfDay( &lntime );
1494   return ( (uint64_t)lntime.tv_sec * 1000 ) + ( (uint64_t)lntime.tv_usec / 1000 );
1495 }
1496
1497 static inline uint64_t ccGetMicrosecondsTime()
1498 {
1499   struct timeval lntime;
1500   ccGetTimeOfDay( &lntime );
1501   return ( (uint64_t)lntime.tv_sec * 1000000 ) + (uint64_t)lntime.tv_usec;
1502 }
1503
1504 static inline uint64_t ccGetNanosecondsTime()
1505 {
1506   struct timeval lntime;
1507   ccGetTimeOfDay( &lntime );
1508   return ( (uint64_t)lntime.tv_sec * 1000000000 ) + ( (uint64_t)lntime.tv_usec * 1000 );
1509 }
1510
1511
1512 ////
1513
1514
1515 /* Returned string must be free()d */
1516 char *ccGetSystemName();