ulldiv实现
来源:互联网 发布:windows与linux双系统 编辑:程序博客网 时间:2024/04/21 00:19
title ulldiv - unsigned long divide routine
;***
;ulldiv.asm - unsigned long divide routine
;
; Copyright (c) 1985-1997, Microsoft Corporation. All rights reserved.
;
;Purpose:
; defines the unsigned long divide routine
; __aulldiv
;
;*******************************************************************************
.xlist
include cruntime.inc
include mm.inc
.list
;***
;ulldiv - unsigned long divide
;
;Purpose:
; Does a unsigned long divide of the arguments. Arguments are
; not changed.
;
;Entry:
; Arguments are passed on the stack:
; 1st pushed: divisor (QWORD)
; 2nd pushed: dividend (QWORD)
;
;Exit:
; EDX:EAX contains the quotient (dividend/divisor)
; NOTE: this routine removes the parameters from the stack.
;
;Uses:
; ECX
;
;Exceptions:
;
;*******************************************************************************
CODESEG
_aulldiv PROC NEAR
push ebx
push esi
; Set up the local stack and save the index registers. When this is done
; the stack frame will look as follows (assuming that the expression a/b will
; generate a call to uldiv(a, b)):
;
; -----------------
; | |
; |---------------|
; | |
; |--divisor (b)--|
; | |
; |---------------|
; | |
; |--dividend (a)-|
; | |
; |---------------|
; | return addr** |
; |---------------|
; | EBX |
; |---------------|
; ESP---->| ESI |
; -----------------
;
DVND equ [esp + 12] ; stack address of dividend (a)
DVSR equ [esp + 20] ; stack address of divisor (b)
;
; Now do the divide. First look to see if the divisor is less than 4194304K.
; If so, then we can use a simple algorithm with word divides, otherwise
; things get a little more complex.
;
mov eax,HIWORD(DVSR) ; check to see if divisor < 4194304K
or eax,eax
jnz short L1 ; nope, gotta do this the hard way
mov ecx,LOWORD(DVSR) ; load divisor
mov eax,HIWORD(DVND) ; load high word of dividend
xor edx,edx
div ecx ; get high order bits of quotient
mov ebx,eax ; save high bits of quotient
mov eax,LOWORD(DVND) ; edx:eax <- remainder:lo word of dividend
div ecx ; get low order bits of quotient
mov edx,ebx ; edx:eax <- quotient hi:quotient lo
jmp short L2 ; restore stack and return
;
; Here we do it the hard way. Remember, eax contains DVSRHI
;
L1:
mov ecx,eax ; ecx:ebx <- divisor
mov ebx,LOWORD(DVSR)
mov edx,HIWORD(DVND) ; edx:eax <- dividend
mov eax,LOWORD(DVND)
L3:
shr ecx,1 ; shift divisor right one bit; hi bit <- 0
rcr ebx,1
shr edx,1 ; shift dividend right one bit; hi bit <- 0
rcr eax,1
or ecx,ecx
jnz short L3 ; loop until divisor < 4194304K
div ebx ; now divide, ignore remainder
mov esi,eax ; save quotient
;
; We may be off by one, so to check, we will multiply the quotient
; by the divisor and check the result against the orignal dividend
; Note that we must also check for overflow, which can occur if the
; dividend is close to 2**64 and the quotient is off by 1.
;
mul dword ptr HIWORD(DVSR) ; QUOT * HIWORD(DVSR)
mov ecx,eax
mov eax,LOWORD(DVSR)
mul esi ; QUOT * LOWORD(DVSR)
add edx,ecx ; EDX:EAX = QUOT * DVSR
jc short L4 ; carry means Quotient is off by 1
;
; do long compare here between original dividend and the result of the
; multiply in edx:eax. If original is larger or equal, we are ok, otherwise
; subtract one (1) from the quotient.
;
cmp edx,HIWORD(DVND) ; compare hi words of result and original
ja short L4 ; if result > original, do subtract
jb short L5 ; if result < original, we are ok
cmp eax,LOWORD(DVND) ; hi words are equal, compare lo words
jbe short L5 ; if less or equal we are ok, else subtract
L4:
dec esi ; subtract 1 from quotient
L5:
xor edx,edx ; edx:eax <- quotient
mov eax,esi
;
; Just the cleanup left to do. edx:eax contains the quotient.
; Restore the saved registers and return.
;
L2:
pop esi
pop ebx
ret 16
_aulldiv ENDP
求助!! 关于ulldiv的实现
pigsanddogs
我爱吃猪肉,但是长不胖,为什么??
发表于:2007-08-31 18:05:36 楼主
对于64位无符号除法, vc调用一个库函数_ulldiv来实现,
原代码在rtl/intel/ulldiv.asm有实现,简单描述成c语言就是这样的
假设a,b,c都是64位数, 那么要求c = a/b
a ' = a;
b ' = b;
if (b ' < 0x100000000)
{
return a '/b '; //因为如果b能用一个dword表示, 那么可以edx:eax div ecx实现
}
while(b >= 0x10000000) //否则, 就把b和a同时右移, 移动到b能放到一个dword结束
{
b ' > >= 2;
a ' > >= 2;
}
c = a '/b '; // 这个时候,再次可以用edx:eax div ecx来实现.
if (c*b > a) --c; // 如果c*b比a要大的话, 说明刚得到的c比实际的c要少一.
return c;
其实算法很巧妙, 而且人也非常容易理解.
我把2个数都缩小一点, 然后来除,
但问题是:
怎么证明这个都缩小以后算出来的c就比原来的c大1, 或者跟原来的c一样的大?
ps: 这个问题困惑了我1个月, 天天晚上就是想办法证明,
但总是做不出来. 现在睡觉都睡不好了. 帮我解决下, 另外我在送100分.
问题点数:30 回复次数:5 显示所有回复显示星级回复显示楼主回复
galois_godel
等 级:
发表于:2007-09-01 14:12:431楼 得分:0
对吗?
a=2^63=9223372036854775808
b=2^32+3=4294967299
c=a/b=2147483646
b '=b > >2=1073741824
a '=a > >2=2305843009213693952
c '=a '/b '=2147483648
相差2拉
galois_godel
等 级:
发表于:2007-09-01 14:14:392楼 得分:0
如果a <=2^60 可以证明是正确的
如果每次只一位的话,a <=2^62时也是对的
galois_godel
等 级:
发表于:2007-09-01 14:20:063楼 得分:0
每次只移一位的话,a <=2^64 都是对的,也就说就对了
galois_godel
等 级:
发表于:2007-09-01 14:34:134楼 得分:0
shr ecx,1 ; shift divisor right one bit; hi bit <- 0
rcr ebx,1
shr edx,1 ; shift dividend right one bit; hi bit <- 0
rcr eax,1
好像就是一位一位移的阿
galois_godel
等 级:
发表于:2007-09-01 14:46:365楼 得分:0
证明如下:
c=a/b=(a 'N+a0)/(b 'N+b0) <=(a 'N+N-1)/(b 'N)=(a '+(N-1)/N)/b '=c '
c=a/b=(a 'N+a0)/(b 'N+b0) >=(a 'N)/(b 'N+N-1)=c '/(1+(N-1)/(b 'N))
c+c*(N-1)/(b 'N) >=c ' (1)
注意到b ' >=2^31
如果b >=2^33
c=a/b <=2^64/2^33=2^31 <=b '
c*(N-1)/(b 'N) <=1 则 c+1 >=c ' (2)
如果b <2^33 那么移一位就可以了,也就是N=2
c=a/b <=2^64/2^32=2^32
c*(N-1)/(b 'N)=c/(2b ') <=2^32/(2*2^31)=1
也可以得出 c+1 >=c ' (2)
综合 (1),(2), c= <c ' <=c+1
也是 把c '=c, 或者 c '=c+1 就可以了
- ulldiv实现
- 实现
- 实现
- 红黑树实现 实现代码
- java实现排列组合实现
- 实现Runnable 实现线程
- 实现ViewPager多种实现
- 双向LSTM实现实现
- 实现缓存 java实现
- 三子棋的实现的实现的实现
- 四则运算实现
- 继承实现
- 重载实现
- 实现缩略图
- split实现
- 实现缩略图
- wmi实现
- 实现缩略图
- 在VC中使用VB制作的控件
- iTechTag:声望创造价值
- C++:类的大小(sizeof)
- vc++中各种字符串(转载)
- ARM经典300问
- ulldiv实现
- AJAX传值技巧--"php://input"的特殊文件
- C#-- 几个C#编程的小技巧[转]
- 求高人帮我解答以下几道题目
- 给想回国从事IT工作的参考:国内IT工资探讨
- 男人要会伤心 女人要会愤怒
- 2007.09.01 想改变一下生活
- 同事成下属,关系如何处?
- 函数返回值的问题