dnl Intel P6 mpn_sqr_basecase -- square an mpn number.
dnl Copyright 1999, 2000, 2002 Free Software Foundation, Inc.
dnl This file is part of the GNU MP Library.
dnl
dnl The GNU MP Library is free software; you can redistribute it and/or modify
dnl it under the terms of either:
dnl
dnl * the GNU Lesser General Public License as published by the Free
dnl Software Foundation; either version 3 of the License, or (at your
dnl option) any later version.
dnl
dnl or
dnl
dnl * the GNU General Public License as published by the Free Software
dnl Foundation; either version 2 of the License, or (at your option) any
dnl later version.
dnl
dnl or both in parallel, as here.
dnl
dnl The GNU MP Library is distributed in the hope that it will be useful, but
dnl WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
dnl or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
dnl for more details.
dnl
dnl You should have received copies of the GNU General Public License and the
dnl GNU Lesser General Public License along with the GNU MP Library. If not,
dnl see https://www.gnu.org/licenses/.
include(`../config.m4')
C P6: approx 4.0 cycles per cross product, or 7.75 cycles per triangular
C product (measured on the speed difference between 20 and 40 limbs,
C which is the Karatsuba recursing range).
dnl These are the same as in mpn/x86/k6/sqr_basecase.asm, see that file for
dnl a description. The only difference here is that UNROLL_COUNT can go up
dnl to 64 (not 63) making SQR_TOOM2_THRESHOLD_MAX 67.
C void mpn_sqr_basecase (mp_ptr dst, mp_srcptr src, mp_size_t size);
C
C The algorithm is basically the same as mpn/generic/sqr_basecase.c, but a
C lot of function call overheads are avoided, especially when the given size
C is small.
C
C The code size might look a bit excessive, but not all of it is executed so
C it won't all get into the code cache. The 1x1, 2x2 and 3x3 special cases
C clearly apply only to those sizes; mid sizes like 10x10 only need part of
C the unrolled addmul; and big sizes like 40x40 that do use the full
C unrolling will least be making good use of it, because 40x40 will take
C something like 7000 cycles.
C -----------------------------------------------------------------------------
L(three_or_more):
C eax src low limb
C ebx
C ecx dst
C edx size
deflit(`FRAME',0)
leal (%esi,%edx,4), %esi C &src[size]
movl %eax, %ebp C multiplier
leal -4(%edi,%edx,4), %edi C &dst[size-1]
C This loop runs at just over 6 c/l.
L(mul_1):
C eax scratch
C ebx carry
C ecx counter, limbs, negative, -(size-1) to -1
C edx scratch
C esi &src[size]
C edi &dst[size-1]
C ebp multiplier
movl %ebp, %eax
mull (%esi,%ecx,4)
addl %ebx, %eax
movl $0, %ebx
adcl %edx, %ebx
movl %eax, 4(%edi,%ecx,4)
incl %ecx jnz L(mul_1)
movl %ebx, 4(%edi)
C Addmul src[n]*src[n+1..size-1] at dst[2*n-1...], for each n=1..size-2.
C
C The last two addmuls, which are the bottom right corner of the product
C triangle, are left to the end. These are src[size-3]*src[size-2,size-1]
C and src[size-2]*src[size-1]. If size is 4 then it's only these corner
C cases that need to be done.
C
C The unrolled code is the same as mpn_addmul_1(), see that routine for some
C comments.
C
C VAR_COUNTER is the outer loop, running from -(size-4) to -1, inclusive.
C
C VAR_JMP is the computed jump into the unrolled code, stepped by one code
C chunk each outer loop.
dnl This is also hard-coded in the address calculation below.
deflit(CODE_BYTES_PER_LIMB, 15)
dnl With &src[size] and &dst[size-1] pointers, the displacements in the
dnl unrolled code fit in a bytefor UNROLL_COUNT values up to 32, but above
dnl that an offset must be added to them.
deflit(OFFSET,
ifelse(eval(UNROLL_COUNT>32),1,
eval((UNROLL_COUNT-32)*4),
0))
C eax
C ebx carry
C ecx
C edx
C esi &src[size]
C edi &dst[size-1]
C ebp
C The calculated jump mustn't be before the start of the available
C code. This is the limit that UNROLL_COUNT puts on the src operand
C size, but checked here using the jump address directly.
C ----------------------------------------------------------------------------- ALIGN(16)
L(unroll_outer_top):
C eax
C ebx high limb to store
C ecx VAR_JMP
C edx VAR_COUNTER, limbs, negative
C esi &src[size], constant
C edi dst ptr, second highest limb of last addmul
C ebp
movl -12+OFFSET(%esi,%edx,4), %ebp C multiplier
movl %edx, VAR_COUNTER
movl -8+OFFSET(%esi,%edx,4), %eax C first limb of multiplicand
movl %eax, %ecx C low carry
leal CODE_BYTES_PER_LIMB(%edx), %edx
cmovX( %ebx, %ecx) C high carry reverse
cmovX( %eax, %ebx) C low carry reverse
movl %edx, VAR_JMP jmp *%edx
C Must be on an even address here so the low bit of the jump address
C will indicate which way around ecx/ebx should start.
ALIGN(2)
L(unroll_inner_start):
C eax scratch
C ebx carry high
C ecxcarry low
C edx scratch
C esi src pointer
C edi dst pointer
C ebp multiplier
C
C 15 code bytes each limb
C ecx/ebx reversed on each chunk
C ----------------------------------------------------------------------------- ALIGN(16)
L(corner):
C eax
C ebx
C ecx
C edx
C esi &src[size]
C edi &dst[2*size-5]
C ebp
C Left shift of dst[1..2*size-2], the bit shifted out becomes dst[2*size-1].
subl $1, %ecx C size-1
xorl %eax, %eax C ready for final adcl, and clear carry
movl %ecx, %edx
movl PARAM_SRC, %esi
L(lshift):
C eax
C ebx
C ecx counter, size-1 to 1
C edx size-1 (for later use)
C esi src (for later use)
C edi dst, incrementing
C ebp
rcll 4(%edi)
rcll 8(%edi)
leal 8(%edi), %edi
decl %ecx jnz L(lshift)
adcl %eax, %eax
movl %eax, 4(%edi) C dst most significant limb
movl (%esi), %eax C src[0]
leal 4(%esi,%edx,4), %esi C &src[size]
subl %edx, %ecx C -(size-1)
C Now add in the squares on the diagonal, src[0]^2, src[1]^2, ...,
C src[size-1]^2. dst[0] hasn't yet been set at all yet, and just gets the
C low limb of src[0]^2.
mull %eax
movl %eax, (%edi,%ecx,8) C dst[0]
L(diag):
C eax scratch
C ebx scratch
C ecx counter, negative
C edxcarry
C esi &src[size]
C edi dst[2*size-2]
C ebp
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.