internal.odin 26 KB

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