internal.odin 21 KB

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