gonum.org/v1/gonum@v0.14.0/mat/offset.go (about)

     1  // Copyright ©2015 The Gonum Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  //go:build !safe
     6  // +build !safe
     7  
     8  package mat
     9  
    10  import "unsafe"
    11  
    12  // offset returns the number of float64 values b[0] is after a[0].
    13  func offset(a, b []float64) int {
    14  	if &a[0] == &b[0] {
    15  		return 0
    16  	}
    17  	// This expression must be atomic with respect to GC moves.
    18  	// At this stage this is true, because the GC does not
    19  	// move. See https://golang.org/issue/12445.
    20  	return int(uintptr(unsafe.Pointer(&b[0]))-uintptr(unsafe.Pointer(&a[0]))) / int(unsafe.Sizeof(float64(0)))
    21  }
    22  
    23  // offsetComplex returns the number of complex128 values b[0] is after a[0].
    24  func offsetComplex(a, b []complex128) int {
    25  	if &a[0] == &b[0] {
    26  		return 0
    27  	}
    28  	// This expression must be atomic with respect to GC moves.
    29  	// At this stage this is true, because the GC does not
    30  	// move. See https://golang.org/issue/12445.
    31  	return int(uintptr(unsafe.Pointer(&b[0]))-uintptr(unsafe.Pointer(&a[0]))) / int(unsafe.Sizeof(complex128(0)))
    32  }