xref: /dflybsd-src/contrib/mpfr/src/round_raw_generic.c (revision 2786097444a0124b5d33763854de247e230c6629)
14a238c70SJohn Marino /* mpfr_round_raw_generic -- Generic rounding function
24a238c70SJohn Marino 
3*ab6d115fSJohn Marino Copyright 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013 Free Software Foundation, Inc.
4*ab6d115fSJohn Marino Contributed by the AriC and Caramel projects, INRIA.
54a238c70SJohn Marino 
64a238c70SJohn Marino This file is part of the GNU MPFR Library.
74a238c70SJohn Marino 
84a238c70SJohn Marino The GNU MPFR Library is free software; you can redistribute it and/or modify
94a238c70SJohn Marino it under the terms of the GNU Lesser General Public License as published by
104a238c70SJohn Marino the Free Software Foundation; either version 3 of the License, or (at your
114a238c70SJohn Marino option) any later version.
124a238c70SJohn Marino 
134a238c70SJohn Marino The GNU MPFR Library is distributed in the hope that it will be useful, but
144a238c70SJohn Marino WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
154a238c70SJohn Marino or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
164a238c70SJohn Marino License for more details.
174a238c70SJohn Marino 
184a238c70SJohn Marino You should have received a copy of the GNU Lesser General Public License
194a238c70SJohn Marino along with the GNU MPFR Library; see the file COPYING.LESSER.  If not, see
204a238c70SJohn Marino http://www.gnu.org/licenses/ or write to the Free Software Foundation, Inc.,
214a238c70SJohn Marino 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. */
224a238c70SJohn Marino 
234a238c70SJohn Marino #ifndef flag
244a238c70SJohn Marino # error "ERROR: flag must be defined (0 / 1)"
254a238c70SJohn Marino #endif
264a238c70SJohn Marino #ifndef use_inexp
274a238c70SJohn Marino # error "ERROR: use_enexp must be defined (0 / 1)"
284a238c70SJohn Marino #endif
294a238c70SJohn Marino #ifndef mpfr_round_raw_generic
304a238c70SJohn Marino # error "ERROR: mpfr_round_raw_generic must be defined"
314a238c70SJohn Marino #endif
324a238c70SJohn Marino 
334a238c70SJohn Marino /*
344a238c70SJohn Marino  * If flag = 0, puts in y the value of xp (with precision xprec and
354a238c70SJohn Marino  * sign 1 if negative=0, -1 otherwise) rounded to precision yprec and
364a238c70SJohn Marino  * direction rnd_mode. Supposes x is not zero nor NaN nor +/- Infinity
374a238c70SJohn Marino  * (i.e. *xp != 0). In that case, the return value is a possible carry
384a238c70SJohn Marino  * (0 or 1) that may happen during the rounding, in which case the result
394a238c70SJohn Marino  * is a power of two.
404a238c70SJohn Marino  *
414a238c70SJohn Marino  * If inexp != NULL, put in *inexp the inexact flag of the rounding (0, 1, -1).
424a238c70SJohn Marino  * In case of even rounding when rnd = MPFR_RNDN, put MPFR_EVEN_INEX (2) or
434a238c70SJohn Marino  * -MPFR_EVEN_INEX (-2) in *inexp.
444a238c70SJohn Marino  *
454a238c70SJohn Marino  * If flag = 1, just returns whether one should add 1 or not for rounding.
464a238c70SJohn Marino  *
474a238c70SJohn Marino  * Note: yprec may be < MPFR_PREC_MIN; in particular, it may be equal
484a238c70SJohn Marino  * to 1. In this case, the even rounding is done away from 0, which is
494a238c70SJohn Marino  * a natural generalization. Indeed, a number with 1-bit precision can
504a238c70SJohn Marino  * be seen as a denormalized number with more precision.
514a238c70SJohn Marino  */
524a238c70SJohn Marino 
534a238c70SJohn Marino int
mpfr_round_raw_generic(mp_limb_t * yp,const mp_limb_t * xp,mpfr_prec_t xprec,int neg,mpfr_prec_t yprec,mpfr_rnd_t rnd_mode,int * inexp)544a238c70SJohn Marino mpfr_round_raw_generic(
554a238c70SJohn Marino #if flag == 0
564a238c70SJohn Marino                        mp_limb_t *yp,
574a238c70SJohn Marino #endif
584a238c70SJohn Marino                        const mp_limb_t *xp, mpfr_prec_t xprec,
594a238c70SJohn Marino                        int neg, mpfr_prec_t yprec, mpfr_rnd_t rnd_mode
604a238c70SJohn Marino #if use_inexp != 0
614a238c70SJohn Marino                        , int *inexp
624a238c70SJohn Marino #endif
634a238c70SJohn Marino                        )
644a238c70SJohn Marino {
654a238c70SJohn Marino   mp_size_t xsize, nw;
664a238c70SJohn Marino   mp_limb_t himask, lomask, sb;
674a238c70SJohn Marino   int rw;
684a238c70SJohn Marino #if flag == 0
694a238c70SJohn Marino   int carry;
704a238c70SJohn Marino #endif
714a238c70SJohn Marino #if use_inexp == 0
724a238c70SJohn Marino   int *inexp;
734a238c70SJohn Marino #endif
744a238c70SJohn Marino 
754a238c70SJohn Marino   if (use_inexp)
764a238c70SJohn Marino     MPFR_ASSERTD(inexp != ((int*) 0));
774a238c70SJohn Marino   MPFR_ASSERTD(neg == 0 || neg == 1);
784a238c70SJohn Marino 
794a238c70SJohn Marino   if (flag && !use_inexp &&
804a238c70SJohn Marino       (xprec <= yprec || MPFR_IS_LIKE_RNDZ (rnd_mode, neg)))
814a238c70SJohn Marino     return 0;
824a238c70SJohn Marino 
83*ab6d115fSJohn Marino   xsize = MPFR_PREC2LIMBS (xprec);
844a238c70SJohn Marino   nw = yprec / GMP_NUMB_BITS;
854a238c70SJohn Marino   rw = yprec & (GMP_NUMB_BITS - 1);
864a238c70SJohn Marino 
874a238c70SJohn Marino   if (MPFR_UNLIKELY(xprec <= yprec))
884a238c70SJohn Marino     { /* No rounding is necessary. */
894a238c70SJohn Marino       /* if yp=xp, maybe an overlap: MPN_COPY_DECR is ok when src <= dst */
904a238c70SJohn Marino       if (MPFR_LIKELY(rw))
914a238c70SJohn Marino         nw++;
924a238c70SJohn Marino       MPFR_ASSERTD(nw >= 1);
934a238c70SJohn Marino       MPFR_ASSERTD(nw >= xsize);
944a238c70SJohn Marino       if (use_inexp)
954a238c70SJohn Marino         *inexp = 0;
964a238c70SJohn Marino #if flag == 0
974a238c70SJohn Marino       MPN_COPY_DECR(yp + (nw - xsize), xp, xsize);
984a238c70SJohn Marino       MPN_ZERO(yp, nw - xsize);
994a238c70SJohn Marino #endif
1004a238c70SJohn Marino       return 0;
1014a238c70SJohn Marino     }
1024a238c70SJohn Marino 
1034a238c70SJohn Marino   if (use_inexp || !MPFR_IS_LIKE_RNDZ(rnd_mode, neg))
1044a238c70SJohn Marino     {
1054a238c70SJohn Marino       mp_size_t k = xsize - nw - 1;
1064a238c70SJohn Marino 
1074a238c70SJohn Marino       if (MPFR_LIKELY(rw))
1084a238c70SJohn Marino         {
1094a238c70SJohn Marino           nw++;
1104a238c70SJohn Marino           lomask = MPFR_LIMB_MASK (GMP_NUMB_BITS - rw);
1114a238c70SJohn Marino           himask = ~lomask;
1124a238c70SJohn Marino         }
1134a238c70SJohn Marino       else
1144a238c70SJohn Marino         {
1154a238c70SJohn Marino           lomask = ~(mp_limb_t) 0;
1164a238c70SJohn Marino           himask = ~(mp_limb_t) 0;
1174a238c70SJohn Marino         }
1184a238c70SJohn Marino       MPFR_ASSERTD(k >= 0);
1194a238c70SJohn Marino       sb = xp[k] & lomask;  /* First non-significant bits */
1204a238c70SJohn Marino       /* Rounding to nearest ? */
1214a238c70SJohn Marino       if (MPFR_LIKELY( rnd_mode == MPFR_RNDN) )
1224a238c70SJohn Marino         {
1234a238c70SJohn Marino           /* Rounding to nearest */
1244a238c70SJohn Marino           mp_limb_t rbmask = MPFR_LIMB_ONE << (GMP_NUMB_BITS - 1 - rw);
1254a238c70SJohn Marino           if (sb & rbmask) /* rounding bit */
1264a238c70SJohn Marino             sb &= ~rbmask; /* it is 1, clear it */
1274a238c70SJohn Marino           else
1284a238c70SJohn Marino             {
1294a238c70SJohn Marino               /* Rounding bit is 0, behave like rounding to 0 */
1304a238c70SJohn Marino               goto rnd_RNDZ;
1314a238c70SJohn Marino             }
1324a238c70SJohn Marino           while (MPFR_UNLIKELY(sb == 0) && k > 0)
1334a238c70SJohn Marino             sb = xp[--k];
1344a238c70SJohn Marino           /* rounding to nearest, with rounding bit = 1 */
1354a238c70SJohn Marino           if (MPFR_UNLIKELY(sb == 0)) /* Even rounding. */
1364a238c70SJohn Marino             {
1374a238c70SJohn Marino               /* sb == 0 && rnd_mode == MPFR_RNDN */
1384a238c70SJohn Marino               sb = xp[xsize - nw] & (himask ^ (himask << 1));
1394a238c70SJohn Marino               if (sb == 0)
1404a238c70SJohn Marino                 {
1414a238c70SJohn Marino                   if (use_inexp)
1424a238c70SJohn Marino                     *inexp = 2*MPFR_EVEN_INEX*neg-MPFR_EVEN_INEX;
1434a238c70SJohn Marino                   /* ((neg!=0)^(sb!=0)) ? MPFR_EVEN_INEX  : -MPFR_EVEN_INEX;*/
1444a238c70SJohn Marino                   /* Since neg = 0 or 1 and sb=0*/
1454a238c70SJohn Marino #if flag == 1
1464a238c70SJohn Marino                   return 0 /*sb != 0 && rnd_mode != MPFR_RNDZ */;
1474a238c70SJohn Marino #else
1484a238c70SJohn Marino                   MPN_COPY_INCR(yp, xp + xsize - nw, nw);
1494a238c70SJohn Marino                   yp[0] &= himask;
1504a238c70SJohn Marino                   return 0;
1514a238c70SJohn Marino #endif
1524a238c70SJohn Marino                 }
1534a238c70SJohn Marino               else
1544a238c70SJohn Marino                 {
1554a238c70SJohn Marino                   /* sb != 0 && rnd_mode == MPFR_RNDN */
1564a238c70SJohn Marino                   if (use_inexp)
1574a238c70SJohn Marino                     *inexp = MPFR_EVEN_INEX-2*MPFR_EVEN_INEX*neg;
1584a238c70SJohn Marino                   /*((neg!=0)^(sb!=0))? MPFR_EVEN_INEX  : -MPFR_EVEN_INEX; */
1594a238c70SJohn Marino                   /*Since neg= 0 or 1 and sb != 0 */
1604a238c70SJohn Marino                   goto rnd_RNDN_add_one_ulp;
1614a238c70SJohn Marino                 }
1624a238c70SJohn Marino             }
1634a238c70SJohn Marino           else /* sb != 0  && rnd_mode == MPFR_RNDN*/
1644a238c70SJohn Marino             {
1654a238c70SJohn Marino               if (use_inexp)
1664a238c70SJohn Marino                 /* *inexp = (neg == 0) ? 1 : -1; but since neg = 0 or 1 */
1674a238c70SJohn Marino                 *inexp = 1-2*neg;
1684a238c70SJohn Marino             rnd_RNDN_add_one_ulp:
1694a238c70SJohn Marino #if flag == 1
1704a238c70SJohn Marino               return 1; /*sb != 0 && rnd_mode != MPFR_RNDZ;*/
1714a238c70SJohn Marino #else
1724a238c70SJohn Marino               carry = mpn_add_1 (yp, xp + xsize - nw, nw,
1734a238c70SJohn Marino                                  rw ?
1744a238c70SJohn Marino                                  MPFR_LIMB_ONE << (GMP_NUMB_BITS - rw)
1754a238c70SJohn Marino                                  : MPFR_LIMB_ONE);
1764a238c70SJohn Marino               yp[0] &= himask;
1774a238c70SJohn Marino               return carry;
1784a238c70SJohn Marino #endif
1794a238c70SJohn Marino             }
1804a238c70SJohn Marino         }
1814a238c70SJohn Marino       /* Rounding to Zero ? */
1824a238c70SJohn Marino       else if (MPFR_IS_LIKE_RNDZ(rnd_mode, neg))
1834a238c70SJohn Marino         {
1844a238c70SJohn Marino           /* rnd_mode == MPFR_RNDZ */
1854a238c70SJohn Marino         rnd_RNDZ:
1864a238c70SJohn Marino           while (MPFR_UNLIKELY(sb == 0) && k > 0)
1874a238c70SJohn Marino             sb = xp[--k];
1884a238c70SJohn Marino           if (use_inexp)
1894a238c70SJohn Marino             /* rnd_mode == MPFR_RNDZ and neg = 0 or 1 */
1904a238c70SJohn Marino             /* (neg != 0) ^ (rnd_mode != MPFR_RNDZ)) ? 1 : -1);*/
1914a238c70SJohn Marino             *inexp = MPFR_UNLIKELY(sb == 0) ? 0 : (2*neg-1);
1924a238c70SJohn Marino #if flag == 1
1934a238c70SJohn Marino           return 0; /*sb != 0 && rnd_mode != MPFR_RNDZ;*/
1944a238c70SJohn Marino #else
1954a238c70SJohn Marino           MPN_COPY_INCR(yp, xp + xsize - nw, nw);
1964a238c70SJohn Marino           yp[0] &= himask;
1974a238c70SJohn Marino           return 0;
1984a238c70SJohn Marino #endif
1994a238c70SJohn Marino         }
2004a238c70SJohn Marino       else
2014a238c70SJohn Marino         {
2024a238c70SJohn Marino           /* rnd_mode = Away */
2034a238c70SJohn Marino           while (MPFR_UNLIKELY(sb == 0) && k > 0)
2044a238c70SJohn Marino             sb = xp[--k];
2054a238c70SJohn Marino           if (MPFR_UNLIKELY(sb == 0))
2064a238c70SJohn Marino             {
2074a238c70SJohn Marino               /* sb = 0 && rnd_mode != MPFR_RNDZ */
2084a238c70SJohn Marino               if (use_inexp)
2094a238c70SJohn Marino                 /* (neg != 0) ^ (rnd_mode != MPFR_RNDZ)) ? 1 : -1);*/
2104a238c70SJohn Marino                 *inexp = 0;
2114a238c70SJohn Marino #if flag == 1
2124a238c70SJohn Marino               return 0;
2134a238c70SJohn Marino #else
2144a238c70SJohn Marino               MPN_COPY_INCR(yp, xp + xsize - nw, nw);
2154a238c70SJohn Marino               yp[0] &= himask;
2164a238c70SJohn Marino               return 0;
2174a238c70SJohn Marino #endif
2184a238c70SJohn Marino             }
2194a238c70SJohn Marino           else
2204a238c70SJohn Marino             {
2214a238c70SJohn Marino               /* sb != 0 && rnd_mode != MPFR_RNDZ */
2224a238c70SJohn Marino               if (use_inexp)
2234a238c70SJohn Marino                 /* (neg != 0) ^ (rnd_mode != MPFR_RNDZ)) ? 1 : -1);*/
2244a238c70SJohn Marino                 *inexp = 1-2*neg;
2254a238c70SJohn Marino #if flag == 1
2264a238c70SJohn Marino               return 1;
2274a238c70SJohn Marino #else
2284a238c70SJohn Marino               carry = mpn_add_1(yp, xp + xsize - nw, nw,
2294a238c70SJohn Marino                                 rw ? MPFR_LIMB_ONE << (GMP_NUMB_BITS - rw)
2304a238c70SJohn Marino                                 : 1);
2314a238c70SJohn Marino               yp[0] &= himask;
2324a238c70SJohn Marino               return carry;
2334a238c70SJohn Marino #endif
2344a238c70SJohn Marino             }
2354a238c70SJohn Marino         }
2364a238c70SJohn Marino     }
2374a238c70SJohn Marino   else
2384a238c70SJohn Marino     {
2394a238c70SJohn Marino       /* Roundind mode = Zero / No inexact flag */
2404a238c70SJohn Marino #if flag == 1
2414a238c70SJohn Marino       return 0 /*sb != 0 && rnd_mode != MPFR_RNDZ*/;
2424a238c70SJohn Marino #else
2434a238c70SJohn Marino       if (MPFR_LIKELY(rw))
2444a238c70SJohn Marino         {
2454a238c70SJohn Marino           nw++;
2464a238c70SJohn Marino           himask = ~MPFR_LIMB_MASK (GMP_NUMB_BITS - rw);
2474a238c70SJohn Marino         }
2484a238c70SJohn Marino       else
2494a238c70SJohn Marino         himask = ~(mp_limb_t) 0;
2504a238c70SJohn Marino       MPN_COPY_INCR(yp, xp + xsize - nw, nw);
2514a238c70SJohn Marino       yp[0] &= himask;
2524a238c70SJohn Marino       return 0;
2534a238c70SJohn Marino #endif
2544a238c70SJohn Marino     }
2554a238c70SJohn Marino }
2564a238c70SJohn Marino 
2574a238c70SJohn Marino #undef flag
2584a238c70SJohn Marino #undef use_inexp
2594a238c70SJohn Marino #undef mpfr_round_raw_generic
260