bjwbell / gensimd

SIMD for Go
MIT License
161 stars 10 forks source link

Thoughts on type conversions #3

Closed klauspost closed 4 years ago

klauspost commented 9 years ago

A things, that I cannot find any info on is type conversion.

Assume we have a slice a []byte, and we want to apply some transformation to it. As of now, there is no way to transfer it to a M128i without copying the data, unless I am missing it.

Function calls from gc generated code to gensimd code with M128i parameters will obviously be very slow, since all values are copied to the stack.

My proposition to solve this was to enable a way to alias the []byte to a []M128i slice. I implemented it using unsafe, but maybe it should be built-in functionality. Writes to the []M128i slice will be reflected in the original []byte slice, similar to how regular slicing works.

What are your thoughts in this regard?

bjwbell commented 9 years ago

For the builtin option, I imagine the gensimd function will be called with the original type, eg byte[], and in the gensimd function use a conversion function. Example:

func sse_example(data []byte) {
    convertedSlice := ConvertBytesToM128i(data)
    ...
}

The gensimd compiler can do the aliasing without copying (I'll add code to handle ConvertBytesToM128i in gensimd). I'm not tied to the builtin being named ConvertBytesToM128i, it's just an example.

Which method is better, using a builtin or using unsafe?

I'm not sure. I'm open either way. I'm inclined to having the builtin and falling back to unsafe for the native Go implementation of ConvertBytesToM128i. One of the gensimd goals is to stick as closely as possible to Go, with native Go fallbacks when possible.