1 /* mpn_popcount, mpn_hamdist -- mpn bit population count/hamming distance.
3 Copyright 1994, 1996, 2000, 2001, 2002, 2005 Free Software Foundation, Inc.
5 This file is part of the GNU MP Library.
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 3 of the License, or (at your
10 option) any later version.
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.
17 You should have received a copy of the GNU Lesser General Public License
18 along with the GNU MP Library. If not, see http://www.gnu.org/licenses/. */
23 #if OPERATION_popcount
24 #define FNAME mpn_popcount
29 #define FNAME mpn_hamdist
30 #define POPHAM(u,v) u ^ v
40 mp_bitcnt_t result = 0;
41 mp_limb_t p0, p1, p2, p3, x, p01, p23;
44 ASSERT (n >= 1); /* Actually, this code handles any n, but some
45 assembly implementations do not. */
47 for (i = n >> 2; i != 0; i--)
49 p0 = POPHAM (up[0], vp[0]);
50 p0 -= (p0 >> 1) & MP_LIMB_T_MAX/3; /* 2 0-2 */
51 p0 = ((p0 >> 2) & MP_LIMB_T_MAX/5) + (p0 & MP_LIMB_T_MAX/5); /* 4 0-4 */
53 p1 = POPHAM (up[1], vp[1]);
54 p1 -= (p1 >> 1) & MP_LIMB_T_MAX/3; /* 2 0-2 */
55 p1 = ((p1 >> 2) & MP_LIMB_T_MAX/5) + (p1 & MP_LIMB_T_MAX/5); /* 4 0-4 */
57 p01 = p0 + p1; /* 8 0-8 */
58 p01 = ((p01 >> 4) & MP_LIMB_T_MAX/17) + (p01 & MP_LIMB_T_MAX/17); /* 8 0-16 */
60 p2 = POPHAM (up[2], vp[2]);
61 p2 -= (p2 >> 1) & MP_LIMB_T_MAX/3; /* 2 0-2 */
62 p2 = ((p2 >> 2) & MP_LIMB_T_MAX/5) + (p2 & MP_LIMB_T_MAX/5); /* 4 0-4 */
64 p3 = POPHAM (up[3], vp[3]);
65 p3 -= (p3 >> 1) & MP_LIMB_T_MAX/3; /* 2 0-2 */
66 p3 = ((p3 >> 2) & MP_LIMB_T_MAX/5) + (p3 & MP_LIMB_T_MAX/5); /* 4 0-4 */
68 p23 = p2 + p3; /* 8 0-8 */
69 p23 = ((p23 >> 4) & MP_LIMB_T_MAX/17) + (p23 & MP_LIMB_T_MAX/17); /* 8 0-16 */
71 x = p01 + p23; /* 8 0-32 */
72 x = (x >> 8) + x; /* 8 0-64 */
73 x = (x >> 16) + x; /* 8 0-128 */
74 #if GMP_LIMB_BITS > 32
75 x = ((x >> 32) & 0xff) + (x & 0xff); /* 8 0-256 */
92 p0 = POPHAM (up[0], vp[0]);
93 p0 -= (p0 >> 1) & MP_LIMB_T_MAX/3; /* 2 0-2 */
94 p0 = ((p0 >> 2) & MP_LIMB_T_MAX/5) + (p0 & MP_LIMB_T_MAX/5); /* 4 0-4 */
95 p0 = ((p0 >> 4) + p0) & MP_LIMB_T_MAX/17; /* 8 0-8 */
107 #if GMP_LIMB_BITS > 32