Author Topic: 64bit atoqw or qwtoa in any library  (Read 2689 times)

K_F

  • Member
  • *****
  • Posts: 1287
  • Anybody out there?
64bit atoqw or qwtoa in any library
« on: May 19, 2015, 09:26:38 PM »
Just wondering...As in masm.lib where we have atodw and dwtoa functions.. is there anything available for 64 bit integers (signed and unsigned) ?
  :biggrin:
'Sire, Sire!... the peasants are Revolting !!!'
'Yes, they are.. aren't they....'

dedndave

  • Member
  • *****
  • Posts: 8734
  • Still using Abacus 2.0
    • DednDave
Re: 64bit atoqw or qwtoa in any library
« Reply #1 on: May 19, 2015, 09:40:11 PM »
i don't see any listed
but - this was a favorite topic of discussion in the old forum   :biggrin:
many good routines, including some fast ones by drizz, lingo, and jj - and others

i'm sure you're aware of my ling long kai fang routines (new forum) that will handle any practical size
signed, unsigned, and signed/unsigned selectable routines

K_F

  • Member
  • *****
  • Posts: 1287
  • Anybody out there?
Re: 64bit atoqw or qwtoa in any library
« Reply #2 on: May 19, 2015, 11:40:30 PM »
Didn't know that you spoke Chinese.. :P

No not aware of your stuff - New Forum!!  where ?
 8)
'Sire, Sire!... the peasants are Revolting !!!'
'Yes, they are.. aren't they....'

dedndave

  • Member
  • *****
  • Posts: 8734
  • Still using Abacus 2.0
    • DednDave
Re: 64bit atoqw or qwtoa in any library
« Reply #3 on: May 19, 2015, 11:58:51 PM »

dedndave

  • Member
  • *****
  • Posts: 8734
  • Still using Abacus 2.0
    • DednDave
Re: 64bit atoqw or qwtoa in any library
« Reply #4 on: May 20, 2015, 12:03:04 AM »
this is the thread in the old forum

http://www.masmforum.com/board/index.php?topic=12363

it's been downloaded almost 400 times   :biggrin:

K_F

  • Member
  • *****
  • Posts: 1287
  • Anybody out there?
Re: 64bit atoqw or qwtoa in any library
« Reply #5 on: May 20, 2015, 02:01:49 AM »
Thanks.. I'll have a look at your magnificent coding prowness  ;)
'Sire, Sire!... the peasants are Revolting !!!'
'Yes, they are.. aren't they....'

dedndave

  • Member
  • *****
  • Posts: 8734
  • Still using Abacus 2.0
    • DednDave
Re: 64bit atoqw or qwtoa in any library
« Reply #6 on: May 20, 2015, 02:06:14 AM »
it's not as fast as a dedicated 64-bit routine, of course
but, it fairs pretty well
and, it can handle any size integer you throw at it   :biggrin:

nidud

  • Member
  • *****
  • Posts: 1370
    • https://github.com/nidud/asmc
Re: 64bit atoqw or qwtoa in any library
« Reply #7 on: May 20, 2015, 06:03:04 AM »
A 32-bit ASCII to 64-bit (EDX:EAX) may be written like this:
Code: [Select]
OPTION  PROLOGUE:NONE, EPILOGUE:NONE

atol proc string
push esi
push edi
push ebx
mov ebx,12[esp+4]
xor ecx,ecx
@@:
mov al,[ebx]
inc ebx
cmp al,' '
je @B
push eax
cmp al,'-'
je @2
cmp al,'+'
jne @F
@2:
mov al,[ebx]
inc ebx
@@:
mov cl,al
xor eax,eax
xor edx,edx
@@:
sub cl,'0'
jc @F
cmp cl,9
ja @F
mov esi,edx
mov edi,eax
shl edx,3
shld eax,edx,3
add eax,edi
adc edx,esi
add eax,edi
adc edx,esi
add eax,ecx
adc edx,0
mov cl,[ebx]
inc ebx
jmp @B
@@:
pop ecx
cmp cl,'-'
jne @F
neg eax
neg edx
sbb eax,0
@@:
pop ebx
pop edi
pop esi
ret 4
atol endp

Converting to 64-bit using the same registers will be something like this:
Code: [Select]
xor rcx,rcx
@@:
movzx rax,byte ptr [rbx]
inc rbx
cmp rax,' '
je @B
push rax
cmp rax,'-'
je @2
cmp rax,'+'
jne @F
@2:
mov al,[rbx]
inc rbx
@@:
mov rcx,rax
xor rax,rax
@@:
sub rcx,'0'
jb @F
cmp rcx,9
ja @F
mov rdx,rax
shl rax,3
add rax,rdx
add rax,rdx
add rax,rcx
mov cl,[rbx]
inc rbx
jmp @B
@@:
pop rcx
cmp rcx,'-'
jne @F
neg rax
@@:

dedndave

  • Member
  • *****
  • Posts: 8734
  • Still using Abacus 2.0
    • DednDave
Re: 64bit atoqw or qwtoa in any library
« Reply #8 on: May 20, 2015, 06:40:40 AM »
even though the name of this thread is "qwtoa using xmm registers",
drizz shows a fast routine that does not use them in the old forum...

http://www.masmforum.com/board/index.php?topic=9857.msg72422#msg72422

it's for unsigned, but can be adapted

nidud

  • Member
  • *****
  • Posts: 1370
    • https://github.com/nidud/asmc
Re: 64bit atoqw or qwtoa in any library
« Reply #9 on: May 20, 2015, 07:17:24 AM »
Quote from: drizz
New & improved version! (removed division completely)  ;)

Interesting code.

I tried this a long time ago using shifts but never get it working correctly.

dedndave

  • Member
  • *****
  • Posts: 8734
  • Still using Abacus 2.0
    • DednDave
Re: 64bit atoqw or qwtoa in any library
« Reply #10 on: May 20, 2015, 07:26:45 AM »
 i think drizz uses "multiply-to-divide" to eliminate division
the following post in that thread also has a nice entry by Jochen (jj2007)

K_F

  • Member
  • *****
  • Posts: 1287
  • Anybody out there?
Re: 64bit atoqw or qwtoa in any library
« Reply #11 on: May 20, 2015, 05:13:28 PM »
That's funny.. I was thinking of something very similar to Jochen's method. Just hadn't worked out the finer details..
 :biggrin:
'Sire, Sire!... the peasants are Revolting !!!'
'Yes, they are.. aren't they....'

K_F

  • Member
  • *****
  • Posts: 1287
  • Anybody out there?
Re: 64bit atoqw or qwtoa in any library
« Reply #12 on: May 20, 2015, 08:06:51 PM »
Changed the plan... :biggrin:

Using the FPU... just a quick snippet.. haven't initted the FPU or done some checks... the basic idea

Code: [Select]
;------------------------------------------------------------------------------
;atoqw: Convert signed intger in Ascii format to 64 bit binary number via FPU
;
;Note: The FPU status is not saved or restored. I'll do this later
;------------------------------------------------------------------------------
atoqw Proc ptr_AQW:Dword, ptr_IQW:Dword

Local TmpInt:Dword

;--- CLEAR DESTINATION [EDI] ---
xor eax,eax ;Not sure what this means
mov edi, ptr_IQW ;64Bit number destination ptr
mov [edi], eax ;
mov [edi+4], eax ;

;--- NUMBER STRING LENGTH [ESI] --
mov esi, ptr_AQW ;Ascii string ptr
Invoke szLen, esi ;masm library function
mov ecx, eax ;
dec esi ;Zero offset

;--- LOAD FPU WITH CONSTANTS --
lea edx, TmpInt ;FPU needs Addr for Int loads
m2m TmpInt, 10 ;Stage multiplier
fild DWORD PTR [edx] ;[10]
m2m TmpInt, 1 ;Current stage
fild DWORD PTR [edx] ;[1,10]
fldz ;Running Total  [0,1,10]

Looper:
xor eax, eax ;clear debris
mov al, [esi+ecx] ;load ascii
cmp al, 0 ;just in case
jne @F ;

A2QW_End:
fistp QWORD PTR [edi] ;64 bit number
fistp DWORD PTR [edx] ;clear the barrel
fistp DWORD PTR [edx] ;
Return TRUE ;Indicate success
@@:
;--- IF NEGATIVE - CHANGE SIGN ---
cmp al,'-' ;
jne @F ;
fchs ;
jmp A2QW_End ;
@@:
;--- IF POSITIVE - NO WORRY ---
cmp al,'+'
jne @F
jmp A2QW_End
@@:
;--- CHECK NUMBER RANGE ---
cmp al, '0'
jge @F
jmp A2QW_Error
@@:
;--- ANYTHING ELSE - ERROR ---
cmp al,'9'
jle @F
jmp A2QW_Error
@@:
;--- NUMBER IN RANGE ---
sub eax, 30h ;Remove ascii
mov TmpInt,eax ;
fild DWORD PTR [edx] ;[Number,Total,1,10]
fmul st(0), st(2) ;
Faddp st(1), st(0) ;[Number + Total,1,10]
fmul st(1), st(2) ;Stage adjust [Total,Stage x 10,10]
Loop Looper
        jmp A2QW_End ;

;--- ERROR DUMP AND FLAG ---
A2QW_Error:
fistp DWORD PTR [edx] ;Clear barrel
fistp DWORD PTR [edx] ;
fistp DWORD PTR [edx] ;
ret FALSE ;
atoqw Endp
« Last Edit: May 21, 2015, 04:47:00 AM by K_F »
'Sire, Sire!... the peasants are Revolting !!!'
'Yes, they are.. aren't they....'

dedndave

  • Member
  • *****
  • Posts: 8734
  • Still using Abacus 2.0
    • DednDave
Re: 64bit atoqw or qwtoa in any library
« Reply #13 on: May 20, 2015, 08:51:02 PM »
that's great, if it suits your specific needs   :t
in some cases, however, you don't want to use FPU or XMM registers,
because they are holding caller values when the function is used   :P

K_F

  • Member
  • *****
  • Posts: 1287
  • Anybody out there?
Re: 64bit atoqw or qwtoa in any library
« Reply #14 on: May 21, 2015, 04:46:05 AM »
Ja.. one would have to keep an eye on that.
Batch processing a whole lot of string numbers (reading a large data file full of integer number strings)  , would make it worth while to save/restore the FPU/XMM registers.
Added a jmp after the loop  :P

This is for a 32 bit app at the mo. If it was a 64 bit app, then one could use nidud's plan
'Sire, Sire!... the peasants are Revolting !!!'
'Yes, they are.. aren't they....'