summaryrefslogtreecommitdiff
path: root/gmp/mpn/generic/popham.c
blob: be7c525036145af3670a714c3382f71cdcce75a7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
/* mpn_popcount, mpn_hamdist -- mpn bit population count/hamming distance.

Copyright 1994, 1996, 2000, 2001, 2002, 2005 Free Software Foundation, Inc.

This file is part of the GNU MP Library.

The GNU MP Library is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 3 of the License, or (at your
option) any later version.

The GNU MP Library is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
License for more details.

You should have received a copy of the GNU Lesser General Public License
along with the GNU MP Library.  If not, see http://www.gnu.org/licenses/.  */

#include "gmp.h"
#include "gmp-impl.h"

#if OPERATION_popcount
#define FNAME mpn_popcount
#define POPHAM(u,v) u
#endif

#if OPERATION_hamdist
#define FNAME mpn_hamdist
#define POPHAM(u,v) u ^ v
#endif

unsigned long
FNAME (mp_srcptr up,
#if OPERATION_hamdist
       mp_srcptr vp,
#endif
       mp_size_t n)
{
  unsigned long result = 0;
  mp_limb_t p0, p1, p2, p3, x, p01, p23;
  mp_size_t i;

  ASSERT (n >= 1);		/* Actually, this code handles any n, but some
				   assembly implementations do not.  */

  for (i = n >> 2; i != 0; i--)
    {
      p0 = POPHAM (up[0], vp[0]);
      p0 -= (p0 >> 1) & MP_LIMB_T_MAX/3;				/* 2 0-2 */
      p0 = ((p0 >> 2) & MP_LIMB_T_MAX/5) + (p0 & MP_LIMB_T_MAX/5);	/* 4 0-4 */

      p1 = POPHAM (up[1], vp[1]);
      p1 -= (p1 >> 1) & MP_LIMB_T_MAX/3;				/* 2 0-2 */
      p1 = ((p1 >> 2) & MP_LIMB_T_MAX/5) + (p1 & MP_LIMB_T_MAX/5);	/* 4 0-4 */

      p01 = p0 + p1;							/* 8 0-8 */
      p01 = ((p01 >> 4) & MP_LIMB_T_MAX/17) + (p01 & MP_LIMB_T_MAX/17);	/* 8 0-16 */

      p2 = POPHAM (up[2], vp[2]);
      p2 -= (p2 >> 1) & MP_LIMB_T_MAX/3;				/* 2 0-2 */
      p2 = ((p2 >> 2) & MP_LIMB_T_MAX/5) + (p2 & MP_LIMB_T_MAX/5);	/* 4 0-4 */

      p3 = POPHAM (up[3], vp[3]);
      p3 -= (p3 >> 1) & MP_LIMB_T_MAX/3;				/* 2 0-2 */
      p3 = ((p3 >> 2) & MP_LIMB_T_MAX/5) + (p3 & MP_LIMB_T_MAX/5);	/* 4 0-4 */

      p23 = p2 + p3;							/* 8 0-8 */
      p23 = ((p23 >> 4) & MP_LIMB_T_MAX/17) + (p23 & MP_LIMB_T_MAX/17);	/* 8 0-16 */

      x = p01 + p23;							/* 8 0-32 */
      x = (x >> 8) + x;							/* 8 0-64 */
      x = (x >> 16) + x;						/* 8 0-128 */
#if GMP_LIMB_BITS > 32
      x = ((x >> 32) & 0xff) + (x & 0xff);				/* 8 0-256 */
      result += x;
#else
      result += x & 0xff;
#endif
      up += 4;
#if OPERATION_hamdist
      vp += 4;
#endif
    }

  n &= 3;
  if (n != 0)
    {
      x = 0;
      do
	{
	  p0 = POPHAM (up[0], vp[0]);
	  p0 -= (p0 >> 1) & MP_LIMB_T_MAX/3;				/* 2 0-2 */
	  p0 = ((p0 >> 2) & MP_LIMB_T_MAX/5) + (p0 & MP_LIMB_T_MAX/5);	/* 4 0-4 */
	  p0 = ((p0 >> 4) + p0) & MP_LIMB_T_MAX/17;			/* 8 0-8 */

	  x += p0;
	  up += 1;
#if OPERATION_hamdist
	  vp += 1;
#endif
	}
      while (--n);

      x = (x >> 8) + x;
      x = (x >> 16) + x;
#if GMP_LIMB_BITS > 32
      x = (x >> 32) + x;
#endif
      result += x & 0xff;
    }

  return result;
}