core_builtin_soa.odin 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  1. package runtime
  2. import "base: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. @(builtin, require_results)
  40. raw_soa_footer_slice :: proc(array: ^$T/#soa[]$E) -> (footer: ^Raw_SOA_Footer_Slice) {
  41. if array == nil {
  42. return nil
  43. }
  44. field_count := uintptr(intrinsics.type_struct_field_count(E))
  45. footer = (^Raw_SOA_Footer_Slice)(uintptr(array) + field_count*size_of(rawptr))
  46. return
  47. }
  48. @(builtin, require_results)
  49. raw_soa_footer_dynamic_array :: proc(array: ^$T/#soa[dynamic]$E) -> (footer: ^Raw_SOA_Footer_Dynamic_Array) {
  50. if array == nil {
  51. return nil
  52. }
  53. field_count: uintptr
  54. when intrinsics.type_is_array(E) {
  55. field_count = len(E)
  56. } else {
  57. field_count = uintptr(intrinsics.type_struct_field_count(E))
  58. }
  59. footer = (^Raw_SOA_Footer_Dynamic_Array)(uintptr(array) + field_count*size_of(rawptr))
  60. return
  61. }
  62. raw_soa_footer :: proc{
  63. raw_soa_footer_slice,
  64. raw_soa_footer_dynamic_array,
  65. }
  66. @(builtin, require_results)
  67. make_soa_aligned :: proc($T: typeid/#soa[]$E, length: int, alignment: int, allocator := context.allocator, loc := #caller_location) -> (array: T, err: Allocator_Error) #optional_allocator_error {
  68. if length <= 0 {
  69. return
  70. }
  71. array.allocator = allocator
  72. footer := raw_soa_footer(&array)
  73. if size_of(E) == 0 {
  74. footer.len = length
  75. return
  76. }
  77. max_align := max(alignment, align_of(E))
  78. ti := type_info_of(typeid_of(T))
  79. ti = type_info_base(ti)
  80. si := &ti.variant.(Type_Info_Struct)
  81. field_count := uintptr(intrinsics.type_struct_field_count(E))
  82. total_size := 0
  83. for i in 0..<field_count {
  84. type := si.types[i].variant.(Type_Info_Pointer).elem
  85. total_size += type.size * length
  86. total_size = align_forward_int(total_size, max_align)
  87. }
  88. allocator := allocator
  89. if allocator.procedure == nil {
  90. allocator = context.allocator
  91. }
  92. assert(allocator.procedure != nil)
  93. new_bytes: []byte
  94. new_bytes, err = allocator.procedure(
  95. allocator.data, .Alloc, total_size, max_align,
  96. nil, 0, loc,
  97. )
  98. if new_bytes == nil || err != nil {
  99. return
  100. }
  101. new_data := raw_data(new_bytes)
  102. data := uintptr(&array)
  103. offset := 0
  104. for i in 0..<field_count {
  105. type := si.types[i].variant.(Type_Info_Pointer).elem
  106. offset = align_forward_int(offset, max_align)
  107. (^uintptr)(data)^ = uintptr(new_data) + uintptr(offset)
  108. data += size_of(rawptr)
  109. offset += type.size * length
  110. }
  111. footer.len = length
  112. return
  113. }
  114. @(builtin, require_results)
  115. make_soa_slice :: proc($T: typeid/#soa[]$E, length: int, allocator := context.allocator, loc := #caller_location) -> (array: T, err: Allocator_Error) #optional_allocator_error {
  116. return make_soa_aligned(T, length, align_of(E), allocator, loc)
  117. }
  118. @(builtin, require_results)
  119. make_soa_dynamic_array :: proc($T: typeid/#soa[dynamic]$E, allocator := context.allocator, loc := #caller_location) -> (array: T, err: Allocator_Error) #optional_allocator_error {
  120. context.allocator = allocator
  121. reserve_soa(&array, DEFAULT_RESERVE_CAPACITY, loc) or_return
  122. return array, nil
  123. }
  124. @(builtin, require_results)
  125. make_soa_dynamic_array_len :: proc($T: typeid/#soa[dynamic]$E, #any_int length: int, allocator := context.allocator, loc := #caller_location) -> (array: T, err: Allocator_Error) #optional_allocator_error {
  126. context.allocator = allocator
  127. resize_soa(&array, length, loc) or_return
  128. return array, nil
  129. }
  130. @(builtin, require_results)
  131. make_soa_dynamic_array_len_cap :: proc($T: typeid/#soa[dynamic]$E, #any_int length, capacity: int, allocator := context.allocator, loc := #caller_location) -> (array: T, err: Allocator_Error) #optional_allocator_error {
  132. context.allocator = allocator
  133. reserve_soa(&array, capacity, loc) or_return
  134. resize_soa(&array, length, loc) or_return
  135. return array, nil
  136. }
  137. @builtin
  138. make_soa :: proc{
  139. make_soa_slice,
  140. make_soa_dynamic_array,
  141. make_soa_dynamic_array_len,
  142. make_soa_dynamic_array_len_cap,
  143. }
  144. @builtin
  145. resize_soa :: proc(array: ^$T/#soa[dynamic]$E, length: int, loc := #caller_location) -> Allocator_Error {
  146. if array == nil {
  147. return nil
  148. }
  149. reserve_soa(array, length, loc) or_return
  150. footer := raw_soa_footer(array)
  151. footer.len = length
  152. return nil
  153. }
  154. @builtin
  155. reserve_soa :: proc(array: ^$T/#soa[dynamic]$E, capacity: int, loc := #caller_location) -> Allocator_Error {
  156. if array == nil {
  157. return nil
  158. }
  159. old_cap := cap(array)
  160. if capacity <= old_cap {
  161. return nil
  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 nil
  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 := array.allocator.procedure(
  194. array.allocator.data, .Alloc, new_size, max_align,
  195. nil, old_size, loc,
  196. ) or_return
  197. new_data := raw_data(new_bytes)
  198. footer.cap = capacity
  199. old_offset := 0
  200. new_offset := 0
  201. for i in 0..<field_count {
  202. type := si.types[i].variant.(Type_Info_Pointer).elem
  203. old_offset = align_forward_int(old_offset, max_align)
  204. new_offset = align_forward_int(new_offset, max_align)
  205. new_data_elem := rawptr(uintptr(new_data) + uintptr(new_offset))
  206. old_data_elem := rawptr(uintptr(old_data) + uintptr(old_offset))
  207. mem_copy(new_data_elem, old_data_elem, type.size * old_cap)
  208. (^rawptr)(uintptr(array) + i*size_of(rawptr))^ = new_data_elem
  209. old_offset += type.size * old_cap
  210. new_offset += type.size * capacity
  211. }
  212. array.allocator.procedure(
  213. array.allocator.data, .Free, 0, max_align,
  214. old_data, old_size, loc,
  215. ) or_return
  216. return nil
  217. }
  218. @builtin
  219. append_soa_elem :: proc(array: ^$T/#soa[dynamic]$E, arg: E, loc := #caller_location) -> (n: int, err: Allocator_Error) #optional_allocator_error {
  220. if array == nil {
  221. return 0, nil
  222. }
  223. if cap(array) <= len(array) + 1 {
  224. cap := 2 * cap(array) + 8
  225. err = reserve_soa(array, cap, loc) // do not 'or_return' here as it could be a partial success
  226. }
  227. footer := raw_soa_footer(array)
  228. if size_of(E) > 0 && cap(array)-len(array) > 0 {
  229. ti := type_info_of(T)
  230. ti = type_info_base(ti)
  231. si := &ti.variant.(Type_Info_Struct)
  232. field_count: uintptr
  233. when intrinsics.type_is_array(E) {
  234. field_count = len(E)
  235. } else {
  236. field_count = uintptr(intrinsics.type_struct_field_count(E))
  237. }
  238. data := (^rawptr)(array)^
  239. soa_offset := 0
  240. item_offset := 0
  241. arg_copy := arg
  242. arg_ptr := &arg_copy
  243. max_align :: align_of(E)
  244. for i in 0..<field_count {
  245. type := si.types[i].variant.(Type_Info_Pointer).elem
  246. soa_offset = align_forward_int(soa_offset, max_align)
  247. item_offset = align_forward_int(item_offset, type.align)
  248. dst := rawptr(uintptr(data) + uintptr(soa_offset) + uintptr(type.size * footer.len))
  249. src := rawptr(uintptr(arg_ptr) + uintptr(item_offset))
  250. mem_copy(dst, src, type.size)
  251. soa_offset += type.size * cap(array)
  252. item_offset += type.size
  253. }
  254. footer.len += 1
  255. return 1, err
  256. }
  257. return 0, err
  258. }
  259. @builtin
  260. append_soa_elems :: proc(array: ^$T/#soa[dynamic]$E, args: ..E, loc := #caller_location) -> (n: int, err: Allocator_Error) #optional_allocator_error {
  261. if array == nil {
  262. return
  263. }
  264. arg_len := len(args)
  265. if arg_len == 0 {
  266. return
  267. }
  268. if cap(array) <= len(array)+arg_len {
  269. cap := 2 * cap(array) + max(8, arg_len)
  270. err = reserve_soa(array, cap, loc) // do not 'or_return' here as it could be a partial success
  271. }
  272. arg_len = min(cap(array)-len(array), arg_len)
  273. footer := raw_soa_footer(array)
  274. if size_of(E) > 0 && arg_len > 0 {
  275. ti := type_info_of(typeid_of(T))
  276. ti = type_info_base(ti)
  277. si := &ti.variant.(Type_Info_Struct)
  278. field_count := uintptr(intrinsics.type_struct_field_count(E))
  279. data := (^rawptr)(array)^
  280. soa_offset := 0
  281. item_offset := 0
  282. args_ptr := &args[0]
  283. max_align :: align_of(E)
  284. for i in 0..<field_count {
  285. type := si.types[i].variant.(Type_Info_Pointer).elem
  286. soa_offset = align_forward_int(soa_offset, max_align)
  287. item_offset = align_forward_int(item_offset, type.align)
  288. dst := uintptr(data) + uintptr(soa_offset) + uintptr(type.size * footer.len)
  289. src := uintptr(args_ptr) + uintptr(item_offset)
  290. for j in 0..<arg_len {
  291. d := rawptr(dst + uintptr(j*type.size))
  292. s := rawptr(src + uintptr(j*size_of(E)))
  293. mem_copy(d, s, type.size)
  294. }
  295. soa_offset += type.size * cap(array)
  296. item_offset += type.size
  297. }
  298. }
  299. footer.len += arg_len
  300. return arg_len, err
  301. }
  302. // The append_soa built-in procedure appends elements to the end of an #soa dynamic array
  303. @builtin
  304. append_soa :: proc{
  305. append_soa_elem,
  306. append_soa_elems,
  307. }
  308. delete_soa_slice :: proc(array: $T/#soa[]$E, allocator := context.allocator, loc := #caller_location) -> Allocator_Error {
  309. when intrinsics.type_struct_field_count(E) != 0 {
  310. array := array
  311. ptr := (^rawptr)(&array)^
  312. free(ptr, allocator, loc) or_return
  313. }
  314. return nil
  315. }
  316. delete_soa_dynamic_array :: proc(array: $T/#soa[dynamic]$E, loc := #caller_location) -> Allocator_Error {
  317. when intrinsics.type_struct_field_count(E) != 0 {
  318. array := array
  319. ptr := (^rawptr)(&array)^
  320. footer := raw_soa_footer(&array)
  321. free(ptr, footer.allocator, loc) or_return
  322. }
  323. return nil
  324. }
  325. @builtin
  326. delete_soa :: proc{
  327. delete_soa_slice,
  328. delete_soa_dynamic_array,
  329. }
  330. clear_soa_dynamic_array :: proc(array: ^$T/#soa[dynamic]$E) {
  331. when intrinsics.type_struct_field_count(E) != 0 {
  332. footer := raw_soa_footer(array)
  333. footer.len = 0
  334. }
  335. }
  336. @builtin
  337. clear_soa :: proc{
  338. clear_soa_dynamic_array,
  339. }