diff mbox series

[RISU,v3,01/18] risugen_common: add helper functions insnv, randint

Message ID 20190711223300.6061-2-jan.bobek@gmail.com (mailing list archive)
State New, archived
Headers show
Series Support for generating x86 SIMD test images | expand

Commit Message

Jan Bobek July 11, 2019, 10:32 p.m. UTC
insnv allows emitting variable-length instructions in little-endian or
big-endian byte order; it subsumes functionality of former insn16()
and insn32() functions.

randint can reliably generate signed or unsigned integers of arbitrary
width.

Signed-off-by: Jan Bobek <jan.bobek@gmail.com>
---
 risugen_common.pm | 55 +++++++++++++++++++++++++++++++++++++++++------
 1 file changed, 48 insertions(+), 7 deletions(-)

Comments

Richard Henderson July 12, 2019, 5:48 a.m. UTC | #1
On 7/12/19 12:32 AM, Jan Bobek wrote:
> insnv allows emitting variable-length instructions in little-endian or
> big-endian byte order; it subsumes functionality of former insn16()
> and insn32() functions.
> 
> randint can reliably generate signed or unsigned integers of arbitrary
> width.
> 
> Signed-off-by: Jan Bobek <jan.bobek@gmail.com>
> ---
>  risugen_common.pm | 55 +++++++++++++++++++++++++++++++++++++++++------
>  1 file changed, 48 insertions(+), 7 deletions(-)
> 
> diff --git a/risugen_common.pm b/risugen_common.pm
> index 71ee996..d63250a 100644
> --- a/risugen_common.pm
> +++ b/risugen_common.pm
> @@ -23,8 +23,9 @@ BEGIN {
>      require Exporter;
>  
>      our @ISA = qw(Exporter);
> -    our @EXPORT = qw(open_bin close_bin set_endian insn32 insn16 $bytecount
> -                   progress_start progress_update progress_end
> +    our @EXPORT = qw(open_bin close_bin set_endian insn32 insn16
> +                   $bytecount insnv randint progress_start
> +                   progress_update progress_end
>                     eval_with_fields is_pow_of_2 sextract ctz
>                     dump_insn_details);
>  }
> @@ -37,7 +38,7 @@ my $bigendian = 0;
>  # (default is little endian, 0).
>  sub set_endian
>  {
> -    $bigendian = @_;
> +    ($bigendian) = @_;
>  }
>  
>  sub open_bin
> @@ -52,18 +53,58 @@ sub close_bin
>      close(BIN) or die "can't close output file: $!";
>  }
>  
> +sub insnv(%)
> +{
> +    my (%args) = @_;
> +
> +    # Default to big-endian order, so that the instruction bytes are
> +    # emitted in the same order as they are written in the
> +    # configuration file.
> +    $args{bigendian} = 1 unless defined $args{bigendian};
> +
> +    for (my $bitcur = 0; $bitcur < $args{width}; $bitcur += 8) {
> +        my $value = $args{value} >> ($args{bigendian}
> +                                     ? $args{width} - $bitcur - 8
> +                                     : $bitcur);
> +
> +        print BIN pack("C", $value & 0xff);
> +        $bytecount += 1;
> +    }

Looks like bytecount is no longer used?

Otherwise,
Reviewed-by: Richard Henderson <richard.henderson@linaro.org>


r~
Alex Bennée July 12, 2019, 12:41 p.m. UTC | #2
Jan Bobek <jan.bobek@gmail.com> writes:

> insnv allows emitting variable-length instructions in little-endian or
> big-endian byte order; it subsumes functionality of former insn16()
> and insn32() functions.
>
> randint can reliably generate signed or unsigned integers of arbitrary
> width.
>
> Signed-off-by: Jan Bobek <jan.bobek@gmail.com>

Reviewed-by: Alex Bennée <alex.bennee@linaro.org>

> ---
>  risugen_common.pm | 55 +++++++++++++++++++++++++++++++++++++++++------
>  1 file changed, 48 insertions(+), 7 deletions(-)
>
> diff --git a/risugen_common.pm b/risugen_common.pm
> index 71ee996..d63250a 100644
> --- a/risugen_common.pm
> +++ b/risugen_common.pm
> @@ -23,8 +23,9 @@ BEGIN {
>      require Exporter;
>
>      our @ISA = qw(Exporter);
> -    our @EXPORT = qw(open_bin close_bin set_endian insn32 insn16 $bytecount
> -                   progress_start progress_update progress_end
> +    our @EXPORT = qw(open_bin close_bin set_endian insn32 insn16
> +                   $bytecount insnv randint progress_start
> +                   progress_update progress_end
>                     eval_with_fields is_pow_of_2 sextract ctz
>                     dump_insn_details);
>  }
> @@ -37,7 +38,7 @@ my $bigendian = 0;
>  # (default is little endian, 0).
>  sub set_endian
>  {
> -    $bigendian = @_;
> +    ($bigendian) = @_;
>  }
>
>  sub open_bin
> @@ -52,18 +53,58 @@ sub close_bin
>      close(BIN) or die "can't close output file: $!";
>  }
>
> +sub insnv(%)
> +{
> +    my (%args) = @_;
> +
> +    # Default to big-endian order, so that the instruction bytes are
> +    # emitted in the same order as they are written in the
> +    # configuration file.
> +    $args{bigendian} = 1 unless defined $args{bigendian};
> +
> +    for (my $bitcur = 0; $bitcur < $args{width}; $bitcur += 8) {
> +        my $value = $args{value} >> ($args{bigendian}
> +                                     ? $args{width} - $bitcur - 8
> +                                     : $bitcur);
> +
> +        print BIN pack("C", $value & 0xff);
> +        $bytecount += 1;
> +    }
> +}
> +
>  sub insn32($)
>  {
>      my ($insn) = @_;
> -    print BIN pack($bigendian ? "N" : "V", $insn);
> -    $bytecount += 4;
> +    insnv(value => $insn, width => 32, bigendian => $bigendian);
>  }
>
>  sub insn16($)
>  {
>      my ($insn) = @_;
> -    print BIN pack($bigendian ? "n" : "v", $insn);
> -    $bytecount += 2;
> +    insnv(value => $insn, width => 16, bigendian => $bigendian);
> +}
> +
> +sub randint
> +{
> +    my (%args) = @_;
> +    my $width = $args{width};
> +
> +    if ($width > 32) {
> +        # Generate at most 32 bits at once; Perl's rand() does not
> +        # behave well with ranges that are too large.
> +        my $lower = randint(%args, width => 32);
> +        my $upper = randint(%args, width => $args{width} - 32);
> +        # Use arithmetic rather than bitwise operators, since bitwise
> +        # ops turn signed integers into unsigned.
> +        return $upper * (1 << 32) + $lower;
> +    } elsif ($width > 0) {
> +        my $halfrange = 1 << ($width - 1);
> +        my $value = int(rand(2 * $halfrange));
> +        $value -= $halfrange if defined $args{signed} && $args{signed};
> +        return $value;
> +    } else {
> +        return 0;
> +    }
>  }
>
>  # Progress bar implementation


--
Alex Bennée
Jan Bobek July 14, 2019, 9:55 p.m. UTC | #3
On 7/12/19 1:48 AM, Richard Henderson wrote:
> On 7/12/19 12:32 AM, Jan Bobek wrote:
>> insnv allows emitting variable-length instructions in little-endian or
>> big-endian byte order; it subsumes functionality of former insn16()
>> and insn32() functions.
>>
>> randint can reliably generate signed or unsigned integers of arbitrary
>> width.
>>
>> Signed-off-by: Jan Bobek <jan.bobek@gmail.com>
>> ---
>>  risugen_common.pm | 55 +++++++++++++++++++++++++++++++++++++++++------
>>  1 file changed, 48 insertions(+), 7 deletions(-)
>>
>> diff --git a/risugen_common.pm b/risugen_common.pm
>> index 71ee996..d63250a 100644
>> --- a/risugen_common.pm
>> +++ b/risugen_common.pm
>> @@ -23,8 +23,9 @@ BEGIN {
>>      require Exporter;
>>  
>>      our @ISA = qw(Exporter);
>> -    our @EXPORT = qw(open_bin close_bin set_endian insn32 insn16 $bytecount
>> -                   progress_start progress_update progress_end
>> +    our @EXPORT = qw(open_bin close_bin set_endian insn32 insn16
>> +                   $bytecount insnv randint progress_start
>> +                   progress_update progress_end
>>                     eval_with_fields is_pow_of_2 sextract ctz
>>                     dump_insn_details);
>>  }
>> @@ -37,7 +38,7 @@ my $bigendian = 0;
>>  # (default is little endian, 0).
>>  sub set_endian
>>  {
>> -    $bigendian = @_;
>> +    ($bigendian) = @_;
>>  }
>>  
>>  sub open_bin
>> @@ -52,18 +53,58 @@ sub close_bin
>>      close(BIN) or die "can't close output file: $!";
>>  }
>>  
>> +sub insnv(%)
>> +{
>> +    my (%args) = @_;
>> +
>> +    # Default to big-endian order, so that the instruction bytes are
>> +    # emitted in the same order as they are written in the
>> +    # configuration file.
>> +    $args{bigendian} = 1 unless defined $args{bigendian};
>> +
>> +    for (my $bitcur = 0; $bitcur < $args{width}; $bitcur += 8) {
>> +        my $value = $args{value} >> ($args{bigendian}
>> +                                     ? $args{width} - $bitcur - 8
>> +                                     : $bitcur);
>> +
>> +        print BIN pack("C", $value & 0xff);
>> +        $bytecount += 1;
>> +    }
> 
> Looks like bytecount is no longer used?

bytecount is an exported variable, a quick git grep shows that
it's being used in risugen_arm.pm (sub thumb_align4).

> Otherwise,
> Reviewed-by: Richard Henderson <richard.henderson@linaro.org>
> 
> 
> r~
>
diff mbox series

Patch

diff --git a/risugen_common.pm b/risugen_common.pm
index 71ee996..d63250a 100644
--- a/risugen_common.pm
+++ b/risugen_common.pm
@@ -23,8 +23,9 @@  BEGIN {
     require Exporter;
 
     our @ISA = qw(Exporter);
-    our @EXPORT = qw(open_bin close_bin set_endian insn32 insn16 $bytecount
-                   progress_start progress_update progress_end
+    our @EXPORT = qw(open_bin close_bin set_endian insn32 insn16
+                   $bytecount insnv randint progress_start
+                   progress_update progress_end
                    eval_with_fields is_pow_of_2 sextract ctz
                    dump_insn_details);
 }
@@ -37,7 +38,7 @@  my $bigendian = 0;
 # (default is little endian, 0).
 sub set_endian
 {
-    $bigendian = @_;
+    ($bigendian) = @_;
 }
 
 sub open_bin
@@ -52,18 +53,58 @@  sub close_bin
     close(BIN) or die "can't close output file: $!";
 }
 
+sub insnv(%)
+{
+    my (%args) = @_;
+
+    # Default to big-endian order, so that the instruction bytes are
+    # emitted in the same order as they are written in the
+    # configuration file.
+    $args{bigendian} = 1 unless defined $args{bigendian};
+
+    for (my $bitcur = 0; $bitcur < $args{width}; $bitcur += 8) {
+        my $value = $args{value} >> ($args{bigendian}
+                                     ? $args{width} - $bitcur - 8
+                                     : $bitcur);
+
+        print BIN pack("C", $value & 0xff);
+        $bytecount += 1;
+    }
+}
+
 sub insn32($)
 {
     my ($insn) = @_;
-    print BIN pack($bigendian ? "N" : "V", $insn);
-    $bytecount += 4;
+    insnv(value => $insn, width => 32, bigendian => $bigendian);
 }
 
 sub insn16($)
 {
     my ($insn) = @_;
-    print BIN pack($bigendian ? "n" : "v", $insn);
-    $bytecount += 2;
+    insnv(value => $insn, width => 16, bigendian => $bigendian);
+}
+
+sub randint
+{
+    my (%args) = @_;
+    my $width = $args{width};
+
+    if ($width > 32) {
+        # Generate at most 32 bits at once; Perl's rand() does not
+        # behave well with ranges that are too large.
+        my $lower = randint(%args, width => 32);
+        my $upper = randint(%args, width => $args{width} - 32);
+        # Use arithmetic rather than bitwise operators, since bitwise
+        # ops turn signed integers into unsigned.
+        return $upper * (1 << 32) + $lower;
+    } elsif ($width > 0) {
+        my $halfrange = 1 << ($width - 1);
+        my $value = int(rand(2 * $halfrange));
+        $value -= $halfrange if defined $args{signed} && $args{signed};
+        return $value;
+    } else {
+        return 0;
+    }
 }
 
 # Progress bar implementation