FAQ
I've been working on cgo bindings for fftw and I've run into a dilemma. In
order to perform an in-place half-complex dft, both the input and output
arrays must be backed by the same array. For a forward transform the real
input array is of length n and the complex output array is of length n/2 +
1. The complex array is made up of n+2 floats while the real array is made
up of only n. FFTW requires that the float array be padded with the two
extra floats to be the same length in memory as the complex array. The last
two floats are never used when accessing or modifying the real array.


For now I allocate the complex array, then use reflection to assign the
underlying array of the complex array to the real array. Is there any
reason I should set the length of the real array to n and the capacity to
n+2 to account for the two padding floats or to just set both the length
and capacity to n since the last two floats are never used?

Set real array length and capacity to n:

cmplxLen := int(n>>1 + 1)
plan.Complex = make([]complex128, cmplxLen)

header := *(*reflect.SliceHeader)(unsafe.Pointer(&plan.Real))
header.Len = n
header.Cap = n
header.Data = uintptr(unsafe.Pointer(&plan.Complex[0]))

plan.Real = *(*[]float64)(unsafe.Pointer(&header))


Or set real array length to n and capacity to n+2:

cmplxLen := int(n>>1 + 1)
plan.Complex = make([]complex128, cmplxLen)

header := *(*reflect.SliceHeader)(unsafe.Pointer(&plan.Real))
header.Len = n
header.Cap = n+2
header.Data = uintptr(unsafe.Pointer(&plan.Complex[0]))

plan.Real = *(*[]float64)(unsafe.Pointer(&header))


I'm not especially familiar with the mechanics of go's garbage collection
or the low-level details of arrays and slicing in go. Any best-practices
advice is welcome even though doing this is already unsafe.

--
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+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Search Discussions

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedFeb 6, '14 at 3:10p
activeFeb 6, '14 at 3:10p
posts1
users1
websitegolang.org

1 user in discussion

BeMasher: 1 post

People

Translate

site design / logo © 2021 Grokbase