core_builtin_soa.odin 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  1. package runtime
  2. import "core:intrinsics"
  3. _ :: intrinsics
  4. /*
  5. SOA types are implemented with this sort of layout:
  6. SOA Fixed Array
  7. struct {
  8. f0: [N]T0,
  9. f1: [N]T1,
  10. f2: [N]T2,
  11. }
  12. SOA Slice
  13. struct {
  14. f0: ^T0,
  15. f1: ^T1,
  16. f2: ^T2,
  17. len: int,
  18. }
  19. SOA Dynamic Array
  20. struct {
  21. f0: ^T0,
  22. f1: ^T1,
  23. f2: ^T2,
  24. len: int,
  25. cap: int,
  26. allocator: Allocator,
  27. }
  28. A footer is used rather than a header purely to simplify access to the fields internally
  29. i.e. field index of the AOS == SOA
  30. */
  31. Raw_SOA_Footer_Slice :: struct {
  32. len: int,
  33. }
  34. Raw_SOA_Footer_Dynamic_Array :: struct {
  35. len: int,
  36. cap: int,
  37. allocator: Allocator,
  38. }
  39. raw_soa_footer_slice :: proc(array: ^$T/#soa[]$E) -> (footer: ^Raw_SOA_Footer_Slice) {
  40. if array == nil {
  41. return nil
  42. }
  43. field_count := uintptr(intrinsics.type_struct_field_count(E))
  44. footer = (^Raw_SOA_Footer_Slice)(uintptr(array) + field_count*size_of(rawptr))
  45. return
  46. }
  47. raw_soa_footer_dynamic_array :: proc(array: ^$T/#soa[dynamic]$E) -> (footer: ^Raw_SOA_Footer_Dynamic_Array) {
  48. if array == nil {
  49. return nil
  50. }
  51. field_count: uintptr
  52. when intrinsics.type_is_array(E) {
  53. field_count = len(E)
  54. } else {
  55. field_count = uintptr(intrinsics.type_struct_field_count(E))
  56. }
  57. footer = (^Raw_SOA_Footer_Dynamic_Array)(uintptr(array) + field_count*size_of(rawptr))
  58. return
  59. }
  60. raw_soa_footer :: proc{
  61. raw_soa_footer_slice,
  62. raw_soa_footer_dynamic_array,
  63. }
  64. @builtin
  65. make_soa_aligned :: proc($T: typeid/#soa[]$E, length: int, alignment: int, allocator := context.allocator, loc := #caller_location) -> (array: T, err: Allocator_Error) #optional_second {
  66. if length <= 0 {
  67. return
  68. }
  69. footer := raw_soa_footer(&array)
  70. if size_of(E) == 0 {
  71. footer.len = length
  72. return
  73. }
  74. max_align := max(alignment, align_of(E))
  75. ti := type_info_of(typeid_of(T))
  76. ti = type_info_base(ti)
  77. si := &ti.variant.(Type_Info_Struct)
  78. field_count := uintptr(intrinsics.type_struct_field_count(E))
  79. total_size := 0
  80. for i in 0..<field_count {
  81. type := si.types[i].variant.(Type_Info_Pointer).elem
  82. total_size += type.size * length
  83. total_size = align_forward_int(total_size, max_align)
  84. }
  85. allocator := allocator
  86. if allocator.procedure == nil {
  87. allocator = context.allocator
  88. }
  89. assert(allocator.procedure != nil)
  90. new_bytes: []byte
  91. new_bytes, err = allocator.procedure(
  92. allocator.data, .Alloc, total_size, max_align,
  93. nil, 0, loc,
  94. )
  95. if new_bytes == nil || err != nil {
  96. return
  97. }
  98. new_data := raw_data(new_bytes)
  99. data := uintptr(&array)
  100. offset := 0
  101. for i in 0..<field_count {
  102. type := si.types[i].variant.(Type_Info_Pointer).elem
  103. offset = align_forward_int(offset, max_align)
  104. (^uintptr)(data)^ = uintptr(new_data) + uintptr(offset)
  105. data += size_of(rawptr)
  106. offset += type.size * length
  107. }
  108. footer.len = length
  109. return
  110. }
  111. @builtin
  112. make_soa_slice :: proc($T: typeid/#soa[]$E, length: int, allocator := context.allocator, loc := #caller_location) -> (array: T, err: Allocator_Error) #optional_second {
  113. return make_soa_aligned(T, length, align_of(E), allocator, loc)
  114. }
  115. @builtin
  116. make_soa_dynamic_array :: proc($T: typeid/#soa[dynamic]$E, allocator := context.allocator, loc := #caller_location) -> (array: T) {
  117. context.allocator = allocator
  118. reserve_soa(&array, DEFAULT_RESERVE_CAPACITY, loc)
  119. return
  120. }
  121. @builtin
  122. make_soa_dynamic_array_len :: proc($T: typeid/#soa[dynamic]$E, auto_cast length: int, allocator := context.allocator, loc := #caller_location) -> (array: T) {
  123. context.allocator = allocator
  124. resize_soa(&array, length, loc)
  125. return
  126. }
  127. @builtin
  128. make_soa_dynamic_array_len_cap :: proc($T: typeid/#soa[dynamic]$E, auto_cast length, capacity: int, allocator := context.allocator, loc := #caller_location) -> (array: T) {
  129. context.allocator = allocator
  130. if reserve_soa(&array, capacity, loc) {
  131. resize_soa(&array, length, loc)
  132. }
  133. return
  134. }
  135. @builtin
  136. make_soa :: proc{
  137. make_soa_slice,
  138. make_soa_dynamic_array,
  139. make_soa_dynamic_array_len,
  140. make_soa_dynamic_array_len_cap,
  141. }
  142. @builtin
  143. resize_soa :: proc(array: ^$T/#soa[dynamic]$E, length: int, loc := #caller_location) -> bool {
  144. if array == nil {
  145. return false
  146. }
  147. if !reserve_soa(array, length, loc) {
  148. return false
  149. }
  150. footer := raw_soa_footer(array)
  151. footer.len = length
  152. return true
  153. }
  154. @builtin
  155. reserve_soa :: proc(array: ^$T/#soa[dynamic]$E, capacity: int, loc := #caller_location) -> bool {
  156. if array == nil {
  157. return false
  158. }
  159. old_cap := cap(array)
  160. if capacity <= old_cap {
  161. return true
  162. }
  163. if array.allocator.procedure == nil {
  164. array.allocator = context.allocator
  165. }
  166. assert(array.allocator.procedure != nil)
  167. footer := raw_soa_footer(array)
  168. if size_of(E) == 0 {
  169. footer.cap = capacity
  170. return true
  171. }
  172. ti := type_info_of(typeid_of(T))
  173. ti = type_info_base(ti)
  174. si := &ti.variant.(Type_Info_Struct)
  175. field_count: uintptr
  176. when intrinsics.type_is_array(E) {
  177. field_count = len(E)
  178. } else {
  179. field_count = uintptr(intrinsics.type_struct_field_count(E))
  180. }
  181. assert(footer.cap == old_cap)
  182. old_size := 0
  183. new_size := 0
  184. max_align :: align_of(E)
  185. for i in 0..<field_count {
  186. type := si.types[i].variant.(Type_Info_Pointer).elem
  187. old_size += type.size * old_cap
  188. new_size += type.size * capacity
  189. old_size = align_forward_int(old_size, max_align)
  190. new_size = align_forward_int(new_size, max_align)
  191. }
  192. old_data := (^rawptr)(array)^
  193. new_bytes, err := array.allocator.procedure(
  194. array.allocator.data, .Alloc, new_size, max_align,
  195. nil, old_size, loc,
  196. )
  197. if new_bytes == nil || err != nil {
  198. return false
  199. }
  200. new_data := raw_data(new_bytes)
  201. footer.cap = capacity
  202. old_offset := 0
  203. new_offset := 0
  204. for i in 0..<field_count {
  205. type := si.types[i].variant.(Type_Info_Pointer).elem
  206. old_offset = align_forward_int(old_offset, max_align)
  207. new_offset = align_forward_int(new_offset, max_align)
  208. new_data_elem := rawptr(uintptr(new_data) + uintptr(new_offset))
  209. old_data_elem := rawptr(uintptr(old_data) + uintptr(old_offset))
  210. mem_copy(new_data_elem, old_data_elem, type.size * old_cap)
  211. (^rawptr)(uintptr(array) + i*size_of(rawptr))^ = new_data_elem
  212. old_offset += type.size * old_cap
  213. new_offset += type.size * capacity
  214. }
  215. _, err = array.allocator.procedure(
  216. array.allocator.data, .Free, 0, max_align,
  217. old_data, old_size, loc,
  218. )
  219. return true
  220. }
  221. @builtin
  222. append_soa_elem :: proc(array: ^$T/#soa[dynamic]$E, arg: E, loc := #caller_location) {
  223. if array == nil {
  224. return
  225. }
  226. arg_len := 1
  227. if cap(array) <= len(array)+arg_len {
  228. cap := 2 * cap(array) + max(8, arg_len)
  229. _ = reserve_soa(array, cap, loc)
  230. }
  231. arg_len = min(cap(array)-len(array), arg_len)
  232. footer := raw_soa_footer(array)
  233. if size_of(E) > 0 && arg_len > 0 {
  234. ti := type_info_of(typeid_of(T))
  235. ti = type_info_base(ti)
  236. si := &ti.variant.(Type_Info_Struct)
  237. field_count: uintptr
  238. when intrinsics.type_is_array(E) {
  239. field_count = len(E)
  240. } else {
  241. field_count = uintptr(intrinsics.type_struct_field_count(E))
  242. }
  243. data := (^rawptr)(array)^
  244. soa_offset := 0
  245. item_offset := 0
  246. arg_copy := arg
  247. arg_ptr := &arg_copy
  248. max_align :: align_of(E)
  249. for i in 0..<field_count {
  250. type := si.types[i].variant.(Type_Info_Pointer).elem
  251. soa_offset = align_forward_int(soa_offset, max_align)
  252. item_offset = align_forward_int(item_offset, type.align)
  253. dst := rawptr(uintptr(data) + uintptr(soa_offset) + uintptr(type.size * footer.len))
  254. src := rawptr(uintptr(arg_ptr) + uintptr(item_offset))
  255. mem_copy(dst, src, type.size)
  256. soa_offset += type.size * cap(array)
  257. item_offset += type.size
  258. }
  259. }
  260. footer.len += arg_len
  261. }
  262. @builtin
  263. append_soa_elems :: proc(array: ^$T/#soa[dynamic]$E, args: ..E, loc := #caller_location) {
  264. if array == nil {
  265. return
  266. }
  267. arg_len := len(args)
  268. if arg_len == 0 {
  269. return
  270. }
  271. if cap(array) <= len(array)+arg_len {
  272. cap := 2 * cap(array) + max(8, arg_len)
  273. _ = reserve_soa(array, cap, loc)
  274. }
  275. arg_len = min(cap(array)-len(array), arg_len)
  276. footer := raw_soa_footer(array)
  277. if size_of(E) > 0 && arg_len > 0 {
  278. ti := type_info_of(typeid_of(T))
  279. ti = type_info_base(ti)
  280. si := &ti.variant.(Type_Info_Struct)
  281. field_count := uintptr(intrinsics.type_struct_field_count(E))
  282. data := (^rawptr)(array)^
  283. soa_offset := 0
  284. item_offset := 0
  285. args_ptr := &args[0]
  286. max_align :: align_of(E)
  287. for i in 0..<field_count {
  288. type := si.types[i].variant.(Type_Info_Pointer).elem
  289. soa_offset = align_forward_int(soa_offset, max_align)
  290. item_offset = align_forward_int(item_offset, type.align)
  291. dst := uintptr(data) + uintptr(soa_offset) + uintptr(type.size * footer.len)
  292. src := uintptr(args_ptr) + uintptr(item_offset)
  293. for j in 0..<arg_len {
  294. d := rawptr(dst + uintptr(j*type.size))
  295. s := rawptr(src + uintptr(j*size_of(E)))
  296. mem_copy(d, s, type.size)
  297. }
  298. soa_offset += type.size * cap(array)
  299. item_offset += type.size
  300. }
  301. }
  302. footer.len += arg_len
  303. }
  304. // The append_soa built-in procedure appends elements to the end of an #soa dynamic array
  305. @builtin
  306. append_soa :: proc{
  307. append_soa_elem,
  308. append_soa_elems,
  309. }
  310. delete_soa_slice :: proc(array: $T/#soa[]$E, allocator := context.allocator, loc := #caller_location) {
  311. when intrinsics.type_struct_field_count(E) != 0 {
  312. array := array
  313. ptr := (^rawptr)(&array)^
  314. free(ptr, allocator, loc)
  315. }
  316. }
  317. delete_soa_dynamic_array :: proc(array: $T/#soa[dynamic]$E, loc := #caller_location) {
  318. when intrinsics.type_struct_field_count(E) != 0 {
  319. array := array
  320. ptr := (^rawptr)(&array)^
  321. footer := raw_soa_footer(&array)
  322. free(ptr, footer.allocator, loc)
  323. }
  324. }
  325. @builtin
  326. delete_soa :: proc{
  327. delete_soa_slice,
  328. delete_soa_dynamic_array,
  329. }