The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/libkern/qdivrem.c

Version: -  FREEBSD  -  FREEBSD-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

    1 /*-
    2  * SPDX-License-Identifier: BSD-3-Clause
    3  *
    4  * Copyright (c) 1992, 1993
    5  *      The Regents of the University of California.  All rights reserved.
    6  *
    7  * This software was developed by the Computer Systems Engineering group
    8  * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
    9  * contributed to Berkeley.
   10  *
   11  * Redistribution and use in source and binary forms, with or without
   12  * modification, are permitted provided that the following conditions
   13  * are met:
   14  * 1. Redistributions of source code must retain the above copyright
   15  *    notice, this list of conditions and the following disclaimer.
   16  * 2. Redistributions in binary form must reproduce the above copyright
   17  *    notice, this list of conditions and the following disclaimer in the
   18  *    documentation and/or other materials provided with the distribution.
   19  * 3. Neither the name of the University nor the names of its contributors
   20  *    may be used to endorse or promote products derived from this software
   21  *    without specific prior written permission.
   22  *
   23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
   24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
   27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   33  * SUCH DAMAGE.
   34  */
   35 
   36 #include <sys/cdefs.h>
   37 __FBSDID("$FreeBSD$");
   38 
   39 /*
   40  * Multiprecision divide.  This algorithm is from Knuth vol. 2 (2nd ed),
   41  * section 4.3.1, pp. 257--259.
   42  */
   43 
   44 #include <libkern/quad.h>
   45 
   46 #define B       (1 << HALF_BITS)        /* digit base */
   47 
   48 /* Combine two `digits' to make a single two-digit number. */
   49 #define COMBINE(a, b) (((u_long)(a) << HALF_BITS) | (b))
   50 
   51 /* select a type for digits in base B: use unsigned short if they fit */
   52 #if ULONG_MAX == 0xffffffff && USHRT_MAX >= 0xffff
   53 typedef unsigned short digit;
   54 #else
   55 typedef u_long digit;
   56 #endif
   57 
   58 /*
   59  * Shift p[0]..p[len] left `sh' bits, ignoring any bits that
   60  * `fall out' the left (there never will be any such anyway).
   61  * We may assume len >= 0.  NOTE THAT THIS WRITES len+1 DIGITS.
   62  */
   63 static void
   64 __shl(digit *p, int len, int sh)
   65 {
   66         int i;
   67 
   68         for (i = 0; i < len; i++)
   69                 p[i] = LHALF(p[i] << sh) | (p[i + 1] >> (HALF_BITS - sh));
   70         p[i] = LHALF(p[i] << sh);
   71 }
   72 
   73 /*
   74  * __qdivrem(u, v, rem) returns u/v and, optionally, sets *rem to u%v.
   75  *
   76  * We do this in base 2-sup-HALF_BITS, so that all intermediate products
   77  * fit within u_long.  As a consequence, the maximum length dividend and
   78  * divisor are 4 `digits' in this base (they are shorter if they have
   79  * leading zeros).
   80  */
   81 u_quad_t
   82 __qdivrem(u_quad_t uq, u_quad_t vq, u_quad_t *arq)
   83 {
   84         union uu tmp;
   85         digit *u, *v, *q;
   86         digit v1, v2;
   87         u_long qhat, rhat, t;
   88         int m, n, d, j, i;
   89         digit uspace[5], vspace[5], qspace[5];
   90 
   91         /*
   92          * Take care of special cases: divide by zero, and u < v.
   93          */
   94         if (__predict_false(vq == 0)) {
   95                 /* divide by zero. */
   96                 static volatile const unsigned int zero = 0;
   97 
   98                 tmp.ul[H] = tmp.ul[L] = 1 / zero;
   99                 if (arq)
  100                         *arq = uq;
  101                 return (tmp.q);
  102         }
  103         if (uq < vq) {
  104                 if (arq)
  105                         *arq = uq;
  106                 return (0);
  107         }
  108         u = &uspace[0];
  109         v = &vspace[0];
  110         q = &qspace[0];
  111 
  112         /*
  113          * Break dividend and divisor into digits in base B, then
  114          * count leading zeros to determine m and n.  When done, we
  115          * will have:
  116          *      u = (u[1]u[2]...u[m+n]) sub B
  117          *      v = (v[1]v[2]...v[n]) sub B
  118          *      v[1] != 0
  119          *      1 < n <= 4 (if n = 1, we use a different division algorithm)
  120          *      m >= 0 (otherwise u < v, which we already checked)
  121          *      m + n = 4
  122          * and thus
  123          *      m = 4 - n <= 2
  124          */
  125         tmp.uq = uq;
  126         u[0] = 0;
  127         u[1] = HHALF(tmp.ul[H]);
  128         u[2] = LHALF(tmp.ul[H]);
  129         u[3] = HHALF(tmp.ul[L]);
  130         u[4] = LHALF(tmp.ul[L]);
  131         tmp.uq = vq;
  132         v[1] = HHALF(tmp.ul[H]);
  133         v[2] = LHALF(tmp.ul[H]);
  134         v[3] = HHALF(tmp.ul[L]);
  135         v[4] = LHALF(tmp.ul[L]);
  136         for (n = 4; v[1] == 0; v++) {
  137                 if (--n == 1) {
  138                         u_long rbj;     /* r*B+u[j] (not root boy jim) */
  139                         digit q1, q2, q3, q4;
  140 
  141                         /*
  142                          * Change of plan, per exercise 16.
  143                          *      r = 0;
  144                          *      for j = 1..4:
  145                          *              q[j] = floor((r*B + u[j]) / v),
  146                          *              r = (r*B + u[j]) % v;
  147                          * We unroll this completely here.
  148                          */
  149                         t = v[2];       /* nonzero, by definition */
  150                         q1 = u[1] / t;
  151                         rbj = COMBINE(u[1] % t, u[2]);
  152                         q2 = rbj / t;
  153                         rbj = COMBINE(rbj % t, u[3]);
  154                         q3 = rbj / t;
  155                         rbj = COMBINE(rbj % t, u[4]);
  156                         q4 = rbj / t;
  157                         if (arq)
  158                                 *arq = rbj % t;
  159                         tmp.ul[H] = COMBINE(q1, q2);
  160                         tmp.ul[L] = COMBINE(q3, q4);
  161                         return (tmp.q);
  162                 }
  163         }
  164 
  165         /*
  166          * By adjusting q once we determine m, we can guarantee that
  167          * there is a complete four-digit quotient at &qspace[1] when
  168          * we finally stop.
  169          */
  170         for (m = 4 - n; u[1] == 0; u++)
  171                 m--;
  172         for (i = 4 - m; --i >= 0;)
  173                 q[i] = 0;
  174         q += 4 - m;
  175 
  176         /*
  177          * Here we run Program D, translated from MIX to C and acquiring
  178          * a few minor changes.
  179          *
  180          * D1: choose multiplier 1 << d to ensure v[1] >= B/2.
  181          */
  182         d = 0;
  183         for (t = v[1]; t < B / 2; t <<= 1)
  184                 d++;
  185         if (d > 0) {
  186                 __shl(&u[0], m + n, d);         /* u <<= d */
  187                 __shl(&v[1], n - 1, d);         /* v <<= d */
  188         }
  189         /*
  190          * D2: j = 0.
  191          */
  192         j = 0;
  193         v1 = v[1];      /* for D3 -- note that v[1..n] are constant */
  194         v2 = v[2];      /* for D3 */
  195         do {
  196                 digit uj0, uj1, uj2;
  197 
  198                 /*
  199                  * D3: Calculate qhat (\^q, in TeX notation).
  200                  * Let qhat = min((u[j]*B + u[j+1])/v[1], B-1), and
  201                  * let rhat = (u[j]*B + u[j+1]) mod v[1].
  202                  * While rhat < B and v[2]*qhat > rhat*B+u[j+2],
  203                  * decrement qhat and increase rhat correspondingly.
  204                  * Note that if rhat >= B, v[2]*qhat < rhat*B.
  205                  */
  206                 uj0 = u[j + 0]; /* for D3 only -- note that u[j+...] change */
  207                 uj1 = u[j + 1]; /* for D3 only */
  208                 uj2 = u[j + 2]; /* for D3 only */
  209                 if (uj0 == v1) {
  210                         qhat = B;
  211                         rhat = uj1;
  212                         goto qhat_too_big;
  213                 } else {
  214                         u_long nn = COMBINE(uj0, uj1);
  215                         qhat = nn / v1;
  216                         rhat = nn % v1;
  217                 }
  218                 while (v2 * qhat > COMBINE(rhat, uj2)) {
  219         qhat_too_big:
  220                         qhat--;
  221                         if ((rhat += v1) >= B)
  222                                 break;
  223                 }
  224                 /*
  225                  * D4: Multiply and subtract.
  226                  * The variable `t' holds any borrows across the loop.
  227                  * We split this up so that we do not require v[0] = 0,
  228                  * and to eliminate a final special case.
  229                  */
  230                 for (t = 0, i = n; i > 0; i--) {
  231                         t = u[i + j] - v[i] * qhat - t;
  232                         u[i + j] = LHALF(t);
  233                         t = (B - HHALF(t)) & (B - 1);
  234                 }
  235                 t = u[j] - t;
  236                 u[j] = LHALF(t);
  237                 /*
  238                  * D5: test remainder.
  239                  * There is a borrow if and only if HHALF(t) is nonzero;
  240                  * in that (rare) case, qhat was too large (by exactly 1).
  241                  * Fix it by adding v[1..n] to u[j..j+n].
  242                  */
  243                 if (HHALF(t)) {
  244                         qhat--;
  245                         for (t = 0, i = n; i > 0; i--) { /* D6: add back. */
  246                                 t += u[i + j] + v[i];
  247                                 u[i + j] = LHALF(t);
  248                                 t = HHALF(t);
  249                         }
  250                         u[j] = LHALF(u[j] + t);
  251                 }
  252                 q[j] = qhat;
  253         } while (++j <= m);             /* D7: loop on j. */
  254 
  255         /*
  256          * If caller wants the remainder, we have to calculate it as
  257          * u[m..m+n] >> d (this is at most n digits and thus fits in
  258          * u[m+1..m+n], but we may need more source digits).
  259          */
  260         if (arq) {
  261                 if (d) {
  262                         for (i = m + n; i > m; --i)
  263                                 u[i] = (u[i] >> d) |
  264                                     LHALF(u[i - 1] << (HALF_BITS - d));
  265                         u[i] = 0;
  266                 }
  267                 tmp.ul[H] = COMBINE(uspace[1], uspace[2]);
  268                 tmp.ul[L] = COMBINE(uspace[3], uspace[4]);
  269                 *arq = tmp.q;
  270         }
  271 
  272         tmp.ul[H] = COMBINE(qspace[1], qspace[2]);
  273         tmp.ul[L] = COMBINE(qspace[3], qspace[4]);
  274         return (tmp.q);
  275 }

Cache object: 6b3e54b6844548e62a5d1a8d06fe83b6


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.