From nobody Mon Feb 9 15:08:27 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=gmail.com ARC-Seal: i=1; a=rsa-sha256; t=1561956082; cv=none; d=zoho.com; s=zohoarc; b=D/R7nXgB5LzzGSxpC2CmPGHabe6VJAvZyfTdarQ+ujF47SaJBSd+tQ04B5THpa+NCAGyRlYcg2YZ9dZqiAJFERH7Ie/vF5kNEfgEqzuXZZMkZf9pgFPZypb+L4VW7jYvqUx25eXVaWfGYB5om2NAVxZLOIm21tqJqtSEL6FX61k= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zoho.com; s=zohoarc; t=1561956082; h=Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To:ARC-Authentication-Results; bh=liyGMfmhycaZ3/V3dGPSjjHCU+qnWveeLZ45quM65dU=; b=gOR8buk/vJc31QnScSZhOcG0P9RI6s3rh/I/rqvTQdqhEWU8T1jw6/DN6l4WW/0BL8wSg0ellWi6SfojviSg8FvM/c2oYp9bnJrhS9M+htZJ3/ECPckCqBrlpmx6T2R095o08RKu62+RYXadGiOgTBDL+aDekqi/dsH6NXG7E84= ARC-Authentication-Results: i=1; mx.zoho.com; dkim=fail; spf=pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1561956082433390.0392270851895; Sun, 30 Jun 2019 21:41:22 -0700 (PDT) Received: from localhost ([::1]:47676 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.86_2) (envelope-from ) id 1hho81-0001ei-G3 for importer@patchew.org; Mon, 01 Jul 2019 00:41:21 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:56611) by lists.gnu.org with esmtp (Exim 4.86_2) (envelope-from ) id 1hho2n-0004tE-MU for qemu-devel@nongnu.org; Mon, 01 Jul 2019 00:35:58 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1hho2m-0004Tu-H2 for qemu-devel@nongnu.org; Mon, 01 Jul 2019 00:35:57 -0400 Received: from mail-yb1-xb42.google.com ([2607:f8b0:4864:20::b42]:37617) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1hho2m-0004TY-C6 for qemu-devel@nongnu.org; Mon, 01 Jul 2019 00:35:56 -0400 Received: by mail-yb1-xb42.google.com with SMTP id l22so3629246ybf.4 for ; Sun, 30 Jun 2019 21:35:56 -0700 (PDT) Received: from dionysus.attlocal.net (69-222-133-165.lightspeed.tukrga.sbcglobal.net. [69.222.133.165]) by smtp.gmail.com with ESMTPSA id k126sm1108564ywf.36.2019.06.30.21.35.55 (version=TLS1_3 cipher=AEAD-AES256-GCM-SHA384 bits=256/256); Sun, 30 Jun 2019 21:35:55 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=liyGMfmhycaZ3/V3dGPSjjHCU+qnWveeLZ45quM65dU=; b=AW8Krlrm9cwM40wtC/fhxqMgCOxlAIAJV7GOu3ZOkLkJyaRgxOdygv+yLyplgnzjJk sm1AFgR+DKzamouOn6edeQjnMGWVF4DQmZrvYoWbiKXtyWrA3T85Hy4M2rWRD2fQeAXX e2t+JSQMii/j1TN0m6gXCV+hmXH5vSXom3kLpWuq7rZu6AWcd/yapqDgdzBfxc3UQbbu 0gttVwyC5ydVGedX3TsH0wYcovwE0IQKXD3MM/sGWmMR+4zOmerhAhXyi0j0cFMR9v9/ 1ktxcICC8oeCUa8q7Cqm3hPLBuZJA2Rsi0wjaqStEGAFRNmr1Pf/TkDttOOxM5x3G5VW 6BZw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=liyGMfmhycaZ3/V3dGPSjjHCU+qnWveeLZ45quM65dU=; b=H8Anx4QpZ5663oYw29PFKiJgLkniYIkhgNx/dwukycq00Dc7FwSntXlU3QrIyXP6r7 eD1K1inbLdJ9AG7XNY/kL9gDByUxVvLwmsbfVdO6v74fMLsBs3s83B6A8Hw3ou3qXLUP JSGXiKk2ktqt9vNXhRgizkm1vULA3A6GLdADOF0U/PnQnYMDZjNzn0Cyp1AGwuAl2NQG mvDNA2chuEhpk68nSxz3z8pPZDlTAl/st4aVBKUmN5pQNyPtDSkBEDOPoPFQ21EshXp5 Mk6+R9FLKrnTLEXUOhg8Y+3l0ofgfLGEJU+dEMnrlFChOXdwSEKeWbGZIoNzGIFyxamD e/MQ== X-Gm-Message-State: APjAAAV4ZtmqCo6Am9v9plXnoHLFPipHsLudDKMINNr/01CAa1sKk8yx 21Tbzn0cPsbGVjzFcoaRxNll853Z X-Google-Smtp-Source: APXvYqzDIUYPTSC0Jt3PQFx7dqDr/C/2oCHyL42kGXdGB9tQzioCj/guBZp9a5BDlO5cadi0/j4DmA== X-Received: by 2002:a25:aab0:: with SMTP id t45mr14038514ybi.201.1561955755659; Sun, 30 Jun 2019 21:35:55 -0700 (PDT) From: Jan Bobek To: qemu-devel@nongnu.org Date: Mon, 1 Jul 2019 00:35:23 -0400 Message-Id: <20190701043536.26019-2-jan.bobek@gmail.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20190701043536.26019-1-jan.bobek@gmail.com> References: <20190701043536.26019-1-jan.bobek@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2607:f8b0:4864:20::b42 Subject: [Qemu-devel] [RISU RFC PATCH v2 01/14] risugen_common: add insnv, randint_constr, rand_fill X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Jan Bobek , =?UTF-8?q?Alex=20Benn=C3=A9e?= , Richard Henderson Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Type: text/plain; charset="utf-8" Add three common utility functions: - insnv allows emitting variable-length instructions in little-endian or big-endian byte order; it subsumes functionality of former insn16() and insn32() functions. - randint_constr allows generating random integers according to several constraints passed as arguments. - rand_fill uses randint_constr to fill a given hash with (optionally constrained) random values. Signed-off-by: Jan Bobek Reviewed-by: Richard Henderson --- risugen_common.pm | 107 +++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 101 insertions(+), 6 deletions(-) diff --git a/risugen_common.pm b/risugen_common.pm index 71ee996..c5d861e 100644 --- a/risugen_common.pm +++ b/risugen_common.pm @@ -23,7 +23,8 @@ BEGIN { require Exporter; =20 our @ISA =3D qw(Exporter); - our @EXPORT =3D qw(open_bin close_bin set_endian insn32 insn16 $byteco= unt + our @EXPORT =3D qw(open_bin close_bin set_endian insn32 insn16 + $bytecount insnv randint_constr rand_fill progress_start progress_update progress_end eval_with_fields is_pow_of_2 sextract ctz dump_insn_details); @@ -37,7 +38,7 @@ my $bigendian =3D 0; # (default is little endian, 0). sub set_endian { - $bigendian =3D @_; + ($bigendian) =3D @_; } =20 sub open_bin @@ -52,18 +53,112 @@ sub close_bin close(BIN) or die "can't close output file: $!"; } =20 +sub insnv(%) +{ + my (%args) =3D @_; + + # 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} =3D 1 unless defined $args{bigendian}; + + my $bitcur =3D 0; + my $bitend =3D 8 * $args{len}; + while ($bitcur < $bitend) { + my $format; + my $bitlen; + + if ($bitcur + 64 <=3D $bitend) { + $format =3D "Q"; + $bitlen =3D 64; + } elsif ($bitcur + 32 <=3D $bitend) { + $format =3D "L"; + $bitlen =3D 32; + } elsif ($bitcur + 16 <=3D $bitend) { + $format =3D "S"; + $bitlen =3D 16; + } else { + $format =3D "C"; + $bitlen =3D 8; + } + + $format .=3D ($args{bigendian} ? ">" : "<") if $bitlen > 8; + + my $bitmask =3D (1 << $bitlen) - 1; + my $value =3D $args{value} >> ($args{bigendian} + ? $bitend - $bitcur - $bitlen + : $bitcur); + + print BIN pack($format, $value & $bitmask); + $bytecount +=3D $bitlen / 8; + + $bitcur +=3D $bitlen; + } +} + sub insn32($) { my ($insn) =3D @_; - print BIN pack($bigendian ? "N" : "V", $insn); - $bytecount +=3D 4; + insnv(value =3D> $insn, len =3D> 4, bigendian =3D> $bigendian); } =20 sub insn16($) { my ($insn) =3D @_; - print BIN pack($bigendian ? "n" : "v", $insn); - $bytecount +=3D 2; + insnv(value =3D> $insn, len =3D> 2, bigendian =3D> $bigendian); +} + +sub randint_constr(%) +{ + my (%args) =3D @_; + my $bitlen =3D $args{bitlen}; + my $halfrange =3D 1 << ($bitlen - 1); + + while (1) { + my $value =3D int(rand(2 * $halfrange)); + $value -=3D $halfrange if defined $args{signed} && $args{signed}; + $value &=3D ~$args{fixedbitmask} if defined $args{fixedbitmask}; + $value |=3D $args{fixedbits} if defined $args{fixedbits}; + + if (defined $args{constraint}) { + # The idea is: if the most significant bit of + # $args{constraint} is zero, $args{constraint} is the + # value we want to return; if the most significant bit is + # one, ~$args{constraint} (its bit inversion) is the value + # we want to *avoid*, so we try again. + + if (!($args{constraint} >> 63)) { + $value =3D $args{constraint}; + } elsif ($value =3D=3D ~$args{constraint}) { + next; + } + } + + return $value; + } +} + +sub rand_fill($$) +{ + my ($target, $constraints) =3D @_; + + for (keys %{$target}) { + my %args =3D (bitlen =3D> $target->{$_}{bitlen}); + + $args{fixedbits} =3D $target->{$_}{fixedbits} + if defined $target->{$_}{fixedbits}; + $args{fixedbitmask} =3D $target->{$_}{fixedbitmask} + if defined $target->{$_}{fixedbitmask}; + $args{signed} =3D $target->{$_}{signed} + if defined $target->{$_}{signed}; + + $args{constraint} =3D $constraints->{$_} + if defined $constraints->{$_}; + + $target->{$_} =3D randint_constr(%args); + } + + return $target; } =20 # Progress bar implementation --=20 2.20.1