github.com/aergoio/aergo@v1.3.1/libtool/src/gmp-6.1.2/mpf/pow_ui.c (about)

     1  /* mpf_pow_ui -- Compute b^e.
     2  
     3  Copyright 1998, 1999, 2001, 2012, 2015 Free Software Foundation, Inc.
     4  
     5  This file is part of the GNU MP Library.
     6  
     7  The GNU MP Library is free software; you can redistribute it and/or modify
     8  it under the terms of either:
     9  
    10    * the GNU Lesser General Public License as published by the Free
    11      Software Foundation; either version 3 of the License, or (at your
    12      option) any later version.
    13  
    14  or
    15  
    16    * the GNU General Public License as published by the Free Software
    17      Foundation; either version 2 of the License, or (at your option) any
    18      later version.
    19  
    20  or both in parallel, as here.
    21  
    22  The GNU MP Library is distributed in the hope that it will be useful, but
    23  WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
    24  or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    25  for more details.
    26  
    27  You should have received copies of the GNU General Public License and the
    28  GNU Lesser General Public License along with the GNU MP Library.  If not,
    29  see https://www.gnu.org/licenses/.  */
    30  
    31  #include "gmp.h"
    32  #include "gmp-impl.h"
    33  #include "longlong.h"
    34  
    35  /* This uses a plain right-to-left square-and-multiply algorithm.
    36  
    37     FIXME: When popcount(e) is not too small, it would probably speed things up
    38     to use a k-ary sliding window algorithm.  */
    39  
    40  void
    41  mpf_pow_ui (mpf_ptr r, mpf_srcptr b, unsigned long int e)
    42  {
    43    mpf_t t;
    44    int cnt;
    45  
    46    if (e <= 1)
    47      {
    48        if (e == 0)
    49  	mpf_set_ui (r, 1);
    50        else
    51  	mpf_set (r, b);
    52        return;
    53      }
    54  
    55    count_leading_zeros (cnt, (mp_limb_t) e);
    56    cnt = GMP_LIMB_BITS - 1 - cnt;
    57  
    58    /* Increase computation precision as a function of the exponent.  Adding
    59       log2(popcount(e) + log2(e)) bits should be sufficient, but we add log2(e),
    60       i.e. much more.  With mpf's rounding of precision to whole limbs, this
    61       will be excessive only when limbs are artificially small.  */
    62    mpf_init2 (t, mpf_get_prec (r) + cnt);
    63  
    64    mpf_set (t, b);		/* consume most significant bit */
    65    while (--cnt > 0)
    66      {
    67        mpf_mul (t, t, t);
    68        if ((e >> cnt) & 1)
    69  	mpf_mul (t, t, b);
    70      }
    71  
    72    /* Do the last iteration specially in order to save a copy operation.  */
    73    if (e & 1)
    74      {
    75        mpf_mul (t, t, t);
    76        mpf_mul (r, t, b);
    77      }
    78    else
    79      {
    80        mpf_mul (r, t, t);
    81      }
    82  
    83    mpf_clear (t);
    84  }