1.\" $NetBSD: fast_divide32.3,v 1.4 2010/03/20 14:45:18 jruoho 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 March 19, 2010 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 CPU instructions. 59The constants 60.Fa m , 61.Fa s1 , 62and 63.Fa s2 64must first be preset for a given value of 65.Fa div 66with the 67.Nm fast_divide32_prepare 68function. 69.Sh RATIONALE 70These functions are useful for inner loops and other performance-sensitive 71tasks. 72The functions expand to code that is typically slightly larger than 73a plain division instruction, but requires less time to execute. 74The code for constant 75.Fa div 76arguments should be equivalent to the assembly created by GCC. 77.Sh EXAMPLE 78The following example computes q=a/b and r=a%b: 79.Bd -literal 80uint32_t a, b, q, r, m; 81uint8_t s1, s2; 82 83fast_divide32_prepare(b, \*[Am]m, \*[Am]s1, \*[Am]s2); 84 85q = fast_divide32(a, b, m, s1, s2); 86r = fast_remainder32(a, b, m, s1, s2); 87.Ed 88.Sh SEE ALSO 89.Rs 90.%A Torbj\(:orn Granlund 91.%A Peter L. Montgomery 92.%T Division by Invariant Integers Using Multiplication 93.%J ACM SIGPLAN Notices 94.%D June 1994 95.%N Issue 6 96.%V Volume 29 97.%P 61-72 98.%U http://gmplib.org/~tege/divcnst-pldi94.pdf 99.Re 100.Sh HISTORY 101The 102.Nm 103function appeared in 104.Nx 6.0 . 105