lists.openwall.net   lists  /  announce  owl-users  owl-dev  john-users  john-dev  passwdqc-users  yescrypt  popa3d-users  /  oss-security  kernel-hardening  musl  sabotage  tlsify  passwords  /  crypt-dev  xvendor  /  Bugtraq  Full-Disclosure  linux-kernel  linux-netdev  linux-ext4  linux-hardening  linux-cve-announce  PHC 
Open Source and information security mailing list archives
 
Hash Suite: Windows password security audit tool. GUI, reports in PDF.
[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <20211026151451.7f3e09a4@gandalf.local.home>
Date:   Tue, 26 Oct 2021 15:14:51 -0400
From:   Steven Rostedt <rostedt@...dmis.org>
To:     Kalesh Singh <kaleshsingh@...gle.com>
Cc:     surenb@...gle.com, hridya@...gle.com, namhyung@...nel.org,
        kernel-team@...roid.com, Jonathan Corbet <corbet@....net>,
        Ingo Molnar <mingo@...hat.com>, Shuah Khan <shuah@...nel.org>,
        Masami Hiramatsu <mhiramat@...nel.org>,
        Tom Zanussi <zanussi@...nel.org>, linux-doc@...r.kernel.org,
        linux-kernel@...r.kernel.org, linux-kselftest@...r.kernel.org
Subject: Re: [PATCH v4 6/8] tracing/histogram: Optimize division by a power
 of 2

On Mon, 25 Oct 2021 13:08:38 -0700
Kalesh Singh <kaleshsingh@...gle.com> wrote:

> == Results ==
> 
> Divisor is a power of 2 (divisor == 32):
> 
>    test_hist_field_div_not_optimized  | 8,717,091 cpu-cycles
>    test_hist_field_div_optimized      | 1,643,137 cpu-cycles
> 
> If the divisor is a power of 2, the optimized version is ~5.3x faster.
> 
> Divisor is not a power of 2 (divisor == 33):
> 
>    test_hist_field_div_not_optimized  | 4,444,324 cpu-cycles
>    test_hist_field_div_optimized      | 5,497,958 cpu-cycles

To optimize this even more, if the divisor is constant, we could make a
separate function to not do the branch, and just shift or divide.

And even if it is not a power of 2, for constants, we could implement a
multiplication and shift, and guarantee an accuracy up to a defined max.


If div is a constant, then we can calculate the mult and shift, and max
dividend. Let's use 20 for shift.

	// This works best for small divisors
	if (div > max_div) {
		// only do a real division
		return;
	}
	shift = 20;
	mult = ((1 << shift) + div - 1) / div;
	delta = mult * div - (1 << shift);
	if (!delta) {
		/* div is a power of 2 */
		max = -1;
		return;
	}
	max = (1 << shift) / delta;

We would of course need to use 64 bit operations (maybe only do this for 64
bit machines). And perhaps even use bigger shift values to get a bigger max.

Then we could do:

	if (val1 < max)
		return (val1 * mult) >> shift;

-- Steve

Powered by blists - more mailing lists

Powered by Openwall GNU/*/Linux Powered by OpenVZ