internal.odin 22 KB

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