internal.odin 26 KB

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