Home | History | Annotate | Line # | Download | only in generic
popham.c revision 1.1
      1 /* mpn_popcount, mpn_hamdist -- mpn bit population count/hamming distance.
      2 
      3 Copyright 1994, 1996, 2000, 2001, 2002, 2005 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 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.
     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.  If not, see http://www.gnu.org/licenses/.  */
     19 
     20 #include "gmp.h"
     21 #include "gmp-impl.h"
     22 
     23 #if OPERATION_popcount
     24 #define FNAME mpn_popcount
     25 #define POPHAM(u,v) u
     26 #endif
     27 
     28 #if OPERATION_hamdist
     29 #define FNAME mpn_hamdist
     30 #define POPHAM(u,v) u ^ v
     31 #endif
     32 
     33 mp_bitcnt_t
     34 FNAME (mp_srcptr up,
     35 #if OPERATION_hamdist
     36        mp_srcptr vp,
     37 #endif
     38        mp_size_t n)
     39 {
     40   mp_bitcnt_t result = 0;
     41   mp_limb_t p0, p1, p2, p3, x, p01, p23;
     42   mp_size_t i;
     43 
     44   ASSERT (n >= 1);		/* Actually, this code handles any n, but some
     45 				   assembly implementations do not.  */
     46 
     47   for (i = n >> 2; i != 0; i--)
     48     {
     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 */
     52 
     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 */
     56 
     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 */
     59 
     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 */
     63 
     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 */
     67 
     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 */
     70 
     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 */
     76       result += x;
     77 #else
     78       result += x & 0xff;
     79 #endif
     80       up += 4;
     81 #if OPERATION_hamdist
     82       vp += 4;
     83 #endif
     84     }
     85 
     86   n &= 3;
     87   if (n != 0)
     88     {
     89       x = 0;
     90       do
     91 	{
     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 */
     96 
     97 	  x += p0;
     98 	  up += 1;
     99 #if OPERATION_hamdist
    100 	  vp += 1;
    101 #endif
    102 	}
    103       while (--n);
    104 
    105       x = (x >> 8) + x;
    106       x = (x >> 16) + x;
    107 #if GMP_LIMB_BITS > 32
    108       x = (x >> 32) + x;
    109 #endif
    110       result += x & 0xff;
    111     }
    112 
    113   return result;
    114 }
    115