@@ -602,74 +602,111 @@ ndigits(x::Integer; base::Integer=10, pad::Integer=1) = max(pad, ndigits0z(x, ba
602
602
# # integer to string functions ##
603
603
604
604
function bin (x:: Unsigned , pad:: Integer , neg:: Bool )
605
- i = neg + max (pad,sizeof (x)<< 3 - leading_zeros (x))
606
- a = StringVector (i)
605
+ m = 8 sizeof (x) - leading_zeros (x):: Int
606
+ n = neg + max ((pad % Int):: Int , m)
607
+ a = StringVector (n)
608
+ # for i in 0x0:UInt(n-1) # automatic vectorization produces redundant codes
609
+ # @inbounds a[n - i] = 0x30 + (((x >> i) % UInt8)::UInt8 & 0x1)
610
+ # end
611
+ i = n
612
+ @inbounds while i >= 4
613
+ b = UInt32 ((x % UInt8):: UInt8 )
614
+ d = 0x30303030 + ((b * 0x08040201 ) >> 0x3 ) & 0x01010101
615
+ a[i- 3 ] = (d >> 0x00 ) % UInt8
616
+ a[i- 2 ] = (d >> 0x08 ) % UInt8
617
+ a[i- 1 ] = (d >> 0x10 ) % UInt8
618
+ a[i] = (d >> 0x18 ) % UInt8
619
+ x >>= 0x4
620
+ i -= 4
621
+ end
607
622
while i > neg
608
- @inbounds a[i] = 48 + (x & 0x1 )
609
- x >>= 1
623
+ @inbounds a[i] = 0x30 + ((x % UInt8) :: UInt8 & 0x1 )
624
+ x >>= 0x1
610
625
i -= 1
611
626
end
612
627
if neg; @inbounds a[1 ]= 0x2d ; end
613
628
String (a)
614
629
end
615
630
616
631
function oct (x:: Unsigned , pad:: Integer , neg:: Bool )
617
- i = neg + max (pad,div ((sizeof (x)<< 3 )- leading_zeros (x)+ 2 ,3 ))
618
- a = StringVector (i)
632
+ m = div (8 sizeof (x) - leading_zeros (x):: Int + 2 , 3 )
633
+ n = neg + max ((pad % Int):: Int , m)
634
+ a = StringVector (n)
635
+ i = n
619
636
while i > neg
620
- @inbounds a[i] = 48 + (x & 0x7 )
621
- x >>= 3
637
+ @inbounds a[i] = 0x30 + ((x % UInt8) :: UInt8 & 0x7 )
638
+ x >>= 0x3
622
639
i -= 1
623
640
end
624
641
if neg; @inbounds a[1 ]= 0x2d ; end
625
642
String (a)
626
643
end
627
644
645
+ # 2-digit decimal characters ("00":"99")
646
+ const _dec_d100 = UInt16[(0x30 + i % 10 ) << 0x8 + (0x30 + i ÷ 10 ) for i = 0 : 99 ]
647
+
628
648
function dec (x:: Unsigned , pad:: Integer , neg:: Bool )
629
- i = neg + ndigits (x, base= 10 , pad= pad)
630
- a = StringVector (i)
631
- while i > neg
632
- @inbounds a[i] = 48 + rem (x,10 )
633
- x = oftype (x,div (x,10 ))
634
- i -= 1
649
+ n = neg + ndigits (x, base= 10 , pad= (pad % Int):: Int ):: Int
650
+ a = StringVector (n)
651
+ i = n
652
+ @inbounds while i >= 2
653
+ d, r = divrem (x, 0x64 )
654
+ d100 = _dec_d100[(r % Int):: Int + 1 ]
655
+ a[i- 1 ] = d100 % UInt8
656
+ a[i] = (d100 >> 0x8 ) % UInt8
657
+ x = oftype (x, d)
658
+ i -= 2
659
+ end
660
+ if i > neg
661
+ @inbounds a[i] = 0x30 + (rem (x, 0xa ) % UInt8):: UInt8
635
662
end
636
663
if neg; @inbounds a[1 ]= 0x2d ; end
637
664
String (a)
638
665
end
639
666
640
667
function hex (x:: Unsigned , pad:: Integer , neg:: Bool )
641
- i = neg + max (pad,(sizeof (x)<< 1 )- (leading_zeros (x)>> 2 ))
642
- a = StringVector (i)
643
- while i > neg
644
- d = x & 0xf
645
- @inbounds a[i] = 48 + d+ 39 * (d> 9 )
646
- x >>= 4
647
- i -= 1
668
+ m = 2 sizeof (x) - (leading_zeros (x):: Int >> 2 )
669
+ n = neg + max ((pad % Int):: Int , m)
670
+ a = StringVector (n)
671
+ i = n
672
+ while i >= 2
673
+ b = (x % UInt8):: UInt8
674
+ d1, d2 = b >> 0x4 , b & 0xf
675
+ @inbounds a[i- 1 ] = d1 + ifelse (d1 > 0x9 , 0x57 , 0x30 )
676
+ @inbounds a[i] = d2 + ifelse (d2 > 0x9 , 0x57 , 0x30 )
677
+ x >>= 0x8
678
+ i -= 2
679
+ end
680
+ if i > neg
681
+ d = (x % UInt8):: UInt8 & 0xf
682
+ @inbounds a[i] = d + ifelse (d > 0x9 , 0x57 , 0x30 )
648
683
end
649
684
if neg; @inbounds a[1 ]= 0x2d ; end
650
685
String (a)
651
686
end
652
687
653
- const base36digits = [' 0' :' 9' ;' a' :' z' ]
654
- const base62digits = [' 0' :' 9' ;' A' :' Z' ;' a' :' z' ]
688
+ const base36digits = UInt8 [' 0' :' 9' ;' a' :' z' ]
689
+ const base62digits = UInt8 [' 0' :' 9' ;' A' :' Z' ;' a' :' z' ]
655
690
656
- function _base (b:: Integer , x:: Integer , pad:: Integer , neg:: Bool )
657
- (x >= 0 ) | (b < 0 ) || throw (DomainError (x, " For negative `x`, `b` must be negative." ))
691
+ function _base (base:: Integer , x:: Integer , pad:: Integer , neg:: Bool )
692
+ b = (base % Int):: Int
693
+ (x >= 0 ) | (b < 0 ) || throw (DomainError (x, " For negative `x`, `base` must be negative." ))
658
694
2 <= abs (b) <= 62 || throw (DomainError (b, " base must satisfy 2 ≤ abs(base) ≤ 62" ))
659
695
digits = abs (b) <= 36 ? base36digits : base62digits
660
- i = neg + ndigits (x, base= b, pad= pad)
661
- a = StringVector (i)
696
+ n = neg + ndigits (x, base= b, pad= (pad % Int):: Int ):: Int
697
+ a = StringVector (n)
698
+ i = n
662
699
@inbounds while i > neg
663
700
if b > 0
664
- a[i] = digits[1 + rem (x,b) ]
701
+ a[i] = digits[1 + ( rem (x, b) % Int) :: Int ]
665
702
x = div (x,b)
666
703
else
667
- a[i] = digits[1 + mod (x,- b)]
704
+ a[i] = digits[1 + ( mod (x, - b) % Int) :: Int ]
668
705
x = cld (x,b)
669
706
end
670
707
i -= 1
671
708
end
672
- if neg; a[1 ]= ' - ' ; end
709
+ if neg; @inbounds a[1 ]= 0x2d ; end
673
710
String (a)
674
711
end
675
712
0 commit comments