internal.odin 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979
  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_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. abs_f16 :: #force_inline proc "contextless" (x: f16) -> f16 {
  424. return -x if x < 0 else x
  425. }
  426. abs_f32 :: #force_inline proc "contextless" (x: f32) -> f32 {
  427. return -x if x < 0 else x
  428. }
  429. abs_f64 :: #force_inline proc "contextless" (x: f64) -> f64 {
  430. return -x if x < 0 else x
  431. }
  432. min_f16 :: #force_inline proc "contextless" (a, b: f16) -> f16 {
  433. return a if a < b else b
  434. }
  435. min_f32 :: #force_inline proc "contextless" (a, b: f32) -> f32 {
  436. return a if a < b else b
  437. }
  438. min_f64 :: #force_inline proc "contextless" (a, b: f64) -> f64 {
  439. return a if a < b else b
  440. }
  441. max_f16 :: #force_inline proc "contextless" (a, b: f16) -> f16 {
  442. return a if a > b else b
  443. }
  444. max_f32 :: #force_inline proc "contextless" (a, b: f32) -> f32 {
  445. return a if a > b else b
  446. }
  447. max_f64 :: #force_inline proc "contextless" (a, b: f64) -> f64 {
  448. return a if a > b else b
  449. }
  450. abs_complex32 :: #force_inline proc "contextless" (x: complex32) -> f16 {
  451. r, i := real(x), imag(x)
  452. return f16(intrinsics.sqrt(f32(r*r + i*i)))
  453. }
  454. abs_complex64 :: #force_inline proc "contextless" (x: complex64) -> f32 {
  455. r, i := real(x), imag(x)
  456. return intrinsics.sqrt(r*r + i*i)
  457. }
  458. abs_complex128 :: #force_inline proc "contextless" (x: complex128) -> f64 {
  459. r, i := real(x), imag(x)
  460. return intrinsics.sqrt(r*r + i*i)
  461. }
  462. abs_quaternion64 :: #force_inline proc "contextless" (x: quaternion64) -> f16 {
  463. r, i, j, k := real(x), imag(x), jmag(x), kmag(x)
  464. return f16(intrinsics.sqrt(f32(r*r + i*i + j*j + k*k)))
  465. }
  466. abs_quaternion128 :: #force_inline proc "contextless" (x: quaternion128) -> f32 {
  467. r, i, j, k := real(x), imag(x), jmag(x), kmag(x)
  468. return intrinsics.sqrt(r*r + i*i + j*j + k*k)
  469. }
  470. abs_quaternion256 :: #force_inline proc "contextless" (x: quaternion256) -> f64 {
  471. r, i, j, k := real(x), imag(x), jmag(x), kmag(x)
  472. return intrinsics.sqrt(r*r + i*i + j*j + k*k)
  473. }
  474. quo_complex32 :: proc "contextless" (n, m: complex32) -> complex32 {
  475. e, f: f16
  476. if abs(real(m)) >= abs(imag(m)) {
  477. ratio := imag(m) / real(m)
  478. denom := real(m) + ratio*imag(m)
  479. e = (real(n) + imag(n)*ratio) / denom
  480. f = (imag(n) - real(n)*ratio) / denom
  481. } else {
  482. ratio := real(m) / imag(m)
  483. denom := imag(m) + ratio*real(m)
  484. e = (real(n)*ratio + imag(n)) / denom
  485. f = (imag(n)*ratio - real(n)) / denom
  486. }
  487. return complex(e, f)
  488. }
  489. quo_complex64 :: proc "contextless" (n, m: complex64) -> complex64 {
  490. e, f: f32
  491. if abs(real(m)) >= abs(imag(m)) {
  492. ratio := imag(m) / real(m)
  493. denom := real(m) + ratio*imag(m)
  494. e = (real(n) + imag(n)*ratio) / denom
  495. f = (imag(n) - real(n)*ratio) / denom
  496. } else {
  497. ratio := real(m) / imag(m)
  498. denom := imag(m) + ratio*real(m)
  499. e = (real(n)*ratio + imag(n)) / denom
  500. f = (imag(n)*ratio - real(n)) / denom
  501. }
  502. return complex(e, f)
  503. }
  504. quo_complex128 :: proc "contextless" (n, m: complex128) -> complex128 {
  505. e, f: f64
  506. if abs(real(m)) >= abs(imag(m)) {
  507. ratio := imag(m) / real(m)
  508. denom := real(m) + ratio*imag(m)
  509. e = (real(n) + imag(n)*ratio) / denom
  510. f = (imag(n) - real(n)*ratio) / denom
  511. } else {
  512. ratio := real(m) / imag(m)
  513. denom := imag(m) + ratio*real(m)
  514. e = (real(n)*ratio + imag(n)) / denom
  515. f = (imag(n)*ratio - real(n)) / denom
  516. }
  517. return complex(e, f)
  518. }
  519. mul_quaternion64 :: proc "contextless" (q, r: quaternion64) -> quaternion64 {
  520. q0, q1, q2, q3 := real(q), imag(q), jmag(q), kmag(q)
  521. r0, r1, r2, r3 := real(r), imag(r), jmag(r), kmag(r)
  522. t0 := r0*q0 - r1*q1 - r2*q2 - r3*q3
  523. t1 := r0*q1 + r1*q0 - r2*q3 + r3*q2
  524. t2 := r0*q2 + r1*q3 + r2*q0 - r3*q1
  525. t3 := r0*q3 - r1*q2 + r2*q1 + r3*q0
  526. return quaternion(t0, t1, t2, t3)
  527. }
  528. mul_quaternion128 :: proc "contextless" (q, r: quaternion128) -> quaternion128 {
  529. q0, q1, q2, q3 := real(q), imag(q), jmag(q), kmag(q)
  530. r0, r1, r2, r3 := real(r), imag(r), jmag(r), kmag(r)
  531. t0 := r0*q0 - r1*q1 - r2*q2 - r3*q3
  532. t1 := r0*q1 + r1*q0 - r2*q3 + r3*q2
  533. t2 := r0*q2 + r1*q3 + r2*q0 - r3*q1
  534. t3 := r0*q3 - r1*q2 + r2*q1 + r3*q0
  535. return quaternion(t0, t1, t2, t3)
  536. }
  537. mul_quaternion256 :: proc "contextless" (q, r: quaternion256) -> quaternion256 {
  538. q0, q1, q2, q3 := real(q), imag(q), jmag(q), kmag(q)
  539. r0, r1, r2, r3 := real(r), imag(r), jmag(r), kmag(r)
  540. t0 := r0*q0 - r1*q1 - r2*q2 - r3*q3
  541. t1 := r0*q1 + r1*q0 - r2*q3 + r3*q2
  542. t2 := r0*q2 + r1*q3 + r2*q0 - r3*q1
  543. t3 := r0*q3 - r1*q2 + r2*q1 + r3*q0
  544. return quaternion(t0, t1, t2, t3)
  545. }
  546. quo_quaternion64 :: proc "contextless" (q, r: quaternion64) -> quaternion64 {
  547. q0, q1, q2, q3 := real(q), imag(q), jmag(q), kmag(q)
  548. r0, r1, r2, r3 := real(r), imag(r), jmag(r), kmag(r)
  549. invmag2 := 1.0 / (r0*r0 + r1*r1 + r2*r2 + r3*r3)
  550. t0 := (r0*q0 + r1*q1 + r2*q2 + r3*q3) * invmag2
  551. t1 := (r0*q1 - r1*q0 - r2*q3 - r3*q2) * invmag2
  552. t2 := (r0*q2 - r1*q3 - r2*q0 + r3*q1) * invmag2
  553. t3 := (r0*q3 + r1*q2 + r2*q1 - r3*q0) * invmag2
  554. return quaternion(t0, t1, t2, t3)
  555. }
  556. quo_quaternion128 :: proc "contextless" (q, r: quaternion128) -> quaternion128 {
  557. q0, q1, q2, q3 := real(q), imag(q), jmag(q), kmag(q)
  558. r0, r1, r2, r3 := real(r), imag(r), jmag(r), kmag(r)
  559. invmag2 := 1.0 / (r0*r0 + r1*r1 + r2*r2 + r3*r3)
  560. t0 := (r0*q0 + r1*q1 + r2*q2 + r3*q3) * invmag2
  561. t1 := (r0*q1 - r1*q0 - r2*q3 - r3*q2) * invmag2
  562. t2 := (r0*q2 - r1*q3 - r2*q0 + r3*q1) * invmag2
  563. t3 := (r0*q3 + r1*q2 + r2*q1 - r3*q0) * invmag2
  564. return quaternion(t0, t1, t2, t3)
  565. }
  566. quo_quaternion256 :: proc "contextless" (q, r: quaternion256) -> quaternion256 {
  567. q0, q1, q2, q3 := real(q), imag(q), jmag(q), kmag(q)
  568. r0, r1, r2, r3 := real(r), imag(r), jmag(r), kmag(r)
  569. invmag2 := 1.0 / (r0*r0 + r1*r1 + r2*r2 + r3*r3)
  570. t0 := (r0*q0 + r1*q1 + r2*q2 + r3*q3) * invmag2
  571. t1 := (r0*q1 - r1*q0 - r2*q3 - r3*q2) * invmag2
  572. t2 := (r0*q2 - r1*q3 - r2*q0 + r3*q1) * invmag2
  573. t3 := (r0*q3 + r1*q2 + r2*q1 - r3*q0) * invmag2
  574. return quaternion(t0, t1, t2, t3)
  575. }
  576. @(link_name="__truncsfhf2", linkage=RUNTIME_LINKAGE, require=RUNTIME_REQUIRE)
  577. truncsfhf2 :: proc "c" (value: f32) -> u16 {
  578. v: struct #raw_union { i: u32, f: f32 }
  579. i, s, e, m: i32
  580. v.f = value
  581. i = i32(v.i)
  582. s = (i >> 16) & 0x00008000
  583. e = ((i >> 23) & 0x000000ff) - (127 - 15)
  584. m = i & 0x007fffff
  585. if e <= 0 {
  586. if e < -10 {
  587. return u16(s)
  588. }
  589. m = (m | 0x00800000) >> u32(1 - e)
  590. if m & 0x00001000 != 0 {
  591. m += 0x00002000
  592. }
  593. return u16(s | (m >> 13))
  594. } else if e == 0xff - (127 - 15) {
  595. if m == 0 {
  596. return u16(s | 0x7c00) /* NOTE(bill): infinity */
  597. } else {
  598. /* NOTE(bill): NAN */
  599. m >>= 13
  600. return u16(s | 0x7c00 | m | i32(m == 0))
  601. }
  602. } else {
  603. if m & 0x00001000 != 0 {
  604. m += 0x00002000
  605. if (m & 0x00800000) != 0 {
  606. m = 0
  607. e += 1
  608. }
  609. }
  610. if e > 30 {
  611. f := i64(1e12)
  612. for j := 0; j < 10; j += 1 {
  613. /* NOTE(bill): Cause overflow */
  614. g := intrinsics.volatile_load(&f)
  615. g *= g
  616. intrinsics.volatile_store(&f, g)
  617. }
  618. return u16(s | 0x7c00)
  619. }
  620. return u16(s | (e << 10) | (m >> 13))
  621. }
  622. }
  623. @(link_name="__truncdfhf2", linkage=RUNTIME_LINKAGE, require=RUNTIME_REQUIRE)
  624. truncdfhf2 :: proc "c" (value: f64) -> u16 {
  625. return truncsfhf2(f32(value))
  626. }
  627. @(link_name="__gnu_h2f_ieee", linkage=RUNTIME_LINKAGE, require=RUNTIME_REQUIRE)
  628. gnu_h2f_ieee :: proc "c" (value: u16) -> f32 {
  629. fp32 :: struct #raw_union { u: u32, f: f32 }
  630. v: fp32
  631. magic, inf_or_nan: fp32
  632. magic.u = u32((254 - 15) << 23)
  633. inf_or_nan.u = u32((127 + 16) << 23)
  634. v.u = u32(value & 0x7fff) << 13
  635. v.f *= magic.f
  636. if v.f >= inf_or_nan.f {
  637. v.u |= 255 << 23
  638. }
  639. v.u |= u32(value & 0x8000) << 16
  640. return v.f
  641. }
  642. @(link_name="__gnu_f2h_ieee", linkage=RUNTIME_LINKAGE, require=RUNTIME_REQUIRE)
  643. gnu_f2h_ieee :: proc "c" (value: f32) -> u16 {
  644. return truncsfhf2(value)
  645. }
  646. @(link_name="__extendhfsf2", linkage=RUNTIME_LINKAGE, require=RUNTIME_REQUIRE)
  647. extendhfsf2 :: proc "c" (value: u16) -> f32 {
  648. return gnu_h2f_ieee(value)
  649. }
  650. @(link_name="__floattidf", linkage=RUNTIME_LINKAGE, require=RUNTIME_REQUIRE)
  651. floattidf :: proc "c" (a: i128) -> f64 {
  652. when IS_WASM {
  653. return 0
  654. } else {
  655. DBL_MANT_DIG :: 53
  656. if a == 0 {
  657. return 0.0
  658. }
  659. a := a
  660. N :: size_of(i128) * 8
  661. s := a >> (N-1)
  662. a = (a ~ s) - s
  663. sd: = N - intrinsics.count_leading_zeros(a) // number of significant digits
  664. e := i32(sd - 1) // exponent
  665. if sd > DBL_MANT_DIG {
  666. switch sd {
  667. case DBL_MANT_DIG + 1:
  668. a <<= 1
  669. case DBL_MANT_DIG + 2:
  670. // okay
  671. case:
  672. a = i128(u128(a) >> u128(sd - (DBL_MANT_DIG+2))) |
  673. i128(u128(a) & (~u128(0) >> u128(N + DBL_MANT_DIG+2 - sd)) != 0)
  674. }
  675. a |= i128((a & 4) != 0)
  676. a += 1
  677. a >>= 2
  678. if a & (i128(1) << DBL_MANT_DIG) != 0 {
  679. a >>= 1
  680. e += 1
  681. }
  682. } else {
  683. a <<= u128(DBL_MANT_DIG - sd) & 127
  684. }
  685. fb: [2]u32
  686. fb[1] = (u32(s) & 0x80000000) | // sign
  687. (u32(e + 1023) << 20) | // exponent
  688. u32((u64(a) >> 32) & 0x000FFFFF) // mantissa-high
  689. fb[0] = u32(a) // mantissa-low
  690. return transmute(f64)fb
  691. }
  692. }
  693. @(link_name="__floattidf_unsigned", linkage=RUNTIME_LINKAGE, require=RUNTIME_REQUIRE)
  694. floattidf_unsigned :: proc "c" (a: u128) -> f64 {
  695. when IS_WASM {
  696. return 0
  697. } else {
  698. DBL_MANT_DIG :: 53
  699. if a == 0 {
  700. return 0.0
  701. }
  702. a := a
  703. N :: size_of(u128) * 8
  704. sd: = N - intrinsics.count_leading_zeros(a) // number of significant digits
  705. e := i32(sd - 1) // exponent
  706. if sd > DBL_MANT_DIG {
  707. switch sd {
  708. case DBL_MANT_DIG + 1:
  709. a <<= 1
  710. case DBL_MANT_DIG + 2:
  711. // okay
  712. case:
  713. a = u128(u128(a) >> u128(sd - (DBL_MANT_DIG+2))) |
  714. u128(u128(a) & (~u128(0) >> u128(N + DBL_MANT_DIG+2 - sd)) != 0)
  715. }
  716. a |= u128((a & 4) != 0)
  717. a += 1
  718. a >>= 2
  719. if a & (1 << DBL_MANT_DIG) != 0 {
  720. a >>= 1
  721. e += 1
  722. }
  723. } else {
  724. a <<= u128(DBL_MANT_DIG - sd)
  725. }
  726. fb: [2]u32
  727. fb[1] = (0) | // sign
  728. u32((e + 1023) << 20) | // exponent
  729. u32((u64(a) >> 32) & 0x000FFFFF) // mantissa-high
  730. fb[0] = u32(a) // mantissa-low
  731. return transmute(f64)fb
  732. }
  733. }
  734. @(link_name="__fixunsdfti", linkage=RUNTIME_LINKAGE, require=RUNTIME_REQUIRE)
  735. fixunsdfti :: #force_no_inline proc "c" (a: f64) -> u128 {
  736. // TODO(bill): implement `fixunsdfti` correctly
  737. x := u64(a)
  738. return u128(x)
  739. }
  740. @(link_name="__fixunsdfdi", linkage=RUNTIME_LINKAGE, require=RUNTIME_REQUIRE)
  741. fixunsdfdi :: #force_no_inline proc "c" (a: f64) -> i128 {
  742. // TODO(bill): implement `fixunsdfdi` correctly
  743. x := i64(a)
  744. return i128(x)
  745. }
  746. @(link_name="__umodti3", linkage=RUNTIME_LINKAGE, require=RUNTIME_REQUIRE)
  747. umodti3 :: proc "c" (a, b: u128) -> u128 {
  748. r: u128 = ---
  749. _ = udivmod128(a, b, &r)
  750. return r
  751. }
  752. @(link_name="__udivmodti4", linkage=RUNTIME_LINKAGE, require=RUNTIME_REQUIRE)
  753. udivmodti4 :: proc "c" (a, b: u128, rem: ^u128) -> u128 {
  754. return udivmod128(a, b, rem)
  755. }
  756. @(link_name="__udivti3", linkage=RUNTIME_LINKAGE, require=RUNTIME_REQUIRE)
  757. udivti3 :: proc "c" (a, b: u128) -> u128 {
  758. return udivmodti4(a, b, nil)
  759. }
  760. @(link_name="__modti3", linkage=RUNTIME_LINKAGE, require=RUNTIME_REQUIRE)
  761. modti3 :: proc "c" (a, b: i128) -> i128 {
  762. s_a := a >> (128 - 1)
  763. s_b := b >> (128 - 1)
  764. an := (a ~ s_a) - s_a
  765. bn := (b ~ s_b) - s_b
  766. r: u128 = ---
  767. _ = udivmod128(transmute(u128)an, transmute(u128)bn, &r)
  768. return (transmute(i128)r ~ s_a) - s_a
  769. }
  770. @(link_name="__divmodti4", linkage=RUNTIME_LINKAGE, require=RUNTIME_REQUIRE)
  771. divmodti4 :: proc "c" (a, b: i128, rem: ^i128) -> i128 {
  772. u := udivmod128(transmute(u128)a, transmute(u128)b, cast(^u128)rem)
  773. return transmute(i128)u
  774. }
  775. @(link_name="__divti3", linkage=RUNTIME_LINKAGE, require=RUNTIME_REQUIRE)
  776. divti3 :: proc "c" (a, b: i128) -> i128 {
  777. u := udivmodti4(transmute(u128)a, transmute(u128)b, nil)
  778. return transmute(i128)u
  779. }
  780. @(link_name="__fixdfti", linkage=RUNTIME_LINKAGE, require=RUNTIME_REQUIRE)
  781. fixdfti :: proc(a: u64) -> i128 {
  782. significandBits :: 52
  783. typeWidth :: (size_of(u64)*8)
  784. exponentBits :: (typeWidth - significandBits - 1)
  785. maxExponent :: ((1 << exponentBits) - 1)
  786. exponentBias :: (maxExponent >> 1)
  787. implicitBit :: (u64(1) << significandBits)
  788. significandMask :: (implicitBit - 1)
  789. signBit :: (u64(1) << (significandBits + exponentBits))
  790. absMask :: (signBit - 1)
  791. exponentMask :: (absMask ~ significandMask)
  792. // Break a into sign, exponent, significand
  793. aRep := a
  794. aAbs := aRep & absMask
  795. sign := i128(-1 if aRep & signBit != 0 else 1)
  796. exponent := u64((aAbs >> significandBits) - exponentBias)
  797. significand := u64((aAbs & significandMask) | implicitBit)
  798. // If exponent is negative, the result is zero.
  799. if exponent < 0 {
  800. return 0
  801. }
  802. // If the value is too large for the integer type, saturate.
  803. if exponent >= size_of(i128) * 8 {
  804. return max(i128) if sign == 1 else min(i128)
  805. }
  806. // If 0 <= exponent < significandBits, right shift to get the result.
  807. // Otherwise, shift left.
  808. if exponent < significandBits {
  809. return sign * i128(significand >> (significandBits - exponent))
  810. } else {
  811. return sign * (i128(significand) << (exponent - significandBits))
  812. }
  813. }