Author Topic: how to printf from simd registers  (Read 10800 times)

Offline markallyn

  • Jr. Member
  • *
  • Posts: 10
how to printf from simd registers
« on: March 13, 2017, 04:32:26 PM »
Hello everyone,

I'm new to sse2 and trying to get the hang of printing results with printf.  The following code prints out a simple greeting correctly.  Then I try to add two packed doubles and print the results from xmm0..  The correct addttion can be seen using gdb and printing the two doubles stashed in xmm0.   What printf prints is 3.3 and 2.2, not 3.3 and 5.5.  I'm doing (not doing?) something right.  Here's the code:

Quote
extern      printf
extern      exit

SEGMENT      .data
      fmt1  db "Getting started ...",0
      fmt2  db "%s",13,10,0
      fmt5  db "%f %f",13,10,0
      align 16
      val1 dq 1.1, 2.2
      align 16
      val2 dq 2.2, 3.3

SEGMENT      .text
global      _start
   _start:
      mov   rdi, fmt2
      mov   rsi, fmt1
      mov   rax, 0
      call   printf
      
      movapd   xmm0, [val1]
      movapd   xmm1, [val2]
      addpd   xmm0, xmm1  ;xmm0 contains 3.3 and 5.5
      
      mov   rdi, fmt5
      mov   rax, 1
      call   printf  ;printf prints 3.3 and 2.2
      
   
      mov   rdi, 0
      call    exit

Coompiled with:  nasm -g -felf64 -F dwarf xmm.s and linked with:  ld -dynamic-linker /lib64/ld-linker-x86-64.so.s -o xmm xmm.o -lc -lm

Could someone kindly suggest a solution?

Thanks,
Mark Allyn

Offline dreamCoder

  • Full Member
  • **
  • Posts: 107
Re: how to printf from simd registers
« Reply #1 on: March 13, 2017, 11:36:10 PM »
I think you should put the higher half of XMM0 into the lower half of XMM1 and use RAX = 2 since you're using two XMM registers. printf reads lower halfs.

one way;

Code: [Select]
      movapd xmm1,xmm0  ;copy
      psrldq xmm1,8  ;shift right by 8 bytes
      mov rdi,fmt5
      mov rax,2
      call printf


There are many others but the point is to get the higher xmm0 into lower xmm1 to get the second value.

Offline markallyn

  • Jr. Member
  • *
  • Posts: 10
Re: how to printf from simd registers
« Reply #2 on: March 14, 2017, 01:35:58 PM »
dreamCoder:

Thanks, that was very helpful