internal.odin 26 KB

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