core_builtin_soa.odin 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  1. package runtime
  2. import "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(intrinsics.type_struct_field_count(E));
  52. footer = (^Raw_SOA_Footer_Dynamic_Array)(uintptr(array) + field_count*size_of(rawptr));
  53. return;
  54. }
  55. raw_soa_footer :: proc{
  56. raw_soa_footer_slice,
  57. raw_soa_footer_dynamic_array,
  58. };
  59. @builtin
  60. 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 {
  61. if length <= 0 {
  62. return;
  63. }
  64. footer := raw_soa_footer(&array);
  65. if size_of(E) == 0 {
  66. footer.len = length;
  67. return;
  68. }
  69. max_align := max(alignment, align_of(E));
  70. ti := type_info_of(typeid_of(T));
  71. ti = type_info_base(ti);
  72. si := &ti.variant.(Type_Info_Struct);
  73. field_count := uintptr(intrinsics.type_struct_field_count(E));
  74. total_size := 0;
  75. for i in 0..<field_count {
  76. type := si.types[i].variant.(Type_Info_Pointer).elem;
  77. total_size += type.size * length;
  78. total_size = align_forward_int(total_size, max_align);
  79. }
  80. allocator := allocator;
  81. if allocator.procedure == nil {
  82. allocator = context.allocator;
  83. }
  84. assert(allocator.procedure != nil);
  85. new_bytes: []byte;
  86. new_bytes, err = allocator.procedure(
  87. allocator.data, .Alloc, total_size, max_align,
  88. nil, 0, loc,
  89. );
  90. if new_bytes == nil || err != nil {
  91. return;
  92. }
  93. new_data := raw_data(new_bytes);
  94. data := uintptr(&array);
  95. offset := 0;
  96. for i in 0..<field_count {
  97. type := si.types[i].variant.(Type_Info_Pointer).elem;
  98. offset = align_forward_int(offset, max_align);
  99. (^uintptr)(data)^ = uintptr(new_data) + uintptr(offset);
  100. data += size_of(rawptr);
  101. offset += type.size * length;
  102. }
  103. footer.len = length;
  104. return;
  105. }
  106. @builtin
  107. make_soa_slice :: proc($T: typeid/#soa[]$E, length: int, allocator := context.allocator, loc := #caller_location) -> (array: T, err: Allocator_Error) #optional_second {
  108. return make_soa_aligned(T, length, align_of(E), allocator, loc);
  109. }
  110. @builtin
  111. make_soa_dynamic_array :: proc($T: typeid/#soa[dynamic]$E, allocator := context.allocator, loc := #caller_location) -> (array: T) {
  112. context.allocator = allocator;
  113. reserve_soa(&array, DEFAULT_RESERVE_CAPACITY, loc);
  114. return;
  115. }
  116. @builtin
  117. make_soa_dynamic_array_len :: proc($T: typeid/#soa[dynamic]$E, auto_cast length: int, allocator := context.allocator, loc := #caller_location) -> (array: T) {
  118. context.allocator = allocator;
  119. resize_soa(&array, length, loc);
  120. return;
  121. }
  122. @builtin
  123. 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) {
  124. context.allocator = allocator;
  125. if reserve_soa(&array, capacity, loc) {
  126. resize_soa(&array, length, loc);
  127. }
  128. return;
  129. }
  130. @builtin
  131. make_soa :: proc{
  132. make_soa_slice,
  133. make_soa_dynamic_array,
  134. make_soa_dynamic_array_len,
  135. make_soa_dynamic_array_len_cap,
  136. };
  137. @builtin
  138. resize_soa :: proc(array: ^$T/#soa[dynamic]$E, length: int, loc := #caller_location) -> bool {
  139. if array == nil {
  140. return false;
  141. }
  142. if !reserve_soa(array, length, loc) {
  143. return false;
  144. }
  145. footer := raw_soa_footer(array);
  146. footer.len = length;
  147. return true;
  148. }
  149. @builtin
  150. reserve_soa :: proc(array: ^$T/#soa[dynamic]$E, capacity: int, loc := #caller_location) -> bool {
  151. if array == nil {
  152. return false;
  153. }
  154. old_cap := cap(array);
  155. if capacity <= old_cap {
  156. return true;
  157. }
  158. if array.allocator.procedure == nil {
  159. array.allocator = context.allocator;
  160. }
  161. assert(array.allocator.procedure != nil);
  162. footer := raw_soa_footer(array);
  163. if size_of(E) == 0 {
  164. footer.cap = capacity;
  165. return true;
  166. }
  167. ti := type_info_of(typeid_of(T));
  168. ti = type_info_base(ti);
  169. si := &ti.variant.(Type_Info_Struct);
  170. field_count := uintptr(intrinsics.type_struct_field_count(E));
  171. assert(footer.cap == old_cap);
  172. old_size := 0;
  173. new_size := 0;
  174. max_align :: align_of(E);
  175. for i in 0..<field_count {
  176. type := si.types[i].variant.(Type_Info_Pointer).elem;
  177. old_size += type.size * old_cap;
  178. new_size += type.size * capacity;
  179. old_size = align_forward_int(old_size, max_align);
  180. new_size = align_forward_int(new_size, max_align);
  181. }
  182. old_data := (^rawptr)(array)^;
  183. new_bytes, err := array.allocator.procedure(
  184. array.allocator.data, .Alloc, new_size, max_align,
  185. nil, old_size, loc,
  186. );
  187. if new_bytes == nil || err != nil {
  188. return false;
  189. }
  190. new_data := raw_data(new_bytes);
  191. footer.cap = capacity;
  192. old_offset := 0;
  193. new_offset := 0;
  194. for i in 0..<field_count {
  195. type := si.types[i].variant.(Type_Info_Pointer).elem;
  196. old_offset = align_forward_int(old_offset, max_align);
  197. new_offset = align_forward_int(new_offset, max_align);
  198. new_data_elem := rawptr(uintptr(new_data) + uintptr(new_offset));
  199. old_data_elem := rawptr(uintptr(old_data) + uintptr(old_offset));
  200. mem_copy(new_data_elem, old_data_elem, type.size * old_cap);
  201. (^rawptr)(uintptr(array) + i*size_of(rawptr))^ = new_data_elem;
  202. old_offset += type.size * old_cap;
  203. new_offset += type.size * capacity;
  204. }
  205. _, err = array.allocator.procedure(
  206. array.allocator.data, .Free, 0, max_align,
  207. old_data, old_size, loc,
  208. );
  209. return true;
  210. }
  211. @builtin
  212. append_soa_elem :: proc(array: ^$T/#soa[dynamic]$E, arg: E, loc := #caller_location) {
  213. if array == nil {
  214. return;
  215. }
  216. arg_len := 1;
  217. if cap(array) <= len(array)+arg_len {
  218. cap := 2 * cap(array) + max(8, arg_len);
  219. _ = reserve_soa(array, cap, loc);
  220. }
  221. arg_len = min(cap(array)-len(array), arg_len);
  222. footer := raw_soa_footer(array);
  223. if size_of(E) > 0 && arg_len > 0 {
  224. ti := type_info_of(typeid_of(T));
  225. ti = type_info_base(ti);
  226. si := &ti.variant.(Type_Info_Struct);
  227. field_count := uintptr(intrinsics.type_struct_field_count(E));
  228. data := (^rawptr)(array)^;
  229. soa_offset := 0;
  230. item_offset := 0;
  231. arg_copy := arg;
  232. arg_ptr := &arg_copy;
  233. max_align :: align_of(E);
  234. for i in 0..<field_count {
  235. type := si.types[i].variant.(Type_Info_Pointer).elem;
  236. soa_offset = align_forward_int(soa_offset, max_align);
  237. item_offset = align_forward_int(item_offset, type.align);
  238. dst := rawptr(uintptr(data) + uintptr(soa_offset) + uintptr(type.size * footer.len));
  239. src := rawptr(uintptr(arg_ptr) + uintptr(item_offset));
  240. mem_copy(dst, src, type.size);
  241. soa_offset += type.size * cap(array);
  242. item_offset += type.size;
  243. }
  244. }
  245. footer.len += arg_len;
  246. }
  247. @builtin
  248. append_soa_elems :: proc(array: ^$T/#soa[dynamic]$E, args: ..E, loc := #caller_location) {
  249. if array == nil {
  250. return;
  251. }
  252. arg_len := len(args);
  253. if arg_len == 0 {
  254. return;
  255. }
  256. if cap(array) <= len(array)+arg_len {
  257. cap := 2 * cap(array) + max(8, arg_len);
  258. _ = reserve_soa(array, cap, loc);
  259. }
  260. arg_len = min(cap(array)-len(array), arg_len);
  261. footer := raw_soa_footer(array);
  262. if size_of(E) > 0 && arg_len > 0 {
  263. ti := type_info_of(typeid_of(T));
  264. ti = type_info_base(ti);
  265. si := &ti.variant.(Type_Info_Struct);
  266. field_count := uintptr(intrinsics.type_struct_field_count(E));
  267. data := (^rawptr)(array)^;
  268. soa_offset := 0;
  269. item_offset := 0;
  270. args_ptr := &args[0];
  271. max_align :: align_of(E);
  272. for i in 0..<field_count {
  273. type := si.types[i].variant.(Type_Info_Pointer).elem;
  274. soa_offset = align_forward_int(soa_offset, max_align);
  275. item_offset = align_forward_int(item_offset, type.align);
  276. dst := uintptr(data) + uintptr(soa_offset) + uintptr(type.size * footer.len);
  277. src := uintptr(args_ptr) + uintptr(item_offset);
  278. for j in 0..<arg_len {
  279. d := rawptr(dst + uintptr(j*type.size));
  280. s := rawptr(src + uintptr(j*size_of(E)));
  281. mem_copy(d, s, type.size);
  282. }
  283. soa_offset += type.size * cap(array);
  284. item_offset += type.size;
  285. }
  286. }
  287. footer.len += arg_len;
  288. }
  289. // The append_soa built-in procedure appends elements to the end of an #soa dynamic array
  290. @builtin
  291. append_soa :: proc{
  292. append_soa_elem,
  293. append_soa_elems,
  294. };
  295. delete_soa_slice :: proc(array: $T/#soa[]$E, allocator := context.allocator, loc := #caller_location) {
  296. when intrinsics.type_struct_field_count(E) != 0 {
  297. array := array;
  298. ptr := (^rawptr)(&array)^;
  299. free(ptr, allocator, loc);
  300. }
  301. }
  302. delete_soa_dynamic_array :: proc(array: $T/#soa[dynamic]$E, loc := #caller_location) {
  303. when intrinsics.type_struct_field_count(E) != 0 {
  304. array := array;
  305. ptr := (^rawptr)(&array)^;
  306. footer := raw_soa_footer(&array);
  307. free(ptr, footer.allocator, loc);
  308. }
  309. }
  310. @builtin
  311. delete_soa :: proc{
  312. delete_soa_slice,
  313. delete_soa_dynamic_array,
  314. };