ptr.odin 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. package slice
  2. import "core:builtin"
  3. import "core:mem"
  4. ptr_add :: proc(p: $P/^$T, x: int) -> ^T {
  5. return (^T)(uintptr(p) + size_of(T)*x)
  6. }
  7. ptr_sub :: proc(p: $P/^$T, x: int) -> ^T {
  8. return #force_inline ptr_add(p, -x)
  9. }
  10. ptr_swap_non_overlapping :: proc(x, y: rawptr, len: int) {
  11. if len <= 0 {
  12. return
  13. }
  14. if x == y { // Ignore pointers that are the same
  15. return
  16. }
  17. Block :: distinct [4]u64
  18. BLOCK_SIZE :: size_of(Block)
  19. i := 0
  20. t := &Block{}
  21. for ; i + BLOCK_SIZE <= len; i += BLOCK_SIZE {
  22. a := rawptr(uintptr(x) + uintptr(i))
  23. b := rawptr(uintptr(y) + uintptr(i))
  24. mem.copy(t, a, BLOCK_SIZE)
  25. mem.copy(a, b, BLOCK_SIZE)
  26. mem.copy(b, t, BLOCK_SIZE)
  27. }
  28. if i < len {
  29. rem := len - i
  30. a := rawptr(uintptr(x) + uintptr(i))
  31. b := rawptr(uintptr(y) + uintptr(i))
  32. mem.copy(t, a, rem)
  33. mem.copy(a, b, rem)
  34. mem.copy(b, t, rem)
  35. }
  36. }
  37. ptr_swap_overlapping :: proc(x, y: rawptr, len: int) {
  38. if len <= 0 {
  39. return
  40. }
  41. if x == y {
  42. return
  43. }
  44. N :: 512
  45. buffer: [N]byte = ---
  46. a, b := ([^]byte)(x), ([^]byte)(y)
  47. for n := len; n > 0; n -= N {
  48. m := builtin.min(n, N)
  49. mem.copy(&buffer, a, m)
  50. mem.copy(a, b, m)
  51. mem.copy(b, &buffer, m)
  52. a, b = a[N:], b[N:]
  53. }
  54. }
  55. ptr_rotate :: proc(left: int, mid: ^$T, right: int) {
  56. when size_of(T) != 0 {
  57. left, mid, right := left, mid, right
  58. // TODO(bill): Optimization with a buffer for smaller ranges
  59. if left >= right {
  60. for {
  61. ptr_swap_non_overlapping(ptr_sub(mid, right), mid, right)
  62. mid = ptr_sub(mid, right)
  63. left -= right
  64. if left < right {
  65. break
  66. }
  67. }
  68. } else {
  69. ptr_swap_non_overlapping(ptr_sub(mid, left), mid, left)
  70. mid = ptr_add(mid, left)
  71. right -= left
  72. if right < left {
  73. break
  74. }
  75. }
  76. }
  77. }