offset_appengine.go 1.2 KB

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