The MASM Forum

General => The Workshop => Topic started by: Siekmanski on June 24, 2013, 06:02:21 AM

Title: 64bit int to a double float with a SIMD instruction on a 32bit system.
Post by: Siekmanski on June 24, 2013, 06:02:21 AM
How to convert a 64bit int to a double float with a SIMD instruction on a 32bit system ?

I've tried this:

    cvtsi2sd    xmm0,qword ptr[eax]

It seems it only takes a 32bit int.

Title: Re: 64bit int to a double float with a SIMD instruction on a 32bit system.
Post by: qWord on June 24, 2013, 09:25:27 AM
maybe something like that:
.data
    qw QWORD -123456789123
.const
    align 16
    msk0 LABEL OWORD
         QWORD 1,0
    msk1 LABEL OWORD
         REAl8 1.0,65536.0
    msk2 LABEL OWORD
         REAL8 4294967296.0,281474976710656.0
    msk3 LABEL OWORD
         QWORD 8000000000000000h,0
.code
pcmpeqb xmm2,xmm2
mov eax,DWORD ptr qw[4]
movq xmm0,qw
.if SDWORD ptr eax < 0
    pandn xmm0,xmm2
    paddq xmm0,msk0
.endif
pxor xmm1,xmm1
punpcklwd xmm0,xmm1
pshufd xmm1,xmm0,1110y
cvtdq2pd xmm0,xmm0
cvtdq2pd xmm1,xmm1
mulpd xmm0,msk1
mulpd xmm1,msk2
addpd xmm1,xmm0
movapd xmm0,xmm1
shufpd xmm1,xmm0,1
addpd xmm0,xmm1
.if SDWORD ptr eax < 0
    xorpd xmm0,msk3
.endif
Title: Re: 64bit int to a double float with a SIMD instruction on a 32bit system.
Post by: jj2007 on June 24, 2013, 05:07:00 PM
You could use fild and fstp, too.
Title: Re: 64bit int to a double float with a SIMD instruction on a 32bit system.
Post by: Siekmanski on June 24, 2013, 06:31:52 PM
Thanks qWord and jj2007,
Thought there was a single SIMD instruction to do this.
Title: Re: 64bit int to a double float with a SIMD instruction on a 32bit system.
Post by: jj2007 on June 25, 2013, 05:21:59 AM
afaik there is no simple SSE instruction. Both qWord's and my solution will do the job. Time it...

fild MyQwInt
fstp MyDouble

or:

push eax
push edx
movlps [esp], xmm0  ; source
fild QWORD PTR [esp]
fstp REAL8 PTR [esp]
movlps xmm0, [esp]  ; dest
Title: Re: 64bit int to a double float with a SIMD instruction on a 32bit system.
Post by: Gunther on June 25, 2013, 06:34:47 AM
If the entire task isn't so time critical, I would prefer the old FPU solution, because it's more accurate.

Gunther
Title: Re: 64bit int to a double float with a SIMD instruction on a 32bit system.
Post by: Antariy on June 25, 2013, 11:46:34 AM
Quote from: Gunther on June 25, 2013, 06:34:47 AM
If the entire task isn't so time critical, I would prefer the old FPU solution, because it's more accurate.

Right, and I guess, FPU will be faster than multiple-instructions SSE solution :t
Title: Re: 64bit int to a double float with a SIMD instruction on a 32bit system.
Post by: Yuri on June 25, 2013, 05:08:50 PM
Not all 64-bit integers can be converted to a double, because its mantissa has only 53 bits. It looks like nobody cares, or am I missing something? :icon_cool:
Title: Re: 64bit int to a double float with a SIMD instruction on a 32bit system.
Post by: Siekmanski on June 25, 2013, 05:24:53 PM
I was just curious because it works with 64 bit assembly.

cvtsi2sd xmm0,rax                      ; convert qword to double
cvtsi2sd xmm0,dword ptr[mem] ; convert dword integer
Title: Re: 64bit int to a double float with a SIMD instruction on a 32bit system.
Post by: dedndave on June 25, 2013, 06:02:01 PM
Yuri
they can be converted, but you may lose some precision
i.e., some low-order bits will get thrown away for values larger than 53 bits
but, the max double is something like 1.8E+308

it would be easier if he wanted to convert to extended real - lol
that could be done very easily without SSE or FPU code
Title: Re: 64bit int to a double float with a SIMD instruction on a 32bit system.
Post by: Yuri on June 25, 2013, 07:17:48 PM
Quote from: dedndave on June 25, 2013, 06:02:01 PM
Yuri
they can be converted, but you may lose some precision
i.e., some low-order bits will get thrown away for values larger than 53 bits
Yes, that's exactly what I meant. If you lose some bits, it's not the same number anymore. So I would not call it conversion.
Title: Re: 64bit int to a double float with a SIMD instruction on a 32bit system.
Post by: qWord on June 25, 2013, 09:43:24 PM
Quote from: Yuri on June 25, 2013, 07:17:48 PMSo I would not call it conversion.
"conversion" is the correct terminology.
Also, you can safely assume that most people here aware of the precision problematic that comes along with FP calculations   :icon_cool:
Title: Re: 64bit int to a double float with a SIMD instruction on a 32bit system.
Post by: Yuri on June 25, 2013, 10:53:01 PM
Quote from: qWord on June 25, 2013, 09:43:24 PM
Quote from: Yuri on June 25, 2013, 07:17:48 PMSo I would not call it conversion.
"conversion" is the correct terminology.
Also, you can safely assume that most people here aware of the precision problematic that comes along with FP calculations   :icon_cool:
OK, maybe I misunderstood the term. After all, if the same bits were lost in the fractional part of a floating point number, I would not necessarily say the number had changed. It would depend on what precision I needed. Actually the difference between that case and losing bits in a large integer is in scale. For some reason it has never occured to me.  :icon_cool:
Title: Re: 64bit int to a double float with a SIMD instruction on a 32bit system.
Post by: Gunther on June 25, 2013, 10:56:05 PM
Hi qWord,

Quote from: qWord on June 25, 2013, 09:43:24 PM
"conversion" is the correct terminology.

that's right, but the term is a bit delusory in such circumstances.

Quote from: qWord on June 25, 2013, 09:43:24 PM
Also, you can safely assume that most people here aware of the precision problematic that comes along with FP calculations   :icon_cool:

That's for sure.

Gunther