Skip to content

Commit

Permalink
Merge pull request #1021 from MasterDuke17/optimize_string_to_bigint
Browse files Browse the repository at this point in the history
Optimize Actions.(dec|hex|oct|bin)int()
  • Loading branch information
lizmat committed Feb 24, 2017
2 parents 320c2fb + b61b3c7 commit d41b68e
Showing 1 changed file with 35 additions and 6 deletions.
41 changes: 35 additions & 6 deletions src/Perl6/Actions.nqp
Expand Up @@ -7,6 +7,8 @@ use QAST;

my $wantwant := Mu;

my int $?BITS := nqp::iseq_i(0x1ffffffff,8589934591) ?? 64 !! 32;

sub block_closure($code) {
my $closure := QAST::Op.new(
:op('callmethod'), :name('clone'),
Expand Down Expand Up @@ -631,10 +633,17 @@ class Perl6::Actions is HLL::Actions does STDActions {
}
}

sub string_to_bigint($src, $base) {
sub string_to_int($src, int $base, int $chars) {
my $res := nqp::radix($base, ~$src, 0, 2);
$src.CURSOR.panic("'$src' is not a valid number")
unless nqp::iseq_i(nqp::atpos($res, 2), $chars);
nqp::box_i(nqp::atpos($res, 0), $*W.find_symbol(['Int']));
}

sub string_to_bigint($src, int $base, int $chars) {
my $res := nqp::radix_I($base, ~$src, 0, 2, $*W.find_symbol(['Int']));
$src.CURSOR.panic("'$src' is not a valid number")
unless nqp::iseq_i(nqp::unbox_i(nqp::atpos($res, 2)), nqp::chars($src));
unless nqp::iseq_i(nqp::unbox_i(nqp::atpos($res, 2)), $chars);
nqp::atpos($res, 0);
}

Expand Down Expand Up @@ -7263,10 +7272,30 @@ class Perl6::Actions is HLL::Actions does STDActions {
make QAST::WVal.new( :value($v) );
}

method decint($/) { make string_to_bigint( $/, 10); }
method hexint($/) { make string_to_bigint( $/, 16); }
method octint($/) { make string_to_bigint( $/, 8 ); }
method binint($/) { make string_to_bigint( $/, 2 ); }
method decint($/) {
my int $chars := nqp::chars($/);
make $chars > ($?BITS == 64 ?? 16 !! 9)
?? string_to_bigint($/, 10, $chars)
!! string_to_int($/, 10, $chars);
}
method hexint($/) {
my int $chars := nqp::chars($/);
make $chars > ($?BITS == 64 ?? 14 !! 8)
?? string_to_bigint($/, 16, $chars)
!! string_to_int($/, 16, $chars);
}
method octint($/) {
my int $chars := nqp::chars($/);
make $chars > ($?BITS == 64 ?? 20 !! 10)
?? string_to_bigint($/, 8, $chars)
!! string_to_int($/, 8, $chars);
}
method binint($/) {
my int $chars := nqp::chars($/);
make $chars > ($?BITS == 64 ?? 62 !! 30)
?? string_to_bigint($/, 2, $chars)
!! string_to_int($/, 2, $chars);
}

method number:sym<numish>($/) {
make $<numish>.ast;
Expand Down

0 comments on commit d41b68e

Please sign in to comment.