The MASM Forum

General => The Workshop => Topic started by: K_F on May 19, 2015, 09:26:38 PM

Title: 64bit atoqw or qwtoa in any library
Post by: K_F 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:
Title: Re: 64bit atoqw or qwtoa in any library
Post by: dedndave 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
Title: Re: 64bit atoqw or qwtoa in any library
Post by: K_F 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)
Title: Re: 64bit atoqw or qwtoa in any library
Post by: dedndave on May 19, 2015, 11:58:51 PM
new forum = this forum - lol

http://masm32.com/board/index.php?topic=222.0 (http://masm32.com/board/index.php?topic=222.0)
Title: Re: 64bit atoqw or qwtoa in any library
Post by: dedndave 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 (http://www.masmforum.com/board/index.php?topic=12363)

it's been downloaded almost 400 times   :biggrin:
Title: Re: 64bit atoqw or qwtoa in any library
Post by: K_F on May 20, 2015, 02:01:49 AM
Thanks.. I'll have a look at your magnificent coding prowness  ;)
Title: Re: 64bit atoqw or qwtoa in any library
Post by: dedndave 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:
Title: Re: 64bit atoqw or qwtoa in any library
Post by: nidud on May 20, 2015, 06:03:04 AM
deleted
Title: Re: 64bit atoqw or qwtoa in any library
Post by: dedndave 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 (http://www.masmforum.com/board/index.php?topic=9857.msg72422#msg72422)

it's for unsigned, but can be adapted
Title: Re: 64bit atoqw or qwtoa in any library
Post by: nidud on May 20, 2015, 07:17:24 AM
deleted
Title: Re: 64bit atoqw or qwtoa in any library
Post by: dedndave 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)
Title: Re: 64bit atoqw or qwtoa in any library
Post by: K_F 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:
Title: Re: 64bit atoqw or qwtoa in any library
Post by: K_F 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

;------------------------------------------------------------------------------
;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
Title: Re: 64bit atoqw or qwtoa in any library
Post by: dedndave 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
Title: Re: 64bit atoqw or qwtoa in any library
Post by: K_F 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
Title: Re: 64bit atoqw or qwtoa in any library
Post by: jj2007 on May 21, 2015, 06:55:25 AM
Quote from: K_F on May 21, 2015, 04:46:05 AMBatch processing a whole lot of string numbers (reading a large data file full of integer number strings)

How big? Can you post an example file? It could be a candidate for StringToArray():

include \masm32\MasmBasic\MasmBasic.inc      ; download (http://masm32.com/board/index.php?topic=94.0)
.data
txtData      db "12.34 56.78 90.12 34.56 78.90 12.34 $FF", 13, 10
      db "-12.34 -56.78 -90.12 -34.56 -78.90 -12.34 0xFF 1111b 1.234e56", 0      ; see Val (http://www.webalice.it/jj2006/MasmBasicQuickReference.htm#Mb1202)

  Init
  Dim MyData() As REAL8      ; could also be WORD, DWORD, QWORD, REAL4, REAL10
  StringToArray offset txtData, MyData()      ; do the conversion from text to binary
  For_ ecx=0 To eax
      Print Str$("%i\t", ecx), Str$("%4f\n", MyData(ecx))
  Next
  Inkey Str$("%i elements found", ecx)
  Exit
end start


Output:
0       12.34
1       56.78
2       90.12
3       34.56
4       78.90
5       12.34
6       255.0
7       -12.34
8       -56.78
9       -90.12
10      -34.56
11      -78.90
12      -12.34
13      255.0
14      15.00
15      1.234e+56
16 elements found


P.S.: StringToArray FileRead$("Data.txt"), MyData() is also valid syntax.
Title: Re: 64bit atoqw or qwtoa in any library
Post by: K_F on May 21, 2015, 03:12:57 PM
Quote from: jj2007 on May 21, 2015, 06:55:25 AM
Quote from: K_F on May 21, 2015, 04:46:05 AMBatch processing a whole lot of string numbers (reading a large data file full of integer number strings)
How big?
I'm thinking of anywhere from 1K to 64Ks worth of just numbers
Title: Re: 64bit atoqw or qwtoa in any library
Post by: jj2007 on May 21, 2015, 04:34:45 PM
64k will take about one millisecond with StringToArray.
Title: Re: 64bit atoqw or qwtoa in any library
Post by: K_F on May 21, 2015, 04:46:09 PM
Interesting...Is StringToArray available for perusal  :biggrin:

I might add that I'm looking at converting to/from a text file, (un)signed integers 8/16/32/64bits and floats (Real-4/8&10)
:t
Title: Re: 64bit atoqw or qwtoa in any library
Post by: jj2007 on May 21, 2015, 04:49:23 PM
Sure, just install MB, the link is below.

include \masm32\MasmBasic\MasmBasic.inc      ; download (http://masm32.com/board/index.php?topic=94.0)
  Init
  Dim MyData() As REAL8      ; could also be WORD, DWORD, QWORD, REAL4, REAL10
  StringToArray FileRead$("DataBig.txt"), MyData(), staHasText
  For_ ecx=0 To eax
      .if !(ecx & 15)
            Print Str$("\n%__i\t", ecx)
      .endif
      Print Str$("%4f\t", MyData(ecx))
  Next
  Inkey Str$("\n%i elements found", ecx)
  Exit
end start
Title: Re: 64bit atoqw or qwtoa in any library
Post by: dedndave on May 21, 2015, 08:17:47 PM
i don't think that's what he meant - lol