25112 Rev. 3.06 September 2005

Software Optimization Guide for AMD64 Processors

or

edx, '0'

; Convert

9th digit to ASCII.

mov

[edi+8], dl

; Store 9th digit in memory.

lea

edx, [esi+esi*4]

; 5 * fraction, new digit

EDX[31-11]

shr

ecx, 11

; 5th digit

 

or

ecx, '0'

; Convert

5th digit to ASCII.

mov

[edi+4], cl

; Store 5th digit in memory.

shr

edx, 11

; 10th digit

 

or

edx, '0'

; Convert

10th digit to ASCII.

mov

[edi+9], dx

; Store 10th digit and end marker in memory.

pop

ebx

; Restore

register as per

calling convention.

pop

esi

; Restore

register as per

calling convention.

pop

edi

; Restore

register as per

calling convention.

ret

8

; Pop two

DWORD arguments

and return.

}

}

Binary-to-ASCII Decimal Conversion Suppressing Leading Zeros

__declspec(naked) void __stdcall uint_to_ascii_nlz(char *sptr, unsigned int x)

{

__asm {

 

 

push

edi

; Save as per calling conventions.

push

ebx

; Save as per calling conventions.

mov

edi, [esp+12]

; sptr

mov

eax, [esp+16]

; x

mov

ecx, eax

; Save original argument.

mov

edx, 89705F41h

; 1e-9 * 2^61 rounded

mul

edx

; Divide by 1e9 by multiplying with reciprocal.

add

eax, eax

; Round division result.

adc

edx, 0

; EDX[31-29] = argument / 1e9

shr

edx, 29

; Leading decimal digit, 0...4

mov

eax, edx

; Leading digit

mov

ebx, edx

; Initialize digit accumulator with

 

 

; leading digit.

imul

eax, 1000000000

; Leading digit * 1e9

sub

ecx, eax

; Subtract (leading digit * 1e9) from argument.

or

dl, '0'

; Convert leading digit to ASCII.

mov

[edi], dl

; Store leading digit.

cmp

ebx, 1

; Any nonzero digit yet?

sbb

edi, -1

; Yes, increment ptr. No, keep old ptr.

mov

eax, ecx

; Get reduced argument < 1e9.

mov

edx, 0abcc7712h

; 2^28 / 1e8 * 2^30 rounded up

mul

edx

; Divide reduced

shr

eax, 30

; argument < 1e9 by 1e8,

lea

edx, [eax+4*edx+1]

; converting it into 4.28 fixed-point

mov

eax, edx

; format such that 1.0 = 2^28.

shr

eax, 28

; Next digit

and

edx, 0fffffffh

; Fraction part

or

ebx, eax

; Accumulate next digit.

or

eax, '0'

; Convert digit to ASCII.

mov

[edi], al

; Store digit in memory.

lea

eax, [edx*4+edx]

; 5 * fraction, new digit EAX[31-27]

Chapter 8

Integer Optimizations

183

Page 199
Image 199
AMD 250 manual Binary-to-ASCII Decimal Conversion Suppressing Leading Zeros, 183