1 /* If the fallback clor, clzr, ctor and ctzr are used, it might be better
2 * to implement it as functions, as inlined it is almost as large as a
4 * Below is an example of how to do it.
18 .c "%s (0x%016lx) %s = %d\n"
20 .c "%s (0x%08lx) %s = %d\n"
25 #define BIT2(OP, ARG, RES, R0, R1) \
28 beqi OP##R0##R1##ARG %R0 RES \
32 #define BIT1(OP, ARG, RES, V0, V1, V2, R0, R1, R2) \
33 BIT2(OP, ARG, RES, V0, V0) \
34 BIT2(OP, ARG, RES, V0, V1) \
35 BIT2(OP, ARG, RES, V0, V2) \
36 BIT2(OP, ARG, RES, V0, R0) \
37 BIT2(OP, ARG, RES, V0, R1) \
38 BIT2(OP, ARG, RES, V0, R2)
40 #define BIT(OP, ARG, RES, V0, V1, V2, R0, R1, R2) \
41 BIT1(OP, ARG, RES, V1, V2, R0, R1, R2, V0) \
42 BIT1(OP, ARG, RES, V2, R0, R1, R2, V0, V1) \
43 BIT1(OP, ARG, RES, R0, R1, R2, V0, V1, V2) \
44 BIT1(OP, ARG, RES, R1, R2, V0, V1, V2, R0) \
45 BIT1(OP, ARG, RES, R2, V0, V1, V2, R0, R1)
47 #define CLO(ARG, RES) \
48 BIT(clo, ARG, RES, v0, v1, v2, r0, r1, r2)
49 #define CLZ(ARG, RES) \
50 BIT(clz, ARG, RES, v0, v1, v2, r0, r1, r2)
51 #define CTO(ARG, RES) \
52 BIT(cto, ARG, RES, v0, v1, v2, r0, r1, r2)
53 #define CTZ(ARG, RES) \
54 BIT(ctz, ARG, RES, v0, v1, v2, r0, r1, r2)
59 jit_uword_t cto(jit_uword_t r0) {
87 jit_uword_t clo(jit_uword_t r0) {
115 jit_uword_t clz(jit_word_t r1) {
122 r2 = 0xffffffff00000000UL;
167 movi %r2 0xffffffff00000000
203 jit_uword_t ctz(jit_uword_t r1) {
291 char *bitsprint(char *v0, jit_uword_t v1) {
293 memset(v0, '0', __WORDSIZE);
295 for (r0 = 1L << (__WORDSIZE - 1), r1 = 0; r0; r0 >>= 1, ++r1) {
315 addi %r1 %v0 __WORDSIZE
317 movi %r0 $(1 << (__WORDSIZE - 1))
332 int main(int argc, char *argv[]) {
333 jit_uword_t r0, v0, v1, v2;
336 char *fmt = "%s (0x%016lx) %s = %d\n";
337 v0 = 0x8000000000000000UL;
338 v2 = 0xffffffffffffffffUL;
340 char *fmt = "%s (0x%08lx) %s = %d\n";
348 printf(fmt, "clz", v0, buf, r0);
351 printf(fmt, "clo", v2, buf, r0);
354 printf(fmt, "ctz", v0, buf, r0);
357 printf(fmt, "cto", v1, buf, r0);
360 } while ((jit_word_t)v1 > -1);
366 /* Make it "#if 1" for a "debug mode", that helps in regenerating tables,
367 * or temporary state while implementing optimized port specific versions. */
375 movi %v0 0x8000000000000000
376 movi %v2 0xffffffffffffffff
615 CLZ(0x8000000000000000, 0)
616 CLO(0xffffffffffffffff, 64)
617 CTZ(0x8000000000000000, 63)
618 CTO(0x7fffffffffffffff, 63)
619 CLZ(0x4000000000000000, 1)
620 CLO(0xfffffffffffffffe, 63)
621 CTZ(0x4000000000000000, 62)
622 CTO(0x3fffffffffffffff, 62)
623 CLZ(0x2000000000000000, 2)
624 CLO(0xfffffffffffffffc, 62)
625 CTZ(0x2000000000000000, 61)
626 CTO(0x1fffffffffffffff, 61)
627 CLZ(0x1000000000000000, 3)
628 CLO(0xfffffffffffffff8, 61)
629 CTZ(0x1000000000000000, 60)
630 CTO(0x0fffffffffffffff, 60)
631 CLZ(0x0800000000000000, 4)
632 CLO(0xfffffffffffffff0, 60)
633 CTZ(0x0800000000000000, 59)
634 CTO(0x07ffffffffffffff, 59)
635 CLZ(0x0400000000000000, 5)
636 CLO(0xffffffffffffffe0, 59)
637 CTZ(0x0400000000000000, 58)
638 CTO(0x03ffffffffffffff, 58)
639 CLZ(0x0200000000000000, 6)
640 CLO(0xffffffffffffffc0, 58)
641 CTZ(0x0200000000000000, 57)
642 CTO(0x01ffffffffffffff, 57)
643 CLZ(0x0100000000000000, 7)
644 CLO(0xffffffffffffff80, 57)
645 CTZ(0x0100000000000000, 56)
646 CTO(0x00ffffffffffffff, 56)
647 CLZ(0x0080000000000000, 8)
648 CLO(0xffffffffffffff00, 56)
649 CTZ(0x0080000000000000, 55)
650 CTO(0x007fffffffffffff, 55)
651 CLZ(0x0040000000000000, 9)
652 CLO(0xfffffffffffffe00, 55)
653 CTZ(0x0040000000000000, 54)
654 CTO(0x003fffffffffffff, 54)
655 CLZ(0x0020000000000000, 10)
656 CLO(0xfffffffffffffc00, 54)
657 CTZ(0x0020000000000000, 53)
658 CTO(0x001fffffffffffff, 53)
659 CLZ(0x0010000000000000, 11)
660 CLO(0xfffffffffffff800, 53)
661 CTZ(0x0010000000000000, 52)
662 CTO(0x000fffffffffffff, 52)
663 CLZ(0x0008000000000000, 12)
664 CLO(0xfffffffffffff000, 52)
665 CTZ(0x0008000000000000, 51)
666 CTO(0x0007ffffffffffff, 51)
667 CLZ(0x0004000000000000, 13)
668 CLO(0xffffffffffffe000, 51)
669 CTZ(0x0004000000000000, 50)
670 CTO(0x0003ffffffffffff, 50)
671 CLZ(0x0002000000000000, 14)
672 CLO(0xffffffffffffc000, 50)
673 CTZ(0x0002000000000000, 49)
674 CTO(0x0001ffffffffffff, 49)
675 CLZ(0x0001000000000000, 15)
676 CLO(0xffffffffffff8000, 49)
677 CTZ(0x0001000000000000, 48)
678 CTO(0x0000ffffffffffff, 48)
679 CLZ(0x0000800000000000, 16)
680 CLO(0xffffffffffff0000, 48)
681 CTZ(0x0000800000000000, 47)
682 CTO(0x00007fffffffffff, 47)
683 CLZ(0x0000400000000000, 17)
684 CLO(0xfffffffffffe0000, 47)
685 CTZ(0x0000400000000000, 46)
686 CTO(0x00003fffffffffff, 46)
687 CLZ(0x0000200000000000, 18)
688 CLO(0xfffffffffffc0000, 46)
689 CTZ(0x0000200000000000, 45)
690 CTO(0x00001fffffffffff, 45)
691 CLZ(0x0000100000000000, 19)
692 CLO(0xfffffffffff80000, 45)
693 CTZ(0x0000100000000000, 44)
694 CTO(0x00000fffffffffff, 44)
695 CLZ(0x0000080000000000, 20)
696 CLO(0xfffffffffff00000, 44)
697 CTZ(0x0000080000000000, 43)
698 CTO(0x000007ffffffffff, 43)
699 CLZ(0x0000040000000000, 21)
700 CLO(0xffffffffffe00000, 43)
701 CTZ(0x0000040000000000, 42)
702 CTO(0x000003ffffffffff, 42)
703 CLZ(0x0000020000000000, 22)
704 CLO(0xffffffffffc00000, 42)
705 CTZ(0x0000020000000000, 41)
706 CTO(0x000001ffffffffff, 41)
707 CLZ(0x0000010000000000, 23)
708 CLO(0xffffffffff800000, 41)
709 CTZ(0x0000010000000000, 40)
710 CTO(0x000000ffffffffff, 40)
711 CLZ(0x0000008000000000, 24)
712 CLO(0xffffffffff000000, 40)
713 CTZ(0x0000008000000000, 39)
714 CTO(0x0000007fffffffff, 39)
715 CLZ(0x0000004000000000, 25)
716 CLO(0xfffffffffe000000, 39)
717 CTZ(0x0000004000000000, 38)
718 CTO(0x0000003fffffffff, 38)
719 CLZ(0x0000002000000000, 26)
720 CLO(0xfffffffffc000000, 38)
721 CTZ(0x0000002000000000, 37)
722 CTO(0x0000001fffffffff, 37)
723 CLZ(0x0000001000000000, 27)
724 CLO(0xfffffffff8000000, 37)
725 CTZ(0x0000001000000000, 36)
726 CTO(0x0000000fffffffff, 36)
727 CLZ(0x0000000800000000, 28)
728 CLO(0xfffffffff0000000, 36)
729 CTZ(0x0000000800000000, 35)
730 CTO(0x00000007ffffffff, 35)
731 CLZ(0x0000000400000000, 29)
732 CLO(0xffffffffe0000000, 35)
733 CTZ(0x0000000400000000, 34)
734 CTO(0x00000003ffffffff, 34)
735 CLZ(0x0000000200000000, 30)
736 CLO(0xffffffffc0000000, 34)
737 CTZ(0x0000000200000000, 33)
738 CTO(0x00000001ffffffff, 33)
739 CLZ(0x0000000100000000, 31)
740 CLO(0xffffffff80000000, 33)
741 CTZ(0x0000000100000000, 32)
742 CTO(0x00000000ffffffff, 32)
743 CLZ(0x0000000080000000, 32)
744 CLO(0xffffffff00000000, 32)
745 CTZ(0x0000000080000000, 31)
746 CTO(0x000000007fffffff, 31)
747 CLZ(0x0000000040000000, 33)
748 CLO(0xfffffffe00000000, 31)
749 CTZ(0x0000000040000000, 30)
750 CTO(0x000000003fffffff, 30)
751 CLZ(0x0000000020000000, 34)
752 CLO(0xfffffffc00000000, 30)
753 CTZ(0x0000000020000000, 29)
754 CTO(0x000000001fffffff, 29)
755 CLZ(0x0000000010000000, 35)
756 CLO(0xfffffff800000000, 29)
757 CTZ(0x0000000010000000, 28)
758 CTO(0x000000000fffffff, 28)
759 CLZ(0x0000000008000000, 36)
760 CLO(0xfffffff000000000, 28)
761 CTZ(0x0000000008000000, 27)
762 CTO(0x0000000007ffffff, 27)
763 CLZ(0x0000000004000000, 37)
764 CLO(0xffffffe000000000, 27)
765 CTZ(0x0000000004000000, 26)
766 CTO(0x0000000003ffffff, 26)
767 CLZ(0x0000000002000000, 38)
768 CLO(0xffffffc000000000, 26)
769 CTZ(0x0000000002000000, 25)
770 CTO(0x0000000001ffffff, 25)
771 CLZ(0x0000000001000000, 39)
772 CLO(0xffffff8000000000, 25)
773 CTZ(0x0000000001000000, 24)
774 CTO(0x0000000000ffffff, 24)
775 CLZ(0x0000000000800000, 40)
776 CLO(0xffffff0000000000, 24)
777 CTZ(0x0000000000800000, 23)
778 CTO(0x00000000007fffff, 23)
779 CLZ(0x0000000000400000, 41)
780 CLO(0xfffffe0000000000, 23)
781 CTZ(0x0000000000400000, 22)
782 CTO(0x00000000003fffff, 22)
783 CLZ(0x0000000000200000, 42)
784 CLO(0xfffffc0000000000, 22)
785 CTZ(0x0000000000200000, 21)
786 CTO(0x00000000001fffff, 21)
787 CLZ(0x0000000000100000, 43)
788 CLO(0xfffff80000000000, 21)
789 CTZ(0x0000000000100000, 20)
790 CTO(0x00000000000fffff, 20)
791 CLZ(0x0000000000080000, 44)
792 CLO(0xfffff00000000000, 20)
793 CTZ(0x0000000000080000, 19)
794 CTO(0x000000000007ffff, 19)
795 CLZ(0x0000000000040000, 45)
796 CLO(0xffffe00000000000, 19)
797 CTZ(0x0000000000040000, 18)
798 CTO(0x000000000003ffff, 18)
799 CLZ(0x0000000000020000, 46)
800 CLO(0xffffc00000000000, 18)
801 CTZ(0x0000000000020000, 17)
802 CTO(0x000000000001ffff, 17)
803 CLZ(0x0000000000010000, 47)
804 CLO(0xffff800000000000, 17)
805 CTZ(0x0000000000010000, 16)
806 CTO(0x000000000000ffff, 16)
807 CLZ(0x0000000000008000, 48)
808 CLO(0xffff000000000000, 16)
809 CTZ(0x0000000000008000, 15)
810 CTO(0x0000000000007fff, 15)
811 CLZ(0x0000000000004000, 49)
812 CLO(0xfffe000000000000, 15)
813 CTZ(0x0000000000004000, 14)
814 CTO(0x0000000000003fff, 14)
815 CLZ(0x0000000000002000, 50)
816 CLO(0xfffc000000000000, 14)
817 CTZ(0x0000000000002000, 13)
818 CTO(0x0000000000001fff, 13)
819 CLZ(0x0000000000001000, 51)
820 CLO(0xfff8000000000000, 13)
821 CTZ(0x0000000000001000, 12)
822 CTO(0x0000000000000fff, 12)
823 CLZ(0x0000000000000800, 52)
824 CLO(0xfff0000000000000, 12)
825 CTZ(0x0000000000000800, 11)
826 CTO(0x00000000000007ff, 11)
827 CLZ(0x0000000000000400, 53)
828 CLO(0xffe0000000000000, 11)
829 CTZ(0x0000000000000400, 10)
830 CTO(0x00000000000003ff, 10)
831 CLZ(0x0000000000000200, 54)
832 CLO(0xffc0000000000000, 10)
833 CTZ(0x0000000000000200, 9)
834 CTO(0x00000000000001ff, 9)
835 CLZ(0x0000000000000100, 55)
836 CLO(0xff80000000000000, 9)
837 CTZ(0x0000000000000100, 8)
838 CTO(0x00000000000000ff, 8)
839 CLZ(0x0000000000000080, 56)
840 CLO(0xff00000000000000, 8)
841 CTZ(0x0000000000000080, 7)
842 CTO(0x000000000000007f, 7)
843 CLZ(0x0000000000000040, 57)
844 CLO(0xfe00000000000000, 7)
845 CTZ(0x0000000000000040, 6)
846 CTO(0x000000000000003f, 6)
847 CLZ(0x0000000000000020, 58)
848 CLO(0xfc00000000000000, 6)
849 CTZ(0x0000000000000020, 5)
850 CTO(0x000000000000001f, 5)
851 CLZ(0x0000000000000010, 59)
852 CLO(0xf800000000000000, 5)
853 CTZ(0x0000000000000010, 4)
854 CTO(0x000000000000000f, 4)
855 CLZ(0x0000000000000008, 60)
856 CLO(0xf000000000000000, 4)
857 CTZ(0x0000000000000008, 3)
858 CTO(0x0000000000000007, 3)
859 CLZ(0x0000000000000004, 61)
860 CLO(0xe000000000000000, 3)
861 CTZ(0x0000000000000004, 2)
862 CTO(0x0000000000000003, 2)
863 CLZ(0x0000000000000002, 62)
864 CLO(0xc000000000000000, 2)
865 CTZ(0x0000000000000002, 1)
866 CTO(0x0000000000000001, 1)
867 CLZ(0x0000000000000001, 63)
868 CLO(0x8000000000000000, 1)
869 CTZ(0x0000000000000001, 0)
870 CTO(0x0000000000000000, 0)
871 CLZ(0x0000000000000000, 64)
872 CLO(0x0000000000000000, 0)
873 CTZ(0x0000000000000000, 64)
874 CTO(0xffffffffffffffff, 64)