Annotation of OpenXM_contrib/gmp/mpz/tstbit.c, Revision 1.1.1.2
1.1.1.2 ! ohara 1: /* mpz_tstbit -- test a specified bit.
1.1 maekawa 2:
1.1.1.2 ! ohara 3: Copyright 2000, 2002 Free Software Foundation, Inc.
1.1 maekawa 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 the GNU Lesser General Public License as published by
9: the Free Software Foundation; either version 2.1 of the License, or (at your
10: option) any later version.
11:
12: The GNU MP Library is distributed in the hope that it will be useful, but
13: WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14: or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
15: License for more details.
16:
17: You should have received a copy of the GNU Lesser General Public License
18: along with the GNU MP Library; see the file COPYING.LIB. If not, write to
19: the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
20: MA 02111-1307, USA. */
21:
22: #include "gmp.h"
23: #include "gmp-impl.h"
24:
1.1.1.2 ! ohara 25:
! 26: /* For negatives the effective twos complement is achieved by negating the
! 27: limb tested, either with a ones or twos complement. Twos complement
! 28: ("-") is used if there's only zero limbs below the one being tested.
! 29: Ones complement ("~") is used if there's a non-zero below. Note that "-"
! 30: is correct even if the limb examined is 0 (and the true beginning of twos
! 31: complement is further up).
! 32:
! 33: Testing the limbs below p is unavoidable on negatives, but will usually
! 34: need to examine only *(p-1). The search is done from *(p-1) down to
! 35: *u_ptr, since that might give better cache locality, and because a
! 36: non-zero limb is perhaps a touch more likely in the middle of a number
! 37: than at the low end.
! 38:
! 39: Bits past the end of available data simply follow sign of u. Notice that
! 40: the limb_index >= abs_size test covers u=0 too. */
! 41:
1.1 maekawa 42: int
1.1.1.2 ! ohara 43: mpz_tstbit (mpz_srcptr u, unsigned long bit_index)
1.1 maekawa 44: {
1.1.1.2 ! ohara 45: mp_srcptr u_ptr = PTR(u);
! 46: mp_size_t size = SIZ(u);
! 47: unsigned abs_size = ABS(size);
! 48: unsigned long limb_index = bit_index / GMP_NUMB_BITS;
! 49: mp_srcptr p = u_ptr + limb_index;
! 50: mp_limb_t limb;
1.1 maekawa 51:
1.1.1.2 ! ohara 52: if (limb_index >= abs_size)
! 53: return (size < 0);
1.1 maekawa 54:
1.1.1.2 ! ohara 55: limb = *p;
! 56: if (size < 0)
! 57: {
! 58: limb = -limb; /* twos complement */
1.1 maekawa 59:
1.1.1.2 ! ohara 60: while (p != u_ptr)
! 61: {
! 62: p--;
! 63: if (*p != 0)
! 64: {
! 65: limb--; /* make it a ones complement instead */
! 66: break;
! 67: }
! 68: }
1.1 maekawa 69: }
1.1.1.2 ! ohara 70:
! 71: return (limb >> (bit_index % GMP_NUMB_BITS)) & 1;
1.1 maekawa 72: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>