offset.go 1.0 KB

12345678910111213141516171819202122232425262728293031
  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 "unsafe"
  7. // offset returns the number of float64 values b[0] is after a[0].
  8. func offset(a, b []float64) int {
  9. if &a[0] == &b[0] {
  10. return 0
  11. }
  12. // This expression must be atomic with respect to GC moves.
  13. // At this stage this is true, because the GC does not
  14. // move. See https://golang.org/issue/12445.
  15. return int(uintptr(unsafe.Pointer(&b[0]))-uintptr(unsafe.Pointer(&a[0]))) / int(unsafe.Sizeof(float64(0)))
  16. }
  17. // offsetComplex returns the number of complex128 values b[0] is after a[0].
  18. func offsetComplex(a, b []complex128) int {
  19. if &a[0] == &b[0] {
  20. return 0
  21. }
  22. // This expression must be atomic with respect to GC moves.
  23. // At this stage this is true, because the GC does not
  24. // move. See https://golang.org/issue/12445.
  25. return int(uintptr(unsafe.Pointer(&b[0]))-uintptr(unsafe.Pointer(&a[0]))) / int(unsafe.Sizeof(complex128(0)))
  26. }