internal.odin 28 KB

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