… | |
… | |
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 0x0001000a |
45 | #define ECB_VERSION 0x0001000c |
46 | |
46 | |
47 | #include <string.h> /* for memcpy */ |
47 | #include <string.h> /* for memcpy */ |
48 | |
48 | |
49 | #if defined (_WIN32) && !defined (__MINGW32__) |
49 | #if defined (_WIN32) && !defined (__MINGW32__) |
50 | typedef signed char int8_t; |
50 | typedef signed char int8_t; |
… | |
… | |
355 | #define ECB_CONCAT(a, b) ECB_CONCAT_(a, b) |
355 | #define ECB_CONCAT(a, b) ECB_CONCAT_(a, b) |
356 | #define ECB_STRINGIFY_(a) # a |
356 | #define ECB_STRINGIFY_(a) # a |
357 | #define ECB_STRINGIFY(a) ECB_STRINGIFY_(a) |
357 | #define ECB_STRINGIFY(a) ECB_STRINGIFY_(a) |
358 | #define ECB_STRINGIFY_EXPR(expr) ((expr), ECB_STRINGIFY_ (expr)) |
358 | #define ECB_STRINGIFY_EXPR(expr) ((expr), ECB_STRINGIFY_ (expr)) |
359 | |
359 | |
|
|
360 | /* This marks larger functions that do not neccessarily need to be inlined */ |
|
|
361 | /* The idea is to possibly compile the header twice, */ |
|
|
362 | /* once exposing only the declarations, another time to define external functions */ |
|
|
363 | /* TODO: possibly static would be best for these at the moment? */ |
360 | #define ecb_function_ ecb_inline |
364 | #define ecb_function_ ecb_inline |
361 | |
365 | |
362 | #if ECB_GCC_VERSION(3,1) || ECB_CLANG_VERSION(2,8) |
366 | #if ECB_GCC_VERSION(3,1) || ECB_CLANG_VERSION(2,8) |
363 | #define ecb_attribute(attrlist) __attribute__ (attrlist) |
367 | #define ecb_attribute(attrlist) __attribute__ (attrlist) |
364 | #else |
368 | #else |
… | |
… | |
454 | /* count trailing zero bits and count # of one bits */ |
458 | /* count trailing zero bits and count # of one bits */ |
455 | #if ECB_GCC_VERSION(3,4) \ |
459 | #if ECB_GCC_VERSION(3,4) \ |
456 | || (ECB_CLANG_BUILTIN(__builtin_clz) && ECB_CLANG_BUILTIN(__builtin_clzll) \ |
460 | || (ECB_CLANG_BUILTIN(__builtin_clz) && ECB_CLANG_BUILTIN(__builtin_clzll) \ |
457 | && ECB_CLANG_BUILTIN(__builtin_ctz) && ECB_CLANG_BUILTIN(__builtin_ctzll) \ |
461 | && ECB_CLANG_BUILTIN(__builtin_ctz) && ECB_CLANG_BUILTIN(__builtin_ctzll) \ |
458 | && ECB_CLANG_BUILTIN(__builtin_popcount)) |
462 | && ECB_CLANG_BUILTIN(__builtin_popcount)) |
459 | /* we assume int == 32 bit, long == 32 or 64 bit and long long == 64 bit */ |
|
|
460 | #define ecb_ld32(x) (__builtin_clz (x) ^ 31) |
|
|
461 | #define ecb_ld64(x) (__builtin_clzll (x) ^ 63) |
|
|
462 | #define ecb_ctz32(x) __builtin_ctz (x) |
463 | #define ecb_ctz32(x) __builtin_ctz (x) |
|
|
464 | #define ecb_ctz64(x) (__SIZEOF_LONG__ == 64 ? __builtin_ctzl (x) : __builtin_ctzll (x)) |
463 | #define ecb_ctz64(x) __builtin_ctzll (x) |
465 | #define ecb_clz32(x) __builtin_clz (x) |
|
|
466 | #define ecb_clz64(x) (__SIZEOF_LONG__ == 64 ? __builtin_clzl (x) : __builtin_clzll (x)) |
|
|
467 | #define ecb_ld32(x) (ecb_clz32 (x) ^ 31) |
|
|
468 | #define ecb_ld64(x) (ecb_clz64 (x) ^ 63) |
464 | #define ecb_popcount32(x) __builtin_popcount (x) |
469 | #define ecb_popcount32(x) __builtin_popcount (x) |
465 | /* no popcountll */ |
470 | /* ecb_popcount64 is more difficult, see below */ |
466 | #else |
471 | #else |
467 | ecb_function_ ecb_const int ecb_ctz32 (uint32_t x); |
472 | ecb_function_ ecb_const int ecb_ctz32 (uint32_t x); |
468 | ecb_function_ ecb_const int |
473 | ecb_function_ ecb_const int ecb_ctz32 (uint32_t x) |
469 | ecb_ctz32 (uint32_t x) |
|
|
470 | { |
474 | { |
471 | #if 1400 <= _MSC_VER && (_M_IX86 || _M_X64 || _M_IA64 || _M_ARM) |
475 | #if 1400 <= _MSC_VER && (_M_IX86 || _M_X64 || _M_IA64 || _M_ARM) |
472 | unsigned long r; |
476 | unsigned long r; |
473 | _BitScanForward (&r, x); |
477 | _BitScanForward (&r, x); |
474 | return (int)r; |
478 | return (int)r; |
475 | #else |
479 | #else |
476 | int r = 0; |
480 | int r; |
477 | |
481 | |
478 | x &= ~x + 1; /* this isolates the lowest bit */ |
482 | x &= ~x + 1; /* this isolates the lowest bit */ |
479 | |
483 | |
480 | #if ECB_branchless_on_i386 |
484 | #if 1 |
|
|
485 | /* David Seal's algorithm, Message-ID: <32975@armltd.uucp> from 1994 */ |
|
|
486 | /* This happens to return 32 for x == 0, but the API does not support this */ |
|
|
487 | |
|
|
488 | /* -0 marks unused entries */ |
|
|
489 | static unsigned char table[64] = |
|
|
490 | { |
|
|
491 | 32, 0, 1, 12, 2, 6, -0, 13, 3, -0, 7, -0, -0, -0, -0, 14, |
|
|
492 | 10, 4, -0, -0, 8, -0, -0, 25, -0, -0, -0, -0, -0, 21, 27, 15, |
|
|
493 | 31, 11, 5, -0, -0, -0, -0, -0, 9, -0, -0, 24, -0, -0, 20, 26, |
|
|
494 | 30, -0, -0, -0, -0, 23, -0, 19, 29, -0, 22, 18, 28, 17, 16, -0 |
|
|
495 | }; |
|
|
496 | |
|
|
497 | /* magic constant results in 33 unique values in the upper 6 bits */ |
|
|
498 | x *= 0x0450fbafU; /* == 17 * 65 * 65535 */ |
|
|
499 | |
|
|
500 | r = table [x >> 26]; |
|
|
501 | #elif 0 /* branchless on i386, typically */ |
|
|
502 | r = 0; |
481 | r += !!(x & 0xaaaaaaaa) << 0; |
503 | r += !!(x & 0xaaaaaaaa) << 0; |
482 | r += !!(x & 0xcccccccc) << 1; |
504 | r += !!(x & 0xcccccccc) << 1; |
483 | r += !!(x & 0xf0f0f0f0) << 2; |
505 | r += !!(x & 0xf0f0f0f0) << 2; |
484 | r += !!(x & 0xff00ff00) << 3; |
506 | r += !!(x & 0xff00ff00) << 3; |
485 | r += !!(x & 0xffff0000) << 4; |
507 | r += !!(x & 0xffff0000) << 4; |
486 | #else |
508 | #else /* branchless on modern compilers, typically */ |
|
|
509 | r = 0; |
487 | if (x & 0xaaaaaaaa) r += 1; |
510 | if (x & 0xaaaaaaaa) r += 1; |
488 | if (x & 0xcccccccc) r += 2; |
511 | if (x & 0xcccccccc) r += 2; |
489 | if (x & 0xf0f0f0f0) r += 4; |
512 | if (x & 0xf0f0f0f0) r += 4; |
490 | if (x & 0xff00ff00) r += 8; |
513 | if (x & 0xff00ff00) r += 8; |
491 | if (x & 0xffff0000) r += 16; |
514 | if (x & 0xffff0000) r += 16; |
… | |
… | |
494 | return r; |
517 | return r; |
495 | #endif |
518 | #endif |
496 | } |
519 | } |
497 | |
520 | |
498 | ecb_function_ ecb_const int ecb_ctz64 (uint64_t x); |
521 | ecb_function_ ecb_const int ecb_ctz64 (uint64_t x); |
499 | ecb_function_ ecb_const int |
522 | ecb_function_ ecb_const int ecb_ctz64 (uint64_t x) |
500 | ecb_ctz64 (uint64_t x) |
|
|
501 | { |
523 | { |
502 | #if 1400 <= _MSC_VER && (_M_X64 || _M_IA64 || _M_ARM) |
524 | #if 1400 <= _MSC_VER && (_M_X64 || _M_IA64 || _M_ARM) |
503 | unsigned long r; |
525 | unsigned long r; |
504 | _BitScanForward64 (&r, x); |
526 | _BitScanForward64 (&r, x); |
505 | return (int)r; |
527 | return (int)r; |
… | |
… | |
507 | int shift = x & 0xffffffff ? 0 : 32; |
529 | int shift = x & 0xffffffff ? 0 : 32; |
508 | return ecb_ctz32 (x >> shift) + shift; |
530 | return ecb_ctz32 (x >> shift) + shift; |
509 | #endif |
531 | #endif |
510 | } |
532 | } |
511 | |
533 | |
|
|
534 | ecb_function_ ecb_const int ecb_clz32 (uint32_t x); |
|
|
535 | ecb_function_ ecb_const int ecb_clz32 (uint32_t x) |
|
|
536 | { |
|
|
537 | #if 1400 <= _MSC_VER && (_M_IX86 || _M_X64 || _M_IA64 || _M_ARM) |
|
|
538 | unsigned long r; |
|
|
539 | _BitScanReverse (&r, x); |
|
|
540 | return 31 - (int)r; |
|
|
541 | #else |
|
|
542 | |
|
|
543 | /* Robert Harley's algorithm from comp.arch 1996-12-07 */ |
|
|
544 | /* This happens to return 32 for x == 0, but the API does not support this */ |
|
|
545 | |
|
|
546 | /* -0 marks unused table elements */ |
|
|
547 | static unsigned char table[64] = |
|
|
548 | { |
|
|
549 | 32, 31, -0, 16, -0, 30, 3, -0, 15, -0, -0, -0, 29, 10, 2, -0, |
|
|
550 | -0, -0, 12, 14, 21, -0, 19, -0, -0, 28, -0, 25, -0, 9, 1, -0, |
|
|
551 | 17, -0, 4, -0, -0, -0, 11, -0, 13, 22, 20, -0, 26, -0, -0, 18, |
|
|
552 | 5, -0, -0, 23, -0, 27, -0, 6, -0, 24, 7, -0, 8, -0, 0, -0 |
|
|
553 | }; |
|
|
554 | |
|
|
555 | /* propagate leftmost 1 bit to the right */ |
|
|
556 | x |= x >> 1; |
|
|
557 | x |= x >> 2; |
|
|
558 | x |= x >> 4; |
|
|
559 | x |= x >> 8; |
|
|
560 | x |= x >> 16; |
|
|
561 | |
|
|
562 | /* magic constant results in 33 unique values in the upper 6 bits */ |
|
|
563 | x *= 0x06EB14F9U; /* == 7 * 255 * 255 * 255 */ |
|
|
564 | |
|
|
565 | return table [x >> 26]; |
|
|
566 | #endif |
|
|
567 | } |
|
|
568 | |
|
|
569 | ecb_function_ ecb_const int ecb_clz64 (uint64_t x); |
|
|
570 | ecb_function_ ecb_const int ecb_clz64 (uint64_t x) |
|
|
571 | { |
|
|
572 | #if 1400 <= _MSC_VER && (_M_X64 || _M_IA64 || _M_ARM) |
|
|
573 | unsigned long r; |
|
|
574 | _BitScanReverse64 (&r, x); |
|
|
575 | return 63 - (int)r; |
|
|
576 | #else |
|
|
577 | uint32_t l = x >> 32; |
|
|
578 | int shift = l ? 0 : 32; |
|
|
579 | return ecb_clz32 (l ? l : x) + shift; |
|
|
580 | #endif |
|
|
581 | } |
|
|
582 | |
512 | ecb_function_ ecb_const int ecb_popcount32 (uint32_t x); |
583 | ecb_function_ ecb_const int ecb_popcount32 (uint32_t x); |
513 | ecb_function_ ecb_const int |
584 | ecb_function_ ecb_const int ecb_popcount32 (uint32_t x) |
514 | ecb_popcount32 (uint32_t x) |
|
|
515 | { |
585 | { |
516 | x -= (x >> 1) & 0x55555555; |
586 | x -= (x >> 1) & 0x55555555; |
517 | x = ((x >> 2) & 0x33333333) + (x & 0x33333333); |
587 | x = ((x >> 2) & 0x33333333) + (x & 0x33333333); |
518 | x = ((x >> 4) + x) & 0x0f0f0f0f; |
588 | x = ((x >> 4) + x) & 0x0f0f0f0f; |
519 | x *= 0x01010101; |
589 | x *= 0x01010101; |
… | |
… | |
591 | x = ( x >> 16 ) | ( x << 16); |
661 | x = ( x >> 16 ) | ( x << 16); |
592 | |
662 | |
593 | return x; |
663 | return x; |
594 | } |
664 | } |
595 | |
665 | |
596 | /* popcount64 is only available on 64 bit cpus as gcc builtin */ |
|
|
597 | /* so for this version we are lazy */ |
|
|
598 | ecb_function_ ecb_const int ecb_popcount64 (uint64_t x); |
666 | ecb_function_ ecb_const int ecb_popcount64 (uint64_t x); |
599 | ecb_function_ ecb_const int |
667 | ecb_function_ ecb_const int ecb_popcount64 (uint64_t x) |
600 | ecb_popcount64 (uint64_t x) |
|
|
601 | { |
668 | { |
|
|
669 | /* popcount64 is only available on 64 bit cpus as gcc builtin. */ |
|
|
670 | /* also, gcc/clang make this surprisingly difficult to use */ |
|
|
671 | #if (__SIZEOF_LONG__ == 8) && (ECB_GCC_VERSION(3,4) || ECB_CLANG_BUILTIN (__builtin_popcountl)) |
|
|
672 | return __builtin_popcountl (x); |
|
|
673 | #else |
602 | return ecb_popcount32 (x) + ecb_popcount32 (x >> 32); |
674 | return ecb_popcount32 (x) + ecb_popcount32 (x >> 32); |
|
|
675 | #endif |
603 | } |
676 | } |
604 | |
677 | |
605 | ecb_inline ecb_const uint8_t ecb_rotl8 (uint8_t x, unsigned int count); |
|
|
606 | ecb_inline ecb_const uint8_t ecb_rotr8 (uint8_t x, unsigned int count); |
|
|
607 | ecb_inline ecb_const uint16_t ecb_rotl16 (uint16_t x, unsigned int count); |
|
|
608 | ecb_inline ecb_const uint16_t ecb_rotr16 (uint16_t x, unsigned int count); |
|
|
609 | ecb_inline ecb_const uint32_t ecb_rotl32 (uint32_t x, unsigned int count); |
|
|
610 | ecb_inline ecb_const uint32_t ecb_rotr32 (uint32_t x, unsigned int count); |
|
|
611 | ecb_inline ecb_const uint64_t ecb_rotl64 (uint64_t x, unsigned int count); |
|
|
612 | ecb_inline ecb_const uint64_t ecb_rotr64 (uint64_t x, unsigned int count); |
|
|
613 | |
|
|
614 | ecb_inline ecb_const uint8_t ecb_rotl8 (uint8_t x, unsigned int count) { return (x >> (-count & 7)) | (x << (count & 7)); } |
678 | ecb_inline uint8_t ecb_rotl8 (uint8_t x, unsigned int count) { return (x >> (-count & 7)) | (x << (count & 7)); } |
615 | ecb_inline ecb_const uint8_t ecb_rotr8 (uint8_t x, unsigned int count) { return (x << (-count & 7)) | (x >> (count & 7)); } |
679 | ecb_inline uint8_t ecb_rotr8 (uint8_t x, unsigned int count) { return (x << (-count & 7)) | (x >> (count & 7)); } |
616 | ecb_inline ecb_const uint16_t ecb_rotl16 (uint16_t x, unsigned int count) { return (x >> (-count & 15)) | (x << (count & 15)); } |
680 | ecb_inline uint16_t ecb_rotl16 (uint16_t x, unsigned int count) { return (x >> (-count & 15)) | (x << (count & 15)); } |
617 | ecb_inline ecb_const uint16_t ecb_rotr16 (uint16_t x, unsigned int count) { return (x << (-count & 15)) | (x >> (count & 15)); } |
681 | ecb_inline uint16_t ecb_rotr16 (uint16_t x, unsigned int count) { return (x << (-count & 15)) | (x >> (count & 15)); } |
618 | ecb_inline ecb_const uint32_t ecb_rotl32 (uint32_t x, unsigned int count) { return (x >> (-count & 31)) | (x << (count & 31)); } |
682 | ecb_inline uint32_t ecb_rotl32 (uint32_t x, unsigned int count) { return (x >> (-count & 31)) | (x << (count & 31)); } |
619 | ecb_inline ecb_const uint32_t ecb_rotr32 (uint32_t x, unsigned int count) { return (x << (-count & 31)) | (x >> (count & 31)); } |
683 | ecb_inline uint32_t ecb_rotr32 (uint32_t x, unsigned int count) { return (x << (-count & 31)) | (x >> (count & 31)); } |
620 | ecb_inline ecb_const uint64_t ecb_rotl64 (uint64_t x, unsigned int count) { return (x >> (-count & 63)) | (x << (count & 63)); } |
684 | ecb_inline uint64_t ecb_rotl64 (uint64_t x, unsigned int count) { return (x >> (-count & 63)) | (x << (count & 63)); } |
621 | ecb_inline ecb_const uint64_t ecb_rotr64 (uint64_t x, unsigned int count) { return (x << (-count & 63)) | (x >> (count & 63)); } |
685 | ecb_inline uint64_t ecb_rotr64 (uint64_t x, unsigned int count) { return (x << (-count & 63)) | (x >> (count & 63)); } |
622 | |
686 | |
623 | #if ECB_CPP |
687 | #if ECB_CPP |
624 | |
688 | |
625 | inline uint8_t ecb_ctz (uint8_t v) { return ecb_ctz32 (v); } |
689 | inline uint8_t ecb_ctz (uint8_t v) { return ecb_ctz32 (v); } |
626 | inline uint16_t ecb_ctz (uint16_t v) { return ecb_ctz32 (v); } |
690 | inline uint16_t ecb_ctz (uint16_t v) { return ecb_ctz32 (v); } |
… | |
… | |
671 | #define ecb_bswap16(x) ((uint16_t)_byteswap_ushort ((uint16_t)(x))) |
735 | #define ecb_bswap16(x) ((uint16_t)_byteswap_ushort ((uint16_t)(x))) |
672 | #define ecb_bswap32(x) ((uint32_t)_byteswap_ulong ((uint32_t)(x))) |
736 | #define ecb_bswap32(x) ((uint32_t)_byteswap_ulong ((uint32_t)(x))) |
673 | #define ecb_bswap64(x) ((uint64_t)_byteswap_uint64 ((uint64_t)(x))) |
737 | #define ecb_bswap64(x) ((uint64_t)_byteswap_uint64 ((uint64_t)(x))) |
674 | #else |
738 | #else |
675 | ecb_function_ ecb_const uint16_t ecb_bswap16 (uint16_t x); |
739 | ecb_function_ ecb_const uint16_t ecb_bswap16 (uint16_t x); |
676 | ecb_function_ ecb_const uint16_t |
740 | ecb_function_ ecb_const uint16_t ecb_bswap16 (uint16_t x) |
677 | ecb_bswap16 (uint16_t x) |
|
|
678 | { |
741 | { |
679 | return ecb_rotl16 (x, 8); |
742 | return ecb_rotl16 (x, 8); |
680 | } |
743 | } |
681 | |
744 | |
682 | ecb_function_ ecb_const uint32_t ecb_bswap32 (uint32_t x); |
745 | ecb_function_ ecb_const uint32_t ecb_bswap32 (uint32_t x); |
683 | ecb_function_ ecb_const uint32_t |
746 | ecb_function_ ecb_const uint32_t ecb_bswap32 (uint32_t x) |
684 | ecb_bswap32 (uint32_t x) |
|
|
685 | { |
747 | { |
686 | return (((uint32_t)ecb_bswap16 (x)) << 16) | ecb_bswap16 (x >> 16); |
748 | return (((uint32_t)ecb_bswap16 (x)) << 16) | ecb_bswap16 (x >> 16); |
687 | } |
749 | } |
688 | |
750 | |
689 | ecb_function_ ecb_const uint64_t ecb_bswap64 (uint64_t x); |
751 | ecb_function_ ecb_const uint64_t ecb_bswap64 (uint64_t x); |
690 | ecb_function_ ecb_const uint64_t |
752 | ecb_function_ ecb_const uint64_t ecb_bswap64 (uint64_t x) |
691 | ecb_bswap64 (uint64_t x) |
|
|
692 | { |
753 | { |
693 | return (((uint64_t)ecb_bswap32 (x)) << 32) | ecb_bswap32 (x >> 32); |
754 | return (((uint64_t)ecb_bswap32 (x)) << 32) | ecb_bswap32 (x >> 32); |
694 | } |
755 | } |
695 | #endif |
756 | #endif |
696 | |
757 | |
… | |
… | |
703 | #endif |
764 | #endif |
704 | |
765 | |
705 | /* try to tell the compiler that some condition is definitely true */ |
766 | /* try to tell the compiler that some condition is definitely true */ |
706 | #define ecb_assume(cond) if (!(cond)) ecb_unreachable (); else 0 |
767 | #define ecb_assume(cond) if (!(cond)) ecb_unreachable (); else 0 |
707 | |
768 | |
708 | ecb_inline ecb_const uint32_t ecb_byteorder_helper (void); |
769 | ecb_inline uint32_t ecb_byteorder_helper (void); |
709 | ecb_inline ecb_const uint32_t |
770 | ecb_inline uint32_t ecb_byteorder_helper (void) |
710 | ecb_byteorder_helper (void) |
|
|
711 | { |
771 | { |
712 | /* the union code still generates code under pressure in gcc, */ |
772 | /* the union code still generates code under pressure in gcc, */ |
713 | /* but less than using pointers, and always seems to */ |
773 | /* but less than using pointers, and always seems to */ |
714 | /* successfully return a constant. */ |
774 | /* successfully return a constant. */ |
715 | /* the reason why we have this horrible preprocessor mess */ |
775 | /* the reason why we have this horrible preprocessor mess */ |
… | |
… | |
731 | } u = { 0x11, 0x22, 0x33, 0x44 }; |
791 | } u = { 0x11, 0x22, 0x33, 0x44 }; |
732 | return u.u; |
792 | return u.u; |
733 | #endif |
793 | #endif |
734 | } |
794 | } |
735 | |
795 | |
736 | ecb_inline ecb_const ecb_bool ecb_big_endian (void); |
|
|
737 | ecb_inline ecb_const ecb_bool ecb_big_endian (void) { return ecb_byteorder_helper () == 0x11223344; } |
796 | ecb_inline ecb_const ecb_bool ecb_big_endian (void) { return ecb_byteorder_helper () == 0x11223344; } |
738 | ecb_inline ecb_const ecb_bool ecb_little_endian (void); |
|
|
739 | ecb_inline ecb_const ecb_bool ecb_little_endian (void) { return ecb_byteorder_helper () == 0x44332211; } |
797 | ecb_inline ecb_const ecb_bool ecb_little_endian (void) { return ecb_byteorder_helper () == 0x44332211; } |
740 | |
798 | |
741 | /*****************************************************************************/ |
799 | /*****************************************************************************/ |
742 | /* unaligned load/store */ |
800 | /* unaligned load/store */ |
743 | |
801 | |
… | |
… | |
810 | |
868 | |
811 | /*****************************************************************************/ |
869 | /*****************************************************************************/ |
812 | /* pointer/integer hashing */ |
870 | /* pointer/integer hashing */ |
813 | |
871 | |
814 | /* based on hash by Chris Wellons, https://nullprogram.com/blog/2018/07/31/ */ |
872 | /* based on hash by Chris Wellons, https://nullprogram.com/blog/2018/07/31/ */ |
815 | ecb_function_ uint32_t ecb_mix32 (uint32_t v); |
873 | ecb_function_ ecb_const uint32_t ecb_mix32 (uint32_t v); |
816 | ecb_function_ uint32_t ecb_mix32 (uint32_t v) |
874 | ecb_function_ ecb_const uint32_t ecb_mix32 (uint32_t v) |
817 | { |
875 | { |
818 | v ^= v >> 16; v *= 0x7feb352dU; |
876 | v ^= v >> 16; v *= 0x7feb352dU; |
819 | v ^= v >> 15; v *= 0x846ca68bU; |
877 | v ^= v >> 15; v *= 0x846ca68bU; |
820 | v ^= v >> 16; |
878 | v ^= v >> 16; |
821 | return v; |
879 | return v; |
822 | } |
880 | } |
823 | |
881 | |
824 | ecb_function_ uint32_t ecb_unmix32 (uint32_t v); |
882 | ecb_function_ ecb_const uint32_t ecb_unmix32 (uint32_t v); |
825 | ecb_function_ uint32_t ecb_unmix32 (uint32_t v) |
883 | ecb_function_ ecb_const uint32_t ecb_unmix32 (uint32_t v) |
826 | { |
884 | { |
827 | v ^= v >> 16 ; v *= 0x43021123U; |
885 | v ^= v >> 16 ; v *= 0x43021123U; |
828 | v ^= v >> 15 ^ v >> 30; v *= 0x1d69e2a5U; |
886 | v ^= v >> 15 ^ v >> 30; v *= 0x1d69e2a5U; |
829 | v ^= v >> 16 ; |
887 | v ^= v >> 16 ; |
830 | return v; |
888 | return v; |
831 | } |
889 | } |
832 | |
890 | |
833 | /* based on splitmix64, by Sebastiona Vigna, https://prng.di.unimi.it/splitmix64.c */ |
891 | /* based on splitmix64, by Sebastiona Vigna, https://prng.di.unimi.it/splitmix64.c */ |
834 | ecb_function_ uint64_t ecb_mix64 (uint64_t v); |
892 | ecb_function_ ecb_const uint64_t ecb_mix64 (uint64_t v); |
835 | ecb_function_ uint64_t ecb_mix64 (uint64_t v) |
893 | ecb_function_ ecb_const uint64_t ecb_mix64 (uint64_t v) |
836 | { |
894 | { |
837 | v ^= v >> 30; v *= 0xbf58476d1ce4e5b9U; |
895 | v ^= v >> 30; v *= 0xbf58476d1ce4e5b9U; |
838 | v ^= v >> 27; v *= 0x94d049bb133111ebU; |
896 | v ^= v >> 27; v *= 0x94d049bb133111ebU; |
839 | v ^= v >> 31; |
897 | v ^= v >> 31; |
840 | return v; |
898 | return v; |
841 | } |
899 | } |
842 | |
900 | |
843 | ecb_function_ uint64_t ecb_unmix64 (uint64_t v); |
901 | ecb_function_ ecb_const uint64_t ecb_unmix64 (uint64_t v); |
844 | ecb_function_ uint64_t ecb_unmix64 (uint64_t v) |
902 | ecb_function_ ecb_const uint64_t ecb_unmix64 (uint64_t v) |
845 | { |
903 | { |
846 | v ^= v >> 31 ^ v >> 62; v *= 0x319642b2d24d8ec3U; |
904 | v ^= v >> 31 ^ v >> 62; v *= 0x319642b2d24d8ec3U; |
847 | v ^= v >> 27 ^ v >> 54; v *= 0x96de1b173f119089U; |
905 | v ^= v >> 27 ^ v >> 54; v *= 0x96de1b173f119089U; |
848 | v ^= v >> 30 ^ v >> 60; |
906 | v ^= v >> 30 ^ v >> 60; |
849 | return v; |
907 | return v; |
850 | } |
908 | } |
851 | |
909 | |
852 | ecb_function_ uintptr_t ecb_ptrmix (void *p); |
910 | ecb_function_ ecb_const uintptr_t ecb_ptrmix (void *p); |
853 | ecb_function_ uintptr_t ecb_ptrmix (void *p) |
911 | ecb_function_ ecb_const uintptr_t ecb_ptrmix (void *p) |
854 | { |
912 | { |
855 | #if ECB_PTRSIZE <= 4 |
913 | #if ECB_PTRSIZE <= 4 |
856 | return ecb_mix32 ((uint32_t)p); |
914 | return ecb_mix32 ((uint32_t)p); |
857 | #else |
915 | #else |
858 | return ecb_mix64 ((uint64_t)p); |
916 | return ecb_mix64 ((uint64_t)p); |
859 | #endif |
917 | #endif |
860 | } |
918 | } |
861 | |
919 | |
862 | ecb_function_ void *ecb_ptrunmix (uintptr_t v); |
920 | ecb_function_ ecb_const void *ecb_ptrunmix (uintptr_t v); |
863 | ecb_function_ void *ecb_ptrunmix (uintptr_t v) |
921 | ecb_function_ ecb_const void *ecb_ptrunmix (uintptr_t v) |
864 | { |
922 | { |
865 | #if ECB_PTRSIZE <= 4 |
923 | #if ECB_PTRSIZE <= 4 |
866 | return (void *)ecb_unmix32 (v); |
924 | return (void *)ecb_unmix32 (v); |
867 | #else |
925 | #else |
868 | return (void *)ecb_unmix64 (v); |
926 | return (void *)ecb_unmix64 (v); |
… | |
… | |
884 | } |
942 | } |
885 | |
943 | |
886 | #endif |
944 | #endif |
887 | |
945 | |
888 | /*****************************************************************************/ |
946 | /*****************************************************************************/ |
|
|
947 | /* gray code */ |
|
|
948 | |
|
|
949 | ecb_inline uint_fast8_t ecb_gray_encode8 (uint_fast8_t b) { return b ^ (b >> 1); } |
|
|
950 | ecb_inline uint_fast16_t ecb_gray_encode16 (uint_fast16_t b) { return b ^ (b >> 1); } |
|
|
951 | ecb_inline uint_fast32_t ecb_gray_encode32 (uint_fast32_t b) { return b ^ (b >> 1); } |
|
|
952 | ecb_inline uint_fast64_t ecb_gray_encode64 (uint_fast64_t b) { return b ^ (b >> 1); } |
|
|
953 | |
|
|
954 | ecb_function_ ecb_const uint8_t ecb_gray_decode8 (uint8_t g); |
|
|
955 | ecb_function_ ecb_const uint8_t ecb_gray_decode8 (uint8_t g) |
|
|
956 | { |
|
|
957 | g ^= g >> 1; |
|
|
958 | g ^= g >> 2; |
|
|
959 | g ^= g >> 4; |
|
|
960 | |
|
|
961 | return g; |
|
|
962 | } |
|
|
963 | |
|
|
964 | ecb_function_ ecb_const uint16_t ecb_gray_decode16 (uint16_t g); |
|
|
965 | ecb_function_ ecb_const uint16_t ecb_gray_decode16 (uint16_t g) |
|
|
966 | { |
|
|
967 | g ^= g >> 1; |
|
|
968 | g ^= g >> 2; |
|
|
969 | g ^= g >> 4; |
|
|
970 | g ^= g >> 8; |
|
|
971 | |
|
|
972 | return g; |
|
|
973 | } |
|
|
974 | |
|
|
975 | ecb_function_ ecb_const uint32_t ecb_gray_decode32 (uint32_t g); |
|
|
976 | ecb_function_ ecb_const uint32_t ecb_gray_decode32 (uint32_t g) |
|
|
977 | { |
|
|
978 | g ^= g >> 1; |
|
|
979 | g ^= g >> 2; |
|
|
980 | g ^= g >> 4; |
|
|
981 | g ^= g >> 8; |
|
|
982 | g ^= g >> 16; |
|
|
983 | |
|
|
984 | return g; |
|
|
985 | } |
|
|
986 | |
|
|
987 | ecb_function_ ecb_const uint64_t ecb_gray_decode64 (uint64_t g); |
|
|
988 | ecb_function_ ecb_const uint64_t ecb_gray_decode64 (uint64_t g) |
|
|
989 | { |
|
|
990 | g ^= g >> 1; |
|
|
991 | g ^= g >> 2; |
|
|
992 | g ^= g >> 4; |
|
|
993 | g ^= g >> 8; |
|
|
994 | g ^= g >> 16; |
|
|
995 | g ^= g >> 32; |
|
|
996 | |
|
|
997 | return g; |
|
|
998 | } |
|
|
999 | |
|
|
1000 | #if ECB_CPP |
|
|
1001 | |
|
|
1002 | ecb_inline uint8_t ecb_gray_encode (uint8_t b) { return ecb_gray_encode8 (b); } |
|
|
1003 | ecb_inline uint16_t ecb_gray_encode (uint16_t b) { return ecb_gray_encode16 (b); } |
|
|
1004 | ecb_inline uint32_t ecb_gray_encode (uint32_t b) { return ecb_gray_encode32 (b); } |
|
|
1005 | ecb_inline uint64_t ecb_gray_encode (uint64_t b) { return ecb_gray_encode64 (b); } |
|
|
1006 | |
|
|
1007 | ecb_inline uint8_t ecb_gray_decode (uint8_t g) { return ecb_gray_decode8 (g); } |
|
|
1008 | ecb_inline uint16_t ecb_gray_decode (uint16_t g) { return ecb_gray_decode16 (g); } |
|
|
1009 | ecb_inline uint32_t ecb_gray_decode (uint32_t g) { return ecb_gray_decode32 (g); } |
|
|
1010 | ecb_inline uint64_t ecb_gray_decode (uint64_t g) { return ecb_gray_decode64 (g); } |
|
|
1011 | |
|
|
1012 | #endif |
|
|
1013 | |
|
|
1014 | /*****************************************************************************/ |
|
|
1015 | /* 2d hilbert curves */ |
|
|
1016 | |
|
|
1017 | /* algorithm from the book Hacker's Delight, modified to not */ |
|
|
1018 | /* run into undefined behaviour for n==16 */ |
|
|
1019 | ecb_function_ ecb_const uint32_t ecb_hilbert2d_index_to_coord32 (int n, uint32_t s); |
|
|
1020 | ecb_function_ ecb_const uint32_t ecb_hilbert2d_index_to_coord32 (int n, uint32_t s) |
|
|
1021 | { |
|
|
1022 | uint32_t comp, swap, cs, t, sr; |
|
|
1023 | |
|
|
1024 | /* pad s on the left (unused) bits with 01 (no change groups) */ |
|
|
1025 | s |= 0x55555555U << n << n; |
|
|
1026 | /* "s shift right" */ |
|
|
1027 | sr = (s >> 1) & 0x55555555U; |
|
|
1028 | /* compute complement and swap info in two-bit groups */ |
|
|
1029 | cs = ((s & 0x55555555U) + sr) ^ 0x55555555U; |
|
|
1030 | |
|
|
1031 | /* parallel prefix xor op to propagate both complement |
|
|
1032 | * and swap info together from left to right (there is |
|
|
1033 | * no step "cs ^= cs >> 1", so in effect it computes |
|
|
1034 | * two independent parallel prefix operations on two |
|
|
1035 | * interleaved sets of sixteen bits). |
|
|
1036 | */ |
|
|
1037 | cs ^= cs >> 2; |
|
|
1038 | cs ^= cs >> 4; |
|
|
1039 | cs ^= cs >> 8; |
|
|
1040 | cs ^= cs >> 16; |
|
|
1041 | |
|
|
1042 | /* separate swap and complement bits */ |
|
|
1043 | swap = cs & 0x55555555U; |
|
|
1044 | comp = (cs >> 1) & 0x55555555U; |
|
|
1045 | |
|
|
1046 | /* calculate coordinates in odd and even bit positions */ |
|
|
1047 | t = (s & swap) ^ comp; |
|
|
1048 | s = s ^ sr ^ t ^ (t << 1); |
|
|
1049 | |
|
|
1050 | /* unpad/clear out any junk on the left */ |
|
|
1051 | s = s & ((1 << n << n) - 1); |
|
|
1052 | |
|
|
1053 | /* Now "unshuffle" to separate the x and y bits. */ |
|
|
1054 | t = (s ^ (s >> 1)) & 0x22222222U; s ^= t ^ (t << 1); |
|
|
1055 | t = (s ^ (s >> 2)) & 0x0c0c0c0cU; s ^= t ^ (t << 2); |
|
|
1056 | t = (s ^ (s >> 4)) & 0x00f000f0U; s ^= t ^ (t << 4); |
|
|
1057 | t = (s ^ (s >> 8)) & 0x0000ff00U; s ^= t ^ (t << 8); |
|
|
1058 | |
|
|
1059 | /* now s contains two 16-bit coordinates */ |
|
|
1060 | return s; |
|
|
1061 | } |
|
|
1062 | |
|
|
1063 | /* 64 bit, a straightforward extension to the 32 bit case */ |
|
|
1064 | ecb_function_ ecb_const uint64_t ecb_hilbert2d_index_to_coord64 (int n, uint64_t s); |
|
|
1065 | ecb_function_ ecb_const uint64_t ecb_hilbert2d_index_to_coord64 (int n, uint64_t s) |
|
|
1066 | { |
|
|
1067 | uint64_t comp, swap, cs, t, sr; |
|
|
1068 | |
|
|
1069 | /* pad s on the left (unused) bits with 01 (no change groups) */ |
|
|
1070 | s |= 0x5555555555555555U << n << n; |
|
|
1071 | /* "s shift right" */ |
|
|
1072 | sr = (s >> 1) & 0x5555555555555555U; |
|
|
1073 | /* compute complement and swap info in two-bit groups */ |
|
|
1074 | cs = ((s & 0x5555555555555555U) + sr) ^ 0x5555555555555555U; |
|
|
1075 | |
|
|
1076 | /* parallel prefix xor op to propagate both complement |
|
|
1077 | * and swap info together from left to right (there is |
|
|
1078 | * no step "cs ^= cs >> 1", so in effect it computes |
|
|
1079 | * two independent parallel prefix operations on two |
|
|
1080 | * interleaved sets of thirty-two bits). |
|
|
1081 | */ |
|
|
1082 | cs ^= cs >> 2; |
|
|
1083 | cs ^= cs >> 4; |
|
|
1084 | cs ^= cs >> 8; |
|
|
1085 | cs ^= cs >> 16; |
|
|
1086 | cs ^= cs >> 32; |
|
|
1087 | |
|
|
1088 | /* separate swap and complement bits */ |
|
|
1089 | swap = cs & 0x5555555555555555U; |
|
|
1090 | comp = (cs >> 1) & 0x5555555555555555U; |
|
|
1091 | |
|
|
1092 | /* calculate coordinates in odd and even bit positions */ |
|
|
1093 | t = (s & swap) ^ comp; |
|
|
1094 | s = s ^ sr ^ t ^ (t << 1); |
|
|
1095 | |
|
|
1096 | /* unpad/clear out any junk on the left */ |
|
|
1097 | s = s & ((1 << n << n) - 1); |
|
|
1098 | |
|
|
1099 | /* Now "unshuffle" to separate the x and y bits. */ |
|
|
1100 | t = (s ^ (s >> 1)) & 0x2222222222222222U; s ^= t ^ (t << 1); |
|
|
1101 | t = (s ^ (s >> 2)) & 0x0c0c0c0c0c0c0c0cU; s ^= t ^ (t << 2); |
|
|
1102 | t = (s ^ (s >> 4)) & 0x00f000f000f000f0U; s ^= t ^ (t << 4); |
|
|
1103 | t = (s ^ (s >> 8)) & 0x0000ff000000ff00U; s ^= t ^ (t << 8); |
|
|
1104 | t = (s ^ (s >> 16)) & 0x00000000ffff0000U; s ^= t ^ (t << 16); |
|
|
1105 | |
|
|
1106 | /* now s contains two 32-bit coordinates */ |
|
|
1107 | return s; |
|
|
1108 | } |
|
|
1109 | |
|
|
1110 | /* algorithm from the book Hacker's Delight, but a similar algorithm*/ |
|
|
1111 | /* is given in https://doi.org/10.1002/spe.4380160103 */ |
|
|
1112 | /* this has been slightly improved over the original version */ |
|
|
1113 | ecb_function_ ecb_const uint32_t ecb_hilbert2d_coord_to_index32 (int n, uint32_t xy); |
|
|
1114 | ecb_function_ ecb_const uint32_t ecb_hilbert2d_coord_to_index32 (int n, uint32_t xy) |
|
|
1115 | { |
|
|
1116 | uint32_t row; |
|
|
1117 | uint32_t state = 0; |
|
|
1118 | uint32_t s = 0; |
|
|
1119 | |
|
|
1120 | do |
|
|
1121 | { |
|
|
1122 | --n; |
|
|
1123 | |
|
|
1124 | row = 4 * state |
|
|
1125 | | (2 & (xy >> n >> 15)) |
|
|
1126 | | (1 & (xy >> n )); |
|
|
1127 | |
|
|
1128 | /* these funky constants are lookup tables for two-bit values */ |
|
|
1129 | s = (s << 2) | (0x361e9cb4U >> 2 * row) & 3; |
|
|
1130 | state = (0x8fe65831U >> 2 * row) & 3; |
|
|
1131 | } |
|
|
1132 | while (n > 0); |
|
|
1133 | |
|
|
1134 | return s; |
|
|
1135 | } |
|
|
1136 | |
|
|
1137 | /* 64 bit, essentially the same as 32 bit */ |
|
|
1138 | ecb_function_ ecb_const uint64_t ecb_hilbert2d_coord_to_index64 (int n, uint64_t xy); |
|
|
1139 | ecb_function_ ecb_const uint64_t ecb_hilbert2d_coord_to_index64 (int n, uint64_t xy) |
|
|
1140 | { |
|
|
1141 | uint32_t row; |
|
|
1142 | uint32_t state = 0; |
|
|
1143 | uint64_t s = 0; |
|
|
1144 | |
|
|
1145 | do |
|
|
1146 | { |
|
|
1147 | --n; |
|
|
1148 | |
|
|
1149 | row = 4 * state |
|
|
1150 | | (2 & (xy >> n >> 31)) |
|
|
1151 | | (1 & (xy >> n )); |
|
|
1152 | |
|
|
1153 | /* these funky constants are lookup tables for two-bit values */ |
|
|
1154 | s = (s << 2) | (0x361e9cb4U >> 2 * row) & 3; |
|
|
1155 | state = (0x8fe65831U >> 2 * row) & 3; |
|
|
1156 | } |
|
|
1157 | while (n > 0); |
|
|
1158 | |
|
|
1159 | return s; |
|
|
1160 | } |
|
|
1161 | |
|
|
1162 | /*****************************************************************************/ |
889 | /* division */ |
1163 | /* division */ |
890 | |
1164 | |
891 | #if ECB_GCC_VERSION(3,0) || ECB_C99 |
1165 | #if ECB_GCC_VERSION(3,0) || ECB_C99 |
892 | /* C99 tightened the definition of %, so we can use a more efficient version */ |
1166 | /* C99 tightened the definition of %, so we can use a more efficient version */ |
893 | #define ecb_mod(m,n) ((m) % (n) + ((m) % (n) < 0 ? (n) : 0)) |
1167 | #define ecb_mod(m,n) ((m) % (n) + ((m) % (n) < 0 ? (n) : 0)) |
… | |
… | |
927 | |
1201 | |
928 | /*****************************************************************************/ |
1202 | /*****************************************************************************/ |
929 | /* IEEE 754-2008 half float conversions */ |
1203 | /* IEEE 754-2008 half float conversions */ |
930 | |
1204 | |
931 | ecb_function_ ecb_const uint32_t ecb_binary16_to_binary32 (uint32_t x); |
1205 | ecb_function_ ecb_const uint32_t ecb_binary16_to_binary32 (uint32_t x); |
932 | ecb_function_ ecb_const uint32_t |
1206 | ecb_function_ ecb_const uint32_t ecb_binary16_to_binary32 (uint32_t x) |
933 | ecb_binary16_to_binary32 (uint32_t x) |
|
|
934 | { |
1207 | { |
935 | unsigned int s = (x & 0x8000) << (31 - 15); |
1208 | unsigned int s = (x & 0x8000) << (31 - 15); |
936 | int e = (x >> 10) & 0x001f; |
1209 | int e = (x >> 10) & 0x001f; |
937 | unsigned int m = x & 0x03ff; |
1210 | unsigned int m = x & 0x03ff; |
938 | |
1211 | |
… | |
… | |
959 | |
1232 | |
960 | return s | (e << 23) | (m << (23 - 10)); |
1233 | return s | (e << 23) | (m << (23 - 10)); |
961 | } |
1234 | } |
962 | |
1235 | |
963 | ecb_function_ ecb_const uint16_t ecb_binary32_to_binary16 (uint32_t x); |
1236 | ecb_function_ ecb_const uint16_t ecb_binary32_to_binary16 (uint32_t x); |
964 | ecb_function_ ecb_const uint16_t |
1237 | ecb_function_ ecb_const uint16_t ecb_binary32_to_binary16 (uint32_t x) |
965 | ecb_binary32_to_binary16 (uint32_t x) |
|
|
966 | { |
1238 | { |
967 | unsigned int s = (x >> 16) & 0x00008000; /* sign bit, the easy part */ |
1239 | unsigned int s = (x >> 16) & 0x00008000; /* sign bit, the easy part */ |
968 | int e = ((x >> 23) & 0x000000ff) - (127 - 15); /* the desired exponent */ |
1240 | int e = ((x >> 23) & 0x000000ff) - (127 - 15); /* the desired exponent */ |
969 | unsigned int m = x & 0x007fffff; |
1241 | unsigned int m = x & 0x007fffff; |
970 | |
1242 | |
… | |
… | |
1038 | * format becomes 5.27, 6.26 and so on. |
1310 | * format becomes 5.27, 6.26 and so on. |
1039 | * The rest involves only advancing the pointer if we already generated a |
1311 | * The rest involves only advancing the pointer if we already generated a |
1040 | * non-zero digit, so leading zeroes are overwritten. |
1312 | * non-zero digit, so leading zeroes are overwritten. |
1041 | */ |
1313 | */ |
1042 | |
1314 | |
1043 | // simply return a mask with "bits" bits set |
1315 | /* simply return a mask with "bits" bits set */ |
1044 | #define ecb_i2a_mask(type,bits) ((((type)1) << (bits)) - 1) |
1316 | #define ecb_i2a_mask(type,bits) ((((type)1) << (bits)) - 1) |
1045 | |
1317 | |
1046 | // oputput a single digit. maskvalue is 10**digitidx |
1318 | /* oputput a single digit. maskvalue is 10**digitidx */ |
1047 | #define ecb_i2a_digit(type,bits,digitmask,maskvalue,digitidx) \ |
1319 | #define ecb_i2a_digit(type,bits,digitmask,maskvalue,digitidx) \ |
1048 | if (digitmask >= maskvalue) /* constant, used to decide how many digits to generate */ \ |
1320 | if (digitmask >= maskvalue) /* constant, used to decide how many digits to generate */ \ |
1049 | { \ |
1321 | { \ |
1050 | char digit = x >> (bits - digitidx); /* calculate the topmost digit */ \ |
1322 | char digit = x >> (bits - digitidx); /* calculate the topmost digit */ \ |
1051 | *ptr = digit + '0'; /* output it */ \ |
1323 | *ptr = digit + '0'; /* output it */ \ |
1052 | nz = (digitmask == maskvalue) || nz || digit; /* first term == always output last digit */ \ |
1324 | nz = (digitmask == maskvalue) || nz || digit; /* first term == always output last digit */ \ |
1053 | ptr += nz; /* output digit only if non-zero digit seen */ \ |
1325 | ptr += nz; /* output digit only if non-zero digit seen */ \ |
1054 | x = (x & ecb_i2a_mask (type, bits - digitidx)) * 5; /* *10, but shift decimal point right */ \ |
1326 | x = (x & ecb_i2a_mask (type, bits - digitidx)) * 5; /* *10, but shift decimal point right */ \ |
1055 | } |
1327 | } |
1056 | |
1328 | |
1057 | // convert integer to fixed point format and multiply out digits, highest first |
1329 | /* convert integer to fixed point format and multiply out digits, highest first */ |
1058 | // requires magic constants: max. digits and number of bits after the decimal point |
1330 | /* requires magic constants: max. digits and number of bits after the decimal point */ |
1059 | #define ecb_i2a_def(suffix,ptr,v,type,bits,digitmask,lz) \ |
1331 | #define ecb_i2a_def(suffix,ptr,v,type,bits,digitmask,lz) \ |
1060 | ecb_inline char *ecb_i2a_ ## suffix (char *ptr, uint32_t u) \ |
1332 | ecb_inline char *ecb_i2a_ ## suffix (char *ptr, uint32_t u) \ |
1061 | { \ |
1333 | { \ |
1062 | char nz = lz; /* non-zero digit seen? */ \ |
1334 | char nz = lz; /* non-zero digit seen? */ \ |
1063 | /* convert to x.bits fixed-point */ \ |
1335 | /* convert to x.bits fixed-point */ \ |
… | |
… | |
1074 | ecb_i2a_digit (type,bits,digitmask, 100000000, 8); \ |
1346 | ecb_i2a_digit (type,bits,digitmask, 100000000, 8); \ |
1075 | ecb_i2a_digit (type,bits,digitmask, 1000000000, 9); \ |
1347 | ecb_i2a_digit (type,bits,digitmask, 1000000000, 9); \ |
1076 | return ptr; \ |
1348 | return ptr; \ |
1077 | } |
1349 | } |
1078 | |
1350 | |
1079 | // predefined versions of the above, for various digits |
1351 | /* predefined versions of the above, for various digits */ |
1080 | // ecb_i2a_xN = almost N digits, limit defined by macro |
1352 | /* ecb_i2a_xN = almost N digits, limit defined by macro */ |
1081 | // ecb_i2a_N = up to N digits, leading zeroes suppressed |
1353 | /* ecb_i2a_N = up to N digits, leading zeroes suppressed */ |
1082 | // ecb_i2a_0N = exactly N digits, including leading zeroes |
1354 | /* ecb_i2a_0N = exactly N digits, including leading zeroes */ |
1083 | |
1355 | |
1084 | // non-leading-zero versions, limited range |
1356 | /* non-leading-zero versions, limited range */ |
1085 | #define ECB_I2A_MAX_X5 59074 // limit for ecb_i2a_x5 |
1357 | #define ECB_I2A_MAX_X5 59074 /* limit for ecb_i2a_x5 */ |
1086 | #define ECB_I2A_MAX_X10 2932500665 // limit for ecb_i2a_x10 |
1358 | #define ECB_I2A_MAX_X10 2932500665 /* limit for ecb_i2a_x10 */ |
1087 | ecb_i2a_def ( x5, ptr, v, uint32_t, 26, 10000, 0) |
1359 | ecb_i2a_def ( x5, ptr, v, uint32_t, 26, 10000, 0) |
1088 | ecb_i2a_def (x10, ptr, v, uint64_t, 60, 1000000000, 0) |
1360 | ecb_i2a_def (x10, ptr, v, uint64_t, 60, 1000000000, 0) |
1089 | |
1361 | |
1090 | // non-leading zero versions, all digits, 4 and 9 are optimal for 32/64 bit |
1362 | /* non-leading zero versions, all digits, 4 and 9 are optimal for 32/64 bit */ |
1091 | ecb_i2a_def ( 2, ptr, v, uint32_t, 10, 10, 0) |
1363 | ecb_i2a_def ( 2, ptr, v, uint32_t, 10, 10, 0) |
1092 | ecb_i2a_def ( 3, ptr, v, uint32_t, 12, 100, 0) |
1364 | ecb_i2a_def ( 3, ptr, v, uint32_t, 12, 100, 0) |
1093 | ecb_i2a_def ( 4, ptr, v, uint32_t, 26, 1000, 0) |
1365 | ecb_i2a_def ( 4, ptr, v, uint32_t, 26, 1000, 0) |
1094 | ecb_i2a_def ( 5, ptr, v, uint64_t, 30, 10000, 0) |
1366 | ecb_i2a_def ( 5, ptr, v, uint64_t, 30, 10000, 0) |
1095 | ecb_i2a_def ( 6, ptr, v, uint64_t, 36, 100000, 0) |
1367 | ecb_i2a_def ( 6, ptr, v, uint64_t, 36, 100000, 0) |
1096 | ecb_i2a_def ( 7, ptr, v, uint64_t, 44, 1000000, 0) |
1368 | ecb_i2a_def ( 7, ptr, v, uint64_t, 44, 1000000, 0) |
1097 | ecb_i2a_def ( 8, ptr, v, uint64_t, 50, 10000000, 0) |
1369 | ecb_i2a_def ( 8, ptr, v, uint64_t, 50, 10000000, 0) |
1098 | ecb_i2a_def ( 9, ptr, v, uint64_t, 56, 100000000, 0) |
1370 | ecb_i2a_def ( 9, ptr, v, uint64_t, 56, 100000000, 0) |
1099 | |
1371 | |
1100 | // leading-zero versions, all digits, 04 and 09 are optimal for 32/64 bit |
1372 | /* leading-zero versions, all digits, 04 and 09 are optimal for 32/64 bit */ |
1101 | ecb_i2a_def (02, ptr, v, uint32_t, 10, 10, 1) |
1373 | ecb_i2a_def (02, ptr, v, uint32_t, 10, 10, 1) |
1102 | ecb_i2a_def (03, ptr, v, uint32_t, 12, 100, 1) |
1374 | ecb_i2a_def (03, ptr, v, uint32_t, 12, 100, 1) |
1103 | ecb_i2a_def (04, ptr, v, uint32_t, 26, 1000, 1) |
1375 | ecb_i2a_def (04, ptr, v, uint32_t, 26, 1000, 1) |
1104 | ecb_i2a_def (05, ptr, v, uint64_t, 30, 10000, 1) |
1376 | ecb_i2a_def (05, ptr, v, uint64_t, 30, 10000, 1) |
1105 | ecb_i2a_def (06, ptr, v, uint64_t, 36, 100000, 1) |
1377 | ecb_i2a_def (06, ptr, v, uint64_t, 36, 100000, 1) |
… | |
… | |
1111 | #define ECB_I2A_U32_DIGITS 10 |
1383 | #define ECB_I2A_U32_DIGITS 10 |
1112 | #define ECB_I2A_I64_DIGITS 20 |
1384 | #define ECB_I2A_I64_DIGITS 20 |
1113 | #define ECB_I2A_U64_DIGITS 21 |
1385 | #define ECB_I2A_U64_DIGITS 21 |
1114 | #define ECB_I2A_MAX_DIGITS 21 |
1386 | #define ECB_I2A_MAX_DIGITS 21 |
1115 | |
1387 | |
1116 | ecb_inline char * |
1388 | ecb_function_ char * ecb_i2a_u32 (char *ptr, uint32_t u); |
1117 | ecb_i2a_u32 (char *ptr, uint32_t u) |
1389 | ecb_function_ char * ecb_i2a_u32 (char *ptr, uint32_t u) |
1118 | { |
1390 | { |
1119 | #if ECB_64BIT_NATIVE |
1391 | #if ECB_64BIT_NATIVE |
1120 | if (ecb_expect_true (u <= ECB_I2A_MAX_X10)) |
1392 | if (ecb_expect_true (u <= ECB_I2A_MAX_X10)) |
1121 | ptr = ecb_i2a_x10 (ptr, u); |
1393 | ptr = ecb_i2a_x10 (ptr, u); |
1122 | else // x10 almost, but not fully, covers 32 bit |
1394 | else /* x10 almost, but not fully, covers 32 bit */ |
1123 | { |
1395 | { |
1124 | uint32_t u1 = u % 1000000000; |
1396 | uint32_t u1 = u % 1000000000; |
1125 | uint32_t u2 = u / 1000000000; |
1397 | uint32_t u2 = u / 1000000000; |
1126 | |
1398 | |
1127 | *ptr++ = u2 + '0'; |
1399 | *ptr++ = u2 + '0'; |
… | |
… | |
1152 | #endif |
1424 | #endif |
1153 | |
1425 | |
1154 | return ptr; |
1426 | return ptr; |
1155 | } |
1427 | } |
1156 | |
1428 | |
1157 | ecb_inline char * |
1429 | ecb_function_ char * ecb_i2a_i32 (char *ptr, int32_t v); |
1158 | ecb_i2a_i32 (char *ptr, int32_t v) |
1430 | ecb_function_ char * ecb_i2a_i32 (char *ptr, int32_t v) |
1159 | { |
1431 | { |
1160 | *ptr = '-'; ptr += v < 0; |
1432 | *ptr = '-'; ptr += v < 0; |
1161 | uint32_t u = v < 0 ? -(uint32_t)v : v; |
1433 | uint32_t u = v < 0 ? -(uint32_t)v : v; |
1162 | |
1434 | |
1163 | #if ECB_64BIT_NATIVE |
1435 | #if ECB_64BIT_NATIVE |
1164 | ptr = ecb_i2a_x10 (ptr, u); // x10 fully covers 31 bit |
1436 | ptr = ecb_i2a_x10 (ptr, u); /* x10 fully covers 31 bit */ |
1165 | #else |
1437 | #else |
1166 | ptr = ecb_i2a_u32 (ptr, u); |
1438 | ptr = ecb_i2a_u32 (ptr, u); |
1167 | #endif |
1439 | #endif |
1168 | |
1440 | |
1169 | return ptr; |
1441 | return ptr; |
1170 | } |
1442 | } |
1171 | |
1443 | |
1172 | ecb_inline char * |
1444 | ecb_function_ char * ecb_i2a_u64 (char *ptr, uint64_t u); |
1173 | ecb_i2a_u64 (char *ptr, uint64_t u) |
1445 | ecb_function_ char * ecb_i2a_u64 (char *ptr, uint64_t u) |
1174 | { |
1446 | { |
1175 | #if ECB_64BIT_NATIVE |
1447 | #if ECB_64BIT_NATIVE |
1176 | if (ecb_expect_true (u <= ECB_I2A_MAX_X10)) |
1448 | if (ecb_expect_true (u <= ECB_I2A_MAX_X10)) |
1177 | ptr = ecb_i2a_x10 (ptr, u); |
1449 | ptr = ecb_i2a_x10 (ptr, u); |
1178 | else if (ecb_expect_false (u <= ECB_I2A_MAX_X10 * 1000000000)) |
1450 | else if (ecb_expect_false (u <= ECB_I2A_MAX_X10 * 1000000000)) |
… | |
… | |
1208 | #endif |
1480 | #endif |
1209 | |
1481 | |
1210 | return ptr; |
1482 | return ptr; |
1211 | } |
1483 | } |
1212 | |
1484 | |
1213 | ecb_inline char * |
1485 | ecb_function_ char * ecb_i2a_i64 (char *ptr, int64_t v); |
1214 | ecb_i2a_i64 (char *ptr, int64_t v) |
1486 | ecb_function_ char * ecb_i2a_i64 (char *ptr, int64_t v) |
1215 | { |
1487 | { |
1216 | *ptr = '-'; ptr += v < 0; |
1488 | *ptr = '-'; ptr += v < 0; |
1217 | uint64_t u = v < 0 ? -(uint64_t)v : v; |
1489 | uint64_t u = v < 0 ? -(uint64_t)v : v; |
1218 | |
1490 | |
1219 | #if ECB_64BIT_NATIVE |
1491 | #if ECB_64BIT_NATIVE |
… | |
… | |
1232 | uint64_t u1 = u % 1000000000; |
1504 | uint64_t u1 = u % 1000000000; |
1233 | uint64_t ua = u / 1000000000; |
1505 | uint64_t ua = u / 1000000000; |
1234 | uint64_t u2 = ua % 1000000000; |
1506 | uint64_t u2 = ua % 1000000000; |
1235 | uint64_t u3 = ua / 1000000000; |
1507 | uint64_t u3 = ua / 1000000000; |
1236 | |
1508 | |
1237 | // 2**31 is 19 digits, so the top is exactly one digit |
1509 | /* 2**31 is 19 digits, so the top is exactly one digit */ |
1238 | *ptr++ = u3 + '0'; |
1510 | *ptr++ = u3 + '0'; |
1239 | ptr = ecb_i2a_09 (ptr, u2); |
1511 | ptr = ecb_i2a_09 (ptr, u2); |
1240 | ptr = ecb_i2a_09 (ptr, u1); |
1512 | ptr = ecb_i2a_09 (ptr, u1); |
1241 | } |
1513 | } |
1242 | #else |
1514 | #else |
… | |
… | |
1296 | #define ecb_frexpf(x,e) (float) frexp ((double) (x), (e)) |
1568 | #define ecb_frexpf(x,e) (float) frexp ((double) (x), (e)) |
1297 | #endif |
1569 | #endif |
1298 | |
1570 | |
1299 | /* convert a float to ieee single/binary32 */ |
1571 | /* convert a float to ieee single/binary32 */ |
1300 | ecb_function_ ecb_const uint32_t ecb_float_to_binary32 (float x); |
1572 | ecb_function_ ecb_const uint32_t ecb_float_to_binary32 (float x); |
1301 | ecb_function_ ecb_const uint32_t |
1573 | ecb_function_ ecb_const uint32_t ecb_float_to_binary32 (float x) |
1302 | ecb_float_to_binary32 (float x) |
|
|
1303 | { |
1574 | { |
1304 | uint32_t r; |
1575 | uint32_t r; |
1305 | |
1576 | |
1306 | #if ECB_STDFP |
1577 | #if ECB_STDFP |
1307 | memcpy (&r, &x, 4); |
1578 | memcpy (&r, &x, 4); |
… | |
… | |
1336 | return r; |
1607 | return r; |
1337 | } |
1608 | } |
1338 | |
1609 | |
1339 | /* converts an ieee single/binary32 to a float */ |
1610 | /* converts an ieee single/binary32 to a float */ |
1340 | ecb_function_ ecb_const float ecb_binary32_to_float (uint32_t x); |
1611 | ecb_function_ ecb_const float ecb_binary32_to_float (uint32_t x); |
1341 | ecb_function_ ecb_const float |
1612 | ecb_function_ ecb_const float ecb_binary32_to_float (uint32_t x) |
1342 | ecb_binary32_to_float (uint32_t x) |
|
|
1343 | { |
1613 | { |
1344 | float r; |
1614 | float r; |
1345 | |
1615 | |
1346 | #if ECB_STDFP |
1616 | #if ECB_STDFP |
1347 | memcpy (&r, &x, 4); |
1617 | memcpy (&r, &x, 4); |
… | |
… | |
1366 | return r; |
1636 | return r; |
1367 | } |
1637 | } |
1368 | |
1638 | |
1369 | /* convert a double to ieee double/binary64 */ |
1639 | /* convert a double to ieee double/binary64 */ |
1370 | ecb_function_ ecb_const uint64_t ecb_double_to_binary64 (double x); |
1640 | ecb_function_ ecb_const uint64_t ecb_double_to_binary64 (double x); |
1371 | ecb_function_ ecb_const uint64_t |
1641 | ecb_function_ ecb_const uint64_t ecb_double_to_binary64 (double x) |
1372 | ecb_double_to_binary64 (double x) |
|
|
1373 | { |
1642 | { |
1374 | uint64_t r; |
1643 | uint64_t r; |
1375 | |
1644 | |
1376 | #if ECB_STDFP |
1645 | #if ECB_STDFP |
1377 | memcpy (&r, &x, 8); |
1646 | memcpy (&r, &x, 8); |
… | |
… | |
1406 | return r; |
1675 | return r; |
1407 | } |
1676 | } |
1408 | |
1677 | |
1409 | /* converts an ieee double/binary64 to a double */ |
1678 | /* converts an ieee double/binary64 to a double */ |
1410 | ecb_function_ ecb_const double ecb_binary64_to_double (uint64_t x); |
1679 | ecb_function_ ecb_const double ecb_binary64_to_double (uint64_t x); |
1411 | ecb_function_ ecb_const double |
1680 | ecb_function_ ecb_const double ecb_binary64_to_double (uint64_t x) |
1412 | ecb_binary64_to_double (uint64_t x) |
|
|
1413 | { |
1681 | { |
1414 | double r; |
1682 | double r; |
1415 | |
1683 | |
1416 | #if ECB_STDFP |
1684 | #if ECB_STDFP |
1417 | memcpy (&r, &x, 8); |
1685 | memcpy (&r, &x, 8); |
… | |
… | |
1436 | return r; |
1704 | return r; |
1437 | } |
1705 | } |
1438 | |
1706 | |
1439 | /* convert a float to ieee half/binary16 */ |
1707 | /* convert a float to ieee half/binary16 */ |
1440 | ecb_function_ ecb_const uint16_t ecb_float_to_binary16 (float x); |
1708 | ecb_function_ ecb_const uint16_t ecb_float_to_binary16 (float x); |
1441 | ecb_function_ ecb_const uint16_t |
1709 | ecb_function_ ecb_const uint16_t ecb_float_to_binary16 (float x) |
1442 | ecb_float_to_binary16 (float x) |
|
|
1443 | { |
1710 | { |
1444 | return ecb_binary32_to_binary16 (ecb_float_to_binary32 (x)); |
1711 | return ecb_binary32_to_binary16 (ecb_float_to_binary32 (x)); |
1445 | } |
1712 | } |
1446 | |
1713 | |
1447 | /* convert an ieee half/binary16 to float */ |
1714 | /* convert an ieee half/binary16 to float */ |
1448 | ecb_function_ ecb_const float ecb_binary16_to_float (uint16_t x); |
1715 | ecb_function_ ecb_const float ecb_binary16_to_float (uint16_t x); |
1449 | ecb_function_ ecb_const float |
1716 | ecb_function_ ecb_const float ecb_binary16_to_float (uint16_t x) |
1450 | ecb_binary16_to_float (uint16_t x) |
|
|
1451 | { |
1717 | { |
1452 | return ecb_binary32_to_float (ecb_binary16_to_binary32 (x)); |
1718 | return ecb_binary32_to_float (ecb_binary16_to_binary32 (x)); |
1453 | } |
1719 | } |
1454 | |
1720 | |
1455 | #endif |
1721 | #endif |