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 struct rte_reciprocal rte_reciprocal_value(uint32_t d)
46 struct rte_reciprocal R;
50 l = rte_fls_u32(d - 1);
51 m = ((1ULL << 32) * ((1ULL << l) - d));
56 R.sh1 = RTE_MIN(l, 1);
57 R.sh2 = RTE_MAX(l - 1, 0);
63 * Code taken from Hacker's Delight:
64 * http://www.hackersdelight.org/hdcodetxt/divlu.c.txt
65 * License permits inclusion here per:
66 * http://www.hackersdelight.org/permissions.htm
69 divide_128_div_64_to_64(uint64_t u1, uint64_t u0, uint64_t v, uint64_t *r)
71 const uint64_t b = (1ULL << 32); /* Number base (16 bits). */
72 uint64_t un1, un0, /* Norm. dividend LSD's. */
73 vn1, vn0, /* Norm. divisor digits. */
74 q1, q0, /* Quotient digits. */
75 un64, un21, un10, /* Dividend digit pairs. */
76 rhat; /* A remainder. */
77 int s; /* Shift amount for norm. */
79 /* If overflow, set rem. to an impossible value. */
86 /* Count leading zeros. */
87 s = __builtin_clzll(v);
90 un64 = (u1 << s) | ((u0 >> (64 - s)) & (-s >> 31));
102 un0 = un10 & 0xFFFFFFFF;
105 rhat = un64 - q1*vn1;
107 if (q1 >= b || q1*vn0 > b*rhat + un1) {
114 un21 = un64*b + un1 - q1*v;
117 rhat = un21 - q0*vn1;
119 if (q0 >= b || q0*vn0 > b*rhat + un0) {
127 *r = (un21*b + un0 - q0*v) >> s;
131 struct rte_reciprocal_u64
132 rte_reciprocal_value_u64(uint64_t d)
134 struct rte_reciprocal_u64 R;
138 l = 63 - __builtin_clzll(d);
140 m = divide_128_div_64_to_64((1ULL << l), 0, d, NULL) << 1;
141 m = (1ULL << l) - d ? m + 2 : 1;
144 R.sh1 = l > 1 ? 1 : l;
145 R.sh2 = (l > 0) ? l : 0;
146 R.sh2 -= R.sh2 && (m == 1) ? 1 : 0;