github.com/jingcheng-WU/gonum@v0.9.1-0.20210323123734-f1a2a11a8f7b/mat/offset_appengine.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 "reflect"
    10  
    11  var sizeOfFloat64 = int(reflect.TypeOf(float64(0)).Size())
    12  
    13  // offset returns the number of float64 values b[0] is after a[0].
    14  func offset(a, b []float64) int {
    15  	va0 := reflect.ValueOf(a).Index(0)
    16  	vb0 := reflect.ValueOf(b).Index(0)
    17  	if va0.Addr() == vb0.Addr() {
    18  		return 0
    19  	}
    20  	// This expression must be atomic with respect to GC moves.
    21  	// At this stage this is true, because the GC does not
    22  	// move. See https://golang.org/issue/12445.
    23  	return int(vb0.UnsafeAddr()-va0.UnsafeAddr()) / sizeOfFloat64
    24  }
    25  
    26  var sizeOfComplex128 = int(reflect.TypeOf(complex128(0)).Size())
    27  
    28  // offsetComplex returns the number of complex128 values b[0] is after a[0].
    29  func offsetComplex(a, b []complex128) int {
    30  	va0 := reflect.ValueOf(a).Index(0)
    31  	vb0 := reflect.ValueOf(b).Index(0)
    32  	if va0.Addr() == vb0.Addr() {
    33  		return 0
    34  	}
    35  	// This expression must be atomic with respect to GC moves.
    36  	// At this stage this is true, because the GC does not
    37  	// move. See https://golang.org/issue/12445.
    38  	return int(vb0.UnsafeAddr()-va0.UnsafeAddr()) / sizeOfComplex128
    39  }