From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from zimbra.cs.ucla.edu (zimbra.cs.ucla.edu [131.179.128.68]) by sourceware.org (Postfix) with ESMTPS id 97B7F3858D33 for ; Thu, 28 Jul 2022 20:35:09 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 97B7F3858D33 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=cs.ucla.edu Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=cs.ucla.edu Received: from localhost (localhost [127.0.0.1]) by zimbra.cs.ucla.edu (Postfix) with ESMTP id 9D1A31600D4; Thu, 28 Jul 2022 13:35:08 -0700 (PDT) Received: from zimbra.cs.ucla.edu ([127.0.0.1]) by localhost (zimbra.cs.ucla.edu [127.0.0.1]) (amavisd-new, port 10032) with ESMTP id NBror4HMa1qJ; Thu, 28 Jul 2022 13:35:08 -0700 (PDT) Received: from localhost (localhost [127.0.0.1]) by zimbra.cs.ucla.edu (Postfix) with ESMTP id F307F1600D9; Thu, 28 Jul 2022 13:35:07 -0700 (PDT) X-Virus-Scanned: amavisd-new at zimbra.cs.ucla.edu Received: from zimbra.cs.ucla.edu ([127.0.0.1]) by localhost (zimbra.cs.ucla.edu [127.0.0.1]) (amavisd-new, port 10026) with ESMTP id AkrCbIHsppGi; Thu, 28 Jul 2022 13:35:07 -0700 (PDT) Received: from [192.168.1.9] (cpe-172-91-119-151.socal.res.rr.com [172.91.119.151]) by zimbra.cs.ucla.edu (Postfix) with ESMTPSA id CCABE1600D4; Thu, 28 Jul 2022 13:35:07 -0700 (PDT) Message-ID: Date: Thu, 28 Jul 2022 13:35:07 -0700 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101 Thunderbird/91.11.0 Content-Language: en-US To: Adhemerval Zanella References: <20220728124528.39169-1-adhemerval.zanella@linaro.org> From: Paul Eggert Organization: UCLA Computer Science Department Cc: libc-alpha@sourceware.org Subject: Re: [PATCH] stdlib: Simplify arc4random_uniform In-Reply-To: <20220728124528.39169-1-adhemerval.zanella@linaro.org> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-3.3 required=5.0 tests=BAYES_00, KAM_DMARC_STATUS, NICE_REPLY_A, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 28 Jul 2022 20:35:11 -0000 On 7/28/22 05:45, Adhemerval Zanella via Libc-alpha wrote: > It uses the bitmask with rejection [1], which calculates a mask > being the lowest power of two bounding the request upper bound, > successively queries new random values, and rejects values > outside the requested range. > > Performance-wise, there is no much gain in trying to converse converse -> conserve > bits since arc4random is wrapper on getrandom syscall. It should > be cheaper to just query a uint32_t value. The algorithm also > avoids mudulo and divide operations, which might be costly mudulo -> modulo > + int bits_left = z; > + while (bits_left >= bits) > + { ... > + bits_left -= bits; > + } Clearer would be "for (int bits_left = z; bits_left >= bits; bits_left -= bits)". Looks good otherwise; thanks.