internal.odin 27 KB

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