-
Notifications
You must be signed in to change notification settings - Fork 117
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2013 from ghaerr/watc
[libc] Add fast divmod.asm to Watcom printf
- Loading branch information
Showing
2 changed files
with
50 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
; Fast 32-bit combined divide and modulo routine | ||
; | ||
; unsigned long __divmod(unsigned long val, unsigned int *baserem) | ||
; Unsigned divide 32-bits by 16-bits | ||
; Store denominator in *baserem before calling | ||
; Returns 32-bit quotient in DX:AX and remainder in *baserem | ||
; | ||
; Designed for a fast replacement of the following code | ||
; unsigned int rem, base; | ||
; rem = val % base; | ||
; val = val / base; | ||
; New code: | ||
; rem = base; | ||
; val = __divmod(val, &rem); | ||
; | ||
; from by OpenWatcom ltoa.c __uldiv routine | ||
; 14 Sep 2024 Greg Haerr | ||
|
||
include mdef.inc | ||
include struct.inc | ||
|
||
modstart divmod | ||
|
||
xdefp "C",__divmod | ||
defpe __divmod | ||
|
||
; divides DX:AX / [BX] | ||
; returns DX:AX with remainder in [BX] | ||
|
||
xor cx,cx ; temp CX = 0 | ||
cmp dx,[bx] ; is upper 16 bits numerator less than denominator | ||
jb short fast ; yes - only one DIV needed | ||
xchg ax,dx ; AX = upper numerator, DX = lower numerator | ||
xchg cx,dx ; DX = 0, CX = lower numerator | ||
div word ptr [bx] ; AX = upper numerator / base, DX = remainder | ||
xchg ax,cx ; AX = lower numerator, CX = high quotient | ||
fast: | ||
div word ptr [bx] ; AX = lower numerator / base, DX = remainder | ||
mov [bx],dx ; store remainder | ||
mov dx,cx ; DX = high quotient, AX = low quotient | ||
ret | ||
|
||
__divmod endp | ||
|
||
endmod | ||
end |