fdiv_ui.c
Upload User: qaz666999
Upload Date: 2022-08-06
Package Size: 2570k
Code Size: 2k
Category:

Algorithm

Development Platform:

Unix_Linux

  1. /* mpz_fdiv_ui -- Division rounding the quotient towards -infinity.
  2.    The remainder gets the same sign as the denominator.
  3. Copyright 1994, 1995, 1996, 2001, 2002, 2004, 2005 Free Software Foundation,
  4. Inc.
  5. This file is part of the GNU MP Library.
  6. The GNU MP Library is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU Lesser General Public License as published by
  8. the Free Software Foundation; either version 3 of the License, or (at your
  9. option) any later version.
  10. The GNU MP Library is distributed in the hope that it will be useful, but
  11. WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  12. or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
  13. License for more details.
  14. You should have received a copy of the GNU Lesser General Public License
  15. along with the GNU MP Library.  If not, see http://www.gnu.org/licenses/.  */
  16. #include "gmp.h"
  17. #include "gmp-impl.h"
  18. unsigned long int
  19. mpz_fdiv_ui (mpz_srcptr dividend, unsigned long int divisor)
  20. {
  21.   mp_size_t ns, nn;
  22.   mp_ptr np;
  23.   mp_limb_t rl;
  24.   if (divisor == 0)
  25.     DIVIDE_BY_ZERO;
  26.   ns = SIZ(dividend);
  27.   if (ns == 0)
  28.     {
  29.       return 0;
  30.     }
  31.   nn = ABS(ns);
  32.   np = PTR(dividend);
  33. #if BITS_PER_ULONG > GMP_NUMB_BITS  /* avoid warnings about shift amount */
  34.   if (divisor > GMP_NUMB_MAX)
  35.     {
  36.       mp_limb_t dp[2], rp[2];
  37.       mp_ptr qp;
  38.       mp_size_t rn;
  39.       TMP_DECL;
  40.       if (nn == 1) /* tdiv_qr requirements; tested above for 0 */
  41. {
  42.   rl = np[0];
  43.   rp[0] = rl;
  44. }
  45.       else
  46. {
  47.   TMP_MARK;
  48.   dp[0] = divisor & GMP_NUMB_MASK;
  49.   dp[1] = divisor >> GMP_NUMB_BITS;
  50.   qp = TMP_ALLOC_LIMBS (nn - 2 + 1);
  51.   mpn_tdiv_qr (qp, rp, (mp_size_t) 0, np, nn, dp, (mp_size_t) 2);
  52.   TMP_FREE;
  53.   rl = rp[0] + (rp[1] << GMP_NUMB_BITS);
  54. }
  55.       if (rl != 0 && ns < 0)
  56. {
  57.   rl = divisor - rl;
  58.   rp[0] = rl & GMP_NUMB_MASK;
  59.   rp[1] = rl >> GMP_NUMB_BITS;
  60. }
  61.       rn = 1 + (rl > GMP_NUMB_MAX);  rn -= (rp[rn - 1] == 0);
  62.     }
  63.   else
  64. #endif
  65.     {
  66.       rl = mpn_mod_1 (np, nn, (mp_limb_t) divisor);
  67.       if (rl == 0)
  68. ;
  69.       else
  70. {
  71.   if (ns < 0)
  72.     rl = divisor - rl;
  73. }
  74.     }
  75.   return rl;
  76. }