internal.odin 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743
  1. package runtime
  2. import "core:os"
  3. mem_copy :: proc "contextless" (dst, src: rawptr, len: int) -> rawptr {
  4. if src == nil do return dst;
  5. // NOTE(bill): This _must_ be implemented like C's memmove
  6. foreign _ {
  7. when size_of(rawptr) == 8 {
  8. @(link_name="llvm.memmove.p0i8.p0i8.i64")
  9. llvm_memmove :: proc(dst, src: rawptr, len: int, align: i32, is_volatile: bool) ---;
  10. } else {
  11. @(link_name="llvm.memmove.p0i8.p0i8.i32")
  12. llvm_memmove :: proc(dst, src: rawptr, len: int, align: i32, is_volatile: bool) ---;
  13. }
  14. }
  15. llvm_memmove(dst, src, len, 1, false);
  16. return dst;
  17. }
  18. print_u64 :: proc(fd: os.Handle, x: u64) {
  19. digits := "0123456789";
  20. a: [129]byte;
  21. i := len(a);
  22. b := u64(10);
  23. u := x;
  24. for u >= b {
  25. i -= 1; a[i] = digits[u % b];
  26. u /= b;
  27. }
  28. i -= 1; a[i] = digits[u % b];
  29. os.write(fd, a[i:]);
  30. }
  31. print_i64 :: proc(fd: os.Handle, x: i64) {
  32. digits := "0123456789";
  33. b :: i64(10);
  34. u := x;
  35. neg := u < 0;
  36. u = abs(u);
  37. a: [129]byte;
  38. i := len(a);
  39. for u >= b {
  40. i -= 1; a[i] = digits[u % b];
  41. u /= b;
  42. }
  43. i -= 1; a[i] = digits[u % b];
  44. if neg {
  45. i -= 1; a[i] = '-';
  46. }
  47. os.write(fd, a[i:]);
  48. }
  49. print_caller_location :: proc(fd: os.Handle, using loc: Source_Code_Location) {
  50. os.write_string(fd, file_path);
  51. os.write_byte(fd, '(');
  52. print_u64(fd, u64(line));
  53. os.write_byte(fd, ':');
  54. print_u64(fd, u64(column));
  55. os.write_byte(fd, ')');
  56. }
  57. print_typeid :: proc(fd: os.Handle, id: typeid) {
  58. ti := type_info_of(id);
  59. print_type(fd, ti);
  60. }
  61. print_type :: proc(fd: os.Handle, ti: ^Type_Info) {
  62. if ti == nil {
  63. os.write_string(fd, "nil");
  64. return;
  65. }
  66. switch info in ti.variant {
  67. case Type_Info_Named:
  68. os.write_string(fd, info.name);
  69. case Type_Info_Integer:
  70. switch ti.id {
  71. case int: os.write_string(fd, "int");
  72. case uint: os.write_string(fd, "uint");
  73. case uintptr: os.write_string(fd, "uintptr");
  74. case:
  75. os.write_byte(fd, info.signed ? 'i' : 'u');
  76. print_u64(fd, u64(8*ti.size));
  77. }
  78. case Type_Info_Rune:
  79. os.write_string(fd, "rune");
  80. case Type_Info_Float:
  81. os.write_byte(fd, 'f');
  82. print_u64(fd, u64(8*ti.size));
  83. case Type_Info_Complex:
  84. os.write_string(fd, "complex");
  85. print_u64(fd, u64(8*ti.size));
  86. case Type_Info_String:
  87. os.write_string(fd, "string");
  88. case Type_Info_Boolean:
  89. switch ti.id {
  90. case bool: os.write_string(fd, "bool");
  91. case:
  92. os.write_byte(fd, 'b');
  93. print_u64(fd, u64(8*ti.size));
  94. }
  95. case Type_Info_Any:
  96. os.write_string(fd, "any");
  97. case Type_Info_Type_Id:
  98. os.write_string(fd, "typeid");
  99. case Type_Info_Pointer:
  100. if info.elem == nil {
  101. os.write_string(fd, "rawptr");
  102. } else {
  103. os.write_string(fd, "^");
  104. print_type(fd, info.elem);
  105. }
  106. case Type_Info_Procedure:
  107. os.write_string(fd, "proc");
  108. if info.params == nil {
  109. os.write_string(fd, "()");
  110. } else {
  111. t := info.params.variant.(Type_Info_Tuple);
  112. os.write_string(fd, "(");
  113. for t, i in t.types {
  114. if i > 0 do os.write_string(fd, ", ");
  115. print_type(fd, t);
  116. }
  117. os.write_string(fd, ")");
  118. }
  119. if info.results != nil {
  120. os.write_string(fd, " -> ");
  121. print_type(fd, info.results);
  122. }
  123. case Type_Info_Tuple:
  124. count := len(info.names);
  125. if count != 1 do os.write_string(fd, "(");
  126. for name, i in info.names {
  127. if i > 0 do os.write_string(fd, ", ");
  128. t := info.types[i];
  129. if len(name) > 0 {
  130. os.write_string(fd, name);
  131. os.write_string(fd, ": ");
  132. }
  133. print_type(fd, t);
  134. }
  135. if count != 1 do os.write_string(fd, ")");
  136. case Type_Info_Array:
  137. os.write_string(fd, "[");
  138. print_u64(fd, u64(info.count));
  139. os.write_string(fd, "]");
  140. print_type(fd, info.elem);
  141. case Type_Info_Dynamic_Array:
  142. os.write_string(fd, "[dynamic]");
  143. print_type(fd, info.elem);
  144. case Type_Info_Slice:
  145. os.write_string(fd, "[]");
  146. print_type(fd, info.elem);
  147. case Type_Info_Map:
  148. os.write_string(fd, "map[");
  149. print_type(fd, info.key);
  150. os.write_byte(fd, ']');
  151. print_type(fd, info.value);
  152. case Type_Info_Struct:
  153. os.write_string(fd, "struct ");
  154. if info.is_packed do os.write_string(fd, "#packed ");
  155. if info.is_raw_union do os.write_string(fd, "#raw_union ");
  156. if info.custom_align {
  157. os.write_string(fd, "#align ");
  158. print_u64(fd, u64(ti.align));
  159. os.write_byte(fd, ' ');
  160. }
  161. os.write_byte(fd, '{');
  162. for name, i in info.names {
  163. if i > 0 do os.write_string(fd, ", ");
  164. os.write_string(fd, name);
  165. os.write_string(fd, ": ");
  166. print_type(fd, info.types[i]);
  167. }
  168. os.write_byte(fd, '}');
  169. case Type_Info_Union:
  170. os.write_string(fd, "union {");
  171. for variant, i in info.variants {
  172. if i > 0 do os.write_string(fd, ", ");
  173. print_type(fd, variant);
  174. }
  175. os.write_string(fd, "}");
  176. case Type_Info_Enum:
  177. os.write_string(fd, "enum ");
  178. print_type(fd, info.base);
  179. os.write_string(fd, " {");
  180. for name, i in info.names {
  181. if i > 0 do os.write_string(fd, ", ");
  182. os.write_string(fd, name);
  183. }
  184. os.write_string(fd, "}");
  185. case Type_Info_Bit_Field:
  186. os.write_string(fd, "bit_field ");
  187. if ti.align != 1 {
  188. os.write_string(fd, "#align ");
  189. print_u64(fd, u64(ti.align));
  190. os.write_byte(fd, ' ');
  191. }
  192. os.write_string(fd, " {");
  193. for name, i in info.names {
  194. if i > 0 do os.write_string(fd, ", ");
  195. os.write_string(fd, name);
  196. os.write_string(fd, ": ");
  197. print_u64(fd, u64(info.bits[i]));
  198. }
  199. os.write_string(fd, "}");
  200. case Type_Info_Bit_Set:
  201. os.write_string(fd, "bit_set[");
  202. switch elem in type_info_base(info.elem).variant {
  203. case Type_Info_Enum:
  204. print_type(fd, info.elem);
  205. case Type_Info_Rune:
  206. os.write_encoded_rune(fd, rune(info.lower));
  207. os.write_string(fd, "..");
  208. os.write_encoded_rune(fd, rune(info.upper));
  209. case:
  210. print_i64(fd, info.lower);
  211. os.write_string(fd, "..");
  212. print_i64(fd, info.upper);
  213. }
  214. if info.underlying != nil {
  215. os.write_string(fd, "; ");
  216. print_type(fd, info.underlying);
  217. }
  218. os.write_byte(fd, ']');
  219. case Type_Info_Opaque:
  220. os.write_string(fd, "opaque ");
  221. print_type(fd, info.elem);
  222. case Type_Info_Simd_Vector:
  223. if info.is_x86_mmx {
  224. os.write_string(fd, "intrinsics.x86_mmx");
  225. } else {
  226. os.write_string(fd, "intrinsics.vector(");
  227. print_u64(fd, u64(info.count));
  228. os.write_string(fd, ", ");
  229. print_type(fd, info.elem);
  230. os.write_byte(fd, ')');
  231. }
  232. }
  233. }
  234. memory_compare :: proc "contextless" (a, b: rawptr, n: int) -> int #no_bounds_check {
  235. x := uintptr(a);
  236. y := uintptr(b);
  237. n := uintptr(n);
  238. SU :: size_of(uintptr);
  239. fast := uintptr(n/SU + 1);
  240. offset := (fast-1)*SU;
  241. curr_block := uintptr(0);
  242. if n < SU {
  243. fast = 0;
  244. }
  245. for /**/; curr_block < fast; curr_block += 1 {
  246. va := (^uintptr)(x + curr_block * size_of(uintptr))^;
  247. vb := (^uintptr)(y + curr_block * size_of(uintptr))^;
  248. if va ~ vb != 0 {
  249. for pos := curr_block*SU; pos < n; pos += 1 {
  250. a := (^byte)(x+pos)^;
  251. b := (^byte)(y+pos)^;
  252. if a ~ b != 0 {
  253. return (int(a) - int(b)) < 0 ? -1 : +1;
  254. }
  255. }
  256. }
  257. }
  258. for /**/; offset < n; offset += 1 {
  259. a := (^byte)(x+offset)^;
  260. b := (^byte)(y+offset)^;
  261. if a ~ b != 0 {
  262. return (int(a) - int(b)) < 0 ? -1 : +1;
  263. }
  264. }
  265. return 0;
  266. }
  267. memory_compare_zero :: proc "contextless" (a: rawptr, n: int) -> int #no_bounds_check {
  268. x := uintptr(a);
  269. n := uintptr(n);
  270. SU :: size_of(uintptr);
  271. fast := uintptr(n/SU + 1);
  272. offset := (fast-1)*SU;
  273. curr_block := uintptr(0);
  274. if n < SU {
  275. fast = 0;
  276. }
  277. for /**/; curr_block < fast; curr_block += 1 {
  278. va := (^uintptr)(x + curr_block * size_of(uintptr))^;
  279. if va ~ 0 != 0 {
  280. for pos := curr_block*SU; pos < n; pos += 1 {
  281. a := (^byte)(x+pos)^;
  282. if a ~ 0 != 0 {
  283. return int(a) < 0 ? -1 : +1;
  284. }
  285. }
  286. }
  287. }
  288. for /**/; offset < n; offset += 1 {
  289. a := (^byte)(x+offset)^;
  290. if a ~ 0 != 0 {
  291. return int(a) < 0 ? -1 : +1;
  292. }
  293. }
  294. return 0;
  295. }
  296. string_eq :: proc "contextless" (a, b: string) -> bool {
  297. switch {
  298. case len(a) != len(b): return false;
  299. case len(a) == 0: return true;
  300. case &a[0] == &b[0]: return true;
  301. }
  302. return string_cmp(a, b) == 0;
  303. }
  304. string_cmp :: proc "contextless" (a, b: string) -> int {
  305. return memory_compare(&a[0], &b[0], min(len(a), len(b)));
  306. }
  307. string_ne :: inline proc "contextless" (a, b: string) -> bool { return !string_eq(a, b); }
  308. string_lt :: inline proc "contextless" (a, b: string) -> bool { return string_cmp(a, b) < 0; }
  309. string_gt :: inline proc "contextless" (a, b: string) -> bool { return string_cmp(a, b) > 0; }
  310. string_le :: inline proc "contextless" (a, b: string) -> bool { return string_cmp(a, b) <= 0; }
  311. string_ge :: inline proc "contextless" (a, b: string) -> bool { return string_cmp(a, b) >= 0; }
  312. cstring_len :: proc "contextless" (s: cstring) -> int {
  313. p0 := uintptr((^byte)(s));
  314. p := p0;
  315. for p != 0 && (^byte)(p)^ != 0 {
  316. p += 1;
  317. }
  318. return int(p - p0);
  319. }
  320. cstring_to_string :: proc "contextless" (s: cstring) -> string {
  321. Raw_String :: struct {
  322. data: ^byte,
  323. len: int,
  324. };
  325. if s == nil do return "";
  326. ptr := (^byte)(s);
  327. n := cstring_len(s);
  328. return transmute(string)Raw_String{ptr, n};
  329. }
  330. complex64_eq :: inline proc "contextless" (a, b: complex64) -> bool { return real(a) == real(b) && imag(a) == imag(b); }
  331. complex64_ne :: inline proc "contextless" (a, b: complex64) -> bool { return real(a) != real(b) || imag(a) != imag(b); }
  332. complex128_eq :: inline proc "contextless" (a, b: complex128) -> bool { return real(a) == real(b) && imag(a) == imag(b); }
  333. complex128_ne :: inline proc "contextless" (a, b: complex128) -> bool { return real(a) != real(b) || imag(a) != imag(b); }
  334. quaternion128_eq :: inline proc "contextless" (a, b: quaternion128) -> bool { return real(a) == real(b) && imag(a) == imag(b) && jmag(a) == jmag(b) && kmag(a) == kmag(b); }
  335. quaternion128_ne :: inline proc "contextless" (a, b: quaternion128) -> bool { return real(a) != real(b) || imag(a) != imag(b) || jmag(a) != jmag(b) || kmag(a) != kmag(b); }
  336. quaternion256_eq :: inline proc "contextless" (a, b: quaternion256) -> bool { return real(a) == real(b) && imag(a) == imag(b) && jmag(a) == jmag(b) && kmag(a) == kmag(b); }
  337. quaternion256_ne :: inline proc "contextless" (a, b: quaternion256) -> bool { return real(a) != real(b) || imag(a) != imag(b) || jmag(a) != jmag(b) || kmag(a) != kmag(b); }
  338. bounds_check_error :: proc "contextless" (file: string, line, column: int, index, count: int) {
  339. if 0 <= index && index < count do return;
  340. handle_error :: proc "contextless" (file: string, line, column: int, index, count: int) {
  341. fd := os.stderr;
  342. print_caller_location(fd, Source_Code_Location{file, line, column, "", 0});
  343. os.write_string(fd, " Index ");
  344. print_i64(fd, i64(index));
  345. os.write_string(fd, " is out of bounds range 0:");
  346. print_i64(fd, i64(count));
  347. os.write_byte(fd, '\n');
  348. debug_trap();
  349. }
  350. handle_error(file, line, column, index, count);
  351. }
  352. slice_handle_error :: proc "contextless" (file: string, line, column: int, lo, hi: int, len: int) {
  353. fd := os.stderr;
  354. print_caller_location(fd, Source_Code_Location{file, line, column, "", 0});
  355. os.write_string(fd, " Invalid slice indices: ");
  356. print_i64(fd, i64(lo));
  357. os.write_string(fd, ":");
  358. print_i64(fd, i64(hi));
  359. os.write_string(fd, ":");
  360. print_i64(fd, i64(len));
  361. os.write_byte(fd, '\n');
  362. debug_trap();
  363. }
  364. slice_expr_error_hi :: proc "contextless" (file: string, line, column: int, hi: int, len: int) {
  365. if 0 <= hi && hi <= len do return;
  366. slice_handle_error(file, line, column, 0, hi, len);
  367. }
  368. slice_expr_error_lo_hi :: proc "contextless" (file: string, line, column: int, lo, hi: int, len: int) {
  369. if 0 <= lo && lo <= len && lo <= hi && hi <= len do return;
  370. slice_handle_error(file, line, column, lo, hi, len);
  371. }
  372. dynamic_array_expr_error :: proc "contextless" (file: string, line, column: int, low, high, max: int) {
  373. if 0 <= low && low <= high && high <= max do return;
  374. handle_error :: proc "contextless" (file: string, line, column: int, low, high, max: int) {
  375. fd := os.stderr;
  376. print_caller_location(fd, Source_Code_Location{file, line, column, "", 0});
  377. os.write_string(fd, " Invalid dynamic array values: ");
  378. print_i64(fd, i64(low));
  379. os.write_string(fd, ":");
  380. print_i64(fd, i64(high));
  381. os.write_string(fd, ":");
  382. print_i64(fd, i64(max));
  383. os.write_byte(fd, '\n');
  384. debug_trap();
  385. }
  386. handle_error(file, line, column, low, high, max);
  387. }
  388. type_assertion_check :: proc "contextless" (ok: bool, file: string, line, column: int, from, to: typeid) {
  389. if ok do return;
  390. handle_error :: proc "contextless" (file: string, line, column: int, from, to: typeid) {
  391. fd := os.stderr;
  392. print_caller_location(fd, Source_Code_Location{file, line, column, "", 0});
  393. os.write_string(fd, " Invalid type assertion from ");
  394. print_typeid(fd, from);
  395. os.write_string(fd, " to ");
  396. print_typeid(fd, to);
  397. os.write_byte(fd, '\n');
  398. debug_trap();
  399. }
  400. handle_error(file, line, column, from, to);
  401. }
  402. string_decode_rune :: inline proc "contextless" (s: string) -> (rune, int) {
  403. // NOTE(bill): Duplicated here to remove dependency on package unicode/utf8
  404. @static accept_sizes := [256]u8{
  405. 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, // 0x00-0x0f
  406. 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, // 0x10-0x1f
  407. 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, // 0x20-0x2f
  408. 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, // 0x30-0x3f
  409. 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, // 0x40-0x4f
  410. 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, // 0x50-0x5f
  411. 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, // 0x60-0x6f
  412. 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, // 0x70-0x7f
  413. 0xf1, 0xf1, 0xf1, 0xf1, 0xf1, 0xf1, 0xf1, 0xf1, 0xf1, 0xf1, 0xf1, 0xf1, 0xf1, 0xf1, 0xf1, 0xf1, // 0x80-0x8f
  414. 0xf1, 0xf1, 0xf1, 0xf1, 0xf1, 0xf1, 0xf1, 0xf1, 0xf1, 0xf1, 0xf1, 0xf1, 0xf1, 0xf1, 0xf1, 0xf1, // 0x90-0x9f
  415. 0xf1, 0xf1, 0xf1, 0xf1, 0xf1, 0xf1, 0xf1, 0xf1, 0xf1, 0xf1, 0xf1, 0xf1, 0xf1, 0xf1, 0xf1, 0xf1, // 0xa0-0xaf
  416. 0xf1, 0xf1, 0xf1, 0xf1, 0xf1, 0xf1, 0xf1, 0xf1, 0xf1, 0xf1, 0xf1, 0xf1, 0xf1, 0xf1, 0xf1, 0xf1, // 0xb0-0xbf
  417. 0xf1, 0xf1, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, // 0xc0-0xcf
  418. 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, // 0xd0-0xdf
  419. 0x13, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x23, 0x03, 0x03, // 0xe0-0xef
  420. 0x34, 0x04, 0x04, 0x04, 0x44, 0xf1, 0xf1, 0xf1, 0xf1, 0xf1, 0xf1, 0xf1, 0xf1, 0xf1, 0xf1, 0xf1, // 0xf0-0xff
  421. };
  422. Accept_Range :: struct {lo, hi: u8};
  423. @static accept_ranges := [5]Accept_Range{
  424. {0x80, 0xbf},
  425. {0xa0, 0xbf},
  426. {0x80, 0x9f},
  427. {0x90, 0xbf},
  428. {0x80, 0x8f},
  429. };
  430. MASKX :: 0b0011_1111;
  431. MASK2 :: 0b0001_1111;
  432. MASK3 :: 0b0000_1111;
  433. MASK4 :: 0b0000_0111;
  434. LOCB :: 0b1000_0000;
  435. HICB :: 0b1011_1111;
  436. RUNE_ERROR :: '\ufffd';
  437. n := len(s);
  438. if n < 1 {
  439. return RUNE_ERROR, 0;
  440. }
  441. s0 := s[0];
  442. x := accept_sizes[s0];
  443. if x >= 0xF0 {
  444. mask := rune(x) << 31 >> 31; // NOTE(bill): Create 0x0000 or 0xffff.
  445. return rune(s[0])&~mask | RUNE_ERROR&mask, 1;
  446. }
  447. sz := x & 7;
  448. accept := accept_ranges[x>>4];
  449. if n < int(sz) {
  450. return RUNE_ERROR, 1;
  451. }
  452. b1 := s[1];
  453. if b1 < accept.lo || accept.hi < b1 {
  454. return RUNE_ERROR, 1;
  455. }
  456. if sz == 2 {
  457. return rune(s0&MASK2)<<6 | rune(b1&MASKX), 2;
  458. }
  459. b2 := s[2];
  460. if b2 < LOCB || HICB < b2 {
  461. return RUNE_ERROR, 1;
  462. }
  463. if sz == 3 {
  464. return rune(s0&MASK3)<<12 | rune(b1&MASKX)<<6 | rune(b2&MASKX), 3;
  465. }
  466. b3 := s[3];
  467. if b3 < LOCB || HICB < b3 {
  468. return RUNE_ERROR, 1;
  469. }
  470. return rune(s0&MASK4)<<18 | rune(b1&MASKX)<<12 | rune(b2&MASKX)<<6 | rune(b3&MASKX), 4;
  471. }
  472. bounds_check_error_loc :: inline proc "contextless" (using loc := #caller_location, index, count: int) {
  473. bounds_check_error(file_path, int(line), int(column), index, count);
  474. }
  475. slice_expr_error_hi_loc :: inline proc "contextless" (using loc := #caller_location, hi: int, len: int) {
  476. slice_expr_error_hi(file_path, int(line), int(column), hi, len);
  477. }
  478. slice_expr_error_lo_hi_loc :: inline proc "contextless" (using loc := #caller_location, lo, hi: int, len: int) {
  479. slice_expr_error_lo_hi(file_path, int(line), int(column), lo, hi, len);
  480. }
  481. dynamic_array_expr_error_loc :: inline proc "contextless" (using loc := #caller_location, low, high, max: int) {
  482. dynamic_array_expr_error(file_path, int(line), int(column), low, high, max);
  483. }
  484. make_slice_error_loc :: inline proc "contextless" (loc := #caller_location, len: int) {
  485. if 0 <= len do return;
  486. handle_error :: proc "contextless" (loc: Source_Code_Location, len: int) {
  487. fd := os.stderr;
  488. print_caller_location(fd, loc);
  489. os.write_string(fd, " Invalid slice length for make: ");
  490. print_i64(fd, i64(len));
  491. os.write_byte(fd, '\n');
  492. debug_trap();
  493. }
  494. handle_error(loc, len);
  495. }
  496. make_dynamic_array_error_loc :: inline proc "contextless" (using loc := #caller_location, len, cap: int) {
  497. if 0 <= len && len <= cap do return;
  498. handle_error :: proc "contextless" (loc: Source_Code_Location, len, cap: int) {
  499. fd := os.stderr;
  500. print_caller_location(fd, loc);
  501. os.write_string(fd, " Invalid dynamic array parameters for make: ");
  502. print_i64(fd, i64(len));
  503. os.write_byte(fd, ':');
  504. print_i64(fd, i64(cap));
  505. os.write_byte(fd, '\n');
  506. debug_trap();
  507. }
  508. handle_error(loc, len, cap);
  509. }
  510. make_map_expr_error_loc :: inline proc "contextless" (loc := #caller_location, cap: int) {
  511. if 0 <= cap do return;
  512. handle_error :: proc "contextless" (loc: Source_Code_Location, cap: int) {
  513. fd := os.stderr;
  514. print_caller_location(fd, loc);
  515. os.write_string(fd, " Invalid map capacity for make: ");
  516. print_i64(fd, i64(cap));
  517. os.write_byte(fd, '\n');
  518. debug_trap();
  519. }
  520. handle_error(loc, cap);
  521. }
  522. @(default_calling_convention = "c")
  523. foreign {
  524. @(link_name="llvm.sqrt.f32") _sqrt_f32 :: proc(x: f32) -> f32 ---
  525. @(link_name="llvm.sqrt.f64") _sqrt_f64 :: proc(x: f64) -> f64 ---
  526. }
  527. abs_f32 :: inline proc "contextless" (x: f32) -> f32 {
  528. foreign {
  529. @(link_name="llvm.fabs.f32") _abs :: proc "c" (x: f32) -> f32 ---
  530. }
  531. return _abs(x);
  532. }
  533. abs_f64 :: inline proc "contextless" (x: f64) -> f64 {
  534. foreign {
  535. @(link_name="llvm.fabs.f64") _abs :: proc "c" (x: f64) -> f64 ---
  536. }
  537. return _abs(x);
  538. }
  539. min_f32 :: proc(a, b: f32) -> f32 {
  540. foreign {
  541. @(link_name="llvm.minnum.f32") _min :: proc "c" (a, b: f32) -> f32 ---
  542. }
  543. return _min(a, b);
  544. }
  545. min_f64 :: proc(a, b: f64) -> f64 {
  546. foreign {
  547. @(link_name="llvm.minnum.f64") _min :: proc "c" (a, b: f64) -> f64 ---
  548. }
  549. return _min(a, b);
  550. }
  551. max_f32 :: proc(a, b: f32) -> f32 {
  552. foreign {
  553. @(link_name="llvm.maxnum.f32") _max :: proc "c" (a, b: f32) -> f32 ---
  554. }
  555. return _max(a, b);
  556. }
  557. max_f64 :: proc(a, b: f64) -> f64 {
  558. foreign {
  559. @(link_name="llvm.maxnum.f64") _max :: proc "c" (a, b: f64) -> f64 ---
  560. }
  561. return _max(a, b);
  562. }
  563. abs_complex64 :: inline proc "contextless" (x: complex64) -> f32 {
  564. r, i := real(x), imag(x);
  565. return _sqrt_f32(r*r + i*i);
  566. }
  567. abs_complex128 :: inline proc "contextless" (x: complex128) -> f64 {
  568. r, i := real(x), imag(x);
  569. return _sqrt_f64(r*r + i*i);
  570. }
  571. abs_quaternion128 :: inline proc "contextless" (x: quaternion128) -> f32 {
  572. r, i, j, k := real(x), imag(x), jmag(x), kmag(x);
  573. return _sqrt_f32(r*r + i*i + j*j + k*k);
  574. }
  575. abs_quaternion256 :: inline proc "contextless" (x: quaternion256) -> f64 {
  576. r, i, j, k := real(x), imag(x), jmag(x), kmag(x);
  577. return _sqrt_f64(r*r + i*i + j*j + k*k);
  578. }
  579. quo_complex64 :: proc "contextless" (n, m: complex64) -> complex64 {
  580. e, f: f32;
  581. if abs(real(m)) >= abs(imag(m)) {
  582. ratio := imag(m) / real(m);
  583. denom := real(m) + ratio*imag(m);
  584. e = (real(n) + imag(n)*ratio) / denom;
  585. f = (imag(n) - real(n)*ratio) / denom;
  586. } else {
  587. ratio := real(m) / imag(m);
  588. denom := imag(m) + ratio*real(m);
  589. e = (real(n)*ratio + imag(n)) / denom;
  590. f = (imag(n)*ratio - real(n)) / denom;
  591. }
  592. return complex(e, f);
  593. }
  594. quo_complex128 :: proc "contextless" (n, m: complex128) -> complex128 {
  595. e, f: f64;
  596. if abs(real(m)) >= abs(imag(m)) {
  597. ratio := imag(m) / real(m);
  598. denom := real(m) + ratio*imag(m);
  599. e = (real(n) + imag(n)*ratio) / denom;
  600. f = (imag(n) - real(n)*ratio) / denom;
  601. } else {
  602. ratio := real(m) / imag(m);
  603. denom := imag(m) + ratio*real(m);
  604. e = (real(n)*ratio + imag(n)) / denom;
  605. f = (imag(n)*ratio - real(n)) / denom;
  606. }
  607. return complex(e, f);
  608. }
  609. mul_quaternion128 :: proc "contextless" (q, r: quaternion128) -> quaternion128 {
  610. q0, q1, q2, q3 := real(q), imag(q), jmag(q), kmag(q);
  611. r0, r1, r2, r3 := real(r), imag(r), jmag(r), kmag(r);
  612. t0 := r0*q0 - r1*q1 - r2*q2 - r3*q3;
  613. t1 := r0*q1 + r1*q0 - r2*q3 + r3*q2;
  614. t2 := r0*q2 + r1*q3 + r2*q0 - r3*q1;
  615. t3 := r0*q3 - r1*q2 + r2*q1 + r3*q0;
  616. return quaternion(t0, t1, t2, t3);
  617. }
  618. mul_quaternion256 :: proc "contextless" (q, r: quaternion256) -> quaternion256 {
  619. q0, q1, q2, q3 := real(q), imag(q), jmag(q), kmag(q);
  620. r0, r1, r2, r3 := real(r), imag(r), jmag(r), kmag(r);
  621. t0 := r0*q0 - r1*q1 - r2*q2 - r3*q3;
  622. t1 := r0*q1 + r1*q0 - r2*q3 + r3*q2;
  623. t2 := r0*q2 + r1*q3 + r2*q0 - r3*q1;
  624. t3 := r0*q3 - r1*q2 + r2*q1 + r3*q0;
  625. return quaternion(t0, t1, t2, t3);
  626. }
  627. quo_quaternion128 :: proc "contextless" (q, r: quaternion128) -> quaternion128 {
  628. q0, q1, q2, q3 := real(q), imag(q), jmag(q), kmag(q);
  629. r0, r1, r2, r3 := real(r), imag(r), jmag(r), kmag(r);
  630. invmag2 := 1.0 / (r0*r0 + r1*r1 + r2*r2 + r3*r3);
  631. t0 := (r0*q0 + r1*q1 + r2*q2 + r3*q3) * invmag2;
  632. t1 := (r0*q1 - r1*q0 - r2*q3 - r3*q2) * invmag2;
  633. t2 := (r0*q2 - r1*q3 - r2*q0 + r3*q1) * invmag2;
  634. t3 := (r0*q3 + r1*q2 + r2*q1 - r3*q0) * invmag2;
  635. return quaternion(t0, t1, t2, t3);
  636. }
  637. quo_quaternion256 :: proc "contextless" (q, r: quaternion256) -> quaternion256 {
  638. q0, q1, q2, q3 := real(q), imag(q), jmag(q), kmag(q);
  639. r0, r1, r2, r3 := real(r), imag(r), jmag(r), kmag(r);
  640. invmag2 := 1.0 / (r0*r0 + r1*r1 + r2*r2 + r3*r3);
  641. t0 := (r0*q0 + r1*q1 + r2*q2 + r3*q3) * invmag2;
  642. t1 := (r0*q1 - r1*q0 - r2*q3 - r3*q2) * invmag2;
  643. t2 := (r0*q2 - r1*q3 - r2*q0 + r3*q1) * invmag2;
  644. t3 := (r0*q3 + r1*q2 + r2*q1 - r3*q0) * invmag2;
  645. return quaternion(t0, t1, t2, t3);
  646. }