New upstream version 17.11
[deb_dpdk.git] / lib / librte_eal / common / rte_reciprocal.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2017 Cavium, Inc
3  */
4 /*-
5  *   BSD LICENSE
6  *
7  *   Copyright(c) Hannes Frederic Sowa
8  *   All rights reserved.
9  *
10  *   Redistribution and use in source and binary forms, with or without
11  *   modification, are permitted provided that the following conditions
12  *   are met:
13  *
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
19  *       distribution.
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.
23  *
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.
35  */
36
37 #include <stdio.h>
38 #include <stdint.h>
39
40 #include <rte_common.h>
41
42 #include "rte_reciprocal.h"
43
44 /* find largest set bit.
45  * portable and slow but does not matter for this usage.
46  */
47 static inline int fls(uint32_t x)
48 {
49         int b;
50
51         for (b = 31; b >= 0; --b) {
52                 if (x & (1u << b))
53                         return b + 1;
54         }
55
56         return 0;
57 }
58
59 struct rte_reciprocal rte_reciprocal_value(uint32_t d)
60 {
61         struct rte_reciprocal R;
62         uint64_t m;
63         int l;
64
65         l = fls(d - 1);
66         m = ((1ULL << 32) * ((1ULL << l) - d));
67         m /= d;
68
69         ++m;
70         R.m = m;
71         R.sh1 = RTE_MIN(l, 1);
72         R.sh2 = RTE_MAX(l - 1, 0);
73
74         return R;
75 }
76
77 /*
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
82  */
83 static uint64_t
84 divide_128_div_64_to_64(uint64_t u1, uint64_t u0, uint64_t v, uint64_t *r)
85 {
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. */
93
94         /* If overflow, set rem. to an impossible value. */
95         if (u1 >= v) {
96                 if (r != NULL)
97                         *r = (uint64_t) -1;
98                 return (uint64_t) -1;
99         }
100
101         /* Count leading zeros. */
102         s = __builtin_clzll(v);
103         if (s > 0) {
104                 v = v << s;
105                 un64 = (u1 << s) | ((u0 >> (64 - s)) & (-s >> 31));
106                 un10 = u0 << s;
107         } else {
108
109                 un64 = u1 | u0;
110                 un10 = u0;
111         }
112
113         vn1 = v >> 32;
114         vn0 = v & 0xFFFFFFFF;
115
116         un1 = un10 >> 32;
117         un0 = un10 & 0xFFFFFFFF;
118
119         q1 = un64/vn1;
120         rhat = un64 - q1*vn1;
121 again1:
122         if (q1 >= b || q1*vn0 > b*rhat + un1) {
123                 q1 = q1 - 1;
124                 rhat = rhat + vn1;
125                 if (rhat < b)
126                         goto again1;
127         }
128
129         un21 = un64*b + un1 - q1*v;
130
131         q0 = un21/vn1;
132         rhat = un21 - q0*vn1;
133 again2:
134         if (q0 >= b || q0*vn0 > b*rhat + un0) {
135                 q0 = q0 - 1;
136                 rhat = rhat + vn1;
137                 if (rhat < b)
138                         goto again2;
139         }
140
141         if (r != NULL)
142                 *r = (un21*b + un0 - q0*v) >> s;
143         return q1*b + q0;
144 }
145
146 struct rte_reciprocal_u64
147 rte_reciprocal_value_u64(uint64_t d)
148 {
149         struct rte_reciprocal_u64 R;
150         uint64_t m;
151         int l;
152
153         l = 63 - __builtin_clzll(d);
154
155         m = divide_128_div_64_to_64((1ULL << l), 0, d, NULL) << 1;
156         m = (1ULL << l) - d ? m + 2 : 1;
157         R.m = m;
158
159         R.sh1 = l > 1 ? 1 : l;
160         R.sh2 = (l > 0) ? l : 0;
161         R.sh2 -= R.sh2 && (m == 1) ? 1 : 0;
162
163         return R;
164 }