internal.odin 30 KB

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