core_builtin.odin 18 KB

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