-
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 #2012 from ghaerr/printf
[libc] Rewrite vfprintf.c and tiny_printf.c to use __divmod for speed
- Loading branch information
Showing
6 changed files
with
216 additions
and
91 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
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 |
---|---|---|
|
@@ -10,6 +10,7 @@ SRCS = \ | |
memset-s.S \ | ||
strcpy-s.S \ | ||
strlen-s.S \ | ||
divmod.S \ | ||
# end of list | ||
|
||
LEFTOUT = \ | ||
|
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,53 @@ | ||
// 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 which calls __udivsi3/__umodsi3: | ||
// unsigned int rem, base; | ||
// rem = val % base; | ||
// val = val / base; | ||
// New code: | ||
// rem = base; | ||
// val = __divmod(val, &rem); | ||
// | ||
// inspired by OpenWatcom ltoa.c __uldiv routine | ||
// 13 Sep 2024 Greg Haerr | ||
|
||
#include <libc-private/call-cvt.h> | ||
|
||
#define NUMLO 2+FAR_ADJ_ | ||
#define NUMHI 4+FAR_ADJ_ | ||
#define ADDR 6+FAR_ADJ_ | ||
|
||
.arch i8086, nojumps | ||
.code16 | ||
.text | ||
|
||
.global __divmod | ||
__divmod: | ||
#ifndef __IA16_CALLCVT_REGPARMCALL | ||
mov %sp,%bx | ||
mov NUMLO(%bx),%ax | ||
mov NUMHI(%bx),%dx | ||
mov ADDR(%bx),%bx | ||
#else | ||
mov %cx,%bx // AX:DX = val, BX = &rem | ||
#endif | ||
|
||
// divides DX:AX / [BX] | ||
// returns DX:AX with remainder in [BX] | ||
|
||
xor %cx,%cx // temp CX = 0 | ||
cmp (%bx),%dx // is upper 16 bits numerator less than denominator | ||
jb 1f // yes - only one DIV needed | ||
xchg %dx,%ax // AX = upper numerator, DX = lower numerator | ||
xchg %dx,%cx // DX = 0, CX = lower numerator | ||
divw (%bx) // AX = upper numerator / base, DX = remainder | ||
xchg %cx,%ax // AX = lower numerator, CX = high quotient | ||
1: divw (%bx) // AX = lower numerator / base, DX = remainder | ||
mov %dx,(%bx) // store remainder | ||
mov %cx,%dx // DX = high quotient, AX = low quotient | ||
RET_(6) |
Oops, something went wrong.