(root)/
gmp-6.3.0/
mpn/
s390_64/
z13/
sqr_basecase.c
       1  /* mpn_sqr_basecase -- Internal routine to square a natural number of length n.
       2     This is a place-holder for z13 to suppress the use of the plain z/arch code.
       3     FIXME: This should really be written in assembly with outer-loop early exit.
       4  
       5     THIS IS AN INTERNAL FUNCTION WITH A MUTABLE INTERFACE.  IT IS ONLY
       6     SAFE TO REACH THIS FUNCTION THROUGH DOCUMENTED INTERFACES.
       7  
       8  
       9  Copyright 1991-1994, 1996, 1997, 2000-2005, 2008, 2010, 2011, 2017, 2023 Free
      10  Software Foundation, Inc.
      11  
      12  This file is part of the GNU MP Library.
      13  
      14  The GNU MP Library is free software; you can redistribute it and/or modify
      15  it under the terms of either:
      16  
      17    * the GNU Lesser General Public License as published by the Free
      18      Software Foundation; either version 3 of the License, or (at your
      19      option) any later version.
      20  
      21  or
      22  
      23    * the GNU General Public License as published by the Free Software
      24      Foundation; either version 2 of the License, or (at your option) any
      25      later version.
      26  
      27  or both in parallel, as here.
      28  
      29  The GNU MP Library is distributed in the hope that it will be useful, but
      30  WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
      31  or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
      32  for more details.
      33  
      34  You should have received copies of the GNU General Public License and the
      35  GNU Lesser General Public License along with the GNU MP Library.  If not,
      36  see https://www.gnu.org/licenses/.  */
      37  
      38  #include "gmp-impl.h"
      39  #include "longlong.h"
      40  
      41  void
      42  mpn_sqr_basecase (mp_ptr rp, mp_srcptr up, mp_size_t un)
      43  {
      44    mp_limb_t u0;
      45    mp_limb_t cin;
      46  
      47    u0 = up[0];
      48    umul_ppmm (cin, rp[0], u0, u0);
      49    ++rp;
      50  
      51    if (--un) {
      52      u0 = u0 << 1;
      53      up += 1;
      54  
      55      rp[un] = mpn_mul_1c (rp, up, un, u0, cin);
      56  
      57      for (;;) {
      58        mp_limb_t ci, x0, c0, hi, lo, x1, c1;
      59  
      60        u0 = up[0];
      61        ci = -(up[-1] >> (GMP_NUMB_BITS-1)) & u0; // correction term
      62        x0 = rp[1] + ci;
      63        c0 = x0 < ci;
      64        hi, lo;
      65  
      66        umul_ppmm (hi, lo, u0, u0);
      67        x1 = x0 + lo;
      68        c1 = x1 < lo;
      69        cin = hi + c0 + c1;
      70        rp[1] = x1;
      71        rp += 2;
      72  
      73        if (--un == 0) break;
      74        u0 = (up[-1] >> (GMP_NUMB_BITS-1)) + (u0 << 1);
      75        up += 1;
      76  
      77        rp[un] = mpn_addmul_1c (rp, up, un, u0, cin);
      78      }
      79    }
      80  
      81    rp[0] = cin;
      82  }