internal.odin 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092
  1. package runtime
  2. import "base:intrinsics"
  3. @(private="file")
  4. IS_WASM :: ODIN_ARCH == .wasm32 || ODIN_ARCH == .wasm64p32
  5. @(private)
  6. RUNTIME_LINKAGE :: "strong" when (
  7. (ODIN_USE_SEPARATE_MODULES ||
  8. ODIN_BUILD_MODE == .Dynamic ||
  9. !ODIN_NO_CRT) &&
  10. !IS_WASM) else "internal"
  11. RUNTIME_REQUIRE :: false // !ODIN_TILDE
  12. @(private)
  13. __float16 :: f16 when __ODIN_LLVM_F16_SUPPORTED else u16
  14. @(private)
  15. byte_slice :: #force_inline proc "contextless" (data: rawptr, len: int) -> []byte #no_bounds_check {
  16. return ([^]byte)(data)[:max(len, 0)]
  17. }
  18. is_power_of_two_int :: #force_inline proc "contextless" (x: int) -> bool {
  19. if x <= 0 {
  20. return false
  21. }
  22. return (x & (x-1)) == 0
  23. }
  24. align_forward_int :: #force_inline proc(ptr, align: int) -> int {
  25. assert(is_power_of_two_int(align))
  26. p := ptr
  27. modulo := p & (align-1)
  28. if modulo != 0 {
  29. p += align - modulo
  30. }
  31. return p
  32. }
  33. is_power_of_two_uint :: #force_inline proc "contextless" (x: uint) -> bool {
  34. if x <= 0 {
  35. return false
  36. }
  37. return (x & (x-1)) == 0
  38. }
  39. align_forward_uint :: #force_inline proc(ptr, align: uint) -> uint {
  40. assert(is_power_of_two_uint(align))
  41. p := ptr
  42. modulo := p & (align-1)
  43. if modulo != 0 {
  44. p += align - modulo
  45. }
  46. return p
  47. }
  48. is_power_of_two_uintptr :: #force_inline proc "contextless" (x: uintptr) -> bool {
  49. if x <= 0 {
  50. return false
  51. }
  52. return (x & (x-1)) == 0
  53. }
  54. align_forward_uintptr :: #force_inline proc(ptr, align: uintptr) -> uintptr {
  55. assert(is_power_of_two_uintptr(align))
  56. p := ptr
  57. modulo := p & (align-1)
  58. if modulo != 0 {
  59. p += align - modulo
  60. }
  61. return p
  62. }
  63. is_power_of_two :: proc {
  64. is_power_of_two_int,
  65. is_power_of_two_uint,
  66. is_power_of_two_uintptr,
  67. }
  68. align_forward :: proc {
  69. align_forward_int,
  70. align_forward_uint,
  71. align_forward_uintptr,
  72. }
  73. mem_zero :: proc "contextless" (data: rawptr, len: int) -> rawptr {
  74. if data == nil {
  75. return nil
  76. }
  77. if len <= 0 {
  78. return data
  79. }
  80. intrinsics.mem_zero(data, len)
  81. return data
  82. }
  83. mem_copy :: proc "contextless" (dst, src: rawptr, len: int) -> rawptr {
  84. if src != nil && dst != src && len > 0 {
  85. // NOTE(bill): This _must_ be implemented like C's memmove
  86. intrinsics.mem_copy(dst, src, len)
  87. }
  88. return dst
  89. }
  90. mem_copy_non_overlapping :: proc "contextless" (dst, src: rawptr, len: int) -> rawptr {
  91. if src != nil && dst != src && len > 0 {
  92. // NOTE(bill): This _must_ be implemented like C's memcpy
  93. intrinsics.mem_copy_non_overlapping(dst, src, len)
  94. }
  95. return dst
  96. }
  97. DEFAULT_ALIGNMENT :: 2*align_of(rawptr)
  98. mem_alloc_bytes :: #force_inline proc(size: int, alignment: int = DEFAULT_ALIGNMENT, allocator := context.allocator, loc := #caller_location) -> ([]byte, Allocator_Error) {
  99. if size == 0 {
  100. return nil, nil
  101. }
  102. if allocator.procedure == nil {
  103. return nil, nil
  104. }
  105. return allocator.procedure(allocator.data, .Alloc, size, alignment, nil, 0, loc)
  106. }
  107. mem_alloc :: #force_inline proc(size: int, alignment: int = DEFAULT_ALIGNMENT, allocator := context.allocator, loc := #caller_location) -> ([]byte, Allocator_Error) {
  108. if size == 0 || allocator.procedure == nil {
  109. return nil, nil
  110. }
  111. return allocator.procedure(allocator.data, .Alloc, size, alignment, nil, 0, loc)
  112. }
  113. mem_alloc_non_zeroed :: #force_inline proc(size: int, alignment: int = DEFAULT_ALIGNMENT, allocator := context.allocator, loc := #caller_location) -> ([]byte, Allocator_Error) {
  114. if size == 0 || allocator.procedure == nil {
  115. return nil, nil
  116. }
  117. return allocator.procedure(allocator.data, .Alloc_Non_Zeroed, size, alignment, nil, 0, loc)
  118. }
  119. mem_free :: #force_inline proc(ptr: rawptr, allocator := context.allocator, loc := #caller_location) -> Allocator_Error {
  120. if ptr == nil || allocator.procedure == nil {
  121. return nil
  122. }
  123. _, err := allocator.procedure(allocator.data, .Free, 0, 0, ptr, 0, loc)
  124. return err
  125. }
  126. mem_free_with_size :: #force_inline proc(ptr: rawptr, byte_count: int, allocator := context.allocator, loc := #caller_location) -> Allocator_Error {
  127. if ptr == nil || allocator.procedure == nil {
  128. return nil
  129. }
  130. _, err := allocator.procedure(allocator.data, .Free, 0, 0, ptr, byte_count, loc)
  131. return err
  132. }
  133. mem_free_bytes :: #force_inline proc(bytes: []byte, allocator := context.allocator, loc := #caller_location) -> Allocator_Error {
  134. if bytes == nil || allocator.procedure == nil {
  135. return nil
  136. }
  137. _, err := allocator.procedure(allocator.data, .Free, 0, 0, raw_data(bytes), len(bytes), loc)
  138. return err
  139. }
  140. mem_free_all :: #force_inline proc(allocator := context.allocator, loc := #caller_location) -> (err: Allocator_Error) {
  141. if allocator.procedure != nil {
  142. _, err = allocator.procedure(allocator.data, .Free_All, 0, 0, nil, 0, loc)
  143. }
  144. return
  145. }
  146. _mem_resize :: #force_inline proc(ptr: rawptr, old_size, new_size: int, alignment: int = DEFAULT_ALIGNMENT, allocator := context.allocator, should_zero: bool, loc := #caller_location) -> (data: []byte, err: Allocator_Error) {
  147. if allocator.procedure == nil {
  148. return nil, nil
  149. }
  150. if new_size == 0 {
  151. if ptr != nil {
  152. _, err = allocator.procedure(allocator.data, .Free, 0, 0, ptr, old_size, loc)
  153. return
  154. }
  155. return
  156. } else if ptr == nil {
  157. if should_zero {
  158. return allocator.procedure(allocator.data, .Alloc, new_size, alignment, nil, 0, loc)
  159. } else {
  160. return allocator.procedure(allocator.data, .Alloc_Non_Zeroed, new_size, alignment, nil, 0, loc)
  161. }
  162. } else if old_size == new_size && uintptr(ptr) % uintptr(alignment) == 0 {
  163. data = ([^]byte)(ptr)[:old_size]
  164. return
  165. }
  166. if should_zero {
  167. data, err = allocator.procedure(allocator.data, .Resize, new_size, alignment, ptr, old_size, loc)
  168. } else {
  169. data, err = allocator.procedure(allocator.data, .Resize_Non_Zeroed, new_size, alignment, ptr, old_size, loc)
  170. }
  171. if err == .Mode_Not_Implemented {
  172. if should_zero {
  173. data, err = allocator.procedure(allocator.data, .Alloc, new_size, alignment, nil, 0, loc)
  174. } else {
  175. data, err = allocator.procedure(allocator.data, .Alloc_Non_Zeroed, new_size, alignment, nil, 0, loc)
  176. }
  177. if err != nil {
  178. return
  179. }
  180. copy(data, ([^]byte)(ptr)[:old_size])
  181. _, err = allocator.procedure(allocator.data, .Free, 0, 0, ptr, old_size, loc)
  182. }
  183. return
  184. }
  185. mem_resize :: proc(ptr: rawptr, old_size, new_size: int, alignment: int = DEFAULT_ALIGNMENT, allocator := context.allocator, loc := #caller_location) -> (data: []byte, err: Allocator_Error) {
  186. return _mem_resize(ptr, old_size, new_size, alignment, allocator, true, loc)
  187. }
  188. non_zero_mem_resize :: proc(ptr: rawptr, old_size, new_size: int, alignment: int = DEFAULT_ALIGNMENT, allocator := context.allocator, loc := #caller_location) -> (data: []byte, err: Allocator_Error) {
  189. return _mem_resize(ptr, old_size, new_size, alignment, allocator, false, loc)
  190. }
  191. memory_equal :: proc "contextless" (x, y: rawptr, n: int) -> bool {
  192. switch {
  193. case n == 0: return true
  194. case x == y: return true
  195. }
  196. a, b := ([^]byte)(x), ([^]byte)(y)
  197. length := uint(n)
  198. for i := uint(0); i < length; i += 1 {
  199. if a[i] != b[i] {
  200. return false
  201. }
  202. }
  203. return true
  204. /*
  205. when size_of(uint) == 8 {
  206. if word_length := length >> 3; word_length != 0 {
  207. for _ in 0..<word_length {
  208. if intrinsics.unaligned_load((^u64)(a)) != intrinsics.unaligned_load((^u64)(b)) {
  209. return false
  210. }
  211. a = a[size_of(u64):]
  212. b = b[size_of(u64):]
  213. }
  214. }
  215. if length & 4 != 0 {
  216. if intrinsics.unaligned_load((^u32)(a)) != intrinsics.unaligned_load((^u32)(b)) {
  217. return false
  218. }
  219. a = a[size_of(u32):]
  220. b = b[size_of(u32):]
  221. }
  222. if length & 2 != 0 {
  223. if intrinsics.unaligned_load((^u16)(a)) != intrinsics.unaligned_load((^u16)(b)) {
  224. return false
  225. }
  226. a = a[size_of(u16):]
  227. b = b[size_of(u16):]
  228. }
  229. if length & 1 != 0 && a[0] != b[0] {
  230. return false
  231. }
  232. return true
  233. } else {
  234. if word_length := length >> 2; word_length != 0 {
  235. for _ in 0..<word_length {
  236. if intrinsics.unaligned_load((^u32)(a)) != intrinsics.unaligned_load((^u32)(b)) {
  237. return false
  238. }
  239. a = a[size_of(u32):]
  240. b = b[size_of(u32):]
  241. }
  242. }
  243. length &= 3
  244. if length != 0 {
  245. for i in 0..<length {
  246. if a[i] != b[i] {
  247. return false
  248. }
  249. }
  250. }
  251. return true
  252. }
  253. */
  254. }
  255. memory_compare :: proc "contextless" (a, b: rawptr, n: int) -> int #no_bounds_check {
  256. switch {
  257. case a == b: return 0
  258. case a == nil: return -1
  259. case b == nil: return +1
  260. }
  261. x := uintptr(a)
  262. y := uintptr(b)
  263. n := uintptr(n)
  264. SU :: size_of(uintptr)
  265. fast := n/SU + 1
  266. offset := (fast-1)*SU
  267. curr_block := uintptr(0)
  268. if n < SU {
  269. fast = 0
  270. }
  271. for /**/; curr_block < fast; curr_block += 1 {
  272. va := (^uintptr)(x + curr_block * size_of(uintptr))^
  273. vb := (^uintptr)(y + curr_block * size_of(uintptr))^
  274. if va ~ vb != 0 {
  275. for pos := curr_block*SU; pos < n; pos += 1 {
  276. a := (^byte)(x+pos)^
  277. b := (^byte)(y+pos)^
  278. if a ~ b != 0 {
  279. return -1 if (int(a) - int(b)) < 0 else +1
  280. }
  281. }
  282. }
  283. }
  284. for /**/; offset < n; offset += 1 {
  285. a := (^byte)(x+offset)^
  286. b := (^byte)(y+offset)^
  287. if a ~ b != 0 {
  288. return -1 if (int(a) - int(b)) < 0 else +1
  289. }
  290. }
  291. return 0
  292. }
  293. memory_compare_zero :: proc "contextless" (a: rawptr, n: int) -> int #no_bounds_check {
  294. x := uintptr(a)
  295. n := uintptr(n)
  296. SU :: size_of(uintptr)
  297. fast := n/SU + 1
  298. offset := (fast-1)*SU
  299. curr_block := uintptr(0)
  300. if n < SU {
  301. fast = 0
  302. }
  303. for /**/; curr_block < fast; curr_block += 1 {
  304. va := (^uintptr)(x + curr_block * size_of(uintptr))^
  305. if va ~ 0 != 0 {
  306. for pos := curr_block*SU; pos < n; pos += 1 {
  307. a := (^byte)(x+pos)^
  308. if a ~ 0 != 0 {
  309. return -1 if int(a) < 0 else +1
  310. }
  311. }
  312. }
  313. }
  314. for /**/; offset < n; offset += 1 {
  315. a := (^byte)(x+offset)^
  316. if a ~ 0 != 0 {
  317. return -1 if int(a) < 0 else +1
  318. }
  319. }
  320. return 0
  321. }
  322. string_eq :: proc "contextless" (lhs, rhs: string) -> bool {
  323. x := transmute(Raw_String)lhs
  324. y := transmute(Raw_String)rhs
  325. if x.len != y.len {
  326. return false
  327. }
  328. return #force_inline memory_equal(x.data, y.data, x.len)
  329. }
  330. string_cmp :: proc "contextless" (a, b: string) -> int {
  331. x := transmute(Raw_String)a
  332. y := transmute(Raw_String)b
  333. ret := memory_compare(x.data, y.data, min(x.len, y.len))
  334. if ret == 0 && x.len != y.len {
  335. return -1 if x.len < y.len else +1
  336. }
  337. return ret
  338. }
  339. string_ne :: #force_inline proc "contextless" (a, b: string) -> bool { return !string_eq(a, b) }
  340. string_lt :: #force_inline proc "contextless" (a, b: string) -> bool { return string_cmp(a, b) < 0 }
  341. string_gt :: #force_inline proc "contextless" (a, b: string) -> bool { return string_cmp(a, b) > 0 }
  342. string_le :: #force_inline proc "contextless" (a, b: string) -> bool { return string_cmp(a, b) <= 0 }
  343. string_ge :: #force_inline proc "contextless" (a, b: string) -> bool { return string_cmp(a, b) >= 0 }
  344. cstring_len :: proc "contextless" (s: cstring) -> int {
  345. p0 := uintptr((^byte)(s))
  346. p := p0
  347. for p != 0 && (^byte)(p)^ != 0 {
  348. p += 1
  349. }
  350. return int(p - p0)
  351. }
  352. cstring_to_string :: proc "contextless" (s: cstring) -> string {
  353. if s == nil {
  354. return ""
  355. }
  356. ptr := (^byte)(s)
  357. n := cstring_len(s)
  358. return transmute(string)Raw_String{ptr, n}
  359. }
  360. cstring_eq :: proc "contextless" (lhs, rhs: cstring) -> bool {
  361. x := ([^]byte)(lhs)
  362. y := ([^]byte)(rhs)
  363. if x == y {
  364. return true
  365. }
  366. if (x == nil) ~ (y == nil) {
  367. return false
  368. }
  369. xn := cstring_len(lhs)
  370. yn := cstring_len(rhs)
  371. if xn != yn {
  372. return false
  373. }
  374. return #force_inline memory_equal(x, y, xn)
  375. }
  376. cstring_cmp :: proc "contextless" (lhs, rhs: cstring) -> int {
  377. x := ([^]byte)(lhs)
  378. y := ([^]byte)(rhs)
  379. if x == y {
  380. return 0
  381. }
  382. if (x == nil) ~ (y == nil) {
  383. return -1 if x == nil else +1
  384. }
  385. xn := cstring_len(lhs)
  386. yn := cstring_len(rhs)
  387. ret := memory_compare(x, y, min(xn, yn))
  388. if ret == 0 && xn != yn {
  389. return -1 if xn < yn else +1
  390. }
  391. return ret
  392. }
  393. cstring_ne :: #force_inline proc "contextless" (a, b: cstring) -> bool { return !cstring_eq(a, b) }
  394. cstring_lt :: #force_inline proc "contextless" (a, b: cstring) -> bool { return cstring_cmp(a, b) < 0 }
  395. cstring_gt :: #force_inline proc "contextless" (a, b: cstring) -> bool { return cstring_cmp(a, b) > 0 }
  396. cstring_le :: #force_inline proc "contextless" (a, b: cstring) -> bool { return cstring_cmp(a, b) <= 0 }
  397. cstring_ge :: #force_inline proc "contextless" (a, b: cstring) -> bool { return cstring_cmp(a, b) >= 0 }
  398. complex32_eq :: #force_inline proc "contextless" (a, b: complex32) -> bool { return real(a) == real(b) && imag(a) == imag(b) }
  399. complex32_ne :: #force_inline proc "contextless" (a, b: complex32) -> bool { return real(a) != real(b) || imag(a) != imag(b) }
  400. complex64_eq :: #force_inline proc "contextless" (a, b: complex64) -> bool { return real(a) == real(b) && imag(a) == imag(b) }
  401. complex64_ne :: #force_inline proc "contextless" (a, b: complex64) -> bool { return real(a) != real(b) || imag(a) != imag(b) }
  402. complex128_eq :: #force_inline proc "contextless" (a, b: complex128) -> bool { return real(a) == real(b) && imag(a) == imag(b) }
  403. complex128_ne :: #force_inline proc "contextless" (a, b: complex128) -> bool { return real(a) != real(b) || imag(a) != imag(b) }
  404. quaternion64_eq :: #force_inline proc "contextless" (a, b: quaternion64) -> bool { return real(a) == real(b) && imag(a) == imag(b) && jmag(a) == jmag(b) && kmag(a) == kmag(b) }
  405. quaternion64_ne :: #force_inline proc "contextless" (a, b: quaternion64) -> bool { return real(a) != real(b) || imag(a) != imag(b) || jmag(a) != jmag(b) || kmag(a) != kmag(b) }
  406. quaternion128_eq :: #force_inline proc "contextless" (a, b: quaternion128) -> bool { return real(a) == real(b) && imag(a) == imag(b) && jmag(a) == jmag(b) && kmag(a) == kmag(b) }
  407. quaternion128_ne :: #force_inline proc "contextless" (a, b: quaternion128) -> bool { return real(a) != real(b) || imag(a) != imag(b) || jmag(a) != jmag(b) || kmag(a) != kmag(b) }
  408. quaternion256_eq :: #force_inline proc "contextless" (a, b: quaternion256) -> bool { return real(a) == real(b) && imag(a) == imag(b) && jmag(a) == jmag(b) && kmag(a) == kmag(b) }
  409. quaternion256_ne :: #force_inline proc "contextless" (a, b: quaternion256) -> bool { return real(a) != real(b) || imag(a) != imag(b) || jmag(a) != jmag(b) || kmag(a) != kmag(b) }
  410. string_decode_rune :: #force_inline proc "contextless" (s: string) -> (rune, int) {
  411. // NOTE(bill): Duplicated here to remove dependency on package unicode/utf8
  412. @static accept_sizes := [256]u8{
  413. 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, // 0x00-0x0f
  414. 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, // 0x10-0x1f
  415. 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, // 0x20-0x2f
  416. 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, // 0x30-0x3f
  417. 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, // 0x40-0x4f
  418. 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, // 0x50-0x5f
  419. 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, // 0x60-0x6f
  420. 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, // 0x70-0x7f
  421. 0xf1, 0xf1, 0xf1, 0xf1, 0xf1, 0xf1, 0xf1, 0xf1, 0xf1, 0xf1, 0xf1, 0xf1, 0xf1, 0xf1, 0xf1, 0xf1, // 0x80-0x8f
  422. 0xf1, 0xf1, 0xf1, 0xf1, 0xf1, 0xf1, 0xf1, 0xf1, 0xf1, 0xf1, 0xf1, 0xf1, 0xf1, 0xf1, 0xf1, 0xf1, // 0x90-0x9f
  423. 0xf1, 0xf1, 0xf1, 0xf1, 0xf1, 0xf1, 0xf1, 0xf1, 0xf1, 0xf1, 0xf1, 0xf1, 0xf1, 0xf1, 0xf1, 0xf1, // 0xa0-0xaf
  424. 0xf1, 0xf1, 0xf1, 0xf1, 0xf1, 0xf1, 0xf1, 0xf1, 0xf1, 0xf1, 0xf1, 0xf1, 0xf1, 0xf1, 0xf1, 0xf1, // 0xb0-0xbf
  425. 0xf1, 0xf1, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, // 0xc0-0xcf
  426. 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, // 0xd0-0xdf
  427. 0x13, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x23, 0x03, 0x03, // 0xe0-0xef
  428. 0x34, 0x04, 0x04, 0x04, 0x44, 0xf1, 0xf1, 0xf1, 0xf1, 0xf1, 0xf1, 0xf1, 0xf1, 0xf1, 0xf1, 0xf1, // 0xf0-0xff
  429. }
  430. Accept_Range :: struct {lo, hi: u8}
  431. @static accept_ranges := [5]Accept_Range{
  432. {0x80, 0xbf},
  433. {0xa0, 0xbf},
  434. {0x80, 0x9f},
  435. {0x90, 0xbf},
  436. {0x80, 0x8f},
  437. }
  438. MASKX :: 0b0011_1111
  439. MASK2 :: 0b0001_1111
  440. MASK3 :: 0b0000_1111
  441. MASK4 :: 0b0000_0111
  442. LOCB :: 0b1000_0000
  443. HICB :: 0b1011_1111
  444. RUNE_ERROR :: '\ufffd'
  445. n := len(s)
  446. if n < 1 {
  447. return RUNE_ERROR, 0
  448. }
  449. s0 := s[0]
  450. x := accept_sizes[s0]
  451. if x >= 0xF0 {
  452. mask := rune(x) << 31 >> 31 // NOTE(bill): Create 0x0000 or 0xffff.
  453. return rune(s[0])&~mask | RUNE_ERROR&mask, 1
  454. }
  455. sz := x & 7
  456. accept := accept_ranges[x>>4]
  457. if n < int(sz) {
  458. return RUNE_ERROR, 1
  459. }
  460. b1 := s[1]
  461. if b1 < accept.lo || accept.hi < b1 {
  462. return RUNE_ERROR, 1
  463. }
  464. if sz == 2 {
  465. return rune(s0&MASK2)<<6 | rune(b1&MASKX), 2
  466. }
  467. b2 := s[2]
  468. if b2 < LOCB || HICB < b2 {
  469. return RUNE_ERROR, 1
  470. }
  471. if sz == 3 {
  472. return rune(s0&MASK3)<<12 | rune(b1&MASKX)<<6 | rune(b2&MASKX), 3
  473. }
  474. b3 := s[3]
  475. if b3 < LOCB || HICB < b3 {
  476. return RUNE_ERROR, 1
  477. }
  478. return rune(s0&MASK4)<<18 | rune(b1&MASKX)<<12 | rune(b2&MASKX)<<6 | rune(b3&MASKX), 4
  479. }
  480. string_decode_last_rune :: proc "contextless" (s: string) -> (rune, int) {
  481. RUNE_ERROR :: '\ufffd'
  482. RUNE_SELF :: 0x80
  483. UTF_MAX :: 4
  484. r: rune
  485. size: int
  486. start, end, limit: int
  487. end = len(s)
  488. if end == 0 {
  489. return RUNE_ERROR, 0
  490. }
  491. start = end-1
  492. r = rune(s[start])
  493. if r < RUNE_SELF {
  494. return r, 1
  495. }
  496. limit = max(end - UTF_MAX, 0)
  497. for start-=1; start >= limit; start-=1 {
  498. if (s[start] & 0xc0) != RUNE_SELF {
  499. break
  500. }
  501. }
  502. start = max(start, 0)
  503. r, size = string_decode_rune(s[start:end])
  504. if start+size != end {
  505. return RUNE_ERROR, 1
  506. }
  507. return r, size
  508. }
  509. abs_complex32 :: #force_inline proc "contextless" (x: complex32) -> f16 {
  510. p, q := abs(real(x)), abs(imag(x))
  511. if p < q {
  512. p, q = q, p
  513. }
  514. if p == 0 {
  515. return 0
  516. }
  517. q = q / p
  518. return p * f16(intrinsics.sqrt(f32(1 + q*q)))
  519. }
  520. abs_complex64 :: #force_inline proc "contextless" (x: complex64) -> f32 {
  521. p, q := abs(real(x)), abs(imag(x))
  522. if p < q {
  523. p, q = q, p
  524. }
  525. if p == 0 {
  526. return 0
  527. }
  528. q = q / p
  529. return p * intrinsics.sqrt(1 + q*q)
  530. }
  531. abs_complex128 :: #force_inline proc "contextless" (x: complex128) -> f64 {
  532. p, q := abs(real(x)), abs(imag(x))
  533. if p < q {
  534. p, q = q, p
  535. }
  536. if p == 0 {
  537. return 0
  538. }
  539. q = q / p
  540. return p * intrinsics.sqrt(1 + q*q)
  541. }
  542. abs_quaternion64 :: #force_inline proc "contextless" (x: quaternion64) -> f16 {
  543. r, i, j, k := real(x), imag(x), jmag(x), kmag(x)
  544. return f16(intrinsics.sqrt(f32(r*r + i*i + j*j + k*k)))
  545. }
  546. abs_quaternion128 :: #force_inline proc "contextless" (x: quaternion128) -> f32 {
  547. r, i, j, k := real(x), imag(x), jmag(x), kmag(x)
  548. return intrinsics.sqrt(r*r + i*i + j*j + k*k)
  549. }
  550. abs_quaternion256 :: #force_inline proc "contextless" (x: quaternion256) -> f64 {
  551. r, i, j, k := real(x), imag(x), jmag(x), kmag(x)
  552. return intrinsics.sqrt(r*r + i*i + j*j + k*k)
  553. }
  554. quo_complex32 :: proc "contextless" (n, m: complex32) -> complex32 {
  555. e, f: f16
  556. if abs(real(m)) >= abs(imag(m)) {
  557. ratio := imag(m) / real(m)
  558. denom := real(m) + ratio*imag(m)
  559. e = (real(n) + imag(n)*ratio) / denom
  560. f = (imag(n) - real(n)*ratio) / denom
  561. } else {
  562. ratio := real(m) / imag(m)
  563. denom := imag(m) + ratio*real(m)
  564. e = (real(n)*ratio + imag(n)) / denom
  565. f = (imag(n)*ratio - real(n)) / denom
  566. }
  567. return complex(e, f)
  568. }
  569. quo_complex64 :: proc "contextless" (n, m: complex64) -> complex64 {
  570. e, f: f32
  571. if abs(real(m)) >= abs(imag(m)) {
  572. ratio := imag(m) / real(m)
  573. denom := real(m) + ratio*imag(m)
  574. e = (real(n) + imag(n)*ratio) / denom
  575. f = (imag(n) - real(n)*ratio) / denom
  576. } else {
  577. ratio := real(m) / imag(m)
  578. denom := imag(m) + ratio*real(m)
  579. e = (real(n)*ratio + imag(n)) / denom
  580. f = (imag(n)*ratio - real(n)) / denom
  581. }
  582. return complex(e, f)
  583. }
  584. quo_complex128 :: proc "contextless" (n, m: complex128) -> complex128 {
  585. e, f: f64
  586. if abs(real(m)) >= abs(imag(m)) {
  587. ratio := imag(m) / real(m)
  588. denom := real(m) + ratio*imag(m)
  589. e = (real(n) + imag(n)*ratio) / denom
  590. f = (imag(n) - real(n)*ratio) / denom
  591. } else {
  592. ratio := real(m) / imag(m)
  593. denom := imag(m) + ratio*real(m)
  594. e = (real(n)*ratio + imag(n)) / denom
  595. f = (imag(n)*ratio - real(n)) / denom
  596. }
  597. return complex(e, f)
  598. }
  599. mul_quaternion64 :: proc "contextless" (q, r: quaternion64) -> quaternion64 {
  600. q0, q1, q2, q3 := real(q), imag(q), jmag(q), kmag(q)
  601. r0, r1, r2, r3 := real(r), imag(r), jmag(r), kmag(r)
  602. t0 := r0*q0 - r1*q1 - r2*q2 - r3*q3
  603. t1 := r0*q1 + r1*q0 - r2*q3 + r3*q2
  604. t2 := r0*q2 + r1*q3 + r2*q0 - r3*q1
  605. t3 := r0*q3 - r1*q2 + r2*q1 + r3*q0
  606. return quaternion(w=t0, x=t1, y=t2, z=t3)
  607. }
  608. mul_quaternion128 :: proc "contextless" (q, r: quaternion128) -> quaternion128 {
  609. q0, q1, q2, q3 := real(q), imag(q), jmag(q), kmag(q)
  610. r0, r1, r2, r3 := real(r), imag(r), jmag(r), kmag(r)
  611. t0 := r0*q0 - r1*q1 - r2*q2 - r3*q3
  612. t1 := r0*q1 + r1*q0 - r2*q3 + r3*q2
  613. t2 := r0*q2 + r1*q3 + r2*q0 - r3*q1
  614. t3 := r0*q3 - r1*q2 + r2*q1 + r3*q0
  615. return quaternion(w=t0, x=t1, y=t2, z=t3)
  616. }
  617. mul_quaternion256 :: proc "contextless" (q, r: quaternion256) -> quaternion256 {
  618. q0, q1, q2, q3 := real(q), imag(q), jmag(q), kmag(q)
  619. r0, r1, r2, r3 := real(r), imag(r), jmag(r), kmag(r)
  620. t0 := r0*q0 - r1*q1 - r2*q2 - r3*q3
  621. t1 := r0*q1 + r1*q0 - r2*q3 + r3*q2
  622. t2 := r0*q2 + r1*q3 + r2*q0 - r3*q1
  623. t3 := r0*q3 - r1*q2 + r2*q1 + r3*q0
  624. return quaternion(w=t0, x=t1, y=t2, z=t3)
  625. }
  626. quo_quaternion64 :: proc "contextless" (q, r: quaternion64) -> quaternion64 {
  627. q0, q1, q2, q3 := real(q), imag(q), jmag(q), kmag(q)
  628. r0, r1, r2, r3 := real(r), imag(r), jmag(r), kmag(r)
  629. invmag2 := 1.0 / (r0*r0 + r1*r1 + r2*r2 + r3*r3)
  630. t0 := (r0*q0 + r1*q1 + r2*q2 + r3*q3) * invmag2
  631. t1 := (r0*q1 - r1*q0 - r2*q3 - r3*q2) * invmag2
  632. t2 := (r0*q2 - r1*q3 - r2*q0 + r3*q1) * invmag2
  633. t3 := (r0*q3 + r1*q2 + r2*q1 - r3*q0) * invmag2
  634. return quaternion(w=t0, x=t1, y=t2, z=t3)
  635. }
  636. quo_quaternion128 :: proc "contextless" (q, r: quaternion128) -> quaternion128 {
  637. q0, q1, q2, q3 := real(q), imag(q), jmag(q), kmag(q)
  638. r0, r1, r2, r3 := real(r), imag(r), jmag(r), kmag(r)
  639. invmag2 := 1.0 / (r0*r0 + r1*r1 + r2*r2 + r3*r3)
  640. t0 := (r0*q0 + r1*q1 + r2*q2 + r3*q3) * invmag2
  641. t1 := (r0*q1 - r1*q0 - r2*q3 - r3*q2) * invmag2
  642. t2 := (r0*q2 - r1*q3 - r2*q0 + r3*q1) * invmag2
  643. t3 := (r0*q3 + r1*q2 + r2*q1 - r3*q0) * invmag2
  644. return quaternion(w=t0, x=t1, y=t2, z=t3)
  645. }
  646. quo_quaternion256 :: proc "contextless" (q, r: quaternion256) -> quaternion256 {
  647. q0, q1, q2, q3 := real(q), imag(q), jmag(q), kmag(q)
  648. r0, r1, r2, r3 := real(r), imag(r), jmag(r), kmag(r)
  649. invmag2 := 1.0 / (r0*r0 + r1*r1 + r2*r2 + r3*r3)
  650. t0 := (r0*q0 + r1*q1 + r2*q2 + r3*q3) * invmag2
  651. t1 := (r0*q1 - r1*q0 - r2*q3 - r3*q2) * invmag2
  652. t2 := (r0*q2 - r1*q3 - r2*q0 + r3*q1) * invmag2
  653. t3 := (r0*q3 + r1*q2 + r2*q1 - r3*q0) * invmag2
  654. return quaternion(w=t0, x=t1, y=t2, z=t3)
  655. }
  656. @(link_name="__truncsfhf2", linkage=RUNTIME_LINKAGE, require=RUNTIME_REQUIRE)
  657. truncsfhf2 :: proc "c" (value: f32) -> __float16 {
  658. v: struct #raw_union { i: u32, f: f32 }
  659. i, s, e, m: i32
  660. v.f = value
  661. i = i32(v.i)
  662. s = (i >> 16) & 0x00008000
  663. e = ((i >> 23) & 0x000000ff) - (127 - 15)
  664. m = i & 0x007fffff
  665. if e <= 0 {
  666. if e < -10 {
  667. return transmute(__float16)u16(s)
  668. }
  669. m = (m | 0x00800000) >> u32(1 - e)
  670. if m & 0x00001000 != 0 {
  671. m += 0x00002000
  672. }
  673. return transmute(__float16)u16(s | (m >> 13))
  674. } else if e == 0xff - (127 - 15) {
  675. if m == 0 {
  676. return transmute(__float16)u16(s | 0x7c00) /* NOTE(bill): infinity */
  677. } else {
  678. /* NOTE(bill): NAN */
  679. m >>= 13
  680. return transmute(__float16)u16(s | 0x7c00 | m | i32(m == 0))
  681. }
  682. } else {
  683. if m & 0x00001000 != 0 {
  684. m += 0x00002000
  685. if (m & 0x00800000) != 0 {
  686. m = 0
  687. e += 1
  688. }
  689. }
  690. if e > 30 {
  691. f := i64(1e12)
  692. for j := 0; j < 10; j += 1 {
  693. /* NOTE(bill): Cause overflow */
  694. g := intrinsics.volatile_load(&f)
  695. g *= g
  696. intrinsics.volatile_store(&f, g)
  697. }
  698. return transmute(__float16)u16(s | 0x7c00)
  699. }
  700. return transmute(__float16)u16(s | (e << 10) | (m >> 13))
  701. }
  702. }
  703. @(link_name="__aeabi_d2h", linkage=RUNTIME_LINKAGE, require=RUNTIME_REQUIRE)
  704. aeabi_d2h :: proc "c" (value: f64) -> __float16 {
  705. return truncsfhf2(f32(value))
  706. }
  707. @(link_name="__truncdfhf2", linkage=RUNTIME_LINKAGE, require=RUNTIME_REQUIRE)
  708. truncdfhf2 :: proc "c" (value: f64) -> __float16 {
  709. return truncsfhf2(f32(value))
  710. }
  711. @(link_name="__gnu_h2f_ieee", linkage=RUNTIME_LINKAGE, require=RUNTIME_REQUIRE)
  712. gnu_h2f_ieee :: proc "c" (value_: __float16) -> f32 {
  713. fp32 :: struct #raw_union { u: u32, f: f32 }
  714. value := transmute(u16)value_
  715. v: fp32
  716. magic, inf_or_nan: fp32
  717. magic.u = u32((254 - 15) << 23)
  718. inf_or_nan.u = u32((127 + 16) << 23)
  719. v.u = u32(value & 0x7fff) << 13
  720. v.f *= magic.f
  721. if v.f >= inf_or_nan.f {
  722. v.u |= 255 << 23
  723. }
  724. v.u |= u32(value & 0x8000) << 16
  725. return v.f
  726. }
  727. @(link_name="__gnu_f2h_ieee", linkage=RUNTIME_LINKAGE, require=RUNTIME_REQUIRE)
  728. gnu_f2h_ieee :: proc "c" (value: f32) -> __float16 {
  729. return truncsfhf2(value)
  730. }
  731. @(link_name="__extendhfsf2", linkage=RUNTIME_LINKAGE, require=RUNTIME_REQUIRE)
  732. extendhfsf2 :: proc "c" (value: __float16) -> f32 {
  733. return gnu_h2f_ieee(value)
  734. }
  735. @(link_name="__floattidf", linkage=RUNTIME_LINKAGE, require=RUNTIME_REQUIRE)
  736. floattidf :: proc "c" (a: i128) -> f64 {
  737. when IS_WASM {
  738. return 0
  739. } else {
  740. DBL_MANT_DIG :: 53
  741. if a == 0 {
  742. return 0.0
  743. }
  744. a := a
  745. N :: size_of(i128) * 8
  746. s := a >> (N-1)
  747. a = (a ~ s) - s
  748. sd: = N - intrinsics.count_leading_zeros(a) // number of significant digits
  749. e := i32(sd - 1) // exponent
  750. if sd > DBL_MANT_DIG {
  751. switch sd {
  752. case DBL_MANT_DIG + 1:
  753. a <<= 1
  754. case DBL_MANT_DIG + 2:
  755. // okay
  756. case:
  757. a = i128(u128(a) >> u128(sd - (DBL_MANT_DIG+2))) |
  758. i128(u128(a) & (~u128(0) >> u128(N + DBL_MANT_DIG+2 - sd)) != 0)
  759. }
  760. a |= i128((a & 4) != 0)
  761. a += 1
  762. a >>= 2
  763. if a & (i128(1) << DBL_MANT_DIG) != 0 {
  764. a >>= 1
  765. e += 1
  766. }
  767. } else {
  768. a <<= u128(DBL_MANT_DIG - sd) & 127
  769. }
  770. fb: [2]u32
  771. fb[1] = (u32(s) & 0x80000000) | // sign
  772. (u32(e + 1023) << 20) | // exponent
  773. u32((u64(a) >> 32) & 0x000FFFFF) // mantissa-high
  774. fb[0] = u32(a) // mantissa-low
  775. return transmute(f64)fb
  776. }
  777. }
  778. @(link_name="__floattidf_unsigned", linkage=RUNTIME_LINKAGE, require=RUNTIME_REQUIRE)
  779. floattidf_unsigned :: proc "c" (a: u128) -> f64 {
  780. when IS_WASM {
  781. return 0
  782. } else {
  783. DBL_MANT_DIG :: 53
  784. if a == 0 {
  785. return 0.0
  786. }
  787. a := a
  788. N :: size_of(u128) * 8
  789. sd: = N - intrinsics.count_leading_zeros(a) // number of significant digits
  790. e := i32(sd - 1) // exponent
  791. if sd > DBL_MANT_DIG {
  792. switch sd {
  793. case DBL_MANT_DIG + 1:
  794. a <<= 1
  795. case DBL_MANT_DIG + 2:
  796. // okay
  797. case:
  798. a = u128(u128(a) >> u128(sd - (DBL_MANT_DIG+2))) |
  799. u128(u128(a) & (~u128(0) >> u128(N + DBL_MANT_DIG+2 - sd)) != 0)
  800. }
  801. a |= u128((a & 4) != 0)
  802. a += 1
  803. a >>= 2
  804. if a & (1 << DBL_MANT_DIG) != 0 {
  805. a >>= 1
  806. e += 1
  807. }
  808. } else {
  809. a <<= u128(DBL_MANT_DIG - sd)
  810. }
  811. fb: [2]u32
  812. fb[1] = (0) | // sign
  813. u32((e + 1023) << 20) | // exponent
  814. u32((u64(a) >> 32) & 0x000FFFFF) // mantissa-high
  815. fb[0] = u32(a) // mantissa-low
  816. return transmute(f64)fb
  817. }
  818. }
  819. @(link_name="__fixunsdfti", linkage=RUNTIME_LINKAGE, require=RUNTIME_REQUIRE)
  820. fixunsdfti :: #force_no_inline proc "c" (a: f64) -> u128 {
  821. // TODO(bill): implement `fixunsdfti` correctly
  822. x := u64(a)
  823. return u128(x)
  824. }
  825. @(link_name="__fixunsdfdi", linkage=RUNTIME_LINKAGE, require=RUNTIME_REQUIRE)
  826. fixunsdfdi :: #force_no_inline proc "c" (a: f64) -> i128 {
  827. // TODO(bill): implement `fixunsdfdi` correctly
  828. x := i64(a)
  829. return i128(x)
  830. }
  831. @(link_name="__umodti3", linkage=RUNTIME_LINKAGE, require=RUNTIME_REQUIRE)
  832. umodti3 :: proc "c" (a, b: u128) -> u128 {
  833. r: u128 = ---
  834. _ = udivmod128(a, b, &r)
  835. return r
  836. }
  837. @(link_name="__udivmodti4", linkage=RUNTIME_LINKAGE, require=RUNTIME_REQUIRE)
  838. udivmodti4 :: proc "c" (a, b: u128, rem: ^u128) -> u128 {
  839. return udivmod128(a, b, rem)
  840. }
  841. when !IS_WASM {
  842. @(link_name="__udivti3", linkage=RUNTIME_LINKAGE, require=RUNTIME_REQUIRE)
  843. udivti3 :: proc "c" (a, b: u128) -> u128 {
  844. return udivmodti4(a, b, nil)
  845. }
  846. }
  847. @(link_name="__modti3", linkage=RUNTIME_LINKAGE, require=RUNTIME_REQUIRE)
  848. modti3 :: proc "c" (a, b: i128) -> i128 {
  849. s_a := a >> (128 - 1)
  850. s_b := b >> (128 - 1)
  851. an := (a ~ s_a) - s_a
  852. bn := (b ~ s_b) - s_b
  853. r: u128 = ---
  854. _ = udivmod128(transmute(u128)an, transmute(u128)bn, &r)
  855. return (transmute(i128)r ~ s_a) - s_a
  856. }
  857. @(link_name="__divmodti4", linkage=RUNTIME_LINKAGE, require=RUNTIME_REQUIRE)
  858. divmodti4 :: proc "c" (a, b: i128, rem: ^i128) -> i128 {
  859. u := udivmod128(transmute(u128)a, transmute(u128)b, cast(^u128)rem)
  860. return transmute(i128)u
  861. }
  862. @(link_name="__divti3", linkage=RUNTIME_LINKAGE, require=RUNTIME_REQUIRE)
  863. divti3 :: proc "c" (a, b: i128) -> i128 {
  864. u := udivmodti4(transmute(u128)a, transmute(u128)b, nil)
  865. return transmute(i128)u
  866. }
  867. @(link_name="__fixdfti", linkage=RUNTIME_LINKAGE, require=RUNTIME_REQUIRE)
  868. fixdfti :: proc(a: u64) -> i128 {
  869. significandBits :: 52
  870. typeWidth :: (size_of(u64)*8)
  871. exponentBits :: (typeWidth - significandBits - 1)
  872. maxExponent :: ((1 << exponentBits) - 1)
  873. exponentBias :: (maxExponent >> 1)
  874. implicitBit :: (u64(1) << significandBits)
  875. significandMask :: (implicitBit - 1)
  876. signBit :: (u64(1) << (significandBits + exponentBits))
  877. absMask :: (signBit - 1)
  878. exponentMask :: (absMask ~ significandMask)
  879. // Break a into sign, exponent, significand
  880. aRep := a
  881. aAbs := aRep & absMask
  882. sign := i128(-1 if aRep & signBit != 0 else 1)
  883. exponent := u64((aAbs >> significandBits) - exponentBias)
  884. significand := u64((aAbs & significandMask) | implicitBit)
  885. // If exponent is negative, the result is zero.
  886. if exponent < 0 {
  887. return 0
  888. }
  889. // If the value is too large for the integer type, saturate.
  890. if exponent >= size_of(i128) * 8 {
  891. return max(i128) if sign == 1 else min(i128)
  892. }
  893. // If 0 <= exponent < significandBits, right shift to get the result.
  894. // Otherwise, shift left.
  895. if exponent < significandBits {
  896. return sign * i128(significand >> (significandBits - exponent))
  897. } else {
  898. return sign * (i128(significand) << (exponent - significandBits))
  899. }
  900. }
  901. __write_bits :: proc "contextless" (dst, src: [^]byte, offset: uintptr, size: uintptr) {
  902. for i in 0..<size {
  903. j := offset+i
  904. the_bit := byte((src[i>>3]) & (1<<(i&7)) != 0)
  905. dst[j>>3] &~= 1<<(j&7)
  906. dst[j>>3] |= the_bit<<(j&7)
  907. }
  908. }
  909. __read_bits :: proc "contextless" (dst, src: [^]byte, offset: uintptr, size: uintptr) {
  910. for j in 0..<size {
  911. i := offset+j
  912. the_bit := byte((src[i>>3]) & (1<<(i&7)) != 0)
  913. dst[j>>3] &~= 1<<(j&7)
  914. dst[j>>3] |= the_bit<<(j&7)
  915. }
  916. }