internal.odin 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105
  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) 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 "odin" (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 "odin" (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 "odin" (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, rodata) 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, rodata) 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. nr, ni := f32(real(n)), f32(imag(n))
  556. mr, mi := f32(real(m)), f32(imag(m))
  557. e, f: f32
  558. if abs(mr) >= abs(mi) {
  559. ratio := mi / mr
  560. denom := mr + ratio*mi
  561. e = (nr + ni*ratio) / denom
  562. f = (ni - nr*ratio) / denom
  563. } else {
  564. ratio := mr / mi
  565. denom := mi + ratio*mr
  566. e = (nr*ratio + ni) / denom
  567. f = (ni*ratio - nr) / denom
  568. }
  569. return complex(f16(e), f16(f))
  570. }
  571. quo_complex64 :: proc "contextless" (n, m: complex64) -> complex64 {
  572. e, f: f32
  573. if abs(real(m)) >= abs(imag(m)) {
  574. ratio := imag(m) / real(m)
  575. denom := real(m) + ratio*imag(m)
  576. e = (real(n) + imag(n)*ratio) / denom
  577. f = (imag(n) - real(n)*ratio) / denom
  578. } else {
  579. ratio := real(m) / imag(m)
  580. denom := imag(m) + ratio*real(m)
  581. e = (real(n)*ratio + imag(n)) / denom
  582. f = (imag(n)*ratio - real(n)) / denom
  583. }
  584. return complex(e, f)
  585. }
  586. quo_complex128 :: proc "contextless" (n, m: complex128) -> complex128 {
  587. e, f: f64
  588. if abs(real(m)) >= abs(imag(m)) {
  589. ratio := imag(m) / real(m)
  590. denom := real(m) + ratio*imag(m)
  591. e = (real(n) + imag(n)*ratio) / denom
  592. f = (imag(n) - real(n)*ratio) / denom
  593. } else {
  594. ratio := real(m) / imag(m)
  595. denom := imag(m) + ratio*real(m)
  596. e = (real(n)*ratio + imag(n)) / denom
  597. f = (imag(n)*ratio - real(n)) / denom
  598. }
  599. return complex(e, f)
  600. }
  601. mul_quaternion64 :: proc "contextless" (q, r: quaternion64) -> quaternion64 {
  602. q0, q1, q2, q3 := f32(real(q)), f32(imag(q)), f32(jmag(q)), f32(kmag(q))
  603. r0, r1, r2, r3 := f32(real(r)), f32(imag(r)), f32(jmag(r)), f32(kmag(r))
  604. t0 := r0*q0 - r1*q1 - r2*q2 - r3*q3
  605. t1 := r0*q1 + r1*q0 - r2*q3 + r3*q2
  606. t2 := r0*q2 + r1*q3 + r2*q0 - r3*q1
  607. t3 := r0*q3 - r1*q2 + r2*q1 + r3*q0
  608. return quaternion(w=f16(t0), x=f16(t1), y=f16(t2), z=f16(t3))
  609. }
  610. mul_quaternion128 :: proc "contextless" (q, r: quaternion128) -> quaternion128 {
  611. q0, q1, q2, q3 := real(q), imag(q), jmag(q), kmag(q)
  612. r0, r1, r2, r3 := real(r), imag(r), jmag(r), kmag(r)
  613. t0 := r0*q0 - r1*q1 - r2*q2 - r3*q3
  614. t1 := r0*q1 + r1*q0 - r2*q3 + r3*q2
  615. t2 := r0*q2 + r1*q3 + r2*q0 - r3*q1
  616. t3 := r0*q3 - r1*q2 + r2*q1 + r3*q0
  617. return quaternion(w=t0, x=t1, y=t2, z=t3)
  618. }
  619. mul_quaternion256 :: proc "contextless" (q, r: quaternion256) -> quaternion256 {
  620. q0, q1, q2, q3 := real(q), imag(q), jmag(q), kmag(q)
  621. r0, r1, r2, r3 := real(r), imag(r), jmag(r), kmag(r)
  622. t0 := r0*q0 - r1*q1 - r2*q2 - r3*q3
  623. t1 := r0*q1 + r1*q0 - r2*q3 + r3*q2
  624. t2 := r0*q2 + r1*q3 + r2*q0 - r3*q1
  625. t3 := r0*q3 - r1*q2 + r2*q1 + r3*q0
  626. return quaternion(w=t0, x=t1, y=t2, z=t3)
  627. }
  628. quo_quaternion64 :: proc "contextless" (q, r: quaternion64) -> quaternion64 {
  629. q0, q1, q2, q3 := f32(real(q)), f32(imag(q)), f32(jmag(q)), f32(kmag(q))
  630. r0, r1, r2, r3 := f32(real(r)), f32(imag(r)), f32(jmag(r)), f32(kmag(r))
  631. invmag2 := 1.0 / (r0*r0 + r1*r1 + r2*r2 + r3*r3)
  632. t0 := (r0*q0 + r1*q1 + r2*q2 + r3*q3) * invmag2
  633. t1 := (r0*q1 - r1*q0 - r2*q3 - r3*q2) * invmag2
  634. t2 := (r0*q2 - r1*q3 - r2*q0 + r3*q1) * invmag2
  635. t3 := (r0*q3 + r1*q2 + r2*q1 - r3*q0) * invmag2
  636. return quaternion(w=f16(t0), x=f16(t1), y=f16(t2), z=f16(t3))
  637. }
  638. quo_quaternion128 :: proc "contextless" (q, r: quaternion128) -> quaternion128 {
  639. q0, q1, q2, q3 := real(q), imag(q), jmag(q), kmag(q)
  640. r0, r1, r2, r3 := real(r), imag(r), jmag(r), kmag(r)
  641. invmag2 := 1.0 / (r0*r0 + r1*r1 + r2*r2 + r3*r3)
  642. t0 := (r0*q0 + r1*q1 + r2*q2 + r3*q3) * invmag2
  643. t1 := (r0*q1 - r1*q0 - r2*q3 - r3*q2) * invmag2
  644. t2 := (r0*q2 - r1*q3 - r2*q0 + r3*q1) * invmag2
  645. t3 := (r0*q3 + r1*q2 + r2*q1 - r3*q0) * invmag2
  646. return quaternion(w=t0, x=t1, y=t2, z=t3)
  647. }
  648. quo_quaternion256 :: proc "contextless" (q, r: quaternion256) -> quaternion256 {
  649. q0, q1, q2, q3 := real(q), imag(q), jmag(q), kmag(q)
  650. r0, r1, r2, r3 := real(r), imag(r), jmag(r), kmag(r)
  651. invmag2 := 1.0 / (r0*r0 + r1*r1 + r2*r2 + r3*r3)
  652. t0 := (r0*q0 + r1*q1 + r2*q2 + r3*q3) * invmag2
  653. t1 := (r0*q1 - r1*q0 - r2*q3 - r3*q2) * invmag2
  654. t2 := (r0*q2 - r1*q3 - r2*q0 + r3*q1) * invmag2
  655. t3 := (r0*q3 + r1*q2 + r2*q1 - r3*q0) * invmag2
  656. return quaternion(w=t0, x=t1, y=t2, z=t3)
  657. }
  658. @(link_name="__truncsfhf2", linkage=RUNTIME_LINKAGE, require=RUNTIME_REQUIRE)
  659. truncsfhf2 :: proc "c" (value: f32) -> __float16 {
  660. v: struct #raw_union { i: u32, f: f32 }
  661. i, s, e, m: i32
  662. v.f = value
  663. i = i32(v.i)
  664. s = (i >> 16) & 0x00008000
  665. e = ((i >> 23) & 0x000000ff) - (127 - 15)
  666. m = i & 0x007fffff
  667. if e <= 0 {
  668. if e < -10 {
  669. return transmute(__float16)u16(s)
  670. }
  671. m = (m | 0x00800000) >> u32(1 - e)
  672. if m & 0x00001000 != 0 {
  673. m += 0x00002000
  674. }
  675. return transmute(__float16)u16(s | (m >> 13))
  676. } else if e == 0xff - (127 - 15) {
  677. if m == 0 {
  678. return transmute(__float16)u16(s | 0x7c00) /* NOTE(bill): infinity */
  679. } else {
  680. /* NOTE(bill): NAN */
  681. m >>= 13
  682. return transmute(__float16)u16(s | 0x7c00 | m | i32(m == 0))
  683. }
  684. } else {
  685. if m & 0x00001000 != 0 {
  686. m += 0x00002000
  687. if (m & 0x00800000) != 0 {
  688. m = 0
  689. e += 1
  690. }
  691. }
  692. if e > 30 {
  693. f := i64(1e12)
  694. for j := 0; j < 10; j += 1 {
  695. /* NOTE(bill): Cause overflow */
  696. g := intrinsics.volatile_load(&f)
  697. g *= g
  698. intrinsics.volatile_store(&f, g)
  699. }
  700. return transmute(__float16)u16(s | 0x7c00)
  701. }
  702. return transmute(__float16)u16(s | (e << 10) | (m >> 13))
  703. }
  704. }
  705. @(link_name="__aeabi_d2h", linkage=RUNTIME_LINKAGE, require=RUNTIME_REQUIRE)
  706. aeabi_d2h :: proc "c" (value: f64) -> __float16 {
  707. return truncsfhf2(f32(value))
  708. }
  709. @(link_name="__truncdfhf2", linkage=RUNTIME_LINKAGE, require=RUNTIME_REQUIRE)
  710. truncdfhf2 :: proc "c" (value: f64) -> __float16 {
  711. return truncsfhf2(f32(value))
  712. }
  713. @(link_name="__gnu_h2f_ieee", linkage=RUNTIME_LINKAGE, require=RUNTIME_REQUIRE)
  714. gnu_h2f_ieee :: proc "c" (value_: __float16) -> f32 {
  715. fp32 :: struct #raw_union { u: u32, f: f32 }
  716. value := transmute(u16)value_
  717. v: fp32
  718. magic, inf_or_nan: fp32
  719. magic.u = u32((254 - 15) << 23)
  720. inf_or_nan.u = u32((127 + 16) << 23)
  721. v.u = u32(value & 0x7fff) << 13
  722. v.f *= magic.f
  723. if v.f >= inf_or_nan.f {
  724. v.u |= 255 << 23
  725. }
  726. v.u |= u32(value & 0x8000) << 16
  727. return v.f
  728. }
  729. @(link_name="__gnu_f2h_ieee", linkage=RUNTIME_LINKAGE, require=RUNTIME_REQUIRE)
  730. gnu_f2h_ieee :: proc "c" (value: f32) -> __float16 {
  731. return truncsfhf2(value)
  732. }
  733. @(link_name="__extendhfsf2", linkage=RUNTIME_LINKAGE, require=RUNTIME_REQUIRE)
  734. extendhfsf2 :: proc "c" (value: __float16) -> f32 {
  735. return gnu_h2f_ieee(value)
  736. }
  737. @(link_name="__floattidf", linkage=RUNTIME_LINKAGE, require=RUNTIME_REQUIRE)
  738. floattidf :: proc "c" (a: i128) -> f64 {
  739. DBL_MANT_DIG :: 53
  740. if a == 0 {
  741. return 0.0
  742. }
  743. a := a
  744. N :: size_of(i128) * 8
  745. s := a >> (N-1)
  746. a = (a ~ s) - s
  747. sd: = N - intrinsics.count_leading_zeros(a) // number of significant digits
  748. e := i32(sd - 1) // exponent
  749. if sd > DBL_MANT_DIG {
  750. switch sd {
  751. case DBL_MANT_DIG + 1:
  752. a <<= 1
  753. case DBL_MANT_DIG + 2:
  754. // okay
  755. case:
  756. a = i128(u128(a) >> u128(sd - (DBL_MANT_DIG+2))) |
  757. i128(u128(a) & (~u128(0) >> u128(N + DBL_MANT_DIG+2 - sd)) != 0)
  758. }
  759. a |= i128((a & 4) != 0)
  760. a += 1
  761. a >>= 2
  762. if a & (i128(1) << DBL_MANT_DIG) != 0 {
  763. a >>= 1
  764. e += 1
  765. }
  766. } else {
  767. a <<= u128(DBL_MANT_DIG - sd) & 127
  768. }
  769. fb: [2]u32
  770. fb[1] = (u32(s) & 0x80000000) | // sign
  771. (u32(e + 1023) << 20) | // exponent
  772. u32((u64(a) >> 32) & 0x000FFFFF) // mantissa-high
  773. fb[0] = u32(a) // mantissa-low
  774. return transmute(f64)fb
  775. }
  776. @(link_name="__floattidf_unsigned", linkage=RUNTIME_LINKAGE, require=RUNTIME_REQUIRE)
  777. floattidf_unsigned :: proc "c" (a: u128) -> f64 {
  778. DBL_MANT_DIG :: 53
  779. if a == 0 {
  780. return 0.0
  781. }
  782. a := a
  783. N :: size_of(u128) * 8
  784. sd: = N - intrinsics.count_leading_zeros(a) // number of significant digits
  785. e := i32(sd - 1) // exponent
  786. if sd > DBL_MANT_DIG {
  787. switch sd {
  788. case DBL_MANT_DIG + 1:
  789. a <<= 1
  790. case DBL_MANT_DIG + 2:
  791. // okay
  792. case:
  793. a = u128(u128(a) >> u128(sd - (DBL_MANT_DIG+2))) |
  794. u128(u128(a) & (~u128(0) >> u128(N + DBL_MANT_DIG+2 - sd)) != 0)
  795. }
  796. a |= u128((a & 4) != 0)
  797. a += 1
  798. a >>= 2
  799. if a & (1 << DBL_MANT_DIG) != 0 {
  800. a >>= 1
  801. e += 1
  802. }
  803. } else {
  804. a <<= u128(DBL_MANT_DIG - sd)
  805. }
  806. fb: [2]u32
  807. fb[1] = (0) | // sign
  808. u32((e + 1023) << 20) | // exponent
  809. u32((u64(a) >> 32) & 0x000FFFFF) // mantissa-high
  810. fb[0] = u32(a) // mantissa-low
  811. return transmute(f64)fb
  812. }
  813. @(link_name="__fixunsdfti", linkage=RUNTIME_LINKAGE, require=RUNTIME_REQUIRE)
  814. fixunsdfti :: #force_no_inline proc "c" (a: f64) -> u128 {
  815. // TODO(bill): implement `fixunsdfti` correctly
  816. x := u64(a)
  817. return u128(x)
  818. }
  819. @(link_name="__fixunsdfdi", linkage=RUNTIME_LINKAGE, require=RUNTIME_REQUIRE)
  820. fixunsdfdi :: #force_no_inline proc "c" (a: f64) -> i128 {
  821. // TODO(bill): implement `fixunsdfdi` correctly
  822. x := i64(a)
  823. return i128(x)
  824. }
  825. @(link_name="__umodti3", linkage=RUNTIME_LINKAGE, require=RUNTIME_REQUIRE)
  826. umodti3 :: proc "c" (a, b: u128) -> u128 {
  827. r: u128 = ---
  828. _ = udivmod128(a, b, &r)
  829. return r
  830. }
  831. @(link_name="__udivmodti4", linkage=RUNTIME_LINKAGE, require=RUNTIME_REQUIRE)
  832. udivmodti4 :: proc "c" (a, b: u128, rem: ^u128) -> u128 {
  833. return udivmod128(a, b, rem)
  834. }
  835. when !IS_WASM {
  836. @(link_name="__udivti3", linkage=RUNTIME_LINKAGE, require=RUNTIME_REQUIRE)
  837. udivti3 :: proc "c" (a, b: u128) -> u128 {
  838. return udivmodti4(a, b, nil)
  839. }
  840. }
  841. @(link_name="__modti3", linkage=RUNTIME_LINKAGE, require=RUNTIME_REQUIRE)
  842. modti3 :: proc "c" (a, b: i128) -> i128 {
  843. s_a := a >> (128 - 1)
  844. s_b := b >> (128 - 1)
  845. an := (a ~ s_a) - s_a
  846. bn := (b ~ s_b) - s_b
  847. r: u128 = ---
  848. _ = udivmod128(u128(an), u128(bn), &r)
  849. return (i128(r) ~ s_a) - s_a
  850. }
  851. @(link_name="__divmodti4", linkage=RUNTIME_LINKAGE, require=RUNTIME_REQUIRE)
  852. divmodti4 :: proc "c" (a, b: i128, rem: ^i128) -> i128 {
  853. s_a := a >> (128 - 1) // -1 if negative or 0
  854. s_b := b >> (128 - 1)
  855. an := (a ~ s_a) - s_a // absolute
  856. bn := (b ~ s_b) - s_b
  857. s_b ~= s_a // quotient sign
  858. u_s_b := u128(s_b)
  859. u_s_a := u128(s_a)
  860. r: u128 = ---
  861. u := i128((udivmodti4(u128(an), u128(bn), &r) ~ u_s_b) - u_s_b) // negate if negative
  862. rem^ = i128((r ~ u_s_a) - u_s_a)
  863. return u
  864. }
  865. @(link_name="__divti3", linkage=RUNTIME_LINKAGE, require=RUNTIME_REQUIRE)
  866. divti3 :: proc "c" (a, b: i128) -> i128 {
  867. s_a := a >> (128 - 1) // -1 if negative or 0
  868. s_b := b >> (128 - 1)
  869. an := (a ~ s_a) - s_a // absolute
  870. bn := (b ~ s_b) - s_b
  871. s_a ~= s_b // quotient sign
  872. u_s_a := u128(s_a)
  873. return i128((udivmodti4(u128(an), u128(bn), nil) ~ u_s_a) - u_s_a) // negate if negative
  874. }
  875. @(link_name="__fixdfti", linkage=RUNTIME_LINKAGE, require=RUNTIME_REQUIRE)
  876. fixdfti :: proc "c" (a: u64) -> i128 {
  877. significandBits :: 52
  878. typeWidth :: (size_of(u64)*8)
  879. exponentBits :: (typeWidth - significandBits - 1)
  880. maxExponent :: ((1 << exponentBits) - 1)
  881. exponentBias :: (maxExponent >> 1)
  882. implicitBit :: (u64(1) << significandBits)
  883. significandMask :: (implicitBit - 1)
  884. signBit :: (u64(1) << (significandBits + exponentBits))
  885. absMask :: (signBit - 1)
  886. exponentMask :: (absMask ~ significandMask)
  887. // Break a into sign, exponent, significand
  888. aRep := a
  889. aAbs := aRep & absMask
  890. sign := i128(-1 if aRep & signBit != 0 else 1)
  891. exponent := u64((aAbs >> significandBits) - exponentBias)
  892. significand := u64((aAbs & significandMask) | implicitBit)
  893. // If exponent is negative, the result is zero.
  894. if exponent < 0 {
  895. return 0
  896. }
  897. // If the value is too large for the integer type, saturate.
  898. if exponent >= size_of(i128) * 8 {
  899. return max(i128) if sign == 1 else min(i128)
  900. }
  901. // If 0 <= exponent < significandBits, right shift to get the result.
  902. // Otherwise, shift left.
  903. if exponent < significandBits {
  904. return sign * i128(significand >> (significandBits - exponent))
  905. } else {
  906. return sign * (i128(significand) << (exponent - significandBits))
  907. }
  908. }
  909. __write_bits :: proc "contextless" (dst, src: [^]byte, offset: uintptr, size: uintptr) {
  910. for i in 0..<size {
  911. j := offset+i
  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. }
  917. __read_bits :: proc "contextless" (dst, src: [^]byte, offset: uintptr, size: uintptr) {
  918. for j in 0..<size {
  919. i := offset+j
  920. the_bit := byte((src[i>>3]) & (1<<(i&7)) != 0)
  921. dst[j>>3] &~= 1<<(j&7)
  922. dst[j>>3] |= the_bit<<(j&7)
  923. }
  924. }