Home | History | Annotate | Line # | Download | only in src
      1 /* mpfr_ui_div -- divide a machine integer by a floating-point number
      2 
      3 Copyright 2000-2023 Free Software Foundation, Inc.
      4 Contributed by the AriC and Caramba projects, INRIA.
      5 
      6 This file is part of the GNU MPFR Library.
      7 
      8 The GNU MPFR Library is free software; you can redistribute it and/or modify
      9 it under the terms of the GNU Lesser General Public License as published by
     10 the Free Software Foundation; either version 3 of the License, or (at your
     11 option) any later version.
     12 
     13 The GNU MPFR Library is distributed in the hope that it will be useful, but
     14 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
     15 or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
     16 License for more details.
     17 
     18 You should have received a copy of the GNU Lesser General Public License
     19 along with the GNU MPFR Library; see the file COPYING.LESSER.  If not, see
     20 https://www.gnu.org/licenses/ or write to the Free Software Foundation, Inc.,
     21 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. */
     22 
     23 
     24 #define MPFR_NEED_LONGLONG_H
     25 #include "mpfr-impl.h"
     26 
     27 int
     28 mpfr_ui_div (mpfr_ptr y, unsigned long int u, mpfr_srcptr x, mpfr_rnd_t rnd_mode)
     29 {
     30   MPFR_LOG_FUNC
     31     (("u=%lu x[%Pd]=%.*Rg rnd=%d",
     32       u, mpfr_get_prec(x), mpfr_log_prec, x, rnd_mode),
     33      ("y[%Pd]=%.*Rg", mpfr_get_prec(y), mpfr_log_prec, y));
     34 
     35   if (MPFR_UNLIKELY(MPFR_IS_SINGULAR(x)))
     36     {
     37       if (MPFR_IS_NAN(x))
     38         {
     39           MPFR_SET_NAN(y);
     40           MPFR_RET_NAN;
     41         }
     42       else if (MPFR_IS_INF(x)) /* u/Inf = 0 */
     43         {
     44           MPFR_SET_ZERO(y);
     45           MPFR_SET_SAME_SIGN(y,x);
     46           MPFR_RET(0);
     47         }
     48       else /* u / 0 */
     49         {
     50           MPFR_ASSERTD(MPFR_IS_ZERO(x));
     51           if (u)
     52             {
     53               /* u > 0, so y = sign(x) * Inf */
     54               MPFR_SET_SAME_SIGN(y, x);
     55               MPFR_SET_INF(y);
     56               MPFR_SET_DIVBY0 ();
     57               MPFR_RET(0);
     58             }
     59           else
     60             {
     61               /* 0 / 0 */
     62               MPFR_SET_NAN(y);
     63               MPFR_RET_NAN;
     64             }
     65         }
     66     }
     67   else if (MPFR_LIKELY(u != 0))
     68     {
     69       int inex;
     70       MPFR_SAVE_EXPO_DECL (expo);
     71 
     72       /* Optimization note: Exponent save/restore operations may be
     73          removed if mpfr_div works even when uu is out-of-range. */
     74       MPFR_SAVE_EXPO_MARK (expo);
     75 
     76 #ifdef MPFR_LONG_WITHIN_LIMB
     77       {
     78         mpfr_t uu;
     79         mp_limb_t up[1];
     80         int cnt;
     81 
     82         MPFR_TMP_INIT1(up, uu, GMP_NUMB_BITS);
     83         MPFR_ASSERTN(u == (mp_limb_t) u);
     84         count_leading_zeros(cnt, (mp_limb_t) u);
     85         up[0] = (mp_limb_t) u << cnt;
     86 
     87         MPFR_SET_EXP (uu, GMP_NUMB_BITS - cnt);
     88         inex = mpfr_div (y, uu, x, rnd_mode);
     89       }
     90 #else
     91       {
     92       mpfr_t uu;
     93 
     94       mpfr_init2 (uu, sizeof (unsigned long) * CHAR_BIT);
     95       mpfr_set_ui (uu, u, MPFR_RNDZ);
     96       inex = mpfr_div (y, uu, x, rnd_mode);
     97       mpfr_clear (uu);
     98       }
     99 #endif
    100 
    101       MPFR_SAVE_EXPO_UPDATE_FLAGS (expo, __gmpfr_flags);
    102       MPFR_SAVE_EXPO_FREE (expo);
    103       return mpfr_check_range (y, inex, rnd_mode);
    104     }
    105   else /* u = 0, and x != 0 */
    106     {
    107       MPFR_SET_ZERO(y);         /* if u=0, then set y to 0 */
    108       MPFR_SET_SAME_SIGN(y, x); /* u considered as +0: sign(+0/x) = sign(x) */
    109       MPFR_RET(0);
    110     }
    111 }
    112