1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2017 Cavium, Inc
7 * Copyright(c) Hannes Frederic Sowa
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
14 * * Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * * Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in
18 * the documentation and/or other materials provided with the
20 * * Neither the name of Intel Corporation nor the names of its
21 * contributors may be used to endorse or promote products derived
22 * from this software without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
27 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
28 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
29 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
30 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
31 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
32 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
33 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
34 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
40 #include <rte_common.h>
42 #include "rte_reciprocal.h"
44 /* find largest set bit.
45 * portable and slow but does not matter for this usage.
47 static inline int fls(uint32_t x)
51 for (b = 31; b >= 0; --b) {
59 struct rte_reciprocal rte_reciprocal_value(uint32_t d)
61 struct rte_reciprocal R;
66 m = ((1ULL << 32) * ((1ULL << l) - d));
71 R.sh1 = RTE_MIN(l, 1);
72 R.sh2 = RTE_MAX(l - 1, 0);
78 * Code taken from Hacker's Delight:
79 * http://www.hackersdelight.org/hdcodetxt/divlu.c.txt
80 * License permits inclusion here per:
81 * http://www.hackersdelight.org/permissions.htm
84 divide_128_div_64_to_64(uint64_t u1, uint64_t u0, uint64_t v, uint64_t *r)
86 const uint64_t b = (1ULL << 32); /* Number base (16 bits). */
87 uint64_t un1, un0, /* Norm. dividend LSD's. */
88 vn1, vn0, /* Norm. divisor digits. */
89 q1, q0, /* Quotient digits. */
90 un64, un21, un10, /* Dividend digit pairs. */
91 rhat; /* A remainder. */
92 int s; /* Shift amount for norm. */
94 /* If overflow, set rem. to an impossible value. */
101 /* Count leading zeros. */
102 s = __builtin_clzll(v);
105 un64 = (u1 << s) | ((u0 >> (64 - s)) & (-s >> 31));
114 vn0 = v & 0xFFFFFFFF;
117 un0 = un10 & 0xFFFFFFFF;
120 rhat = un64 - q1*vn1;
122 if (q1 >= b || q1*vn0 > b*rhat + un1) {
129 un21 = un64*b + un1 - q1*v;
132 rhat = un21 - q0*vn1;
134 if (q0 >= b || q0*vn0 > b*rhat + un0) {
142 *r = (un21*b + un0 - q0*v) >> s;
146 struct rte_reciprocal_u64
147 rte_reciprocal_value_u64(uint64_t d)
149 struct rte_reciprocal_u64 R;
153 l = 63 - __builtin_clzll(d);
155 m = divide_128_div_64_to_64((1ULL << l), 0, d, NULL) << 1;
156 m = (1ULL << l) - d ? m + 2 : 1;
159 R.sh1 = l > 1 ? 1 : l;
160 R.sh2 = (l > 0) ? l : 0;
161 R.sh2 -= R.sh2 && (m == 1) ? 1 : 0;