Received: by 2002:ac0:bc90:0:0:0:0:0 with SMTP id a16csp2614765img; Sun, 24 Mar 2019 13:48:54 -0700 (PDT) X-Google-Smtp-Source: APXvYqzX3Kj2oS1Y8q0qhS9JT7GBtkujIT34lUTFD5o3H+ROV/1/vZtSPuk4QI1aWK6qHjlT1HOs X-Received: by 2002:a63:1060:: with SMTP id 32mr20008015pgq.126.1553460534019; Sun, 24 Mar 2019 13:48:54 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1553460534; cv=none; d=google.com; s=arc-20160816; b=ijIg71SV56WIxKICmW5xW7SaKA/6W9yfqaxCtUUfw1Gq1uwRoYNXx323oN8AUnrwhI DaxfZfFgbS+CsDljfci0Fk/okd+9In7pW8DEvag1ZUgreZdM4MWJJgA86q7XwNZKEXgE 8hy4TXQzYnU8Q9KqY4vrAvRn3Gq5QwaK36IkV5xSn3knodYlARJtfC99sGausECceEQP PSGbGSI6PN9u3M43lhwRH1pFpuz5lY0XWM6VjVjY5HNBXXgCybDVNAFChA4CiKzmHuuG pNGB0QnSQLMWiYBMYDjtbIXRTC6PLD932PoPU1UTYuPZnGdIl9AHU4rix/rzGlVBHlpY LDZw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:dkim-signature; bh=PsKs0zryp5EFYqTFh7fd44lSi5JHRMFjPXkNHBb+aJI=; b=r53UGe3qP7t0iiteLoy2sq95tUQyMj9guf+q9d6B+GRfDgvZOg1wq1MiqU5v7ivJ1/ BkHlydeAFVwJQbNsEQKQ69SjKYEujyJUri2DXlCE2hQsL/nSEwbQRqu9oOldTk7ROP7V MbLT4sL5M6Vd0CZcWpUiFdeLt7cLkaKdiXmCpWgQb64ploLUdDSsEBlRYbPlnvyfh9JA bRuf4oTsxeXxaDDoDqLpxGAxpA69P/xa9+PSVs5ZTX/Q2Er/3jdzsE2vhSOetG/6A5RW LIbsWNyyy9qbIoSDy6oUevmfXDs3b+5RuVFIdfdOO3fPGFMX4EpqHHqXsdZiz/ukhoED KtVw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@zx2c4.com header.s=mail header.b=mZ81QrQc; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=zx2c4.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id r17si11380476pgv.328.2019.03.24.13.48.38; Sun, 24 Mar 2019 13:48:54 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@zx2c4.com header.s=mail header.b=mZ81QrQc; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=zx2c4.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727275AbfCXUsE (ORCPT + 99 others); Sun, 24 Mar 2019 16:48:04 -0400 Received: from frisell.zx2c4.com ([192.95.5.64]:42799 "EHLO frisell.zx2c4.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726317AbfCXUsE (ORCPT ); Sun, 24 Mar 2019 16:48:04 -0400 Received: by frisell.zx2c4.com (ZX2C4 Mail Server) with ESMTP id 34d5742e for ; Sun, 24 Mar 2019 20:25:59 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=zx2c4.com; h=mime-version :references:in-reply-to:from:date:message-id:subject:to:cc :content-type; s=mail; bh=LixV6Hl3KLHD9vhRxzJjKZmKu28=; b=mZ81Qr QciJXXmPLvB08EvMdrD0E+9yTLxM3+sHXd+c2KHtNUdEK1vrTVMLruAu19fMNQFC zyXi33bxNI8CWLMcJlLeyqsfbDpKLwbvBM5Q/SVO839ycEmjSpaIGc+Xpc0DvWH2 wHHsXgzMYOdi3pRRhp31CQ36GQNcuR6jp+jOR0q1E8IAdLOAAS0M63w1r/PUpTen mLisvjhPXWQvPlZbh3u4auai0+3SIQ77+T6w+H0SHuk3ULRfCtn33lpwdRkMecON JmmnGksCuRqMXNqO79vsmtfu0M55nzP22j/et1tWuDxtR59UiEf0xXckvMgDSkOI bA5+sb2Xqx5QWiPw== Received: by frisell.zx2c4.com (ZX2C4 Mail Server) with ESMTPSA id 021fd5ec (TLSv1.2:ECDHE-RSA-AES256-GCM-SHA384:256:NO) for ; Sun, 24 Mar 2019 20:25:59 +0000 (UTC) Received: by mail-oi1-f177.google.com with SMTP id t206so5419008oib.3 for ; Sun, 24 Mar 2019 13:48:02 -0700 (PDT) X-Gm-Message-State: APjAAAVQIUkArVOiUoni3YwJOvaOSczO+DJHRxwr4nDov3BIADZx6Kem LPY6Bd8oC2VD8sM+I2XbtwjKgJ99Aa5FWmJ7jwU= X-Received: by 2002:aca:550c:: with SMTP id j12mr9696195oib.52.1553460481491; Sun, 24 Mar 2019 13:48:01 -0700 (PDT) MIME-Version: 1.0 References: <201903241244.x2OCiL8P011277@sdf.org> In-Reply-To: <201903241244.x2OCiL8P011277@sdf.org> From: "Jason A. Donenfeld" Date: Sun, 24 Mar 2019 21:47:50 +0100 X-Gmail-Original-Message-ID: Message-ID: Subject: Re: [RFC PATCH] random: add get_random_max() function To: George Spelvin Cc: LKML , "Theodore Ts'o" Content-Type: text/plain; charset="UTF-8" Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org I generally use a slightly simpler algorithm in various different projects: //[0, bound) static unsigned long random_bounded(unsigned long bound) { unsigned long ret; const unsigned long max_mod_bound = (1 + ~bound) % bound; if (bound < 2) return 0; do ret = random_integer(); while (ret < max_mod_bound); return ret % bound; } //[min, max_plus_one) static unsigned long random_range(unsigned long min, unsigned long max_plus_one) { return random_bounded(max_plus_one - min) + min; } Is the motivation behind using Lemire that you avoid the division (via the modulo) in favor of a multiplication?