core_builtin.odin 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723
  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_with_size(raw_data(str), len(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_with_size(raw_data(array), cap(array)*size_of(E), 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_with_size(raw_data(array), len(array)*size_of(E), allocator, loc)
  129. }
  130. @builtin
  131. delete_map :: proc(m: $T/map[$K]$V, loc := #caller_location) -> Allocator_Error {
  132. return map_free_dynamic(transmute(Raw_Map)m, map_info(T), loc)
  133. }
  134. @builtin
  135. delete :: proc{
  136. delete_string,
  137. delete_cstring,
  138. delete_dynamic_array,
  139. delete_slice,
  140. delete_map,
  141. }
  142. // The new built-in procedure allocates memory. The first argument is a type, not a value, and the value
  143. // return is a pointer to a newly allocated value of that type using the specified allocator, default is context.allocator
  144. @builtin
  145. new :: proc($T: typeid, allocator := context.allocator, loc := #caller_location) -> (^T, Allocator_Error) #optional_allocator_error {
  146. return new_aligned(T, align_of(T), allocator, loc)
  147. }
  148. new_aligned :: proc($T: typeid, alignment: int, allocator := context.allocator, loc := #caller_location) -> (t: ^T, err: Allocator_Error) {
  149. data := mem_alloc_bytes(size_of(T), alignment, allocator, loc) or_return
  150. t = (^T)(raw_data(data))
  151. return
  152. }
  153. @builtin
  154. new_clone :: proc(data: $T, allocator := context.allocator, loc := #caller_location) -> (t: ^T, err: Allocator_Error) #optional_allocator_error {
  155. t_data := mem_alloc_bytes(size_of(T), align_of(T), allocator, loc) or_return
  156. t = (^T)(raw_data(t_data))
  157. if t != nil {
  158. t^ = data
  159. }
  160. return
  161. }
  162. DEFAULT_RESERVE_CAPACITY :: 16
  163. make_aligned :: proc($T: typeid/[]$E, #any_int len: int, alignment: int, allocator := context.allocator, loc := #caller_location) -> (T, Allocator_Error) #optional_allocator_error {
  164. make_slice_error_loc(loc, len)
  165. data, err := mem_alloc_bytes(size_of(E)*len, alignment, allocator, loc)
  166. if data == nil && size_of(E) != 0 {
  167. return nil, err
  168. }
  169. s := Raw_Slice{raw_data(data), len}
  170. return transmute(T)s, err
  171. }
  172. @(builtin)
  173. make_slice :: proc($T: typeid/[]$E, #any_int len: int, allocator := context.allocator, loc := #caller_location) -> (T, Allocator_Error) #optional_allocator_error {
  174. return make_aligned(T, len, align_of(E), allocator, loc)
  175. }
  176. @(builtin)
  177. make_dynamic_array :: proc($T: typeid/[dynamic]$E, allocator := context.allocator, loc := #caller_location) -> (T, Allocator_Error) #optional_allocator_error {
  178. return make_dynamic_array_len_cap(T, 0, DEFAULT_RESERVE_CAPACITY, allocator, loc)
  179. }
  180. @(builtin)
  181. make_dynamic_array_len :: proc($T: typeid/[dynamic]$E, #any_int len: int, allocator := context.allocator, loc := #caller_location) -> (T, Allocator_Error) #optional_allocator_error {
  182. return make_dynamic_array_len_cap(T, len, len, allocator, loc)
  183. }
  184. @(builtin)
  185. 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_allocator_error {
  186. make_dynamic_array_error_loc(loc, len, cap)
  187. data := mem_alloc_bytes(size_of(E)*cap, align_of(E), allocator, loc) or_return
  188. s := Raw_Dynamic_Array{raw_data(data), len, cap, allocator}
  189. if data == nil && size_of(E) != 0 {
  190. s.len, s.cap = 0, 0
  191. }
  192. array = transmute(T)s
  193. return
  194. }
  195. @(builtin)
  196. make_map :: proc($T: typeid/map[$K]$E, #any_int capacity: int = 1<<MAP_MIN_LOG2_CAPACITY, allocator := context.allocator, loc := #caller_location) -> (m: T, err: Allocator_Error) #optional_allocator_error {
  197. make_map_expr_error_loc(loc, capacity)
  198. context.allocator = allocator
  199. err = reserve_map(&m, capacity, loc)
  200. return
  201. }
  202. @(builtin)
  203. make_multi_pointer :: proc($T: typeid/[^]$E, #any_int len: int, allocator := context.allocator, loc := #caller_location) -> (mp: T, err: Allocator_Error) #optional_allocator_error {
  204. make_slice_error_loc(loc, len)
  205. data := mem_alloc_bytes(size_of(E)*len, align_of(E), allocator, loc) or_return
  206. if data == nil && size_of(E) != 0 {
  207. return
  208. }
  209. mp = cast(T)raw_data(data)
  210. return
  211. }
  212. // The make built-in procedure allocates and initializes a value of type slice, dynamic array, or map (only)
  213. // Similar to new, the first argument is a type, not a value. Unlike new, make's return type is the same as the
  214. // type of its argument, not a pointer to it.
  215. // Make uses the specified allocator, default is context.allocator, default is context.allocator
  216. @builtin
  217. make :: proc{
  218. make_slice,
  219. make_dynamic_array,
  220. make_dynamic_array_len,
  221. make_dynamic_array_len_cap,
  222. make_map,
  223. make_multi_pointer,
  224. }
  225. @builtin
  226. clear_map :: proc "contextless" (m: ^$T/map[$K]$V) {
  227. if m == nil {
  228. return
  229. }
  230. map_clear_dynamic((^Raw_Map)(m), map_info(T))
  231. }
  232. @builtin
  233. reserve_map :: proc(m: ^$T/map[$K]$V, capacity: int, loc := #caller_location) -> Allocator_Error {
  234. return __dynamic_map_reserve((^Raw_Map)(m), map_info(T), uint(capacity), loc) if m != nil else nil
  235. }
  236. /*
  237. Shrinks the capacity of a map down to the current length.
  238. */
  239. @builtin
  240. shrink_map :: proc(m: ^$T/map[$K]$V, loc := #caller_location) -> (did_shrink: bool) {
  241. if m != nil {
  242. err := map_shrink_dynamic((^Raw_Map)(m), map_info(T), loc)
  243. did_shrink = err == nil
  244. }
  245. return
  246. }
  247. // The delete_key built-in procedure deletes the element with the specified key (m[key]) from the map.
  248. // If m is nil, or there is no such element, this procedure is a no-op
  249. @builtin
  250. delete_key :: proc(m: ^$T/map[$K]$V, key: K) -> (deleted_key: K, deleted_value: V) {
  251. if m != nil {
  252. key := key
  253. old_k, old_v, ok := map_erase_dynamic((^Raw_Map)(m), map_info(T), uintptr(&key))
  254. if ok {
  255. deleted_key = (^K)(old_k)^
  256. deleted_value = (^V)(old_v)^
  257. }
  258. }
  259. return
  260. }
  261. @builtin
  262. append_elem :: proc(array: ^$T/[dynamic]$E, arg: E, loc := #caller_location) -> int {
  263. if array == nil {
  264. return 0
  265. }
  266. when size_of(E) == 0 {
  267. array.len += 1
  268. return 1
  269. } else {
  270. if cap(array) < len(array)+1 {
  271. cap := 2 * cap(array) + max(8, 1)
  272. _ = reserve(array, cap, loc)
  273. }
  274. if cap(array)-len(array) > 0 {
  275. a := (^Raw_Dynamic_Array)(array)
  276. when size_of(E) != 0 {
  277. data := ([^]E)(a.data)
  278. assert(condition=data != nil, loc=loc)
  279. data[a.len] = arg
  280. }
  281. a.len += 1
  282. return 1
  283. }
  284. return 0
  285. }
  286. }
  287. @builtin
  288. append_elems :: proc(array: ^$T/[dynamic]$E, args: ..E, loc := #caller_location) -> int {
  289. if array == nil {
  290. return 0
  291. }
  292. arg_len := len(args)
  293. if arg_len <= 0 {
  294. return 0
  295. }
  296. when size_of(E) == 0 {
  297. array.len += arg_len
  298. return arg_len
  299. } else {
  300. if cap(array) < len(array)+arg_len {
  301. cap := 2 * cap(array) + max(8, arg_len)
  302. _ = reserve(array, cap, loc)
  303. }
  304. arg_len = min(cap(array)-len(array), arg_len)
  305. if arg_len > 0 {
  306. a := (^Raw_Dynamic_Array)(array)
  307. when size_of(E) != 0 {
  308. data := ([^]E)(a.data)
  309. assert(condition=data != nil, loc=loc)
  310. intrinsics.mem_copy(&data[a.len], raw_data(args), size_of(E) * arg_len)
  311. }
  312. a.len += arg_len
  313. }
  314. return arg_len
  315. }
  316. }
  317. // The append_string built-in procedure appends a string to the end of a [dynamic]u8 like type
  318. @builtin
  319. append_elem_string :: proc(array: ^$T/[dynamic]$E/u8, arg: $A/string, loc := #caller_location) -> int {
  320. args := transmute([]E)arg
  321. return append_elems(array=array, args=args, loc=loc)
  322. }
  323. // The append_string built-in procedure appends multiple strings to the end of a [dynamic]u8 like type
  324. @builtin
  325. append_string :: proc(array: ^$T/[dynamic]$E/u8, args: ..string, loc := #caller_location) -> (n: int) {
  326. for arg in args {
  327. n += append(array = array, args = transmute([]E)(arg), loc = loc)
  328. }
  329. return
  330. }
  331. // The append built-in procedure appends elements to the end of a dynamic array
  332. @builtin append :: proc{append_elem, append_elems, append_elem_string}
  333. @builtin
  334. append_nothing :: proc(array: ^$T/[dynamic]$E, loc := #caller_location) -> int {
  335. if array == nil {
  336. return 0
  337. }
  338. prev_len := len(array)
  339. resize(array, len(array)+1)
  340. return len(array)-prev_len
  341. }
  342. @builtin
  343. inject_at_elem :: proc(array: ^$T/[dynamic]$E, index: int, arg: E, loc := #caller_location) -> (ok: bool) #no_bounds_check {
  344. if array == nil {
  345. return
  346. }
  347. n := max(len(array), index)
  348. m :: 1
  349. new_size := n + m
  350. if resize(array, new_size, loc) {
  351. when size_of(E) != 0 {
  352. copy(array[index + m:], array[index:])
  353. array[index] = arg
  354. }
  355. ok = true
  356. }
  357. return
  358. }
  359. @builtin
  360. inject_at_elems :: proc(array: ^$T/[dynamic]$E, index: int, args: ..E, loc := #caller_location) -> (ok: bool) #no_bounds_check {
  361. if array == nil {
  362. return
  363. }
  364. if len(args) == 0 {
  365. ok = true
  366. return
  367. }
  368. n := max(len(array), index)
  369. m := len(args)
  370. new_size := n + m
  371. if resize(array, new_size, loc) {
  372. when size_of(E) != 0 {
  373. copy(array[index + m:], array[index:])
  374. copy(array[index:], args)
  375. }
  376. ok = true
  377. }
  378. return
  379. }
  380. @builtin
  381. inject_at_elem_string :: proc(array: ^$T/[dynamic]$E/u8, index: int, arg: string, loc := #caller_location) -> (ok: bool) #no_bounds_check {
  382. if array == nil {
  383. return
  384. }
  385. if len(arg) == 0 {
  386. ok = true
  387. return
  388. }
  389. n := max(len(array), index)
  390. m := len(arg)
  391. new_size := n + m
  392. if resize(array, new_size, loc) {
  393. copy(array[index+m:], array[index:])
  394. copy(array[index:], arg)
  395. ok = true
  396. }
  397. return
  398. }
  399. @builtin inject_at :: proc{inject_at_elem, inject_at_elems, inject_at_elem_string}
  400. @builtin
  401. assign_at_elem :: proc(array: ^$T/[dynamic]$E, index: int, arg: E, loc := #caller_location) -> (ok: bool) #no_bounds_check {
  402. if index < len(array) {
  403. array[index] = arg
  404. ok = true
  405. } else if resize(array, index+1, loc) {
  406. array[index] = arg
  407. ok = true
  408. }
  409. return
  410. }
  411. @builtin
  412. assign_at_elems :: proc(array: ^$T/[dynamic]$E, index: int, args: ..E, loc := #caller_location) -> (ok: bool) #no_bounds_check {
  413. if index+len(args) < len(array) {
  414. copy(array[index:], args)
  415. ok = true
  416. } else if resize(array, index+1+len(args), loc) {
  417. copy(array[index:], args)
  418. ok = true
  419. }
  420. return
  421. }
  422. @builtin
  423. assign_at_elem_string :: proc(array: ^$T/[dynamic]$E/u8, index: int, arg: string, loc := #caller_location) -> (ok: bool) #no_bounds_check {
  424. if len(args) == 0 {
  425. ok = true
  426. } else if index+len(args) < len(array) {
  427. copy(array[index:], args)
  428. ok = true
  429. } else if resize(array, index+1+len(args), loc) {
  430. copy(array[index:], args)
  431. ok = true
  432. }
  433. return
  434. }
  435. @builtin assign_at :: proc{assign_at_elem, assign_at_elems, assign_at_elem_string}
  436. @builtin
  437. clear_dynamic_array :: proc "contextless" (array: ^$T/[dynamic]$E) {
  438. if array != nil {
  439. (^Raw_Dynamic_Array)(array).len = 0
  440. }
  441. }
  442. @builtin
  443. reserve_dynamic_array :: proc(array: ^$T/[dynamic]$E, capacity: int, loc := #caller_location) -> bool {
  444. if array == nil {
  445. return false
  446. }
  447. a := (^Raw_Dynamic_Array)(array)
  448. if capacity <= a.cap {
  449. return true
  450. }
  451. if a.allocator.procedure == nil {
  452. a.allocator = context.allocator
  453. }
  454. assert(a.allocator.procedure != nil)
  455. old_size := a.cap * size_of(E)
  456. new_size := capacity * size_of(E)
  457. allocator := a.allocator
  458. new_data, err := mem_resize(a.data, old_size, new_size, align_of(E), allocator, loc)
  459. if new_data == nil || err != nil {
  460. return false
  461. }
  462. a.data = raw_data(new_data)
  463. a.cap = capacity
  464. return true
  465. }
  466. @builtin
  467. resize_dynamic_array :: proc(array: ^$T/[dynamic]$E, length: int, loc := #caller_location) -> bool {
  468. if array == nil {
  469. return false
  470. }
  471. a := (^Raw_Dynamic_Array)(array)
  472. if length <= a.cap {
  473. a.len = max(length, 0)
  474. return true
  475. }
  476. if a.allocator.procedure == nil {
  477. a.allocator = context.allocator
  478. }
  479. assert(a.allocator.procedure != nil)
  480. old_size := a.cap * size_of(E)
  481. new_size := length * size_of(E)
  482. allocator := a.allocator
  483. new_data, err := mem_resize(a.data, old_size, new_size, align_of(E), allocator, loc)
  484. if new_data == nil || err != nil {
  485. return false
  486. }
  487. a.data = raw_data(new_data)
  488. a.len = length
  489. a.cap = length
  490. return true
  491. }
  492. /*
  493. Shrinks the capacity of a dynamic array down to the current length, or the given capacity.
  494. If `new_cap` is negative, then `len(array)` is used.
  495. Returns false if `cap(array) < new_cap`, or the allocator report failure.
  496. If `len(array) < new_cap`, then `len(array)` will be left unchanged.
  497. */
  498. shrink_dynamic_array :: proc(array: ^$T/[dynamic]$E, new_cap := -1, loc := #caller_location) -> (did_shrink: bool) {
  499. if array == nil {
  500. return
  501. }
  502. a := (^Raw_Dynamic_Array)(array)
  503. new_cap := new_cap if new_cap >= 0 else a.len
  504. if new_cap > a.cap {
  505. return
  506. }
  507. if a.allocator.procedure == nil {
  508. a.allocator = context.allocator
  509. }
  510. assert(a.allocator.procedure != nil)
  511. old_size := a.cap * size_of(E)
  512. new_size := new_cap * size_of(E)
  513. new_data, err := mem_resize(a.data, old_size, new_size, align_of(E), a.allocator, loc)
  514. if err != nil {
  515. return
  516. }
  517. a.data = raw_data(new_data)
  518. a.len = min(new_cap, a.len)
  519. a.cap = new_cap
  520. return true
  521. }
  522. @builtin
  523. map_insert :: proc(m: ^$T/map[$K]$V, key: K, value: V, loc := #caller_location) -> (ptr: ^V) {
  524. key, value := key, value
  525. return (^V)(__dynamic_map_set_without_hash((^Raw_Map)(m), map_info(T), rawptr(&key), rawptr(&value), loc))
  526. }
  527. @builtin
  528. incl_elem :: proc(s: ^$S/bit_set[$E; $U], elem: E) {
  529. s^ |= {elem}
  530. }
  531. @builtin
  532. incl_elems :: proc(s: ^$S/bit_set[$E; $U], elems: ..E) {
  533. for elem in elems {
  534. s^ |= {elem}
  535. }
  536. }
  537. @builtin
  538. incl_bit_set :: proc(s: ^$S/bit_set[$E; $U], other: S) {
  539. s^ |= other
  540. }
  541. @builtin
  542. excl_elem :: proc(s: ^$S/bit_set[$E; $U], elem: E) {
  543. s^ &~= {elem}
  544. }
  545. @builtin
  546. excl_elems :: proc(s: ^$S/bit_set[$E; $U], elems: ..E) {
  547. for elem in elems {
  548. s^ &~= {elem}
  549. }
  550. }
  551. @builtin
  552. excl_bit_set :: proc(s: ^$S/bit_set[$E; $U], other: S) {
  553. s^ &~= other
  554. }
  555. @builtin incl :: proc{incl_elem, incl_elems, incl_bit_set}
  556. @builtin excl :: proc{excl_elem, excl_elems, excl_bit_set}
  557. @builtin
  558. card :: proc(s: $S/bit_set[$E; $U]) -> int {
  559. when size_of(S) == 1 {
  560. return int(intrinsics.count_ones(transmute(u8)s))
  561. } else when size_of(S) == 2 {
  562. return int(intrinsics.count_ones(transmute(u16)s))
  563. } else when size_of(S) == 4 {
  564. return int(intrinsics.count_ones(transmute(u32)s))
  565. } else when size_of(S) == 8 {
  566. return int(intrinsics.count_ones(transmute(u64)s))
  567. } else when size_of(S) == 16 {
  568. return int(intrinsics.count_ones(transmute(u128)s))
  569. } else {
  570. #panic("Unhandled card bit_set size")
  571. }
  572. }
  573. @builtin
  574. @(disabled=ODIN_DISABLE_ASSERT)
  575. assert :: proc(condition: bool, message := "", loc := #caller_location) {
  576. if !condition {
  577. // NOTE(bill): This is wrapped in a procedure call
  578. // to improve performance to make the CPU not
  579. // execute speculatively, making it about an order of
  580. // magnitude faster
  581. @(cold)
  582. internal :: proc(message: string, loc: Source_Code_Location) {
  583. p := context.assertion_failure_proc
  584. if p == nil {
  585. p = default_assertion_failure_proc
  586. }
  587. p("runtime assertion", message, loc)
  588. }
  589. internal(message, loc)
  590. }
  591. }
  592. @builtin
  593. @(disabled=ODIN_DISABLE_ASSERT)
  594. panic :: proc(message: string, loc := #caller_location) -> ! {
  595. p := context.assertion_failure_proc
  596. if p == nil {
  597. p = default_assertion_failure_proc
  598. }
  599. p("panic", message, loc)
  600. }
  601. @builtin
  602. @(disabled=ODIN_DISABLE_ASSERT)
  603. unimplemented :: proc(message := "", loc := #caller_location) -> ! {
  604. p := context.assertion_failure_proc
  605. if p == nil {
  606. p = default_assertion_failure_proc
  607. }
  608. p("not yet implemented", message, loc)
  609. }