SSE & floating point

General FreeBASIC programming questions.
Post Reply
deltarho[1859]
Posts: 4292
Joined: Jan 02, 2017 0:34
Location: UK
Contact:

SSE & floating point

Post by deltarho[1859] »

Does anyone know how to load a xmm register with a single precision variable?
srvaldez
Posts: 3373
Joined: Sep 25, 2005 21:54

Re: SSE & floating point

Post by srvaldez »

hello deltarho[1859]
have tried movss [v] ?
deltarho[1859]
Posts: 4292
Joined: Jan 02, 2017 0:34
Location: UK
Contact:

Re: SSE & floating point

Post by deltarho[1859] »

That was my first failure of many. <smile> Thanks srvaldez.
frisian
Posts: 249
Joined: Oct 08, 2009 17:25

Re: SSE & floating point

Post by frisian »

Load a xmm register with a single. (dword)
Reg32 is a 32bit register, mem32 is a 4 byte memory location.
Movd xmm0, reg32/mem32
Movd reg32/mem32, xmm0

Load a xmm register with a double. (qword)
Reg64 is a 64bit register, mem64 is a 8 byte memory location
Movq xmm0, reg64/mem64
Movq reg64/mem64, xmm0

It's not possible to load a immediate value into xmm´s registers.

Code: Select all

Dim As Single  a1 = 3.1415 , a2 = 123.456, a3
Dim As Double d1

Asm
    mov eax,  0x40490E56    # Single=3.141499996185303
    movd xmm0, eax
    movd xmm1, [a2]
    mulss xmm0, xmm1
    movd [a3], xmm0
End Asm

Print
Print "a1 =";a1, "a2 =";a2
Print
Print "a1 * a2 ="; a1 * a2, "a3 ="; a3

Asm
    .data
    aa1: 
    .single 3.1415
    .single 123.456
    .text
    movd xmm0, [aa1]
    movd xmm1, [aa1 + 4]
    'multiply singles
    mulss xmm0, xmm1
    movd [a3], xmm0
End Asm

Print
Print "a1 =";a1, "a2 =";a2
Print
Print "a1 * a2 ="; a1 * a2, "a3 ="; a3


Asm
    .data
    ss1: 
    .single 3.1415
    .single 123.456
    .text
    movss xmm0, [ss1]
    movss xmm1, [ss1 + 4]
    mulss xmm0, xmm1
    movd [a3], xmm0
End Asm

Print
Print "a1 =";a1, "a2 =";a2
Print
Print "a1 * a2 ="; a1 * a2, "a3 ="; a3

Asm
    .data
    dd1: 
    .double 3.1415
    .double 123.456
    .text
    movq xmm0, [dd1]
    movq xmm1, [dd1 + 8]
    ' multipy doubles
    mulsd xmm0, xmm1
    movq [d1], xmm0
    ' convert double to single
    cvtsd2ss xmm1, xmm0
    movd [a3], xmm1
End Asm

Print
Print "single ="; a3, "double ="; d1

Sleep
End
Edit: Extended the sample program.
Last edited by frisian on May 13, 2019 20:37, edited 1 time in total.
deltarho[1859]
Posts: 4292
Joined: Jan 02, 2017 0:34
Location: UK
Contact:

Re: SSE & floating point

Post by deltarho[1859] »

Thanks frisian.

If 'movd xmm1, [a2]' is OK why didn't you use 'movd xmm0, [a1]'?
frisian
Posts: 249
Joined: Oct 08, 2009 17:25

Re: SSE & floating point

Post by frisian »

There a two ways to load a value into a xmm register. To show both methods I used the register method for xmm0 and the memory method for xmm1. I agree that the memory method is best way. It´s also possible to load integers into xmm registers and then convert them in single/double.

I extended the program in my previous post to include movss and the use of doubles.
deltarho[1859]
Posts: 4292
Joined: Jan 02, 2017 0:34
Location: UK
Contact:

Re: SSE & floating point [Solved]

Post by deltarho[1859] »

Thanks frisian, you have solved my problem. <smile>
jj2007
Posts: 2326
Joined: Oct 23, 2016 15:28
Location: Roma, Italia
Contact:

Re: SSE & floating point

Post by jj2007 »

Two dedicated instructions are available (plus several non-dedicated ones like movq, movlps, movups etc):

include \masm32\MasmBasic\MasmBasic.inc ; download
.data
s1 REAL4 11111.11111
s2 REAL4 22222.22222
s3 REAL4 33333.33333
s4 REAL4 44444.44444
s5 REAL4 55555.55555

Init
movss xmm1, s1
movd xmm2, s2
movq xmm3, s3
movlps xmm4, REAL8 ptr s4
movups xmm5, OWORD ptr s5
deb 1, "Just a test", s:xmm1, s:xmm2, s:xmm3, s:xmm4, s:xmm5
EndOfCode


Output:

Code: Select all

Just a test
s:xmm1          11111.1113
s:xmm2          22222.2227
s:xmm3          33333.3320
s:xmm4          44444.4453
s:xmm5          55555.5547
Note that #3 (movq xmm3, s3) moves a QWORD (aka double or REAL8), so it should correctly be written as movq xmm3, QWORD PTR s3; however, early MASM versions, UAsm and AsmC tolerate the shorter version.
Post Reply