Type errors for __m128 and unable to create functions with it

208 views
Skip to first unread message

Aditya Avinash

unread,
Nov 25, 2013, 5:15:38 AM11/25/13
to golan...@googlegroups.com
Hi All,

I am trying this code.

/*#cgo CFLAGS: -std=c99 -msse3 -w
#include<stdio.h>
#include<xmmintrin.h>

typedef __m128 m128;

*/
import "C"

import "fmt"

func SSE(x []float32){
        fmt.Println(C.m128(x[0]))
        fmt.Println(x)
}

This works fine if I am not using "C.m128"

I am getting these errors. What to do?

sse.go: In function 'typeof':
sse.go:8:14: error: expected declaration specifiers before '*' token
sse.go:9:24: error: expected expression before 'm128'
sse.go:10:1: error: parameter '__cgodebug_data' is initialized
sse.go:11:2: error: expected expression before 'm128'
sse.go:8:1: error: old-style parameter declarations in prototyped function definition
sse.go:8:1: error: parameter name omitted
sse.go:13:1: error: expected '{' at end of input

Thank you.

Péter Szilágyi

unread,
Nov 25, 2013, 5:51:32 AM11/25/13
to Aditya Avinash, golang-nuts
Hi,

  I haven't played too much with cgo, but after a few minutes, here's what I've got.
  • Direct conversion between float(s) and __m128 is unsafe and not recommended, instead, you should load/store your floats into an __m128 field.
  • You'll need to convert Go's float32 to C float (and a pointer, if passing the whole slice).
  • The compiler flags were just getting in my way (never played with them), and without the code seems to run just fine. Maybe somebody can shed some light on them :)
  I believe the code below does what you aimed for ;).

Cheers,
  Peter

PS: Of course, you could call the load method directly and not wrap it, I just thought it cleaner this way in this demo code.

package main

/*
#include<stdio.h>
#include<xmmintrin.h>

typedef __m128 m128;

m128 load(float* nums) {
return _mm_loadu_ps(nums);
}
*/
import "C"
import "fmt"

func main() {
SSE([]float32{0.0, 1.0, 2.0, 3.0})
}

func SSE(x []float32) {
fmt.Println(C.load((*C.float)(&x[0])))
fmt.Println(x)
}

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Aditya Avinash

unread,
Nov 25, 2013, 6:52:46 AM11/25/13
to golan...@googlegroups.com, Aditya Avinash
Hi Peter,

That's the answer I wanted! Thank you very much! Jumping on the floor!! :)

On Monday, November 25, 2013 4:21:32 PM UTC+5:30, Péter Szilágyi wrote:
Hi,

  I haven't played too much with cgo, but after a few minutes, here's what I've got.
  • Direct conversion between float(s) and __m128 is unsafe and not recommended, instead, you should load/store your floats into an __m128 field.
  • You'll need to convert Go's float32 to C float (and a pointer, if passing the whole slice).
  • The compiler flags were just getting in my way (never played with them), and without the code seems to run just fine. Maybe somebody can shed some light on them :)
About the flags, in most of the systems, SSE is not activated. By using -msse3 flag, it gets enabled by Gcc.
  I believe the code below does what you aimed for ;).

Yes, it worked perfect!
 

Cheers,
  Peter

PS: Of course, you could call the load method directly and not wrap it, I just thought it cleaner this way in this demo code.

I haven't tried this one!
 
package main

/*
#include<stdio.h>
#include<xmmintrin.h>

typedef __m128 m128;

m128 load(float* nums) {
return _mm_loadu_ps(nums);
}
*/
import "C"
import "fmt"

func main() {
SSE([]float32{0.0, 1.0, 2.0, 3.0})
}

func SSE(x []float32) {
fmt.Println(C.load((*C.float)(&x[0])))
fmt.Println(x)
}

Technically, _mm_load1_ps() takes any pointer. When I try this,

func main(){
    var z float32
    z = 0.1
    fmt.Println(C._mm_load1_ps((*C.float)(&z)))
    var i int
    i = 1
    fmt.Println(C._mm_load1_ps((*C.int)(&i)))
}


I am getting:

cannot convert &i (type *int) to type *_Ctype_int
cannot use (*_Ctype_int)(&i) (type *_Ctype_int) as type *_Ctype_float in function argument.


Message has been deleted

Péter Szilágyi

unread,
Nov 25, 2013, 7:44:08 AM11/25/13
to Aditya Avinash, golang-nuts
Hi,

  According to MSDN [1], _mm_load1_ps takes a float pointer, not arbitrary pointer, hence why the first call works, and the second results in the conversion error.

Cheers,
  Peter



Reply all
Reply to author
Forward
0 new messages