Skip to content

Commit

Permalink
Merge pull request #2013 from ghaerr/watc
Browse files Browse the repository at this point in the history
[libc] Add fast divmod.asm to Watcom printf
  • Loading branch information
ghaerr authored Sep 14, 2024
2 parents e0dc002 + 047d048 commit 870f159
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 4 deletions.
8 changes: 4 additions & 4 deletions libc/stdio/vfprintf.c
Original file line number Diff line number Diff line change
Expand Up @@ -227,12 +227,12 @@ vfprintf(FILE *op, const char *fmt, va_list ap)
p = buf + sizeof(buf) - 1;
*p = '\0';
do {
#ifdef __WATCOMC__
c = v % radix;
v = v / radix;
#else
#if 1
c = radix;
v = __divmod(v, &c); /* remainder returned in c */
#else
c = v % radix;
v = v / radix;
#endif
if (c > 9)
*--p = ((*fmt == 'X')? 'A': 'a') - 10 + c;
Expand Down
46 changes: 46 additions & 0 deletions libc/watcom/asm/divmod.asm
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

0 comments on commit 870f159

Please sign in to comment.