internal.odin 27 KB

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