internal.odin 29 KB

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