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