xref: /netbsd/external/lgpl3/mpfr/dist/src/powerof2.c (revision 606004a0)
115e9af00Smrg /* mpfr_powerof2_raw -- test whether a floating-point number is a power of 2
215e9af00Smrg 
3*606004a0Smrg Copyright 2002-2023 Free Software Foundation, Inc.
46c7ec94dSmrg Contributed by the AriC and Caramba projects, INRIA.
515e9af00Smrg 
615e9af00Smrg This file is part of the GNU MPFR Library.
715e9af00Smrg 
815e9af00Smrg The GNU MPFR Library is free software; you can redistribute it and/or modify
915e9af00Smrg it under the terms of the GNU Lesser General Public License as published by
1015e9af00Smrg the Free Software Foundation; either version 3 of the License, or (at your
1115e9af00Smrg option) any later version.
1215e9af00Smrg 
1315e9af00Smrg The GNU MPFR Library is distributed in the hope that it will be useful, but
1415e9af00Smrg WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
1515e9af00Smrg or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
1615e9af00Smrg License for more details.
1715e9af00Smrg 
1815e9af00Smrg You should have received a copy of the GNU Lesser General Public License
1915e9af00Smrg along with the GNU MPFR Library; see the file COPYING.LESSER.  If not, see
204da858a9Smrg https://www.gnu.org/licenses/ or write to the Free Software Foundation, Inc.,
2115e9af00Smrg 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. */
2215e9af00Smrg 
2315e9af00Smrg #include "mpfr-impl.h"
2415e9af00Smrg 
2515e9af00Smrg /* This is an internal function and one assumes that x is a non-special
2615e9af00Smrg  * number (more precisely, only its significand is considered and this
2715e9af00Smrg  * function can be used even if the exponent field of x has not been
2815e9af00Smrg  * initialized). It returns 1 (true) if |x| is a power of 2, else 0.
2915e9af00Smrg  */
3015e9af00Smrg 
3115e9af00Smrg int
mpfr_powerof2_raw(mpfr_srcptr x)3215e9af00Smrg mpfr_powerof2_raw (mpfr_srcptr x)
3315e9af00Smrg {
3415e9af00Smrg   /* This is an internal function, and we may call it with some
3515e9af00Smrg      wrong numbers (ie good mantissa but wrong flags or exp)
3615e9af00Smrg      So we don't want to test if it is a pure FP.
3715e9af00Smrg      MPFR_ASSERTN(MPFR_IS_PURE_FP(x)); */
386c7ec94dSmrg   return mpfr_powerof2_raw2 (MPFR_MANT(x), MPFR_LIMB_SIZE(x));
396c7ec94dSmrg }
406c7ec94dSmrg 
416c7ec94dSmrg int
mpfr_powerof2_raw2(const mp_limb_t * xp,mp_size_t xn)426c7ec94dSmrg mpfr_powerof2_raw2 (const mp_limb_t *xp, mp_size_t xn)
436c7ec94dSmrg {
446c7ec94dSmrg   if (xp[--xn] != MPFR_LIMB_HIGHBIT)
4515e9af00Smrg     return 0;
4615e9af00Smrg   while (xn > 0)
4715e9af00Smrg     if (xp[--xn] != 0)
4815e9af00Smrg       return 0;
4915e9af00Smrg   return 1;
5015e9af00Smrg }
51