(root)/
gmp-6.3.0/
mpz/
tstbit.c
       1  /* mpz_tstbit -- test a specified bit.
       2  
       3  Copyright 2000, 2002 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-impl.h"
      32  
      33  
      34  /* For negatives the effective twos complement is achieved by negating the
      35     limb tested, either with a ones or twos complement.  Twos complement
      36     ("-") is used if there's only zero limbs below the one being tested.
      37     Ones complement ("~") is used if there's a non-zero below.  Note that "-"
      38     is correct even if the limb examined is 0 (and the true beginning of twos
      39     complement is further up).
      40  
      41     Testing the limbs below p is unavoidable on negatives, but will usually
      42     need to examine only *(p-1).  The search is done from *(p-1) down to
      43     *u_ptr, since that might give better cache locality, and because a
      44     non-zero limb is perhaps a touch more likely in the middle of a number
      45     than at the low end.
      46  
      47     Bits past the end of available data simply follow sign of u.  Notice that
      48     the limb_index >= abs_size test covers u=0 too.  */
      49  
      50  int
      51  mpz_tstbit (mpz_srcptr u, mp_bitcnt_t bit_index) __GMP_NOTHROW
      52  {
      53    mp_srcptr      u_ptr      = PTR(u);
      54    mp_size_t      size       = SIZ(u);
      55    unsigned       abs_size   = ABS(size);
      56    mp_size_t      limb_index = bit_index / GMP_NUMB_BITS;
      57    mp_srcptr      p          = u_ptr + limb_index;
      58    mp_limb_t      limb;
      59  
      60    if (limb_index >= abs_size)
      61      return (size < 0);
      62  
      63    limb = *p;
      64    if (size < 0)
      65      {
      66        limb = -limb;     /* twos complement */
      67  
      68        while (p != u_ptr)
      69  	{
      70  	  p--;
      71  	  if (*p != 0)
      72  	    {
      73  	      limb--;	/* make it a ones complement instead */
      74  	      break;
      75  	    }
      76  	}
      77      }
      78  
      79    return (limb >> (bit_index % GMP_NUMB_BITS)) & 1;
      80  }