core_builtin.odin 19 KB

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