github.com/jingcheng-WU/gonum@v0.9.1-0.20210323123734-f1a2a11a8f7b/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  // +build !safe
     6  
     7  package mat
     8  
     9  import "unsafe"
    10  
    11  // offset returns the number of float64 values b[0] is after a[0].
    12  func offset(a, b []float64) int {
    13  	if &a[0] == &b[0] {
    14  		return 0
    15  	}
    16  	// This expression must be atomic with respect to GC moves.
    17  	// At this stage this is true, because the GC does not
    18  	// move. See https://golang.org/issue/12445.
    19  	return int(uintptr(unsafe.Pointer(&b[0]))-uintptr(unsafe.Pointer(&a[0]))) / int(unsafe.Sizeof(float64(0)))
    20  }
    21  
    22  // offsetComplex returns the number of complex128 values b[0] is after a[0].
    23  func offsetComplex(a, b []complex128) int {
    24  	if &a[0] == &b[0] {
    25  		return 0
    26  	}
    27  	// This expression must be atomic with respect to GC moves.
    28  	// At this stage this is true, because the GC does not
    29  	// move. See https://golang.org/issue/12445.
    30  	return int(uintptr(unsafe.Pointer(&b[0]))-uintptr(unsafe.Pointer(&a[0]))) / int(unsafe.Sizeof(complex128(0)))
    31  }