core_builtin.odin 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751
  1. package runtime
  2. import "core:intrinsics"
  3. @builtin
  4. Maybe :: union($T: typeid) {T}
  5. @builtin
  6. container_of :: #force_inline proc "contextless" (ptr: $P/^$Field_Type, $T: typeid, $field_name: string) -> ^T
  7. where intrinsics.type_has_field(T, field_name),
  8. intrinsics.type_field_type(T, field_name) == Field_Type {
  9. offset :: offset_of_by_string(T, field_name)
  10. return (^T)(uintptr(ptr) - offset) if ptr != nil else nil
  11. }
  12. @thread_local global_default_temp_allocator_data: Default_Temp_Allocator
  13. @builtin
  14. init_global_temporary_allocator :: proc(size: int, backup_allocator := context.allocator) {
  15. default_temp_allocator_init(&global_default_temp_allocator_data, size, backup_allocator)
  16. }
  17. @builtin
  18. copy_slice :: proc "contextless" (dst, src: $T/[]$E) -> int {
  19. n := max(0, min(len(dst), len(src)))
  20. if n > 0 {
  21. intrinsics.mem_copy(raw_data(dst), raw_data(src), n*size_of(E))
  22. }
  23. return n
  24. }
  25. @builtin
  26. copy_from_string :: proc "contextless" (dst: $T/[]$E/u8, src: $S/string) -> int {
  27. n := max(0, min(len(dst), len(src)))
  28. if n > 0 {
  29. intrinsics.mem_copy(raw_data(dst), raw_data(src), n)
  30. }
  31. return n
  32. }
  33. @builtin
  34. copy :: proc{copy_slice, copy_from_string}
  35. @builtin
  36. unordered_remove :: proc(array: ^$D/[dynamic]$T, index: int, loc := #caller_location) #no_bounds_check {
  37. bounds_check_error_loc(loc, index, len(array))
  38. n := len(array)-1
  39. if index != n {
  40. array[index] = array[n]
  41. }
  42. (^Raw_Dynamic_Array)(array).len -= 1
  43. }
  44. @builtin
  45. ordered_remove :: proc(array: ^$D/[dynamic]$T, index: int, loc := #caller_location) #no_bounds_check {
  46. bounds_check_error_loc(loc, index, len(array))
  47. if index+1 < len(array) {
  48. copy(array[index:], array[index+1:])
  49. }
  50. (^Raw_Dynamic_Array)(array).len -= 1
  51. }
  52. @builtin
  53. remove_range :: proc(array: ^$D/[dynamic]$T, lo, hi: int, loc := #caller_location) #no_bounds_check {
  54. slice_expr_error_lo_hi_loc(loc, lo, hi, len(array))
  55. n := max(hi-lo, 0)
  56. if n > 0 {
  57. if hi != len(array) {
  58. copy(array[lo:], array[hi:])
  59. }
  60. (^Raw_Dynamic_Array)(array).len -= n
  61. }
  62. }
  63. @builtin
  64. pop :: proc(array: ^$T/[dynamic]$E, loc := #caller_location) -> (res: E) #no_bounds_check {
  65. assert(len(array) > 0, "", loc)
  66. res = array[len(array)-1]
  67. (^Raw_Dynamic_Array)(array).len -= 1
  68. return res
  69. }
  70. @builtin
  71. pop_safe :: proc(array: ^$T/[dynamic]$E) -> (res: E, ok: bool) #no_bounds_check {
  72. if len(array) == 0 {
  73. return
  74. }
  75. res, ok = array[len(array)-1], true
  76. (^Raw_Dynamic_Array)(array).len -= 1
  77. return
  78. }
  79. @builtin
  80. pop_front :: proc(array: ^$T/[dynamic]$E, loc := #caller_location) -> (res: E) #no_bounds_check {
  81. assert(len(array) > 0, "", loc)
  82. res = array[0]
  83. if len(array) > 1 {
  84. copy(array[0:], array[1:])
  85. }
  86. (^Raw_Dynamic_Array)(array).len -= 1
  87. return res
  88. }
  89. @builtin
  90. pop_front_safe :: proc(array: ^$T/[dynamic]$E) -> (res: E, ok: bool) #no_bounds_check {
  91. if len(array) == 0 {
  92. return
  93. }
  94. res, ok = array[0], true
  95. if len(array) > 1 {
  96. copy(array[0:], array[1:])
  97. }
  98. (^Raw_Dynamic_Array)(array).len -= 1
  99. return
  100. }
  101. @builtin
  102. clear :: proc{clear_dynamic_array, clear_map}
  103. @builtin
  104. reserve :: proc{reserve_dynamic_array, reserve_map}
  105. @builtin
  106. resize :: proc{resize_dynamic_array}
  107. // Shrinks the capacity of a dynamic array or map down to the current length, or the given capacity.
  108. @builtin
  109. shrink :: proc{shrink_dynamic_array, shrink_map}
  110. @builtin
  111. free :: proc{mem_free}
  112. @builtin
  113. free_all :: proc{mem_free_all}
  114. @builtin
  115. delete_string :: proc(str: string, allocator := context.allocator, loc := #caller_location) -> Allocator_Error {
  116. return mem_free(raw_data(str), allocator, loc)
  117. }
  118. @builtin
  119. delete_cstring :: proc(str: cstring, allocator := context.allocator, loc := #caller_location) -> Allocator_Error {
  120. return mem_free((^byte)(str), allocator, loc)
  121. }
  122. @builtin
  123. delete_dynamic_array :: proc(array: $T/[dynamic]$E, loc := #caller_location) -> Allocator_Error {
  124. return mem_free(raw_data(array), array.allocator, loc)
  125. }
  126. @builtin
  127. delete_slice :: proc(array: $T/[]$E, allocator := context.allocator, loc := #caller_location) -> Allocator_Error {
  128. return mem_free(raw_data(array), allocator, loc)
  129. }
  130. @builtin
  131. delete_map :: proc(m: $T/map[$K]$V, loc := #caller_location) -> Allocator_Error {
  132. raw := transmute(Raw_Map)m
  133. err := delete_slice(raw.hashes, raw.entries.allocator, loc)
  134. err1 := mem_free(raw.entries.data, raw.entries.allocator, loc)
  135. if err == nil {
  136. err = err1
  137. }
  138. return err
  139. }
  140. @builtin
  141. delete :: proc{
  142. delete_string,
  143. delete_cstring,
  144. delete_dynamic_array,
  145. delete_slice,
  146. delete_map,
  147. }
  148. // The new built-in procedure allocates memory. The first argument is a type, not a value, and the value
  149. // return is a pointer to a newly allocated value of that type using the specified allocator, default is context.allocator
  150. @builtin
  151. new :: proc($T: typeid, allocator := context.allocator, loc := #caller_location) -> (^T, Allocator_Error) #optional_second {
  152. return new_aligned(T, align_of(T), allocator, loc)
  153. }
  154. new_aligned :: proc($T: typeid, alignment: int, allocator := context.allocator, loc := #caller_location) -> (t: ^T, err: Allocator_Error) {
  155. data := mem_alloc_bytes(size_of(T), alignment, allocator, loc) or_return
  156. t = (^T)(raw_data(data))
  157. return
  158. }
  159. @builtin
  160. new_clone :: proc(data: $T, allocator := context.allocator, loc := #caller_location) -> (t: ^T, err: Allocator_Error) #optional_second {
  161. t_data := mem_alloc_bytes(size_of(T), align_of(T), allocator, loc) or_return
  162. t = (^T)(raw_data(t_data))
  163. if t != nil {
  164. t^ = data
  165. }
  166. return
  167. }
  168. DEFAULT_RESERVE_CAPACITY :: 16
  169. make_aligned :: proc($T: typeid/[]$E, #any_int len: int, alignment: int, allocator := context.allocator, loc := #caller_location) -> (T, Allocator_Error) #optional_second {
  170. make_slice_error_loc(loc, len)
  171. data, err := mem_alloc_bytes(size_of(E)*len, alignment, allocator, loc)
  172. if data == nil && size_of(E) != 0 {
  173. return nil, err
  174. }
  175. s := Raw_Slice{raw_data(data), len}
  176. return transmute(T)s, err
  177. }
  178. @(builtin)
  179. make_slice :: proc($T: typeid/[]$E, #any_int len: int, allocator := context.allocator, loc := #caller_location) -> (T, Allocator_Error) #optional_second {
  180. return make_aligned(T, len, align_of(E), allocator, loc)
  181. }
  182. @(builtin)
  183. make_dynamic_array :: proc($T: typeid/[dynamic]$E, allocator := context.allocator, loc := #caller_location) -> (T, Allocator_Error) #optional_second {
  184. return make_dynamic_array_len_cap(T, 0, DEFAULT_RESERVE_CAPACITY, allocator, loc)
  185. }
  186. @(builtin)
  187. make_dynamic_array_len :: proc($T: typeid/[dynamic]$E, #any_int len: int, allocator := context.allocator, loc := #caller_location) -> (T, Allocator_Error) #optional_second {
  188. return make_dynamic_array_len_cap(T, len, len, allocator, loc)
  189. }
  190. @(builtin)
  191. make_dynamic_array_len_cap :: proc($T: typeid/[dynamic]$E, #any_int len: int, #any_int cap: int, allocator := context.allocator, loc := #caller_location) -> (array: T, err: Allocator_Error) #optional_second {
  192. make_dynamic_array_error_loc(loc, len, cap)
  193. data := mem_alloc_bytes(size_of(E)*cap, align_of(E), allocator, loc) or_return
  194. s := Raw_Dynamic_Array{raw_data(data), len, cap, allocator}
  195. if data == nil && size_of(E) != 0 {
  196. s.len, s.cap = 0, 0
  197. }
  198. array = transmute(T)s
  199. return
  200. }
  201. @(builtin)
  202. make_map :: proc($T: typeid/map[$K]$E, #any_int cap: int = DEFAULT_RESERVE_CAPACITY, allocator := context.allocator, loc := #caller_location) -> T {
  203. make_map_expr_error_loc(loc, cap)
  204. context.allocator = allocator
  205. m: T
  206. reserve_map(&m, cap)
  207. return m
  208. }
  209. @(builtin)
  210. make_multi_pointer :: proc($T: typeid/[^]$E, #any_int len: int, allocator := context.allocator, loc := #caller_location) -> (mp: T, err: Allocator_Error) #optional_second {
  211. make_slice_error_loc(loc, len)
  212. data := mem_alloc_bytes(size_of(E)*len, align_of(E), allocator, loc) or_return
  213. if data == nil && size_of(E) != 0 {
  214. return
  215. }
  216. mp = cast(T)raw_data(data)
  217. return
  218. }
  219. // The make built-in procedure allocates and initializes a value of type slice, dynamic array, or map (only)
  220. // Similar to new, the first argument is a type, not a value. Unlike new, make's return type is the same as the
  221. // type of its argument, not a pointer to it.
  222. // Make uses the specified allocator, default is context.allocator, default is context.allocator
  223. @builtin
  224. make :: proc{
  225. make_slice,
  226. make_dynamic_array,
  227. make_dynamic_array_len,
  228. make_dynamic_array_len_cap,
  229. make_map,
  230. make_multi_pointer,
  231. }
  232. @builtin
  233. clear_map :: proc "contextless" (m: ^$T/map[$K]$V) {
  234. if m == nil {
  235. return
  236. }
  237. raw_map := (^Raw_Map)(m)
  238. entries := (^Raw_Dynamic_Array)(&raw_map.entries)
  239. entries.len = 0
  240. for _, i in raw_map.hashes {
  241. raw_map.hashes[i] = -1
  242. }
  243. }
  244. @builtin
  245. reserve_map :: proc(m: ^$T/map[$K]$V, capacity: int, loc := #caller_location) {
  246. if m != nil {
  247. __dynamic_map_reserve(__get_map_header(m), capacity, loc)
  248. }
  249. }
  250. /*
  251. Shrinks the capacity of a map down to the current length, or the given capacity.
  252. If `new_cap` is negative, then `len(m)` is used.
  253. Returns false if `cap(m) < new_cap`, or the allocator report failure.
  254. If `len(m) < new_cap`, then `len(m)` will be left unchanged.
  255. */
  256. @builtin
  257. shrink_map :: proc(m: ^$T/map[$K]$V, new_cap := -1, loc := #caller_location) -> (did_shrink: bool) {
  258. if m != nil {
  259. new_cap := new_cap if new_cap >= 0 else len(m)
  260. return __dynamic_map_shrink(__get_map_header(m), new_cap, loc)
  261. }
  262. return
  263. }
  264. // The delete_key built-in procedure deletes the element with the specified key (m[key]) from the map.
  265. // If m is nil, or there is no such element, this procedure is a no-op
  266. @builtin
  267. delete_key :: proc(m: ^$T/map[$K]$V, key: K) -> (deleted_key: K, deleted_value: V) {
  268. if m != nil {
  269. key := key
  270. h := __get_map_header(m)
  271. hash := __get_map_hash(&key)
  272. fr := __dynamic_map_find(h, hash)
  273. if fr.entry_index >= 0 {
  274. entry := __dynamic_map_get_entry(h, fr.entry_index)
  275. deleted_key = (^K)(uintptr(entry)+h.key_offset)^
  276. deleted_value = (^V)(uintptr(entry)+h.value_offset)^
  277. __dynamic_map_erase(h, fr)
  278. }
  279. }
  280. return
  281. }
  282. @builtin
  283. append_elem :: proc(array: ^$T/[dynamic]$E, arg: E, loc := #caller_location) {
  284. if array == nil {
  285. return
  286. }
  287. if cap(array) < len(array)+1 {
  288. cap := 2 * cap(array) + max(8, 1)
  289. _ = reserve(array, cap, loc)
  290. }
  291. if cap(array)-len(array) > 0 {
  292. a := (^Raw_Dynamic_Array)(array)
  293. when size_of(E) != 0 {
  294. data := ([^]E)(a.data)
  295. assert(condition=data != nil, loc=loc)
  296. data[a.len] = arg
  297. }
  298. a.len += 1
  299. }
  300. }
  301. @builtin
  302. append_elems :: proc(array: ^$T/[dynamic]$E, args: ..E, loc := #caller_location) {
  303. if array == nil {
  304. return
  305. }
  306. arg_len := len(args)
  307. if arg_len <= 0 {
  308. return
  309. }
  310. if cap(array) < len(array)+arg_len {
  311. cap := 2 * cap(array) + max(8, arg_len)
  312. _ = reserve(array, cap, loc)
  313. }
  314. arg_len = min(cap(array)-len(array), arg_len)
  315. if arg_len > 0 {
  316. a := (^Raw_Dynamic_Array)(array)
  317. when size_of(E) != 0 {
  318. data := ([^]E)(a.data)
  319. assert(condition=data != nil, loc=loc)
  320. intrinsics.mem_copy(&data[a.len], raw_data(args), size_of(E) * arg_len)
  321. }
  322. a.len += arg_len
  323. }
  324. }
  325. // The append_string built-in procedure appends a string to the end of a [dynamic]u8 like type
  326. @builtin
  327. append_elem_string :: proc(array: ^$T/[dynamic]$E/u8, arg: $A/string, loc := #caller_location) {
  328. args := transmute([]E)arg
  329. append_elems(array=array, args=args, loc=loc)
  330. }
  331. // The append_string built-in procedure appends multiple strings to the end of a [dynamic]u8 like type
  332. @builtin
  333. append_string :: proc(array: ^$T/[dynamic]$E/u8, args: ..string, loc := #caller_location) {
  334. for arg in args {
  335. append(array = array, args = transmute([]E)(arg), loc = loc)
  336. }
  337. }
  338. // The append built-in procedure appends elements to the end of a dynamic array
  339. @builtin append :: proc{append_elem, append_elems, append_elem_string}
  340. @builtin
  341. append_nothing :: proc(array: ^$T/[dynamic]$E, loc := #caller_location) {
  342. if array == nil {
  343. return
  344. }
  345. resize(array, len(array)+1)
  346. }
  347. @builtin
  348. insert_at_elem :: proc(array: ^$T/[dynamic]$E, index: int, arg: E, loc := #caller_location) -> (ok: bool) #no_bounds_check {
  349. if array == nil {
  350. return
  351. }
  352. n := max(len(array), index)
  353. m :: 1
  354. new_size := n + m
  355. if resize(array, new_size, loc) {
  356. when size_of(E) != 0 {
  357. copy(array[index + m:], array[index:])
  358. array[index] = arg
  359. }
  360. ok = true
  361. }
  362. return
  363. }
  364. @builtin
  365. insert_at_elems :: proc(array: ^$T/[dynamic]$E, index: int, args: ..E, loc := #caller_location) -> (ok: bool) #no_bounds_check {
  366. if array == nil {
  367. return
  368. }
  369. if len(args) == 0 {
  370. ok = true
  371. return
  372. }
  373. n := max(len(array), index)
  374. m := len(args)
  375. new_size := n + m
  376. if resize(array, new_size, loc) {
  377. when size_of(E) != 0 {
  378. copy(array[index + m:], array[index:])
  379. copy(array[index:], args)
  380. }
  381. ok = true
  382. }
  383. return
  384. }
  385. @builtin
  386. insert_at_elem_string :: proc(array: ^$T/[dynamic]$E/u8, index: int, arg: string, loc := #caller_location) -> (ok: bool) #no_bounds_check {
  387. if array == nil {
  388. return
  389. }
  390. if len(arg) == 0 {
  391. ok = true
  392. return
  393. }
  394. n := max(len(array), index)
  395. m := len(arg)
  396. new_size := n + m
  397. if resize(array, new_size, loc) {
  398. copy(array[index+m:], array[index:])
  399. copy(array[index:], arg)
  400. ok = true
  401. }
  402. return
  403. }
  404. @builtin insert_at :: proc{insert_at_elem, insert_at_elems, insert_at_elem_string}
  405. @builtin
  406. clear_dynamic_array :: proc "contextless" (array: ^$T/[dynamic]$E) {
  407. if array != nil {
  408. (^Raw_Dynamic_Array)(array).len = 0
  409. }
  410. }
  411. @builtin
  412. reserve_dynamic_array :: proc(array: ^$T/[dynamic]$E, capacity: int, loc := #caller_location) -> bool {
  413. if array == nil {
  414. return false
  415. }
  416. a := (^Raw_Dynamic_Array)(array)
  417. if capacity <= a.cap {
  418. return true
  419. }
  420. if a.allocator.procedure == nil {
  421. a.allocator = context.allocator
  422. }
  423. assert(a.allocator.procedure != nil)
  424. old_size := a.cap * size_of(E)
  425. new_size := capacity * size_of(E)
  426. allocator := a.allocator
  427. new_data, err := allocator.procedure(
  428. allocator.data, .Resize, new_size, align_of(E),
  429. a.data, old_size, loc,
  430. )
  431. if new_data == nil || err != nil {
  432. return false
  433. }
  434. a.data = raw_data(new_data)
  435. a.cap = capacity
  436. return true
  437. }
  438. @builtin
  439. resize_dynamic_array :: proc(array: ^$T/[dynamic]$E, length: int, loc := #caller_location) -> bool {
  440. if array == nil {
  441. return false
  442. }
  443. a := (^Raw_Dynamic_Array)(array)
  444. if length <= a.cap {
  445. a.len = max(length, 0)
  446. return true
  447. }
  448. if a.allocator.procedure == nil {
  449. a.allocator = context.allocator
  450. }
  451. assert(a.allocator.procedure != nil)
  452. old_size := a.cap * size_of(E)
  453. new_size := length * size_of(E)
  454. allocator := a.allocator
  455. new_data, err := allocator.procedure(
  456. allocator.data, .Resize, new_size, align_of(E),
  457. a.data, old_size, loc,
  458. )
  459. if new_data == nil || err != nil {
  460. return false
  461. }
  462. a.data = raw_data(new_data)
  463. a.len = length
  464. a.cap = length
  465. return true
  466. }
  467. /*
  468. Shrinks the capacity of a dynamic array down to the current length, or the given capacity.
  469. If `new_cap` is negative, then `len(array)` is used.
  470. Returns false if `cap(array) < new_cap`, or the allocator report failure.
  471. If `len(array) < new_cap`, then `len(array)` will be left unchanged.
  472. */
  473. shrink_dynamic_array :: proc(array: ^$T/[dynamic]$E, new_cap := -1, loc := #caller_location) -> (did_shrink: bool) {
  474. if array == nil {
  475. return
  476. }
  477. a := (^Raw_Dynamic_Array)(array)
  478. new_cap := new_cap if new_cap >= 0 else a.len
  479. if new_cap > a.cap {
  480. return
  481. }
  482. if a.allocator.procedure == nil {
  483. a.allocator = context.allocator
  484. }
  485. assert(a.allocator.procedure != nil)
  486. old_size := a.cap * size_of(E)
  487. new_size := new_cap * size_of(E)
  488. new_data, err := a.allocator.procedure(
  489. a.allocator.data,
  490. .Resize,
  491. new_size,
  492. align_of(E),
  493. a.data,
  494. old_size,
  495. loc,
  496. )
  497. if err != nil {
  498. return
  499. }
  500. a.data = raw_data(new_data)
  501. a.len = min(new_cap, a.len)
  502. a.cap = new_cap
  503. return true
  504. }
  505. @builtin
  506. map_insert :: proc(m: ^$T/map[$K]$V, key: K, value: V, loc := #caller_location) -> (ptr: ^V) {
  507. key, value := key, value
  508. h := __get_map_header(m)
  509. hash := __get_map_hash(&key)
  510. data := uintptr(__dynamic_map_set(h, hash, &value, loc))
  511. return (^V)(data + h.value_offset)
  512. }
  513. @builtin
  514. incl_elem :: proc(s: ^$S/bit_set[$E; $U], elem: E) {
  515. s^ |= {elem}
  516. }
  517. @builtin
  518. incl_elems :: proc(s: ^$S/bit_set[$E; $U], elems: ..E) {
  519. for elem in elems {
  520. s^ |= {elem}
  521. }
  522. }
  523. @builtin
  524. incl_bit_set :: proc(s: ^$S/bit_set[$E; $U], other: S) {
  525. s^ |= other
  526. }
  527. @builtin
  528. excl_elem :: proc(s: ^$S/bit_set[$E; $U], elem: E) {
  529. s^ &~= {elem}
  530. }
  531. @builtin
  532. excl_elems :: proc(s: ^$S/bit_set[$E; $U], elems: ..E) {
  533. for elem in elems {
  534. s^ &~= {elem}
  535. }
  536. }
  537. @builtin
  538. excl_bit_set :: proc(s: ^$S/bit_set[$E; $U], other: S) {
  539. s^ &~= other
  540. }
  541. @builtin incl :: proc{incl_elem, incl_elems, incl_bit_set}
  542. @builtin excl :: proc{excl_elem, excl_elems, excl_bit_set}
  543. @builtin
  544. card :: proc(s: $S/bit_set[$E; $U]) -> int {
  545. when size_of(S) == 1 {
  546. return int(intrinsics.count_ones(transmute(u8)s))
  547. } else when size_of(S) == 2 {
  548. return int(intrinsics.count_ones(transmute(u16)s))
  549. } else when size_of(S) == 4 {
  550. return int(intrinsics.count_ones(transmute(u32)s))
  551. } else when size_of(S) == 8 {
  552. return int(intrinsics.count_ones(transmute(u64)s))
  553. } else when size_of(S) == 16 {
  554. return int(intrinsics.count_ones(transmute(u128)s))
  555. } else {
  556. #panic("Unhandled card bit_set size")
  557. }
  558. }
  559. @builtin
  560. raw_array_data :: proc "contextless" (a: $P/^($T/[$N]$E)) -> [^]E {
  561. return ([^]E)(a)
  562. }
  563. @builtin
  564. raw_simd_data :: proc "contextless" (a: $P/^($T/#simd[$N]$E)) -> [^]E {
  565. return ([^]E)(a)
  566. }
  567. @builtin
  568. raw_slice_data :: proc "contextless" (s: $S/[]$E) -> [^]E {
  569. ptr := (transmute(Raw_Slice)s).data
  570. return ([^]E)(ptr)
  571. }
  572. @builtin
  573. raw_dynamic_array_data :: proc "contextless" (s: $S/[dynamic]$E) -> [^]E {
  574. ptr := (transmute(Raw_Dynamic_Array)s).data
  575. return ([^]E)(ptr)
  576. }
  577. @builtin
  578. raw_string_data :: proc "contextless" (s: $S/string) -> [^]u8 {
  579. return (transmute(Raw_String)s).data
  580. }
  581. @builtin
  582. raw_data :: proc{raw_array_data, raw_slice_data, raw_dynamic_array_data, raw_string_data, raw_simd_data}
  583. @builtin
  584. @(disabled=ODIN_DISABLE_ASSERT)
  585. assert :: proc(condition: bool, message := "", loc := #caller_location) {
  586. if !condition {
  587. // NOTE(bill): This is wrapped in a procedure call
  588. // to improve performance to make the CPU not
  589. // execute speculatively, making it about an order of
  590. // magnitude faster
  591. @(cold)
  592. internal :: proc(message: string, loc: Source_Code_Location) {
  593. p := context.assertion_failure_proc
  594. if p == nil {
  595. p = default_assertion_failure_proc
  596. }
  597. p("runtime assertion", message, loc)
  598. }
  599. internal(message, loc)
  600. }
  601. }
  602. @builtin
  603. @(disabled=ODIN_DISABLE_ASSERT)
  604. panic :: proc(message: string, loc := #caller_location) -> ! {
  605. p := context.assertion_failure_proc
  606. if p == nil {
  607. p = default_assertion_failure_proc
  608. }
  609. p("panic", message, loc)
  610. }
  611. @builtin
  612. @(disabled=ODIN_DISABLE_ASSERT)
  613. unimplemented :: proc(message := "", loc := #caller_location) -> ! {
  614. p := context.assertion_failure_proc
  615. if p == nil {
  616. p = default_assertion_failure_proc
  617. }
  618. p("not yet implemented", message, loc)
  619. }
  620. @builtin
  621. @(disabled=ODIN_DISABLE_ASSERT)
  622. unreachable :: proc(message := "", loc := #caller_location) -> ! {
  623. p := context.assertion_failure_proc
  624. if p == nil {
  625. p = default_assertion_failure_proc
  626. }
  627. if message != "" {
  628. p("internal error", message, loc)
  629. } else {
  630. p("internal error", "entered unreachable code", loc)
  631. }
  632. }