xref: /netbsd-src/share/man/man3/fast_divide32.3 (revision 17512a6a3373125d0368137df06da0a2d4ac9e5a)
1.\"	$NetBSD: fast_divide32.3,v 1.10 2024/09/07 20:33:53 rillig Exp $
2.\"
3.\" Copyright (c) 2010 The NetBSD Foundation, Inc.
4.\" All rights reserved.
5.\"
6.\" This code is derived from software contributed to The NetBSD Foundation
7.\" by Joerg Sonnenberger.
8.\"
9.\" Redistribution and use in source and binary forms, with or without
10.\" modification, are permitted provided that the following conditions
11.\" are met:
12.\" 1. Redistributions of source code must retain the above copyright
13.\"    notice, this list of conditions and the following disclaimer.
14.\" 2. Redistributions in binary form must reproduce the above copyright
15.\"    notice, this list of conditions and the following disclaimer in the
16.\"    documentation and/or other materials provided with the distribution.
17.\"
18.\" THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
19.\" ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
20.\" TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21.\" PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
22.\" BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23.\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24.\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25.\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26.\" CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27.\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28.\" POSSIBILITY OF SUCH DAMAGE.
29.\"
30.Dd May 10, 2011
31.Dt FAST_DIVIDE32 3
32.Os
33.Sh NAME
34.Nm fast_divide32 ,
35.Nm fast_divide32_prepare ,
36.Nm fast_remainder32
37.Nd fast 32bit division and remainder
38.Sh SYNOPSIS
39.In sys/bitops.h
40.Ft uint32_t
41.Fn fast_divide32 "uint32_t v" "uint32_t div" "uint32_t m" "uint8_t s1" \
42"uint8_t s2"
43.Ft uint32_t
44.Fn fast_remainder32 "uint32_t v" "uint32_t div" "uint32_t m" "uint8_t s1" \
45"uint8_t s2"
46.Ft void
47.Fn fast_divide32_prepare "uint32_t div" "uint32_t *m" "uint8_t *s1" \
48"uint8_t *s2"
49.Sh DESCRIPTION
50The
51.Nm fast_divide32
52and
53.Nm fast_remainder32
54functions compute the equivalent of
55.Fa v / Fa div
56and
57.Fa v % Fa div
58using optimised
59.Tn CPU
60instructions.
61The constants
62.Fa m ,
63.Fa s1 ,
64and
65.Fa s2
66must first be preset for a given value of
67.Fa div
68with the
69.Nm fast_divide32_prepare
70function.
71.Sh RATIONALE
72These functions are useful for inner loops and other performance-sensitive
73tasks.
74The functions expand to code that is typically slightly larger than
75a plain division instruction, but requires less time to execute.
76The code for constant
77.Fa div
78arguments should be equivalent to the assembly created by
79.Tn GCC .
80.Sh EXAMPLES
81The following example computes
82.Va q = a / b
83and
84.Va r = a % b :
85.Bd -literal -offset indent
86uint32_t a, b, q, r, m;
87uint8_t s1, s2;
88
89fast_divide32_prepare(b, &m, &s1, &s2);
90
91q = fast_divide32(a, b, m, s1, s2);
92r = fast_remainder32(a, b, m, s1, s2);
93.Ed
94.Sh SEE ALSO
95.Xr bitops 3 ,
96.Xr div 3 ,
97.Xr remainder 3
98.Rs
99.%A Torbj\(:orn Granlund
100.%A Peter L. Montgomery
101.%T Division by Invariant Integers Using Multiplication
102.%J ACM SIGPLAN Notices
103.%D June 1994
104.%N Issue 6
105.%V Volume 29
106.%P 61-72
107.%U https://gmplib.org/~tege/divcnst-pldi94.pdf
108.Re
109.Sh HISTORY
110The
111.Nm
112function appeared in
113.Nx 6.0 .
114