brender-v1.1.2/ZB/SAFEDIV.ASM
2022-05-04 18:14:23 -07:00

108 lines
1.6 KiB
NASM
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

;; Copyright (c) 1992,1993-1995 Argonaut Technologies Limited. All rights reserved.
;;
;; $Id: safediv.asm 1.5 1995/08/31 16:47:41 sam Exp $
;; $Locker: $
;;
;;
.386p
.model flat,c
.data
db '$Id: safediv.asm 1.5 1995/08/31 16:47:41 sam Exp $',0
align 4
.code
; result = edx:eax/divisor (0 on overflow)
;
safediv macro divisor
local pos1,pos2,zero,nzero;
; Get sign of result into ebx
mov ecx,divisor
mov ebx,ecx
xor ebx,edx
; Make divisor and dividend positive
test edx,edx
jns pos1
neg edx
neg eax
sbb edx,0
pos1:
; edx:eax now positive
test ecx,ecx
jz zero
jns pos2
neg ecx
pos2:
; Compare for overflow
cmp edx,ecx
jae zero
; edx<ecx - div *must* be safe now or I'll eat my hat NOT!
div ecx
; Give result the correct sign
sar ebx,31
xor eax,ebx
sub eax,ebx
jmp nzero
zero:
xor eax,eax
nzero:
endm
; result = (a*b + c*d)/e, 0 if overflow
;
SafeFixedMac2Div proc uses edx ebx ecx, pa: dword, pb: dword, pc: dword, pd: dword, pe: dword
mov eax,pa
imul pb
mov ebx,eax
mov ecx,edx
mov eax,pc
imul pd
add eax,ebx
adc edx,ecx
safediv pe
ret
zero:
xor eax,eax
ret
SafeFixedMac2Div endp
; result = (a*b + c*d + e*f)/g, 0 if overflow
;
SafeFixedMac3Div proc uses edx ebx ecx, pa: dword, pb: dword, pc: dword, pd: dword, pe: dword, pf: dword, pg: dword
mov eax,pa
imul pb
mov ebx,eax
mov ecx,edx
mov eax,pc
imul pd
add ebx,eax
adc ecx,edx
mov eax,pe
imul pf
add eax,ebx
adc edx,ecx
safediv pg
ret
zero:
xor eax,eax
ret
SafeFixedMac3Div endp
end