1 dnl x86 mpn_divexact_by3 -- mpn division by 3, expecting no remainder.
4 dnl Copyright (C) 2000 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 2.1 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
19 dnl License along with the GNU MP Library; see the file COPYING.LIB. If
20 dnl not, write to the Free Software Foundation, Inc., 59 Temple Place -
21 dnl Suite 330, Boston, MA 02111-1307, USA.
24 dnl The following all have their own optimized versions of this routine,
25 dnl but for reference the code here runs as follows.
35 include(`../config.m4')
38 C mp_limb_t mpn_divexact_by3c (mp_ptr dst, mp_srcptr src, mp_size_t size,
41 defframe(PARAM_CARRY,16)
42 defframe(PARAM_SIZE, 12)
43 defframe(PARAM_SRC, 8)
44 defframe(PARAM_DST, 4)
46 dnl multiplicative inverse of 3, modulo 2^32
47 deflit(INVERSE_3, 0xAAAAAAAB)
49 dnl ceil(b/3) and ceil(b*2/3) where b=2^32
50 deflit(ONE_THIRD_CEIL, 0x55555556)
51 deflit(TWO_THIRDS_CEIL, 0xAAAAAAAB)
56 PROLOGUE(mpn_divexact_by3c)
60 pushl %ebp FRAME_pushl()
63 pushl %edi FRAME_pushl()
66 pushl %esi FRAME_pushl()
69 pushl %ebx FRAME_pushl()
71 leal (%ecx,%ebp,4), %ecx
72 movl PARAM_CARRY, %ebx
74 leal (%edi,%ebp,4), %edi
80 C eax scratch, low product
81 C ebx carry limb (0 to 3)
83 C edx scratch, high product
86 C ebp counter, limbs, negative
88 movl (%ecx,%ebp,4), %eax
96 cmpl $ONE_THIRD_CEIL, %eax
97 movl %eax, (%edi,%ebp,4)
99 sbbl $-1, %ebx C +1 if eax>=ceil(b/3)
100 cmpl $TWO_THIRDS_CEIL, %eax
102 sbbl $-1, %ebx C +1 if eax>=ceil(b*2/3)