1 dnl AMD K7 mpn_popcount, mpn_hamdist -- population count and hamming
4 dnl Copyright 2000, 2001, 2002 Free Software Foundation, Inc.
6 dnl This file is part of the GNU MP Library.
8 dnl The GNU MP Library is free software; you can redistribute it and/or
9 dnl modify it under the terms of the GNU Lesser General Public License as
10 dnl published by the Free Software Foundation; either version 3 of the
11 dnl License, or (at your option) any later version.
13 dnl The GNU MP Library is distributed in the hope that it will be useful,
14 dnl but WITHOUT ANY WARRANTY; without even the implied warranty of
15 dnl MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 dnl Lesser General Public License for more details.
18 dnl You should have received a copy of the GNU Lesser General Public License
19 dnl along with the GNU MP Library. If not, see http://www.gnu.org/licenses/.
21 include(`../config.m4')
26 C P3 model 9 (Banias) ? ?
27 C P3 model 13 (Dothan) 5.75 6
30 C unsigned long mpn_popcount (mp_srcptr src, mp_size_t size);
31 C unsigned long mpn_hamdist (mp_srcptr src, mp_srcptr src2, mp_size_t size);
33 C The code here is almost certainly not optimal, but is already a 3x speedup
34 C over the generic C code. The main improvement would be to interleave
35 C processing of two qwords in the loop so as to fully exploit the available
36 C execution units, possibly leading to 3.25 c/l (13 cycles for 4 limbs).
38 C The loop is based on the example "Efficient 64-bit population count using
39 C MMX instructions" in the Athlon Optimization Guide, AMD document 22007,
40 C page 158 of rev E (reference in mpn/x86/k7/README).
42 ifdef(`OPERATION_popcount',,
43 `ifdef(`OPERATION_hamdist',,
44 `m4_error(`Need OPERATION_popcount or OPERATION_hamdist defined
49 `ifdef(`OPERATION_hamdist',`$1')')
53 `ifdef(`OPERATION_popcount',`$1')')
56 defframe(PARAM_SIZE, 12)
57 defframe(PARAM_SRC2, 8)
58 defframe(PARAM_SRC, 4)
59 define(M4_function,mpn_hamdist)
62 defframe(PARAM_SIZE, 8)
63 defframe(PARAM_SRC, 4)
64 define(M4_function,mpn_popcount)
67 MULFUNC_PROLOGUE(mpn_popcount mpn_hamdist)
76 L(rodata_AAAAAAAAAAAAAAAA):
80 L(rodata_3333333333333333):
84 L(rodata_0F0F0F0F0F0F0F0F):
98 movl $0xAAAAAAAA, %eax
99 movl $0x33333333, %edx
104 movl $0x0F0F0F0F, %eax
115 movq L(rodata_AAAAAAAAAAAAAAAA), %mm7
116 movq L(rodata_3333333333333333), %mm6
117 movq L(rodata_0F0F0F0F0F0F0F0F), %mm5
121 define(REG_AAAAAAAAAAAAAAAA,%mm7)
122 define(REG_3333333333333333,%mm6)
123 define(REG_0F0F0F0F0F0F0F0F,%mm5)
124 define(REG_0000000000000000,%mm4)
128 HAM(` movl PARAM_SRC2, %edx')
130 pxor %mm2, %mm2 C total
135 movd (%eax,%ecx,8), %mm1
137 HAM(` movd (%edx,%ecx,8), %mm0
148 C ecx counter, qwords, decrementing
153 C mm2 total (low dword)
156 C mm5 | special constants
160 movq -8(%eax,%ecx,8), %mm1
162 HAM(` pxor -8(%edx,%ecx,8), %mm1')
167 pand REG_AAAAAAAAAAAAAAAA, %mm1
171 psubd %mm1, %mm0 C bit pairs
177 pand REG_3333333333333333, %mm0
178 pand REG_3333333333333333, %mm1
180 paddd %mm1, %mm0 C nibbles
186 pand REG_0F0F0F0F0F0F0F0F, %mm0
187 pand REG_0F0F0F0F0F0F0F0F, %mm1
189 paddd %mm1, %mm0 C bytes
194 paddd %mm0, %mm2 C add to total