Compare commits

..

494 Commits

Author SHA1 Message Date
gingerBill 9e6e769141 Add sanity checks to checker 2021-08-02 18:32:17 +01:00
Jeroen van Rijn c0f746a251 Merge pull request #1065 from Kelimion/string_cut
Add `strings.cut`, which returns a substring.
2021-08-02 18:09:59 +02:00
gingerBill ccbdf086ff Add @(warning=<string>) 2021-08-02 16:58:50 +01:00
Jeroen van Rijn d260ca6738 string.cut uses context.allocator by default. 2021-08-02 17:58:42 +02:00
gingerBill b0e64ca7e8 Prepare tokenizer for optimizations 2021-08-02 16:47:32 +01:00
gingerBill 7f3d4cb504 Remove the literal conversion logic to the parser from the tokenizer 2021-08-02 00:53:45 +01:00
gingerBill 97be36d18a Replace line with column_minus_one in Tokenizer; Remove dead code 2021-08-02 00:26:54 +01:00
gingerBill be76da2c90 Begin optimizing tokenizer; Replace gb_utf8_decode with utf8_decode (CC but easier to change later) 2021-08-01 23:56:17 +01:00
gingerBill b1a8357f50 Clean up a bit of the tokenizer code so that the semicolon insertion rules are in one place 2021-08-01 22:41:00 +01:00
Jeroen van Rijn 0dc900ba34 Add strings.cut, which returns a substring. 2021-08-01 19:41:20 +02:00
gingerBill 700624119b Give begin_error_block its own recursive mutex 2021-07-29 12:35:11 +01:00
gingerBill af32aba7fc Modify MPMCQueue behaviour to use i32 over isize; Correct cache line padding within MPMCQueue 2021-07-28 00:59:30 +01:00
gingerBill 541c79c01a Add mutex for @(builtin) attributes 2021-07-28 00:27:16 +01:00
gingerBill 358226468d EXPERIMENT: Set DEFAULT_TO_THREADED_CHECKER on all platforms 2021-07-27 23:55:55 +01:00
gingerBill d1e5f34f76 Use DEFAULT_TO_THREADED_CHECKER 2021-07-27 23:55:19 +01:00
gingerBill 416dd93bf7 Add accidentally removed call 2021-07-27 23:39:35 +01:00
gingerBill 7c80577160 Correct race condition on untyped expr info map logic on global evaluations 2021-07-27 23:37:55 +01:00
gingerBill a5d6fda433 Define which mutexes are blocking and recursive explicitly 2021-07-27 23:14:01 +01:00
gingerBill 4bc3796f9b Short circuit check_with_workers logic on worker_count == 0 2021-07-27 23:00:51 +01:00
gingerBill 5e12f5a746 Unify threading logic 2021-07-27 22:55:32 +01:00
gingerBill 4080ba4026 Remove global semaphore and place in CheckerInfo 2021-07-27 21:42:43 +01:00
gingerBill e17593be94 Remove dead mutex 2021-07-27 21:14:06 +01:00
gingerBill 44aa69748c Correct logic for check_import_entities - collect file decls 2021-07-27 21:13:03 +01:00
gingerBill 9cd5ea59dd Big simplification and improvement of the entity collection system, reducing unneeded steps for packages 2021-07-27 20:45:50 +01:00
gingerBill 116e98b378 Improve default scope size 2021-07-27 10:59:39 +01:00
gingerBill ae25787f48 Fix syntax error for a missing package name 2021-07-26 11:43:17 +01:00
gingerBill 08dc829b70 Manually short circuit in lb_build_if_stmt for constant conditions 2021-07-26 10:59:07 +01:00
gingerBill 3ac674cf02 Fix typo 2021-07-25 13:12:09 +01:00
gingerBill 6cd06ab95f Minor fix 2021-07-25 13:07:31 +01:00
gingerBill 99080d41f3 INTERNAL USE ONLY: //+lazy build flag 2021-07-25 13:06:09 +01:00
gingerBill 92f3567ee6 Default to using a threaded checker on Windows; Add -no-threaded-checker for Windows 2021-07-24 18:00:19 +01:00
Jeroen van Rijn 481fc8a5b6 Merge pull request #1060 from Kelimion/win_sys
Allow `core:sys/windows` to build on Windows only
2021-07-15 12:15:10 +02:00
Jeroen van Rijn 5dfff51a40 Allow core:sys/windows to build on Windows only 2021-07-15 12:13:57 +02:00
gingerBill 2938ec028f Remove dead code 2021-07-15 00:37:59 +01:00
gingerBill 981b9fb7a8 Remove need for scope mutex, make Scope singly linked list tree with atomic pointers 2021-07-14 23:49:35 +01:00
gingerBill 10f4d8df32 Override libtommath allocation procedures 2021-07-14 23:36:23 +01:00
gingerBill e15858e2be Remove random load balancing in thread_proc_body 2021-07-14 22:53:02 +01:00
gingerBill 6d8302825c Add Greed Work Stealing and Random Load Balancing for check_procedure_bodies 2021-07-14 01:13:39 +01:00
gingerBill fae8bf96dd Muilthread check_collect_entities_all 2021-07-14 00:46:03 +01:00
gingerBill bc59dc6389 Remove duplicate code 2021-07-14 00:36:48 +01:00
gingerBill bd8e2f82be Replace non-recursive mutexes with BlockingMutex; Minor improves to initialization improves 2021-07-14 00:34:34 +01:00
gingerBill 69027b6840 Remove dead mutexes 2021-07-13 23:10:12 +01:00
gingerBill 7a9b7af078 Reduce mutex usage and convert things to queues from arrays 2021-07-13 23:09:24 +01:00
gingerBill 31c7afce1b Minor code clean up to reuse the ProcInfo consumption code 2021-07-13 21:19:13 +01:00
gingerBill bab1873416 Require throughput pass results with -show-more-timings -show-debug-messages rather than just the former flag 2021-07-13 18:39:11 +01:00
gingerBill 5e2950e9fb Move asserts around 2021-07-13 18:35:23 +01:00
gingerBill 74c019f271 Correct lb_big_int_to_llvm 2021-07-13 18:32:53 +01:00
gingerBill a745bb8f42 Add extra message to assert 2021-07-13 18:21:53 +01:00
gingerBill da9870c77d Do manual byte swapping for endianness in lb_big_int_to_llvm 2021-07-13 18:15:47 +01:00
gingerBill 698eeaf7c3 Add (internal flag) -show-debug-messages 2021-07-13 17:40:06 +01:00
gingerBill bd954d9990 Minor code reorganization 2021-07-13 17:28:39 +01:00
gingerBill 4ded42a33b Split up cycle check and adding type info timings 2021-07-13 17:24:20 +01:00
gingerBill 1877965ac3 Short on -threaded-checker 2021-07-13 17:04:08 +01:00
gingerBill cec2309504 Big improvement to the -threaded-checker code, unifying the logic and simplify behaviour 2021-07-13 16:58:40 +01:00
gingerBill ed5a4afc8c Temporarily disable -threaded-checker; Restructure the untyped-expr-info system to be much more thread-friendly 2021-07-13 15:54:56 +01:00
gingerBill f29b51efdd Fix gb_shuffle 2021-07-13 13:09:55 +01:00
gingerBill 3930a32b0c enum Addressing_Mode : u8; 2021-07-12 16:45:54 +01:00
gingerBill 76707e1d2f Add sanity casts for 32/64 bit correctness 2021-07-12 11:03:12 +01:00
gingerBill ff2e5c3efe Simplify big_int_not for negative inputs 2021-07-11 17:43:56 +01:00
gingerBill 3600b2e209 Merge pull request #1057 from odin-lang/new-big-int-library-integration
New Big Int Library Integration
2021-07-11 17:20:57 +01:00
gingerBill eb36a0f3b1 Remove dead file 2021-07-11 16:35:04 +01:00
gingerBill b397254696 Rename libtommath.c to libtommath.cpp 2021-07-11 16:33:58 +01:00
gingerBill 9a37d3b6e5 Add -Wno-unused-value 2021-07-11 16:29:51 +01:00
gingerBill 51c4a19234 Fix tab and space issue in Makefile
FUCK DO I HATE Makefile whitespace sensitivity between spaces and tabs
2021-07-11 16:26:21 +01:00
gingerBill a1a1668dcf Update build.bat and Makefile 2021-07-11 16:23:25 +01:00
gingerBill e308098f18 Add libtommath.c 2021-07-11 16:19:20 +01:00
gingerBill 63b572a0ab Clean up big int to LLVM integer code 2021-07-11 16:18:30 +01:00
gingerBill e90e7d4af9 Change mp_clear calls to big_int_dealloc 2021-07-11 16:10:04 +01:00
gingerBill 460e14e586 Change the compiler's big integer library to use libTomMath
This now replaces Bill's crappy big int implementation
2021-07-11 16:08:16 +01:00
gingerBill ebcabb8a27 Add sanity conversion check for integer to quaternion 2021-07-11 00:51:56 +01:00
gingerBill 257b749e9d Minimize mutex usage in update_expr_type 2021-07-11 00:21:31 +01:00
gingerBill d9e6ade030 Add experimental support for a threaded semantic checker to -threaded-checker 2021-07-10 23:51:37 +01:00
gingerBill 690374d4de Fix typo 2021-07-10 23:07:42 +01:00
gingerBill adb25d9d19 Convert constant tag to the correct type for LLVMAddCase 2021-07-10 22:29:52 +01:00
gingerBill 2949e4b0c7 Fix floattidf typo 2021-07-10 22:23:22 +01:00
gingerBill 6de0181c75 Minor improvements to Map and StringMap 2021-07-10 21:51:39 +01:00
gingerBill 8a6b743d2a Simplify thread_pool_wait_to_process 2021-07-10 21:50:19 +01:00
gingerBill ed8a6f872d Move things around for sanity checking for multithread preparation 2021-07-10 21:29:49 +01:00
gingerBill 0a61d4bf2b Use next_pow2_isize 2021-07-10 19:57:54 +01:00
gingerBill 332461c0d2 Add prototypes for next_pow2 2021-07-10 19:52:26 +01:00
gingerBill d8abe7fc4d Implement MPMCQueue for procedure body checking
This is preparation for basic multithreading in the semantic checker
2021-07-10 19:50:34 +01:00
gingerBill ec9667ef5a Remove debug code 2021-07-10 17:11:54 +01:00
gingerBill 9f7154a039 Prepare for multithreading the semantic checker by giving mutexes to variables of contention
NOTE(bill): I know this is dodgy, but I want to make sure it is correct logic before improve those data structures
2021-07-10 15:14:25 +01:00
gingerBill 4a932616fc Improve CheckerContext usage 2021-07-10 13:02:13 +01:00
gingerBill 73fe36f19c Remove dead variable 2021-07-10 11:59:41 +01:00
gingerBill 4167ae95ae Fix #1050 2021-07-10 11:48:28 +01:00
gingerBill 13c3c5be95 Fix #1028 2021-07-10 11:46:22 +01:00
gingerBill 3afec0bcbe Fix #1054 2021-07-10 11:42:21 +01:00
gingerBill 8b1bfc80fb Fix #1051 2021-07-10 11:27:38 +01:00
gingerBill 3662275119 Allow x in ptr_to_map_or_bit_set 2021-07-10 11:18:19 +01:00
gingerBill 141573c18c Enable Damerau-Levenshtein 2021-07-10 11:09:24 +01:00
gingerBill e692efbe09 Improve update expr type semantics for ternary expressions 2021-07-10 11:08:51 +01:00
gingerBill f6c1a5bf6e Merge branch 'master' of https://github.com/odin-lang/Odin 2021-07-09 15:33:30 +01:00
gingerBill 6afc28f827 Use builtin.min and builtin.max in package slice 2021-07-09 15:33:25 +01:00
gingerBill df6681ad4e Merge pull request #1055 from streof/fix-typos-container-package
Fix typos container package
2021-07-09 13:27:03 +01:00
streof 114efbc57c Fix error: Cannot assign to a procedure parameter 2021-07-09 14:03:49 +02:00
streof 2c71494ad1 Fix error: Undeclared name: p 2021-07-09 13:54:27 +02:00
gingerBill 35230b1a11 Add "Suggestion: Did you mean?" for selector expression typos 2021-07-08 23:15:07 +01:00
gingerBill 7acbf8b7b9 Add slice.min and add slice.max 2021-07-08 11:23:07 +01:00
gingerBill f7413ca974 Fix thread_unix.odin 2021-07-05 16:36:07 +01:00
gingerBill 9b3a0251ca Use or_else in the core library when it makes sense 2021-07-05 16:33:01 +01:00
gingerBill 3b9ca8535f Fix comments 2021-07-05 16:26:11 +01:00
gingerBill a98eee145d Remove try; Replace try x else y with or_else(x, y) 2021-07-05 16:23:13 +01:00
gingerBill c6b9b3b9a4 Fix try parsing for expression statements 2021-07-04 22:52:12 +01:00
gingerBill a4be1a5e4c delete_key now returns the deleted key and deleted value (if found) 2021-07-04 18:52:47 +01:00
gingerBill ee908c00de Add documentation for the overview of package fmt 2021-07-04 18:21:41 +01:00
gingerBill 46264032aa Improve error messages for try expressions 2021-07-04 12:50:44 +01:00
gingerBill 4b831dbddd Try try and or_else built-in procedures with operators try and try else 2021-07-04 12:37:21 +01:00
gingerBill a01d6dcea7 Refactor return logic to be more reusable with lb_emit_try 2021-07-04 01:57:38 +01:00
gingerBill 01a15f78e6 Simplify lb_emit_try return logic 2021-07-04 01:50:37 +01:00
gingerBill 5f71c41582 Fix lb_emit_try 2021-07-04 01:47:43 +01:00
gingerBill e8f2c5a48a [Experimental] Add 'try' and `or_else' built-in procedures 2021-07-04 01:38:43 +01:00
gingerBill 1c76577918 Add slice.sort_by_cmp Ordering based sorting algorithms 2021-07-03 15:16:44 +01:00
gingerBill 4285b58aaa Add #no_bounds_check to linalg procedures 2021-07-03 14:38:41 +01:00
gingerBill 9cc366de97 Be more consistent with runtime intrinsics usage 2021-07-03 14:38:15 +01:00
gingerBill 212d294b84 Deprecate sort.slice and sort.reverse_slice 2021-07-03 14:37:55 +01:00
gingerBill d6125f05d4 Correct does_field_type_allow_using 2021-06-29 12:35:15 +01:00
gingerBill ad22eda87c Improve linalg.transpose type determination for square matrices 2021-06-28 13:23:28 +01:00
gingerBill 8d31ba492d Fix #1042 2021-06-28 11:32:23 +01:00
gingerBill 8f611b3399 Fix #1043 2021-06-28 11:21:03 +01:00
gingerBill 618f858930 Merge branch 'master' of https://github.com/odin-lang/Odin 2021-06-28 11:06:00 +01:00
gingerBill 185277a2b6 Fix swizzling of pointers to arrays 2021-06-28 11:05:52 +01:00
Jeroen van Rijn 8e5c3141f6 Merge pull request #1047 from Kelimion/zlib_optimize
ZLIB: Fix edge case where initial buffer < 258 bytes.
2021-06-27 18:49:09 +02:00
Jeroen van Rijn 87aaa9c3f0 ZLIB: Fix edge case where initial buffer < 258 bytes. 2021-06-27 18:44:36 +02:00
Jeroen van Rijn 095605b7db Merge pull request #1046 from Kelimion/zlib_optimize
ZLIB: Optimize
2021-06-27 16:54:15 +02:00
Jeroen van Rijn 6836b501af Merge branch 'master' into zlib_optimize 2021-06-27 16:50:27 +02:00
Jeroen van Rijn d949d5a046 ZLIB: cleanup. 2021-06-27 16:48:12 +02:00
Jeroen van Rijn 064516bf0b PNG: Inform inflate about expected output size for extra speed. 2021-06-27 13:57:12 +02:00
Jeroen van Rijn eaf88bcc4d PNG: Let PNG use the new compress I/O routines. 2021-06-27 13:51:52 +02:00
gingerBill 76d3bab955 Modify scope finding rules for distinct types in lb_debug_type 2021-06-27 12:21:11 +01:00
Jeroen van Rijn 02f9668185 ZLIB: Split up input from stream and memory into own code paths. 2021-06-27 13:19:24 +02:00
gingerBill abda75feee Add bufio.Lookahead_Reader 2021-06-26 23:45:45 +01:00
gingerBill a779cb2798 Fix #1044 2021-06-26 23:31:04 +01:00
gingerBill 5e42675b42 Allow alternative syntax for offset_of: offset_of(Type, field), offset_of(value.field) 2021-06-26 23:19:46 +01:00
gingerBill 11c565e199 Fix semicolon insertion rule for --- 2021-06-26 22:47:12 +01:00
Jeroen van Rijn 4689a6b341 Refactor compress.Context struct. 2021-06-26 22:25:55 +02:00
Jeroen van Rijn 30a5808460 ZLIB: Moar faster. 2021-06-26 20:40:39 +02:00
Jeroen van Rijn 8ba1c9a6cd ZLIB: Remove superfluous code. 2021-06-26 17:44:12 +02:00
Jeroen van Rijn 65b78b1aa9 So far, so good. 2021-06-26 15:11:15 +02:00
Jeroen van Rijn c369719362 Merge branch 'master' into zlib_optimize 2021-06-26 13:40:22 +02:00
Jeroen van Rijn 40a12cca53 ZLIB: If output size is known, reserve that much. 2021-06-26 13:17:14 +02:00
gingerBill d8940f5fd7 Support compound literals for struct #raw_union types 2021-06-26 12:08:14 +01:00
gingerBill 74dee82dbf Fix compiler errors 2021-06-25 14:33:42 +01:00
gingerBill f1cf724bd4 Add bufio.Scanner 2021-06-25 14:31:35 +01:00
gingerBill fc809d3fad Merge branch 'master' of https://github.com/odin-lang/Odin 2021-06-24 19:41:36 +01:00
gingerBill 42d135aade Change logic for comparison against nil for array-like data types (compare the pointer rather than the length/capacity) 2021-06-24 19:39:51 +01:00
Jeroen van Rijn ab12ca69af Merge pull request #1040 from Kelimion/zlib_optimize
Adler32 speedup.
2021-06-24 19:15:22 +02:00
Jeroen van Rijn 17748f18b9 Adler32 speedup. 2021-06-24 19:06:39 +02:00
gingerBill 3803bdff5f Allow bufio.Reader and bufio.Writer to have a configurable max_consecutive_empty_(reads|writes) field 2021-06-24 15:56:58 +01:00
gingerBill 0a94a67190 Merge branch 'master' of https://github.com/odin-lang/Odin 2021-06-24 15:55:50 +01:00
gingerBill 8dcb14fbc2 Add helper procedures for strings.Reader creation 2021-06-24 15:55:42 +01:00
Jeroen van Rijn f62f40e508 Merge pull request #1039 from Kelimion/zlib_optimize
ZLIB: Another 10%+ faster.
2021-06-24 15:02:02 +02:00
Jeroen van Rijn 980aa37bee ZLIB: Another 10%+ faster. 2021-06-24 14:56:28 +02:00
gingerBill 1e9cc058a0 Update hash.crc32 to use slicing-by-8 algorithm to improve throughput by ~3.5x 2021-06-24 00:03:59 +01:00
Jeroen van Rijn 824efc82b9 Merge pull request #1036 from Kelimion/zlib_optimize
ZLIB: More faster.
2021-06-23 22:25:02 +02:00
Jeroen van Rijn 1cfe226686 ZLIB: More faster. 2021-06-23 22:18:17 +02:00
Jeroen van Rijn ea0ce7bd2c Merge pull request #1035 from Kelimion/zlib_optimize
Zlib optimize
2021-06-23 20:15:32 +02:00
Jeroen van Rijn 342adb627d All reads now skip stream if in memory. 2021-06-23 16:32:48 +02:00
Jeroen van Rijn a70635d2f6 Most reads now go through buffer for zlib. 2021-06-23 16:32:48 +02:00
Jeroen van Rijn 5cb16c4cd1 All reads now go through read_slice. 2021-06-23 16:32:48 +02:00
Jeroen van Rijn 538004ba5f Introduce read_slice, make read_u8 use it. 2021-06-23 16:32:48 +02:00
Jeroen van Rijn 8663c64e47 Refactor ZLIB structs. 2021-06-23 16:32:47 +02:00
gingerBill bb3ffdbdfb Merge branch 'master' of https://github.com/odin-lang/Odin 2021-06-23 14:55:53 +01:00
gingerBill fcf7cf973b Add documentation for -verbose-errors 2021-06-23 14:55:40 +01:00
Jeroen van Rijn fb2e1c32bd Merge pull request #1034 from Kelimion/bytes_util
Move `bytes` utils back to EXR code for the time being.
2021-06-22 16:48:07 +02:00
Jeroen van Rijn ae0b8fce44 Move bytes utils back to EXR code for the time being.
Also, allow PNG example to be run directly from `core:image/png` directory.
2021-06-22 16:39:00 +02:00
Jeroen van Rijn d2e55f9ffa Merge pull request #1032 from Kelimion/png_fix
PNG: Fix leak if you don't ask for metadata.
2021-06-21 22:56:39 +02:00
Jeroen van Rijn d5e2b387fa PNG: Fix leak if you don't ask for metadata. 2021-06-21 22:47:54 +02:00
Jeroen van Rijn 922b511a24 Merge pull request #1031 from Kelimion/zlib_fix
ZLIB: fix.
2021-06-21 22:16:56 +02:00
Jeroen van Rijn 9de9111082 ZLIB: fix. 2021-06-21 22:15:04 +02:00
Jeroen van Rijn 5a7fe2e3d9 Merge pull request #1030 from Kelimion/zlib_optimize
Comment out tracy imports.
2021-06-21 21:43:57 +02:00
Jeroen van Rijn 1e8c12c2a3 Comment out tracy imports. 2021-06-21 21:41:56 +02:00
Jeroen van Rijn b92c70e55c Merge pull request #1029 from Kelimion/zlib_optimize
ZLIB: Start optimization.
2021-06-21 21:26:52 +02:00
Jeroen van Rijn 352494cbb4 ZLIB: Start optimization. 2021-06-21 21:05:52 +02:00
Jeroen van Rijn 797c41950a Merge pull request #1027 from Kelimion/png
Replace `core:image`'s `sidecar` with explicit pointer and type
2021-06-21 16:41:38 +02:00
Jeroen van Rijn e036a321a0 Replace core:image's sidecar with explicit metadata_ptr and metadata_type.
To unpack, use:
```odin

v: ^png.Info;

if img.metadata_ptr != nil && img.metadata_type == png.Info {
	v = (^png.Info)(img.metadata_ptr);
	...
}
```
2021-06-21 16:32:42 +02:00
Jeroen van Rijn 18471f358e Merge pull request #1025 from Kelimion/png_info
Change PNG's img.sidecar to ^Info, make img.depth an int.
2021-06-20 18:40:46 +02:00
Jeroen van Rijn 55d09251d8 Change PNG's img.sidecar to ^Info, make img.depth an int.
For compatibility with the upcoming OpenEXR code, img.depth is now an int.
Like OpenEXR's code, it will now also return metadata as ^Info instead of Info.

The example was updated to retrieve the metadata this way.

It regrettably does not fix: #1018. That seems to be a codegen issue in the test runner or elsewhere.
2021-06-20 18:27:23 +02:00
Jeroen van Rijn d66fd71d21 Merge pull request #1024 from Kelimion/defer_fix
GZIP defer diverging fix in gzip example.
2021-06-20 17:40:15 +02:00
Jeroen van Rijn f10fc2a494 Merge pull request #1023 from Kelimion/test_runner_fix
Fix Windows test runner.
2021-06-20 17:37:04 +02:00
Jeroen van Rijn 055d8c5370 Fix Windows test runner. 2021-06-20 17:33:39 +02:00
Jeroen van Rijn 955472bd21 GZIP defer diverging fix in gzip example. 2021-06-20 17:21:18 +02:00
gingerBill a2d5f660ed Merge pull request #1022 from Kelimion/buffer_convert
Add `bytes.buffer_create_of_type` and `bytes.buffer_convert_to_type`.
2021-06-18 16:41:48 +01:00
Jeroen van Rijn 8a4b9ddaa3 Fix comment. 2021-06-18 15:42:04 +02:00
Jeroen van Rijn 54a2b6f00e Add bytes.buffer_create_of_type and bytes.buffer_convert_to_type.
Convenience functions to reinterpret or cast one buffer to another type, or create a buffer of a specific type.

	Example:
```odin
	fmt.println("Convert []f16le (x2) to []f32 (x2).");
	b := []u8{0, 60, 0, 60}; // == []f16{1.0, 1.0}

	res, backing, had_to_allocate, err := bytes.buffer_convert_to_type(2, f32, f16le, b);
	fmt.printf("res      : %v\n", res);              // [1.000, 1.000]
	fmt.printf("backing  : %v\n", backing);          // &Buffer{buf = [0, 0, 128, 63, 0, 0, 128, 63], off = 0, last_read = Invalid}
	fmt.printf("allocated: %v\n", had_to_allocate);  // true
	fmt.printf("err      : %v\n", err);              // false

	if had_to_allocate { defer bytes.buffer_destroy(backing); }

	fmt.println("\nConvert []f16le (x2) to []u16 (x2).");

	res2: []u16;
	res2, backing, had_to_allocate, err = bytes.buffer_convert_to_type(2, u16, f16le, b);
	fmt.printf("res      : %v\n", res2);             // [15360, 15360]
	fmt.printf("backing  : %v\n", backing);          // Buffer.buf points to `b` because it could be converted in-place.
	fmt.printf("allocated: %v\n", had_to_allocate);  // false
	fmt.printf("err      : %v\n", err);              // false

	if had_to_allocate { defer bytes.buffer_destroy(backing); }

	fmt.println("\nConvert []f16le (x2) to []u16 (x2), force_convert=true.");

	res2, backing, had_to_allocate, err = bytes.buffer_convert_to_type(2, u16, f16le, b, true);
	fmt.printf("res      : %v\n", res2);             // [1, 1]
	fmt.printf("backing  : %v\n", backing);          // Buffer.buf points to `b` because it could be converted in-place.
	fmt.printf("allocated: %v\n", had_to_allocate);  // false
	fmt.printf("err      : %v\n", err);              // false

	if had_to_allocate { defer bytes.buffer_destroy(backing); }
```
2021-06-18 15:25:36 +02:00
gingerBill abe728dbbb Add intrinsics.type_is_endian_platform 2021-06-17 20:39:00 +01:00
gingerBill 574ceb37a9 Correct selector call expression chaining behaviour (a bit of a hack) 2021-06-16 17:04:05 +01:00
gingerBill dbdc4471c2 Fix double evaluation bug with selector call expressions x->y(z) 2021-06-16 14:03:12 +01:00
gingerBill af95381bf8 Add missing -> ! annotation 2021-06-16 12:12:24 +01:00
gingerBill 41f2539484 Improve logic for diverging procedures by checking if it terminates 2021-06-16 12:07:24 +01:00
gingerBill 8f57bb0799 Add unreachable detection for deferred statements in a scope which contains a diverging procedure call
```odin
{
    defer foo(); // Unreachable defer statement due to diverging procedure call at the end of the current scope
    os.exit(0);
}
```
2021-06-16 11:41:29 +01:00
gingerBill 84a4188c72 Fix #1017 2021-06-15 01:16:19 +01:00
gingerBill 31f1e0aeae Fix #1019 2021-06-15 01:13:16 +01:00
gingerBill 4b8cbb5a3b Fix #1015 2021-06-14 13:26:28 +01:00
gingerBill 3e7aabe6d8 Change uses for parapoly records to use $ always 2021-06-14 11:43:35 +01:00
gingerBill d4df325e0a Just create context when required 2021-06-14 11:41:50 +01:00
gingerBill 9f8a63cb43 More minor stylization changes (remove unneeded parentheses) 2021-06-14 11:34:31 +01:00
gingerBill 6f745677b4 Minor formatting changes 2021-06-14 11:30:00 +01:00
gingerBill 86649e6b44 Core library clean up: Make range expressions more consistent and replace uses of .. with ..= 2021-06-14 11:15:25 +01:00
gingerBill 3ca887a60a Add struct_fields_zipped and enum_fields_zipped (allowing for iteration through an #soa slice) 2021-06-14 11:04:51 +01:00
gingerBill 312a1e8a94 Fix context logic 2021-06-13 16:00:20 +01:00
gingerBill 9a311ab9e7 Remove dead code 2021-06-12 16:47:20 +01:00
gingerBill 7d92eaaeb2 Correct context logic in lb_build_addr 2021-06-12 16:45:44 +01:00
gingerBill 582f423b67 Improve vector arithmetic generation for array programming operations 2021-06-12 16:37:20 +01:00
gingerBill c2524464f9 Fix remove_temp_files 2021-06-12 16:23:41 +01:00
gingerBill 55e472cdb6 Fix linkage problem for procedures required by LLVM 2021-06-10 12:23:08 +01:00
gingerBill e6ad773a88 Minor code clean up 2021-06-09 23:47:44 +01:00
gingerBill 82eae32bca Improve code generation for type switch statements to use a jump table by default 2021-06-09 23:46:00 +01:00
gingerBill b0e21bd616 Allow trivial optimizations for switch statements of typeid 2021-06-09 23:05:37 +01:00
gingerBill 7b88bed098 Do trivial SwitchInstr optimization for constant case switch statements 2021-06-09 22:55:08 +01:00
gingerBill 28abf5d33b Correct minimum dependency for complex32 2021-06-08 21:20:33 +01:00
gingerBill fb8ad338d0 Keep -vet happy 2021-06-08 18:26:38 +01:00
gingerBill ee60be0137 Improve code generation hints for return statements which return by pointer 2021-06-08 17:17:53 +01:00
gingerBill 9efd4c5097 Aid code generation on non-release builds 2021-06-08 17:17:24 +01:00
gingerBill f30e6f50bd Reorganize code to improve code generation 2021-06-08 16:21:19 +01:00
gingerBill 8ec2ca9dcd Remove context.thread_id 2021-06-08 15:57:00 +01:00
gingerBill f19bb0f4d4 Make default calling convention code more correct to read 2021-06-08 14:33:49 +01:00
gingerBill 76bb82a726 Minor alignment cleanup for swizzle load 2021-06-08 14:27:36 +01:00
gingerBill 8e62f9c83c Correct is_operand_value for Swizzle addressing modes 2021-06-08 14:23:44 +01:00
gingerBill 696f758435 Fix and improve swizzle loads for ordered indices 2021-06-08 14:19:27 +01:00
gingerBill 6421152104 Fix show-timings header for -lld on windows 2021-06-08 13:19:19 +01:00
gingerBill 1e989f5c10 Fix -lld on Windows 2021-06-08 13:10:22 +01:00
gingerBill 3eb42ecb55 Minor improvements to -use-separate-modules 2021-06-08 13:00:20 +01:00
gingerBill 286cb60c45 Minor changes to tools/odinfmt 2021-06-08 12:18:55 +01:00
gingerBill 28e9a4f79c Replace js_wasm32 with freestanding_wasm32 2021-06-08 12:18:26 +01:00
gingerBill e79fb68291 Correct #soa type creation 2021-06-08 11:23:23 +01:00
gingerBill 16eaa17ed9 Fix -target:js_wasm32 for core:runtime 2021-06-08 11:20:39 +01:00
gingerBill 9491c13a5c Fix #1011 by unifying the logic 2021-06-08 11:09:41 +01:00
gingerBill 8d8adac1b4 Fix lb_build_defer_stmt 2021-06-08 10:43:11 +01:00
gingerBill ba6c63e366 Fix full_path_from_name allocator behaviour 2021-06-08 10:14:35 +01:00
gingerBill 963b1a12d7 Correct code for #simd in unions 2021-06-06 16:56:03 +01:00
gingerBill 89890d7900 Correct union tag size for large alignments 2021-06-06 16:34:51 +01:00
gingerBill 661fcad895 Add examples/all which imports every package
This is useful for knowing what exists and producing documentation with `odin doc`
2021-06-06 13:05:54 +01:00
gingerBill cef16feb0b Fix #861 - Add extra check on missing main 2021-06-06 12:46:59 +01:00
gingerBill 84b851f578 Add warning to variables which may overflow the stack on declaration; #Fix 661 2021-06-06 12:42:39 +01:00
gingerBill 785c27daa7 Fix 128-bit integer to float cast by explicitly calling the procedure direct; Fix #781 2021-06-06 12:35:38 +01:00
gingerBill 795a5910cf Add support for Addressing_OptionalOkPtr
Allowing for `i, ok := &x.(T);` (type assertions) and `v, ok := &m[k];` (map indexing)
2021-06-06 12:18:45 +01:00
gingerBill 4c21f9495d Clean up lbAddr_Swizzle logic for load and store 2021-06-05 23:56:59 +01:00
gingerBill f119fd1ee1 Use shufflevector when possible for lbAddr_Swizzle load 2021-06-05 22:50:23 +01:00
gingerBill 46ab822316 Improved lb_build_assign_stmt_array logic 2021-06-05 22:07:39 +01:00
gingerBill 104aea9f42 Improve error message for addressing a swizzle intermediate array value 2021-06-05 18:25:51 +01:00
gingerBill a2f2041aa6 Fix lb_build_assign_stmt_array for lbAddr_Swizzle 2021-06-05 17:33:42 +01:00
gingerBill 599d18f26f Experimental support for inline swizzling for array types of len <= 4 e.g. v.xyz, v.argb, v.xxx 2021-06-05 17:22:39 +01:00
gingerBill 61084d832d Add missing doc_format flags for entities and improve docs for the odin package 2021-06-05 15:55:19 +01:00
gingerBill b957996577 Add extra documentation to doc_format.odin 2021-06-05 15:26:05 +01:00
gingerBill f41150f8e9 Fix transposing 2021-06-04 15:10:53 +01:00
gingerBill 21adad4e09 Fix typo 2021-06-04 15:09:55 +01:00
gingerBill 47f9e8f850 Fix ast_end_token for [?]T types 2021-06-03 11:35:27 +01:00
gingerBill ba3f2a6a0c All spaces in import-like paths 2021-06-03 10:28:45 +01:00
gingerBill b9888f8f68 Fix linalg.transpose 2021-06-03 10:05:05 +01:00
gingerBill 32cda5d56a Or did it?! 2021-06-02 22:12:38 +01:00
gingerBill a4d9847f45 FINALLY fix lazy_buffer_destroy 2021-06-02 22:12:20 +01:00
gingerBill 8aa6d70dec Fix filepath.lazy_buffer 2021-06-02 12:21:20 +01:00
gingerBill ea6b222430 Clean up filepath.lazy_buffer memory leak 2021-06-02 12:19:25 +01:00
gingerBill 91b4bf3daa Minor clean up 2021-06-02 10:39:47 +01:00
gingerBill 8c943eb054 Make inline array arithmetic use load+extractvalue rather than getelementptr+load to give the optimizer a better hint for vectorization 2021-06-01 10:51:54 +01:00
gingerBill 446703ba75 Improves to array arithmetic on += etc assignment statements 2021-06-01 10:37:31 +01:00
gingerBill 266b5d7d85 Fix container/map.odin 2021-06-01 09:26:01 +01:00
gingerBill d90adb7a8e Fix #998 2021-05-31 21:03:50 +01:00
gingerBill 2573da12fc Fix #992 2021-05-31 20:58:28 +01:00
gingerBill bbc9c6a93c Fix #999 2021-05-31 20:56:07 +01:00
gingerBill 673134185a Fix #996 2021-05-31 20:54:09 +01:00
gingerBill 3bf00e6125 Comment on the required generate_minimum_dependency_set entities 2021-05-31 20:50:21 +01:00
gingerBill 8fd4fe25d6 Remove unneeded minimum dependencies 2021-05-31 20:44:48 +01:00
gingerBill ea1dc5373d Remove unneeded dependencies 2021-05-31 20:39:48 +01:00
gingerBill b8d6dd4eb7 Fix #1004 2021-05-31 20:38:10 +01:00
gingerBill 4d80f8598d Fix polymorphic record "too few" lacking error message 2021-05-31 20:33:14 +01:00
Jeroen van Rijn bc4591fc1e Merge pull request #1006 from jockus/master
Fix for value rather than type used for intrinsics
2021-05-31 14:23:40 +02:00
Joakim Hentula 6465fb8ec7 Fix for value rather than type used for intrinsics 2021-05-31 13:21:13 +01:00
gingerBill 46204ed7f0 Update core:runtime to use the new intrinsics 2021-05-30 13:22:15 +01:00
gingerBill 0f91ffe28f Add intrinsics.{ptr_offset, ptr_sub} 2021-05-30 13:21:56 +01:00
gingerBill 4b46d691f8 Improve logic for intrinsics.{mem_copy, mem_copy_non_overlapping, mem_zero} to use the *.inline LLVM variants when possible 2021-05-30 12:52:44 +01:00
gingerBill 599d0cf6ac Merge pull request #1005 from Kelimion/maths
Add `abs_f16` support + endian versions of maths routines.
2021-05-30 11:48:17 +01:00
Jeroen van Rijn d7dba495fd Last of the Endian float in math.odin. 2021-05-29 18:27:43 +02:00
Jeroen van Rijn c05f6b4a31 Even more Endian maths. 2021-05-29 17:52:47 +02:00
Jeroen van Rijn a0a578c72a More Endian version of maths procs. 2021-05-29 17:21:54 +02:00
Jeroen van Rijn 55fc2c00c0 Add Endian versions of math routines. 2021-05-29 16:22:47 +02:00
Jeroen van Rijn 6944e2fc04 Add abs_f16 dependency when used. 2021-05-29 15:33:11 +02:00
gingerBill c8b353b6d8 Makefile fix 2021-05-27 14:59:45 +01:00
gingerBill 6a8a31824d Minor change to Makefile 2021-05-27 14:55:12 +01:00
gingerBill 275b8d2e8a Merge pull request #925 from Kelimion/testing
Fix `core:sys/win32` tests to use `core:testing`.
2021-05-27 14:51:09 +01:00
gingerBill 8cfdd9805d Merge branch 'master' of https://github.com/odin-lang/Odin 2021-05-27 14:46:10 +01:00
gingerBill 7d304f4e8b Convert tabs to spaces for Makefile (stupid make) 2021-05-27 14:45:57 +01:00
gingerBill b65e5d5e03 Merge pull request #1003 from jockus/master
Fix deleting substring in filepath.rel
2021-05-27 13:35:46 +01:00
jockus b110153b51 Fix accidental removal of newline 2021-05-27 12:04:24 +01:00
jockus b261937233 Remove accidental core:fmt include in path/filepath 2021-05-27 12:03:37 +01:00
jockus 4455ba5b65 Merge branch 'master' of https://github.com/odin-lang/Odin 2021-05-27 12:01:28 +01:00
jockus e8aa767c8d Fix deleting substring in filepath.release 2021-05-27 12:01:13 +01:00
gingerBill bb7bd94b0a Fix comparison bug of enumerated arrays 2021-05-27 09:52:50 +01:00
gingerBill 4a886a1bc5 Disable copy elision on assignments for the time being 2021-05-25 15:43:34 +01:00
gingerBill c21c754b6f Minimize copying on getting the address of a call if required 2021-05-24 23:51:01 +01:00
gingerBill e948fcd2f1 Use intrinsics.mem_zero in runtime.mem_zero 2021-05-24 22:41:18 +01:00
gingerBill 0c46d06e63 Add intrinsics.mem_zero 2021-05-24 22:39:27 +01:00
gingerBill 44b6e7c45d Move the mem zero into a separate procedure for reuse 2021-05-24 22:32:38 +01:00
gingerBill 284a2631fd Refactoring of lbFunctionType retrieval 2021-05-24 22:16:22 +01:00
gingerBill 3f156bcb4b Refactor backend code for assignments; Refactor some statements into separate procedures 2021-05-24 22:09:21 +01:00
gingerBill d35a9e65b6 Heavily improve the copy elision logic in the backend 2021-05-24 20:57:44 +01:00
gingerBill c440296ae8 Add @(link_section=<string>) for global variables 2021-05-24 15:41:22 +01:00
gingerBill 79f115d6a7 Handle #c_vararg correctly 2021-05-24 14:46:03 +01:00
gingerBill 39eccdf6b9 Make js default to nil allocator for the time being 2021-05-23 15:17:58 +01:00
gingerBill 71cfa0c9fe Clean up organization of package runtime 2021-05-23 12:13:13 +01:00
gingerBill e82f8214e8 Add bytes.remove, bytes.remove_all, strings.remove, strings.remove_all 2021-05-23 11:46:43 +01:00
gingerBill b8f8d4c3a1 Modify ABI for the Odin calling conventions on SysV slightly 2021-05-22 11:33:08 +01:00
gingerBill 9e2eb717fe Correct newline_limit logic 2021-05-21 15:22:35 +01:00
gingerBill f0c9f82e1b Minor changes to printer to use a bit set rather than booleans for list options 2021-05-21 13:42:29 +01:00
gingerBill cc1d3a7b19 parser.check_poly_params_for_type 2021-05-21 13:07:00 +01:00
gingerBill 159daba759 Fix ast_end_token 2021-05-21 12:44:45 +01:00
gingerBill d7e85725e1 General improves to formatting for the visitor 2021-05-21 11:16:07 +01:00
gingerBill 247f4f3293 Fix ..= logic in the backend 2021-05-21 10:51:19 +01:00
gingerBill 8758afdf4e General fixes for odinfmt 2021-05-21 10:21:23 +01:00
gingerBill 362f07d7c5 Merge pull request #997 from DanielGavin/prototype-fmt
Add odin/format and odin/printer packages
2021-05-20 21:02:26 +01:00
gingerBill fe74b479c6 Begin changes to sync2 2021-05-20 21:02:05 +01:00
Daniel Gavin 44ee0f2cdc Merge branch 'master' into prototype-fmt 2021-05-20 12:15:14 +02:00
Daniel Gavin 50035f257e don't factor in the rhs length for lines that don't have Equal tokens. 2021-05-20 12:13:23 +02:00
gingerBill 92abddddc5 Be a little more correct with the temporary Ast node 2021-05-19 15:02:36 +01:00
gingerBill 4d580ed693 Keep -vet happy 2021-05-19 14:26:20 +01:00
gingerBill 9c54ed5792 Add range-based error messages to -verbose-errors
Example:
Cannot convert '(1 + 2)' to 'untyped bool' from 'untyped integer'

	x := (1 + 2) * true;
	     ^~~~~~^
2021-05-19 14:15:57 +01:00
gingerBill 5108ebf015 Replace error calls with Token to use TokenPos 2021-05-19 13:02:44 +01:00
gingerBill 86dbcb1b20 Add -verbose-errors which shows the error in the line of code 2021-05-19 12:57:30 +01:00
gingerBill 3ac934dd15 Add suggestion for unused expression on x == y
Expression is not used: 'x == 123'
	Suggestion: Did you mean to do an assignment?
	            'x = 123;'
2021-05-19 11:58:02 +01:00
gingerBill 26ce40c188 Remove @(static) for global variables 2021-05-19 11:51:48 +01:00
gingerBill b34e4a9fd1 More minor linkage changes 2021-05-19 11:46:27 +01:00
gingerBill 28561ef5f5 Minor change to internal linkage stuff 2021-05-19 11:26:05 +01:00
gingerBill 10b798456c Add soa_zip and soa_unzip to demo.odin 2021-05-19 10:55:56 +01:00
gingerBill a580cdbe7b Delete core:sync/sync2/channel* stuff (for the time being) 2021-05-19 10:50:27 +01:00
gingerBill e82e4398b6 Add intrinsics.mem_copy and intrinsics.mem_copy_non_overlapping 2021-05-19 10:50:02 +01:00
gingerBill e0225c3579 Add intrinsics.sqrt for floating-point values 2021-05-19 10:32:41 +01:00
gingerBill 8d044fd442 Minor ABI change and cleanup 2021-05-17 17:08:25 +01:00
gingerBill 6ef96d3300 Improve untyped to typed logic for aiding the backend 2021-05-16 14:44:02 +01:00
gingerBill 2e633f57a0 Add concrete type information for untyped values as procedure arguments 2021-05-16 13:00:16 +01:00
gingerBill 50369cf19c Update LICENSE year 2021-05-16 12:48:36 +01:00
gingerBill 385385364b Fix #988 2021-05-16 12:46:25 +01:00
gingerBill 9ccdc40f65 Make .Optional_Semicolons a flag for the parser 2021-05-16 12:43:35 +01:00
gingerBill df3512b112 Make core:odin use a string for the source rather than []byte 2021-05-16 12:38:27 +01:00
gingerBill ce08e832f7 Allow ..= alongside .. as a "full range" operator; Update core:odin/parser etc 2021-05-16 12:34:35 +01:00
gingerBill 24c89b3eee Minor change 2021-05-16 00:29:22 +01:00
gingerBill 85e5be03d1 Redesign os2.Error to work with the new extended union behaviour 2021-05-16 00:25:47 +01:00
gingerBill b1cfeb6c95 Add missing instruction to pass 2021-05-15 22:14:20 +01:00
gingerBill fffb83282b Add missing instructions to pass 2021-05-15 21:24:17 +01:00
gingerBill 1cf6b6679d Add custom basic dead instruction elimination pass 2021-05-15 21:10:06 +01:00
gingerBill 7886798156 Add space 2021-05-15 19:49:48 +01:00
gingerBill 0ad599675e Improve semicolon insertion rule for dummy tokens ++ and -- 2021-05-15 19:47:00 +01:00
gingerBill 5d03bc61b8 Tokenize ++ and -- as tokens but disallow them in the parser, and give better error messages for they are used as operators/statements 2021-05-15 19:34:46 +01:00
gingerBill 5e31c04a01 Disallow duplicate unary operators for +, -, and ~ 2021-05-15 19:13:34 +01:00
gingerBill 7b7081d607 Remove old dead code 2021-05-15 18:59:54 +01:00
gingerBill 5ae564cc8c Add name to aggregate result pointer to procedures 2021-05-15 18:53:52 +01:00
gingerBill 0507b9ebb7 Fix #987 2021-05-15 18:40:06 +01:00
gingerBill f7b1290fe9 Change for i in x..y {} behaviour
Adds an extra check before incrementation to prevent the possibility of overflowing of `y` is at the limit maximum size of the integer
e.g. `for i in u8(0)..255 {}` (assuming `255` is not known at compile time)
2021-05-15 18:02:06 +01:00
gingerBill b01c2e1017 Disallow slicing of constant values 2021-05-15 16:40:40 +01:00
gingerBill 63b54ce7c6 Add minor ignoring hint on type assertions to get better code generation with no optimizations enabled 2021-05-13 12:48:12 +01:00
gingerBill b8a35c658c Remove incl/excl usage from demo 2021-05-13 12:06:35 +01:00
gingerBill 465b6139d5 Temporarily fix syscall in Linux and Freebsd (eventually to be replaced with a proper implementation) 2021-05-13 12:05:23 +01:00
gingerBill b37d344eb2 Add intrinsics.type_is_variant_of 2021-05-13 12:04:51 +01:00
gingerBill d4ee1a9e19 Correct default procedure parameter logic 2021-05-13 10:07:28 +01:00
gingerBill be12f12c3c Fix #814 2021-05-13 00:57:10 +01:00
gingerBill 2e5f57d8a1 Fix #741 2021-05-13 00:44:33 +01:00
Mikkel Hjortshøj d5c3f99655 Update feature_request.md 2021-05-13 01:26:15 +02:00
Mikkel Hjortshøj 083e9e2053 Update feature_request.md 2021-05-13 01:25:37 +02:00
gingerBill c6c5af527b Fix #746 2021-05-12 23:43:16 +01:00
gingerBill 5420cc083d Implement #807 2021-05-12 23:26:21 +01:00
gingerBill c81f7b31c6 Add explicit numbers to enum AddressingMode 2021-05-12 23:22:43 +01:00
gingerBill fdd0c726bc Improve error message to "fix" #640 2021-05-12 23:12:20 +01:00
gingerBill da9cabc334 Fix #682 2021-05-12 23:02:57 +01:00
gingerBill d962cfdc6b Fix #713 2021-05-12 22:38:37 +01:00
gingerBill dfb8143149 Fix #651 2021-05-12 22:23:43 +01:00
gingerBill d2fcbf0e1d Fix #948 2021-05-12 15:00:19 +01:00
gingerBill e08f39ec28 Remove unneeded assert 2021-05-11 16:20:50 +01:00
gingerBill d353f97f91 Add byval with align, sret attributes for SysV 2021-05-11 13:30:27 +01:00
gingerBill 8144e82c6c Fix docs for -lld 2021-05-11 13:13:34 +01:00
gingerBill d0f7cf74e9 Add llvm-config version 11 check for Darwin in Makefile 2021-05-11 12:31:56 +01:00
gingerBill e1c2528d87 Remove warning on \*nix 2021-05-11 12:29:27 +01:00
gingerBill eac61fb536 Remove LLVMAddLowerConstantIntrinsicsPass 2021-05-11 12:17:48 +01:00
gingerBill 20f7e61363 Revert LLVM-C.lib 2021-05-11 12:11:59 +01:00
gingerBill 8bb6651dda Remove test call for LLVM 2021-05-11 12:08:09 +01:00
gingerBill 8ff80dec58 Minor change (in preparation for something else) to opt passes 2021-05-11 12:00:55 +01:00
gingerBill f31b09212a Improve SysV ABI 2021-05-11 12:00:27 +01:00
gingerBill 073bd3f6c9 Update LLVM-C.lib (since it was missing many required definitions missing from the official one that LLVM supplied) 2021-05-11 10:55:55 +01:00
gingerBill 9a4d942b0b Fix debug information for array types by setting the DISubrange 2021-05-10 21:29:25 +01:00
gingerBill ff6fdc7812 Correct SysV ABI for -> (f32, bool) 2021-05-10 21:16:26 +01:00
gingerBill c85c5ec38c Merge branch 'master' of https://github.com/odin-lang/Odin 2021-05-07 13:18:07 +01:00
gingerBill d70b5475eb Minor code clean up 2021-05-07 13:18:01 +01:00
gingerBill 42138d1ad5 Merge pull request #947 from Kelimion/deprecated
Mark mem.slice_ptr_to_bytes as deprecated.
2021-05-07 10:59:08 +01:00
gingerBill 47f97b8f89 Remove unused hint 2021-05-06 14:09:50 +01:00
gingerBill 902be0d09c Improve type inference system to allow &{} alongside &T{} in some cases 2021-05-06 14:06:22 +01:00
gingerBill 502ad0c10b sync2.Auto_Reset_Event; Make atomic operations names clearer 2021-05-06 14:00:01 +01:00
gingerBill 27f5aadd5a Merge branch 'master' of https://github.com/odin-lang/Odin 2021-05-06 13:59:14 +01:00
gingerBill da7a9a3584 Improve type inference rules for implicit selector expressions
New improvements:
`(.A == x)`
`a_union_containing_many_enums = .A;`
2021-05-06 13:59:05 +01:00
Jeroen van Rijn 03862d1f48 Mark mem.slice_ptr_to_bytes as deprecated.
Use byte_slice instead.

We can't make it an alias *and* mark it as deprecated, regrettably:

```odin
byte_slice :: #force_inline proc "contextless" (data: rawptr, len: int) -> []byte {
    return transmute([]u8)Raw_Slice{data=data, len=max(len, 0)};
}
@(deprecated="use byte_slice")
slice_ptr_to_bytes :: byte_slice;

"mem.odin(145:1) Constant alias declarations cannot have attributes"
```
2021-05-06 13:23:17 +02:00
Jeroen van Rijn 15ce8b0454 Merge pull request #946 from Kelimion/virtual_allocator
Fix typo in core:mem alloc() comment.
2021-05-06 11:33:08 +02:00
Jeroen van Rijn 4f51d74fc2 Fix typo in core:mem alloc() comment. 2021-05-06 11:25:41 +02:00
gingerBill b6a1ec0229 Remove debug code 2021-05-05 23:34:53 +01:00
gingerBill 60685369b9 Fix typo 2021-05-05 23:34:39 +01:00
gingerBill 0f2a9e6143 Fix LLVM store point for nested types containing procedures (e.g. ^^proc()) 2021-05-05 23:00:12 +01:00
gingerBill 278de3a92f Unify AstTernaryExpr with AstTernaryIfExpr
Allow for both syntaxes `x if cond else y` and `cond ? x : y`
Removes the confusing semantics behind `?:` which could be `if` or `when` depending on the context.
2021-05-05 15:22:54 +01:00
gingerBill abdf54800e Improve ternary expression logic within parametric polymorphic parameter assignments 2021-05-05 15:14:39 +01:00
gingerBill 579b317be8 Mark external thread local globals 2021-05-05 11:47:47 +01:00
gingerBill 08360e2337 Minor cleanup 2021-05-05 10:56:19 +01:00
gingerBill f11f84964d Merge branch 'master' of https://github.com/odin-lang/Odin 2021-05-04 23:41:45 +01:00
gingerBill bb9c1d04db Fix missing newline check 2021-05-04 23:41:35 +01:00
Jeroen van Rijn f3c4d97250 Merge pull request #942 from Kelimion/grayscale-alpha-extract
Fix gray+alpha alpha extract.
2021-05-04 17:49:09 +02:00
Jeroen van Rijn afb6ebd21e Fix gray+alpha alpha extract. 2021-05-04 17:48:43 +02:00
gingerBill 6fa5eb9e1f Update README.md 2021-05-04 15:27:41 +01:00
gingerBill 94570a24c1 Move anonymous proc generation into lbGenerator 2021-05-04 00:56:29 +01:00
gingerBill 17001bf38c Nearly approach full functionality for -use-separate-modules coupled with multithreading 2021-05-04 00:45:09 +01:00
gingerBill b83e67f45f Get LLVM backend multithread for object generation with -use-separate-modules 2021-05-04 00:06:20 +01:00
gingerBill a5eea97edb Prepare to multithread object generation 2021-05-03 20:45:27 +01:00
gingerBill 866d5302fe Add [EXPERIMENTAL] warning to -user-separate-modules help message 2021-05-03 20:05:45 +01:00
gingerBill 5d70289b69 Remove test code 2021-05-03 20:03:37 +01:00
gingerBill d76ba7895b Minor fixes to -use-separate-modules 2021-05-03 19:59:17 +01:00
gingerBill dfe1dedeb1 Experimental support for -use-separate-modules 2021-05-03 19:39:36 +01:00
gingerBill d027a5f1a4 Add min_f16 and max_f16 dependencies 2021-05-03 18:48:48 +01:00
gingerBill 746e880eb5 Begin work on making LLVM backend work with multiple modules for possible faster compilation 2021-05-03 17:43:14 +01:00
gingerBill e4286d0ff9 Force zero_init in lb_add_local in certain cases 2021-05-03 15:57:35 +01:00
gingerBill 3a556eb304 Add -test-name:<string> flag to allow specific tests to be ran 2021-05-03 15:44:57 +01:00
gingerBill b44a56118e Begin cleanup for allowing for multiple LLVM modules 2021-05-03 15:26:40 +01:00
gingerBill 0d044eabac Remove non-InContext type creations 2021-05-03 15:00:50 +01:00
Jeroen van Rijn 3cf26af600 Merge pull request #941 from Kelimion/compress_error_handling
Change General_Error.OK to nil
2021-05-03 15:39:42 +02:00
Jeroen van Rijn 9a39ce6b75 Change General_Error.OK to nil 2021-05-03 15:38:43 +02:00
gingerBill 3a5245dcce Make sure builder uses the InContext version 2021-05-03 14:36:27 +01:00
Jeroen van Rijn ca0f36be42 Merge pull request #940 from Kelimion/convert_error_checks
Convert `core:compress` and `core:image` error checks to new union co…
2021-05-03 15:09:52 +02:00
Jeroen van Rijn 59b3c472ca Convert core:compress and core:image error checks to new union comparison.
No more need for `is_kind(err, Error_Value)`, just test err == Error_Value.
2021-05-03 15:08:34 +02:00
gingerBill 357f66fcee Fix typo 2021-05-03 13:45:17 +01:00
gingerBill 050f128554 Correct hashing for union #maybe 2021-05-03 13:40:36 +01:00
gingerBill 1a3784c4df Allow unions which are comparable to also be valid map keys (i.e. hashable) 2021-05-03 13:38:15 +01:00
gingerBill 518ecaf9c9 Allow unions to be comparable if all their variants are comparable 2021-05-03 13:17:16 +01:00
gingerBill 77e2e1e1d0 Add missing skip_possible_newline_for_literal calls 2021-05-03 12:02:08 +01:00
Jeroen van Rijn 448f834b28 Remove debug print in image helper. 2021-05-03 01:23:03 +02:00
Jeroen van Rijn 9212e3176a Merge pull request #939 from Kelimion/new_png_post_processing
Don't need other path for grayscale output.
2021-05-02 21:17:00 +02:00
Jeroen van Rijn 3160a6a12c Don't need other path for grayscale output. 2021-05-02 21:11:06 +02:00
Jeroen van Rijn ef7b72d14c Merge pull request #938 from Kelimion/new_png_post_processing
Add new PNG post processing options.
2021-05-02 20:39:33 +02:00
Jeroen van Rijn 7d534769d6 Add new PNG post processing options. 2021-05-02 20:38:30 +02:00
Jeroen van Rijn 0a81fcc2af Merge pull request #937 from Kelimion/core_time
datetime_to_time's ok should default to true.
2021-05-02 13:24:33 +02:00
Jeroen van Rijn 2451014b6e datetime_to_time's ok should default to true. 2021-05-02 13:23:57 +02:00
gingerBill 348d25c43a Fix parsing error for for 2021-05-02 00:11:36 +01:00
gingerBill 9854dbe889 Remove unused import 2021-05-01 23:14:14 +01:00
Jeroen van Rijn 038337fd07 Port core:sys/win32 tests to test runner. 2021-05-02 00:10:00 +02:00
gingerBill cf0bf1a7cb Add testing.fail_now 2021-05-01 23:06:14 +01:00
gingerBill 364e6c9573 Move comment 2021-05-01 22:58:13 +01:00
gingerBill 52d38ae42b Make the core:testing runner on windows run in a separate thread to handle crashes in more safe manner 2021-05-01 22:54:27 +01:00
Jeroen van Rijn 2dbdff07c5 Merge pull request #935 from Kelimion/zlib_level_0
ZLIB level 0: LEN/NLEN = i16.
2021-05-01 21:57:36 +02:00
Jeroen van Rijn 2ad8f99790 ZLIB level 0: LEN/NLEN = i16. 2021-05-01 21:56:45 +02:00
Daniel Gavin 87a1833862 fix weird behavior of nesting proc types in structs 2021-05-01 21:26:40 +02:00
Jeroen van Rijn 0a0ba95e85 Merge pull request #934 from Kelimion/paeth
Fix Paeth for bit depth < 8.
2021-05-01 20:39:40 +02:00
Jeroen van Rijn 433d742183 Fix Paeth for bit depth < 8. 2021-05-01 20:39:00 +02:00
Daniel Gavin 9c6ab05981 fix tokenizer for ~= and better struct aligning 2021-04-29 00:51:24 +02:00
Daniel Gavin 088f4b5039 fix out of bounds with empty struct 2021-04-28 12:53:04 +02:00
Daniel Gavin 51b198aa56 Merge remote-tracking branch 'upstream/master' into prototype-fmt 2021-04-27 00:06:30 +02:00
Daniel Gavin 87bfd31664 force all enums newlined if there is more than one line 2021-04-23 23:42:53 +02:00
Daniel Gavin 5fba548aa0 more fixes with wierd comment placements 2021-04-23 23:22:48 +02:00
Daniel Gavin aafbf5bac7 odinfmt 2021-04-23 22:55:59 +02:00
Daniel Gavin 951e940470 handle comments in stupid places 2021-04-23 21:52:27 +02:00
Daniel Gavin 3b5b845ea6 refractor 2021-04-23 16:45:55 +02:00
Daniel Gavin 3f9ad6ba09 fix proc group, struct align with internal structs 2021-04-23 16:33:11 +02:00
Daniel Gavin f10f7ebbf1 Merge remote-tracking branch 'upstream/master' into prototype-fmt 2021-04-23 10:24:05 +02:00
Daniel Gavin 40ed7e48d0 remove prints 2021-04-22 00:56:31 +02:00
Daniel Gavin ab53900c95 check comma count in enum instead 2021-04-22 00:55:25 +02:00
Daniel Gavin 43589a56b7 odinfmt on printer 2021-04-22 00:32:36 +02:00
Daniel Gavin de1838c0cb align not mutable correctly 2021-04-22 00:23:17 +02:00
Daniel Gavin 9b8563dfc0 style 2021-04-22 00:08:03 +02:00
Daniel Gavin b18e8898d8 ran odinfmt on source 2021-04-22 00:07:07 +02:00
Daniel Gavin ca112c0b6d better placing of the end brace 2021-04-22 00:04:01 +02:00
Daniel Gavin 6eb64f2bdc align enum 2021-04-21 23:40:08 +02:00
Daniel Gavin 25c3b6dc95 align value decls 2021-04-21 23:21:45 +02:00
Daniel Gavin 3464784e5e add proc to format multiline 2021-04-19 19:41:53 +02:00
Daniel Gavin f1dc7c0b27 more work 2021-04-19 19:38:08 +02:00
Daniel Gavin c708f649ec Merge remote-tracking branch 'upstream/master' into prototype-fmt 2021-04-19 02:13:29 +02:00
Daniel Gavin f7b8b3a340 ensure that the comments gets pushed if it's in the beginning before package 2021-04-18 22:06:32 +02:00
Daniel Gavin 2cbb3443d3 ran odinfmt 2021-04-18 21:53:52 +02:00
Daniel Gavin 11bd518f36 fix bugs 2021-04-18 21:52:00 +02:00
Daniel Gavin a721802337 fixing calls in calls 2021-04-18 21:07:58 +02:00
Daniel Gavin 5a8c7b4f90 support multiline for and if 2021-04-16 02:15:29 +02:00
Daniel Gavin f17fc05ff2 format call and binary expression 2021-04-16 00:44:16 +02:00
Daniel Gavin a12db382e0 damn, ran the odinfmt with spaces instead of tabs... 2021-04-15 00:24:00 +02:00
Daniel Gavin 22daa50374 ran the odinfmt - looks good, except for multi line binary operations 2021-04-15 00:19:13 +02:00
Daniel Gavin a09300fb0e more fixes 2021-04-15 00:11:10 +02:00
Daniel Gavin bab4e5531a remove print 2021-04-14 21:53:11 +02:00
Daniel Gavin 1de928df78 fixed bug in parser on when 2021-04-14 18:43:04 +02:00
Daniel Gavin aded272b33 split stmts 2021-04-14 18:04:48 +02:00
Daniel Gavin b0721f1e0c aligning structs now work 2021-04-14 16:31:31 +02:00
Daniel Gavin 7e90ece84a more switch alignment work 2021-04-14 16:03:36 +02:00
Daniel Gavin 1f563f2810 work on switch alignment 2021-04-14 15:34:50 +02:00
Daniel Gavin c46317c00b fix os error 2021-04-14 02:20:05 +02:00
Daniel Gavin cb4b7efd3e only align comments in same indentation blocks 2021-04-14 01:57:42 +02:00
Daniel Gavin 411beaa3bf work on comments 2021-04-14 01:15:46 +02:00
Daniel Gavin 1cb3a31f32 have to sort attributes because they are not ordered by position(maybe bug on parser) 2021-04-14 00:53:34 +02:00
Daniel Gavin c99afd04ad work 2021-04-14 00:08:09 +02:00
Daniel Gavin 3157467e4b bring over the odinfmt code 2021-04-13 23:59:40 +02:00
Daniel Gavin 2001384ae6 make sure to print comments at the end of the file 2021-04-13 23:52:23 +02:00
Daniel Gavin b09e53d7fe now aligning comments 2021-04-13 23:42:58 +02:00
Daniel Gavin d046c9c072 at the same point as the previous formatter now 2021-04-13 20:18:50 +02:00
Daniel Gavin 2e8da35851 multiline comments work 2021-04-13 15:27:04 +02:00
Daniel Gavin 1d3458cadb single line comments work 2021-04-12 22:35:26 +02:00
Daniel Gavin 9139ca4673 more work on fmt 2021-04-12 17:01:43 +02:00
Daniel Gavin e0e6bba865 start new prototype 2021-04-11 19:47:33 +02:00
342 changed files with 27682 additions and 9734 deletions
@@ -7,6 +7,8 @@ assignees: ''
---
# PLEASE POST THIS IN THE DISCUSSION TAB UNDER "PROPOSALS" OR "IDEAS/REQUESTS"
**Is your feature request related to a problem? Please describe.**
A clear and concise description of what the problem is. Ex. I'm always frustrated when [...]
+1 -1
View File
@@ -1,4 +1,4 @@
Copyright (c) 2016-2020 Ginger Bill. All rights reserved.
Copyright (c) 2016-2021 Ginger Bill. All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
+28 -20
View File
@@ -1,5 +1,5 @@
GIT_SHA=$(shell git rev-parse --short HEAD)
DISABLED_WARNINGS=-Wno-switch -Wno-pointer-sign -Wno-tautological-constant-out-of-range-compare -Wno-tautological-compare -Wno-macro-redefined
DISABLED_WARNINGS=-Wno-switch -Wno-pointer-sign -Wno-tautological-constant-out-of-range-compare -Wno-tautological-compare -Wno-macro-redefined -Wno-unused-value
LDFLAGS=-pthread -ldl -lm -lstdc++
CFLAGS=-std=c++14 -DGIT_SHA=\"$(GIT_SHA)\"
CFLAGS:=$(CFLAGS) -DODIN_VERSION_RAW=\"dev-$(shell date +"%Y-%m")\"
@@ -8,26 +8,31 @@ CC=clang
OS=$(shell uname)
ifeq ($(OS), Darwin)
LLVM_CONFIG=llvm-config
LLVM_CONFIG=llvm-config
ifneq ($(shell llvm-config --version | grep '^11\.'),)
LLVM_CONFIG=llvm-config
else
$(error "Requirement: llvm-config must be version 11")
endif
LDFLAGS:=$(LDFLAGS) -liconv
CFLAGS:=$(CFLAGS) $(shell $(LLVM_CONFIG) --cxxflags --ldflags)
LDFLAGS:=$(LDFLAGS) -lLLVM-C
LDFLAGS:=$(LDFLAGS) -liconv
CFLAGS:=$(CFLAGS) $(shell $(LLVM_CONFIG) --cxxflags --ldflags)
LDFLAGS:=$(LDFLAGS) -lLLVM-C
endif
ifeq ($(OS), Linux)
LLVM_CONFIG=llvm-config-11
ifneq ($(shell which llvm-config-11 2>/dev/null),)
LLVM_CONFIG=llvm-config-11
else
ifneq ($(shell llvm-config --version | grep '^11\.'),)
LLVM_CONFIG=llvm-config
else
$(error "Requirement: llvm-config must be version 11")
endif
endif
LLVM_CONFIG=llvm-config-11
ifneq ($(shell which llvm-config-11 2>/dev/null),)
LLVM_CONFIG=llvm-config-11
else
ifneq ($(shell llvm-config --version | grep '^11\.'),)
LLVM_CONFIG=llvm-config
else
$(error "Requirement: llvm-config must be version 11")
endif
endif
CFLAGS:=$(CFLAGS) $(shell $(LLVM_CONFIG) --cxxflags --ldflags)
LDFLAGS:=$(LDFLAGS) $(shell $(LLVM_CONFIG) --libs core native --system-libs)
CFLAGS:=$(CFLAGS) $(shell $(LLVM_CONFIG) --cxxflags --ldflags)
LDFLAGS:=$(LDFLAGS) $(shell $(LLVM_CONFIG) --libs core native --system-libs)
endif
all: debug demo
@@ -36,13 +41,16 @@ demo:
./odin run examples/demo/demo.odin
debug:
$(CC) src/main.cpp $(DISABLED_WARNINGS) $(CFLAGS) -g $(LDFLAGS) -o odin
$(CC) src/main.cpp src/libtommath.cpp $(DISABLED_WARNINGS) $(CFLAGS) -g $(LDFLAGS) -o odin
release:
$(CC) src/main.cpp $(DISABLED_WARNINGS) $(CFLAGS) -O3 -march=native $(LDFLAGS) -o odin
$(CC) src/main.cpp src/libtommath.cpp $(DISABLED_WARNINGS) $(CFLAGS) -O3 $(LDFLAGS) -o odin
release_native:
$(CC) src/main.cpp src/libtommath.cpp $(DISABLED_WARNINGS) $(CFLAGS) -O3 -march=native $(LDFLAGS) -o odin
nightly:
$(CC) src/main.cpp $(DISABLED_WARNINGS) $(CFLAGS) -DNIGHTLY -O3 $(LDFLAGS) -o odin
$(CC) src/main.cpp src/libtommath.cpp $(DISABLED_WARNINGS) $(CFLAGS) -DNIGHTLY -O3 $(LDFLAGS) -o odin
-2
View File
@@ -100,7 +100,6 @@ In addition, the following platform-specific steps are necessary:
- Windows
* Have Visual Studio installed (MSVC 2010 or later, for the linker)
* Have a copy of `opt.exe` and `llc.exe` in `Odin/bin`. Pre-built Windows binaries can be found [here](https://github.com/odin-lang/Odin/releases/tag/llvm-windows) and *must* be explicitly copied
* Open a valid command prompt:
* **Basic:** run the `x64 Native Tools Command Prompt for VS2017` shortcut bundled with VS 2017, or
* **Advanced:** run `vcvarsall.bat x64` from a blank `cmd` session
@@ -128,7 +127,6 @@ Please read the [Getting Started Guide](https://github.com/odin-lang/Odin/wiki#g
- Windows
* x86-64/amd64
* MSVC 2010 installed (C++11 support)
* [LLVM binaries](https://github.com/odin-lang/Odin/releases/tag/llvm-windows) for `opt.exe`, `llc.exe`, and `lld-link.exe`
* Requires MSVC's link.exe as the linker
* run `vcvarsall.bat` to setup the path
+2 -2
View File
@@ -46,7 +46,7 @@ if %release_mode% EQU 0 ( rem Debug
set compiler_warnings= ^
-W4 -WX ^
-wd4100 -wd4101 -wd4127 -wd4189 ^
-wd4100 -wd4101 -wd4127 -wd4146 -wd4189 ^
-wd4201 -wd4204 ^
-wd4456 -wd4457 -wd4480 ^
-wd4512
@@ -70,7 +70,7 @@ set linker_settings=%libs% %linker_flags%
del *.pdb > NUL 2> NUL
del *.ilk > NUL 2> NUL
cl %compiler_settings% "src\main.cpp" /link %linker_settings% -OUT:%exe_name%
cl %compiler_settings% "src\main.cpp" "src\libtommath.cpp" /link %linker_settings% -OUT:%exe_name%
if %errorlevel% neq 0 goto end_of_build
if %release_mode% EQU 0 odin run examples/demo/demo.odin
+83
View File
@@ -0,0 +1,83 @@
package bufio
import "core:io"
// Loadahead_Reader provides io lookahead.
// This is useful for tokenizers/parsers.
// Loadahead_Reader is similar to bufio.Reader, but unlike bufio.Reader, Loadahead_Reader's buffer size
// will EXACTLY match the specified size, whereas bufio.Reader's buffer size may differ from the specified size.
// This makes sure that the buffer will not be accidentally read beyond the expected size.
Loadahead_Reader :: struct {
r: io.Reader,
buf: []byte,
n: int,
}
lookahead_reader_init :: proc(lr: ^Loadahead_Reader, r: io.Reader, buf: []byte) -> ^Loadahead_Reader {
lr.r = r;
lr.buf = buf;
lr.n = 0;
return lr;
}
lookahead_reader_buffer :: proc(lr: ^Loadahead_Reader) -> []byte {
return lr.buf[:lr.n];
}
// lookahead_reader_peek returns a slice of the Lookahead_Reader which holds n bytes
// If the Lookahead_Reader cannot hold enough bytes, it will read from the underlying reader to populate the rest.
// NOTE: The returned buffer is not a copy of the underlying buffer
lookahead_reader_peek :: proc(lr: ^Loadahead_Reader, n: int) -> ([]byte, io.Error) {
switch {
case n < 0:
return nil, .Negative_Read;
case n > len(lr.buf):
return nil, .Buffer_Full;
}
n := n;
err: io.Error;
read_count: int;
if lr.n < n {
read_count, err = io.read_at_least(lr.r, lr.buf[lr.n:], n-lr.n);
if err == .Unexpected_EOF {
err = .EOF;
}
}
lr.n += read_count;
if n > lr.n {
n = lr.n;
}
return lr.buf[:n], err;
}
// lookahead_reader_peek_all returns a slice of the Lookahead_Reader populating the full buffer
// If the Lookahead_Reader cannot hold enough bytes, it will read from the underlying reader to populate the rest.
// NOTE: The returned buffer is not a copy of the underlying buffer
lookahead_reader_peek_all :: proc(lr: ^Loadahead_Reader) -> ([]byte, io.Error) {
return lookahead_reader_peek(lr, len(lr.buf));
}
// lookahead_reader_consume drops the first n populated bytes from the Lookahead_Reader.
lookahead_reader_consume :: proc(lr: ^Loadahead_Reader, n: int) -> io.Error {
switch {
case n == 0:
return nil;
case n < 0:
return .Negative_Read;
case lr.n < n:
return .Short_Buffer;
}
copy(lr.buf, lr.buf[n:lr.n]);
lr.n -= n;
return nil;
}
lookahead_reader_consume_all :: proc(lr: ^Loadahead_Reader) -> io.Error {
return lookahead_reader_consume(lr, lr.n);
}
+8 -2
View File
@@ -17,6 +17,8 @@ Reader :: struct {
last_byte: int, // last byte read, invalid is -1
last_rune_size: int, // size of last rune read, invalid is -1
max_consecutive_empty_reads: int,
}
@@ -25,7 +27,7 @@ DEFAULT_BUF_SIZE :: 4096;
@(private)
MIN_READ_BUFFER_SIZE :: 16;
@(private)
MAX_CONSECUTIVE_EMPTY_READS :: 128;
DEFAULT_MAX_CONSECUTIVE_EMPTY_READS :: 128;
reader_init :: proc(b: ^Reader, rd: io.Reader, size: int = DEFAULT_BUF_SIZE, allocator := context.allocator) {
size := size;
@@ -71,8 +73,12 @@ _reader_read_new_chunk :: proc(b: ^Reader) -> io.Error {
return .Buffer_Full;
}
if b.max_consecutive_empty_reads <= 0 {
b.max_consecutive_empty_reads = DEFAULT_MAX_CONSECUTIVE_EMPTY_READS;
}
// read new data, and try a limited number of times
for i := MAX_CONSECUTIVE_EMPTY_READS; i > 0; i -= 1 {
for i := b.max_consecutive_empty_reads; i > 0; i -= 1 {
n, err := io.read(b.rd, b.buf[b.w:]);
if n < 0 {
return .Negative_Read;
+340
View File
@@ -0,0 +1,340 @@
package bufio
import "core:bytes"
import "core:io"
import "core:mem"
import "core:unicode/utf8"
import "intrinsics"
// Extra errors returns by scanning procedures
Scanner_Extra_Error :: enum i32 {
Negative_Advance,
Advanced_Too_Far,
Bad_Read_Count,
Too_Long,
Too_Short,
}
Scanner_Error :: union {
io.Error,
Scanner_Extra_Error,
}
// Split_Proc is the signature of the split procedure used to tokenize the input.
Split_Proc :: proc(data: []byte, at_eof: bool) -> (advance: int, token: []byte, err: Scanner_Error, final_token: bool);
Scanner :: struct {
r: io.Reader,
split: Split_Proc,
buf: [dynamic]byte,
max_token_size: int,
start: int,
end: int,
token: []byte,
_err: Scanner_Error,
max_consecutive_empty_reads: int,
successive_empty_token_count: int,
scan_called: bool,
done: bool,
}
DEFAULT_MAX_SCAN_TOKEN_SIZE :: 1<<16;
@(private)
_INIT_BUF_SIZE :: 4096;
scanner_init :: proc(s: ^Scanner, r: io.Reader, buf_allocator := context.allocator) -> ^Scanner {
s.r = r;
s.split = scan_lines;
s.max_token_size = DEFAULT_MAX_SCAN_TOKEN_SIZE;
s.buf.allocator = buf_allocator;
return s;
}
scanner_init_with_buffer :: proc(s: ^Scanner, r: io.Reader, buf: []byte) -> ^Scanner {
s.r = r;
s.split = scan_lines;
s.max_token_size = DEFAULT_MAX_SCAN_TOKEN_SIZE;
s.buf = mem.buffer_from_slice(buf);
resize(&s.buf, cap(s.buf));
return s;
}
scanner_destroy :: proc(s: ^Scanner) {
delete(s.buf);
}
// Returns the first non-EOF error that was encounted by the scanner
scanner_error :: proc(s: ^Scanner) -> Scanner_Error {
switch s._err {
case .EOF, .None:
return nil;
}
return s._err;
}
// Returns the most recent token created by scanner_scan.
// The underlying array may point to data that may be overwritten
// by another call to scanner_scan.
// Treat the returned value as if it is immutable.
scanner_bytes :: proc(s: ^Scanner) -> []byte {
return s.token;
}
// Returns the most recent token created by scanner_scan.
// The underlying array may point to data that may be overwritten
// by another call to scanner_scan.
// Treat the returned value as if it is immutable.
scanner_text :: proc(s: ^Scanner) -> string {
return string(s.token);
}
// scanner_scan advances the scanner
scanner_scan :: proc(s: ^Scanner) -> bool {
set_err :: proc(s: ^Scanner, err: Scanner_Error) {
err := err;
if err == .None {
err = nil;
}
switch s._err {
case nil, .EOF:
s._err = err;
}
}
if s.done {
return false;
}
s.scan_called = true;
for {
// Check if a token is possible with what is available
// Allow the split procedure to recover if it fails
if s.start < s.end || s._err != nil {
advance, token, err, final_token := s.split(s.buf[s.start:s.end], s._err != nil);
if final_token {
s.token = token;
s.done = true;
return true;
}
if err != nil {
set_err(s, err);
return false;
}
// Do advance
if advance < 0 {
set_err(s, .Negative_Advance);
return false;
}
if advance > s.end-s.start {
set_err(s, .Advanced_Too_Far);
return false;
}
s.start += advance;
s.token = token;
if s.token != nil {
if s._err == nil || advance > 0 {
s.successive_empty_token_count = 0;
} else {
s.successive_empty_token_count += 1;
if s.max_consecutive_empty_reads <= 0 {
s.max_consecutive_empty_reads = DEFAULT_MAX_CONSECUTIVE_EMPTY_READS;
}
if s.successive_empty_token_count > s.max_consecutive_empty_reads {
set_err(s, .No_Progress);
return false;
}
}
return true;
}
}
// If an error is hit, no token can be created
if s._err != nil {
s.start = 0;
s.end = 0;
return false;
}
// More data must be required to be read
if s.start > 0 && (s.end == len(s.buf) || s.start > len(s.buf)/2) {
copy(s.buf[:], s.buf[s.start:s.end]);
s.end -= s.start;
s.start = 0;
}
could_be_too_short := false;
// Resize the buffer if full
if s.end == len(s.buf) {
if s.max_token_size <= 0 {
s.max_token_size = DEFAULT_MAX_SCAN_TOKEN_SIZE;
}
if len(s.buf) >= s.max_token_size {
set_err(s, .Too_Long);
return false;
}
// overflow check
new_size := _INIT_BUF_SIZE;
if len(s.buf) > 0 {
overflowed: bool;
if new_size, overflowed = intrinsics.overflow_mul(len(s.buf), 2); overflowed {
set_err(s, .Too_Long);
return false;
}
}
old_size := len(s.buf);
new_size = min(new_size, s.max_token_size);
resize(&s.buf, new_size);
s.end -= s.start;
s.start = 0;
could_be_too_short = old_size >= len(s.buf);
}
// Read data into the buffer
loop := 0;
for {
n, err := io.read(s.r, s.buf[s.end:len(s.buf)]);
if n < 0 || len(s.buf)-s.end < n {
set_err(s, .Bad_Read_Count);
break;
}
s.end += n;
if err != nil {
set_err(s, err);
break;
}
if n > 0 {
s.successive_empty_token_count = 0;
break;
}
loop += 1;
if s.max_consecutive_empty_reads <= 0 {
s.max_consecutive_empty_reads = DEFAULT_MAX_CONSECUTIVE_EMPTY_READS;
}
if loop > s.max_consecutive_empty_reads {
if could_be_too_short {
set_err(s, .Too_Short);
} else {
set_err(s, .No_Progress);
}
break;
}
}
}
}
scan_bytes :: proc(data: []byte, at_eof: bool) -> (advance: int, token: []byte, err: Scanner_Error, final_token: bool) {
if at_eof && len(data) == 0 {
return;
}
return 1, data[0:1], nil, false;
}
scan_runes :: proc(data: []byte, at_eof: bool) -> (advance: int, token: []byte, err: Scanner_Error, final_token: bool) {
if at_eof && len(data) == 0 {
return;
}
if data[0] < utf8.RUNE_SELF {
advance = 1;
token = data[0:1];
return;
}
_, width := utf8.decode_rune(data);
if width > 1 {
advance = width;
token = data[0:width];
return;
}
if !at_eof && !utf8.full_rune(data) {
return;
}
@thread_local ERROR_RUNE := []byte{0xef, 0xbf, 0xbd};
advance = 1;
token = ERROR_RUNE;
return;
}
scan_words :: proc(data: []byte, at_eof: bool) -> (advance: int, token: []byte, err: Scanner_Error, final_token: bool) {
is_space :: proc "contextless" (r: rune) -> bool {
switch r {
// lower ones
case ' ', '\t', '\n', '\v', '\f', '\r':
return true;
case '\u0085', '\u00a0':
return true;
// higher ones
case '\u2000' ..= '\u200a':
return true;
case '\u1680', '\u2028', '\u2029', '\u202f', '\u205f', '\u3000':
return true;
}
return false;
}
// skip spaces at the beginning
start := 0;
for width := 0; start < len(data); start += width {
r: rune;
r, width = utf8.decode_rune(data[start:]);
if !is_space(r) {
break;
}
}
for width, i := 0, start; i < len(data); i += width {
r: rune;
r, width = utf8.decode_rune(data[i:]);
if is_space(r) {
advance = i+width;
token = data[start:i];
return;
}
}
if at_eof && len(data) > start {
advance = len(data);
token = data[start:];
return;
}
advance = start;
return;
}
scan_lines :: proc(data: []byte, at_eof: bool) -> (advance: int, token: []byte, err: Scanner_Error, final_token: bool) {
trim_carriage_return :: proc "contextless" (data: []byte) -> []byte {
if len(data) > 0 && data[len(data)-1] == '\r' {
return data[0:len(data)-1];
}
return data;
}
if at_eof && len(data) == 0 {
return;
}
if i := bytes.index_byte(data, '\n'); i >= 0 {
advance = i+1;
token = trim_carriage_return(data[0:i]);
return;
}
if at_eof {
advance = len(data);
token = trim_carriage_return(data);
}
return;
}
+8 -2
View File
@@ -15,6 +15,8 @@ Writer :: struct {
err: io.Error,
max_consecutive_empty_writes: int,
}
writer_init :: proc(b: ^Writer, wr: io.Writer, size: int = DEFAULT_BUF_SIZE, allocator := context.allocator) {
@@ -185,16 +187,20 @@ writer_read_from :: proc(b: ^Writer, r: io.Reader) -> (n: i64, err: io.Error) {
return n, ferr;
}
}
if b.max_consecutive_empty_writes <= 0 {
b.max_consecutive_empty_writes = DEFAULT_MAX_CONSECUTIVE_EMPTY_READS;
}
m: int;
nr := 0;
for nr < MAX_CONSECUTIVE_EMPTY_READS {
for nr < b.max_consecutive_empty_writes {
m, err = io.read(r, b.buf[b.n:]);
if m != 0 || err != nil {
break;
}
nr += 1;
}
if nr == MAX_CONSECUTIVE_EMPTY_READS {
if nr == b.max_consecutive_empty_writes {
return n, .No_Progress;
}
b.n += m;
+9 -1
View File
@@ -91,7 +91,7 @@ equal_fold :: proc(u, v: []byte) -> bool {
if tr < utf8.RUNE_SELF {
switch sr {
case 'A'..'Z':
case 'A'..='Z':
if tr == (sr+'a')-'A' {
continue loop;
}
@@ -526,6 +526,14 @@ replace :: proc(s, old, new: []byte, n: int, allocator := context.allocator) ->
return;
}
remove :: proc(s, key: []byte, n: int, allocator := context.allocator) -> (output: []byte, was_allocation: bool) {
return replace(s, key, {}, n, allocator);
}
remove_all :: proc(s, key: []byte, allocator := context.allocator) -> (output: []byte, was_allocation: bool) {
return remove(s, key, -1, allocator);
}
@(private) _ascii_space := [256]u8{'\t' = 1, '\n' = 1, '\v' = 1, '\f' = 1, '\r' = 1, ' ' = 1};
+4 -4
View File
@@ -139,18 +139,18 @@ append_token :: proc(a, b: ^Token) -> ^Token {
is_hex_digit :: proc(x: byte) -> bool {
switch x {
case '0'..'9', 'a'..'f', 'A'..'F':
case '0'..='9', 'a'..='f', 'A'..='F':
return true;
}
return false;
}
from_hex :: proc(x: byte) -> i32 {
switch x {
case '0'..'9':
case '0'..='9':
return i32(x) - '0';
case 'a'..'f':
case 'a'..='f':
return i32(x) - 'a' + 10;
case 'A'..'F':
case 'A'..='F':
return i32(x) - 'A' + 10;
}
return 16;
+4 -4
View File
@@ -5,9 +5,9 @@ import "core:unicode/utf8"
unquote_char :: proc(str: string, quote: byte) -> (r: rune, multiple_bytes: bool, tail_string: string, success: bool) {
hex_to_int :: proc(c: byte) -> int {
switch c {
case '0'..'9': return int(c-'0');
case 'a'..'f': return int(c-'a')+10;
case 'A'..'F': return int(c-'A')+10;
case '0'..='9': return int(c-'0');
case 'a'..='f': return int(c-'a')+10;
case 'A'..='F': return int(c-'A')+10;
}
return -1;
}
@@ -45,7 +45,7 @@ unquote_char :: proc(str: string, quote: byte) -> (r: rune, multiple_bytes: bool
case '"': r = '"';
case '\'': r = '\'';
case '0'..'7':
case '0'..='7':
v := int(c-'0');
if len(s) < 2 {
return;
+4 -4
View File
@@ -224,11 +224,11 @@ scan_string :: proc(t: ^Tokenizer) -> string {
digit_val :: proc(r: rune) -> int {
switch r {
case '0'..'9':
case '0'..='9':
return int(r-'0');
case 'A'..'F':
case 'A'..='F':
return int(r-'A' + 10);
case 'a'..'f':
case 'a'..='f':
return int(r-'a' + 10);
}
return 16;
@@ -245,7 +245,7 @@ scan_escape :: proc(t: ^Tokenizer) -> bool {
advance_rune(t);
return true;
case '0'..'7':
case '0'..='7':
for digit_val(t.ch) < 8 {
advance_rune(t);
}
+341 -66
View File
@@ -1,14 +1,50 @@
package compress
/*
Copyright 2021 Jeroen van Rijn <nom@duclavier.com>.
Made available under Odin's BSD-2 license.
List of contributors:
Jeroen van Rijn: Initial implementation, optimization.
*/
import "core:io"
import "core:image"
import "core:bytes"
// Error helper, e.g. is_kind(err, General_Error.OK);
is_kind :: proc(u: $U, x: $V) -> bool {
v, ok := u.(V);
return ok && v == x;
/*
These settings bound how much compression algorithms will allocate for their output buffer.
If streaming their output, these are unnecessary and will be ignored.
*/
/*
When a decompression routine doesn't stream its output, but writes to a buffer,
we pre-allocate an output buffer to speed up decompression. The default is 1 MiB.
*/
COMPRESS_OUTPUT_ALLOCATE_MIN :: int(#config(COMPRESS_OUTPUT_ALLOCATE_MIN, 1 << 20));
/*
This bounds the maximum a buffer will resize to as needed, or the maximum we'll
pre-allocate if you inform the decompression routine you know the payload size.
For reference, the largest payload size of a GZIP file is 4 GiB.
*/
when size_of(uintptr) == 8 {
/*
For 64-bit platforms, we set the default max buffer size to 4 GiB,
which is GZIP and PKZIP's max payload size.
*/
COMPRESS_OUTPUT_ALLOCATE_MAX :: int(#config(COMPRESS_OUTPUT_ALLOCATE_MAX, 1 << 32));
} else {
/*
For 32-bit platforms, we set the default max buffer size to 512 MiB.
*/
COMPRESS_OUTPUT_ALLOCATE_MAX :: int(#config(COMPRESS_OUTPUT_ALLOCATE_MAX, 1 << 29));
}
Error :: union {
General_Error,
Deflate_Error,
@@ -19,11 +55,10 @@ Error :: union {
This is here because png.load will return a this type of error union,
as it may involve an I/O error, a Deflate error, etc.
*/
image.PNG_Error,
image.Error,
}
General_Error :: enum {
OK = 0,
File_Not_Found,
Cannot_Open_File,
File_Too_Short,
@@ -33,6 +68,13 @@ General_Error :: enum {
Checksum_Failed,
Incompatible_Options,
Unimplemented,
/*
Memory errors
*/
Allocation_Failed,
Resize_Failed,
}
GZIP_Error :: enum {
@@ -43,6 +85,20 @@ GZIP_Error :: enum {
Comment_Too_Long,
Payload_Length_Invalid,
Payload_CRC_Invalid,
/*
GZIP's payload can be a maximum of max(u32le), or 4 GiB.
If you tell it you expect it to contain more, that's obviously an error.
*/
Payload_Size_Exceeds_Max_Payload,
/*
For buffered instead of streamed output, the payload size can't exceed
the max set by the `COMPRESS_OUTPUT_ALLOCATE_MAX` switch in compress/common.odin.
You can tweak this setting using `-define:COMPRESS_OUTPUT_ALLOCATE_MAX=size_in_bytes`
*/
Output_Exceeds_COMPRESS_OUTPUT_ALLOCATE_MAX,
}
ZIP_Error :: enum {
@@ -69,135 +125,354 @@ Deflate_Error :: enum {
BType_3,
}
// General context for ZLIB, LZW, etc.
Context :: struct {
code_buffer: u32,
num_bits: i8,
// General I/O context for ZLIB, LZW, etc.
Context_Memory_Input :: struct #packed {
input_data: []u8,
output: ^bytes.Buffer,
bytes_written: i64,
code_buffer: u64,
num_bits: u64,
/*
num_bits will be set to -100 if the buffer is malformed
If we know the data size, we can optimize the reads and writes.
*/
eof: b8,
size_packed: i64,
size_unpacked: i64,
}
#assert(size_of(Context_Memory_Input) == 64);
input: io.Stream,
output: io.Stream,
bytes_written: i64,
// Used to update hash as we write instead of all at once
rolling_hash: u32,
Context_Stream_Input :: struct #packed {
input_data: []u8,
input: io.Stream,
output: ^bytes.Buffer,
bytes_written: i64,
// Sliding window buffer. Size must be a power of two.
window_size: i64,
last: ^[dynamic]byte,
code_buffer: u64,
num_bits: u64,
/*
If we know the data size, we can optimize the reads and writes.
*/
size_packed: i64,
size_unpacked: i64,
/*
Flags:
`input_fully_in_memory`
true = This tells us we read input from `input_data` exclusively. [] = EOF.
false = Try to refill `input_data` from the `input` stream.
*/
input_fully_in_memory: b8,
padding: [1]u8,
}
// Stream helpers
/*
TODO: These need to be optimized.
Streams should really only check if a certain method is available once, perhaps even during setup.
TODO: The stream versions should really only check if a certain method is available once, perhaps even during setup.
Bit and byte readers may be merged so that reading bytes will grab them from the bit buffer first.
This simplifies end-of-stream handling where bits may be left in the bit buffer.
*/
read_data :: #force_inline proc(c: ^Context, $T: typeid) -> (res: T, err: io.Error) {
b := make([]u8, size_of(T), context.temp_allocator);
r, e1 := io.to_reader(c.input);
_, e2 := io.read(r, b);
if !e1 || e2 != .None {
return T{}, e2;
// TODO: Make these return compress.Error errors.
input_size_from_memory :: proc(z: ^Context_Memory_Input) -> (res: i64, err: Error) {
return i64(len(z.input_data)), nil;
}
input_size_from_stream :: proc(z: ^Context_Stream_Input) -> (res: i64, err: Error) {
return io.size(z.input), nil;
}
input_size :: proc{input_size_from_memory, input_size_from_stream};
@(optimization_mode="speed")
read_slice_from_memory :: #force_inline proc(z: ^Context_Memory_Input, size: int) -> (res: []u8, err: io.Error) {
#no_bounds_check {
if len(z.input_data) >= size {
res = z.input_data[:size];
z.input_data = z.input_data[size:];
return res, .None;
}
}
res = (^T)(raw_data(b))^;
return res, .None;
if len(z.input_data) == 0 {
return []u8{}, .EOF;
} else {
return []u8{}, .Short_Buffer;
}
}
read_u8 :: #force_inline proc(z: ^Context) -> (res: u8, err: io.Error) {
return read_data(z, u8);
@(optimization_mode="speed")
read_slice_from_stream :: #force_inline proc(z: ^Context_Stream_Input, size: int) -> (res: []u8, err: io.Error) {
b := make([]u8, size, context.temp_allocator);
_, e := z.input->impl_read(b[:]);
if e == .None {
return b, .None;
}
return []u8{}, e;
}
peek_data :: #force_inline proc(c: ^Context, $T: typeid) -> (res: T, err: io.Error) {
read_slice :: proc{read_slice_from_memory, read_slice_from_stream};
@(optimization_mode="speed")
read_data :: #force_inline proc(z: ^$C, $T: typeid) -> (res: T, err: io.Error) {
b, e := read_slice(z, size_of(T));
if e == .None {
return (^T)(&b[0])^, .None;
}
return T{}, e;
}
@(optimization_mode="speed")
read_u8_from_memory :: #force_inline proc(z: ^Context_Memory_Input) -> (res: u8, err: io.Error) {
#no_bounds_check {
if len(z.input_data) >= 1 {
res = z.input_data[0];
z.input_data = z.input_data[1:];
return res, .None;
}
}
return 0, .EOF;
}
@(optimization_mode="speed")
read_u8_from_stream :: #force_inline proc(z: ^Context_Stream_Input) -> (res: u8, err: io.Error) {
b, e := read_slice_from_stream(z, 1);
if e == .None {
return b[0], .None;
}
return 0, e;
}
read_u8 :: proc{read_u8_from_memory, read_u8_from_stream};
/*
You would typically only use this at the end of Inflate, to drain bits from the code buffer
preferentially.
*/
@(optimization_mode="speed")
read_u8_prefer_code_buffer_lsb :: #force_inline proc(z: ^$C) -> (res: u8, err: io.Error) {
if z.num_bits >= 8 {
res = u8(read_bits_no_refill_lsb(z, 8));
} else {
size, _ := input_size(z);
if size > 0 {
res, err = read_u8(z);
} else {
err = .EOF;
}
}
return;
}
@(optimization_mode="speed")
peek_data_from_memory :: #force_inline proc(z: ^Context_Memory_Input, $T: typeid) -> (res: T, err: io.Error) {
size :: size_of(T);
#no_bounds_check {
if len(z.input_data) >= size {
buf := z.input_data[:size];
return (^T)(&buf[0])^, .None;
}
}
if len(z.input_data) == 0 {
return T{}, .EOF;
} else {
return T{}, .Short_Buffer;
}
}
@(optimization_mode="speed")
peek_data_from_stream :: #force_inline proc(z: ^Context_Stream_Input, $T: typeid) -> (res: T, err: io.Error) {
size :: size_of(T);
// Get current position to read from.
curr, e1 := c.input->impl_seek(0, .Current);
curr, e1 := z.input->impl_seek(0, .Current);
if e1 != .None {
return T{}, e1;
}
r, e2 := io.to_reader_at(c.input);
r, e2 := io.to_reader_at(z.input);
if !e2 {
return T{}, .Empty;
}
b := make([]u8, size_of(T), context.temp_allocator);
_, e3 := io.read_at(r, b, curr);
when size <= 128 {
b: [size]u8;
} else {
b := make([]u8, size, context.temp_allocator);
}
_, e3 := io.read_at(r, b[:], curr);
if e3 != .None {
return T{}, .Empty;
}
res = (^T)(raw_data(b))^;
res = (^T)(&b[0])^;
return res, .None;
}
peek_data :: proc{peek_data_from_memory, peek_data_from_stream};
// Sliding window read back
peek_back_byte :: proc(c: ^Context, offset: i64) -> (res: u8, err: io.Error) {
@(optimization_mode="speed")
peek_back_byte :: #force_inline proc(z: ^$C, offset: i64) -> (res: u8, err: io.Error) {
// Look back into the sliding window.
return c.last[offset % c.window_size], .None;
return z.output.buf[z.bytes_written - offset], .None;
}
// Generalized bit reader LSB
refill_lsb :: proc(z: ^Context, width := i8(24)) {
@(optimization_mode="speed")
refill_lsb_from_memory :: #force_inline proc(z: ^Context_Memory_Input, width := i8(48)) {
refill := u64(width);
b := u64(0);
if z.num_bits > refill {
return;
}
for {
if z.num_bits > width {
if len(z.input_data) != 0 {
b = u64(z.input_data[0]);
z.input_data = z.input_data[1:];
} else {
b = 0;
}
z.code_buffer |= b << u8(z.num_bits);
z.num_bits += 8;
if z.num_bits > refill {
break;
}
if z.code_buffer == 0 && z.num_bits == -1 {
}
}
// Generalized bit reader LSB
@(optimization_mode="speed")
refill_lsb_from_stream :: proc(z: ^Context_Stream_Input, width := i8(24)) {
refill := u64(width);
for {
if z.num_bits > refill {
break;
}
if z.code_buffer == 0 && z.num_bits > 63 {
z.num_bits = 0;
}
if z.code_buffer >= 1 << uint(z.num_bits) {
// Code buffer is malformed.
z.num_bits = -100;
return;
}
c, err := read_u8(z);
if err != .None {
// This is fine at the end of the file.
z.num_bits = -42;
z.eof = true;
z.num_bits = max(u64);
return;
}
z.code_buffer |= (u32(c) << u8(z.num_bits));
b, err := read_u8(z);
if err != .None {
// This is fine at the end of the file.
return;
}
z.code_buffer |= (u64(b) << u8(z.num_bits));
z.num_bits += 8;
}
}
consume_bits_lsb :: #force_inline proc(z: ^Context, width: u8) {
refill_lsb :: proc{refill_lsb_from_memory, refill_lsb_from_stream};
@(optimization_mode="speed")
consume_bits_lsb_from_memory :: #force_inline proc(z: ^Context_Memory_Input, width: u8) {
z.code_buffer >>= width;
z.num_bits -= i8(width);
z.num_bits -= u64(width);
}
peek_bits_lsb :: #force_inline proc(z: ^Context, width: u8) -> u32 {
if z.num_bits < i8(width) {
@(optimization_mode="speed")
consume_bits_lsb_from_stream :: #force_inline proc(z: ^Context_Stream_Input, width: u8) {
z.code_buffer >>= width;
z.num_bits -= u64(width);
}
consume_bits_lsb :: proc{consume_bits_lsb_from_memory, consume_bits_lsb_from_stream};
@(optimization_mode="speed")
peek_bits_lsb_from_memory :: #force_inline proc(z: ^Context_Memory_Input, width: u8) -> u32 {
if z.num_bits < u64(width) {
refill_lsb(z);
}
// assert(z.num_bits >= i8(width));
return z.code_buffer & ~(~u32(0) << width);
return u32(z.code_buffer & ~(~u64(0) << width));
}
peek_bits_no_refill_lsb :: #force_inline proc(z: ^Context, width: u8) -> u32 {
assert(z.num_bits >= i8(width));
return z.code_buffer & ~(~u32(0) << width);
@(optimization_mode="speed")
peek_bits_lsb_from_stream :: #force_inline proc(z: ^Context_Stream_Input, width: u8) -> u32 {
if z.num_bits < u64(width) {
refill_lsb(z);
}
return u32(z.code_buffer & ~(~u64(0) << width));
}
read_bits_lsb :: #force_inline proc(z: ^Context, width: u8) -> u32 {
peek_bits_lsb :: proc{peek_bits_lsb_from_memory, peek_bits_lsb_from_stream};
@(optimization_mode="speed")
peek_bits_no_refill_lsb_from_memory :: #force_inline proc(z: ^Context_Memory_Input, width: u8) -> u32 {
assert(z.num_bits >= u64(width));
return u32(z.code_buffer & ~(~u64(0) << width));
}
@(optimization_mode="speed")
peek_bits_no_refill_lsb_from_stream :: #force_inline proc(z: ^Context_Stream_Input, width: u8) -> u32 {
assert(z.num_bits >= u64(width));
return u32(z.code_buffer & ~(~u64(0) << width));
}
peek_bits_no_refill_lsb :: proc{peek_bits_no_refill_lsb_from_memory, peek_bits_no_refill_lsb_from_stream};
@(optimization_mode="speed")
read_bits_lsb_from_memory :: #force_inline proc(z: ^Context_Memory_Input, width: u8) -> u32 {
k := #force_inline peek_bits_lsb(z, width);
#force_inline consume_bits_lsb(z, width);
return k;
}
@(optimization_mode="speed")
read_bits_lsb_from_stream :: #force_inline proc(z: ^Context_Stream_Input, width: u8) -> u32 {
k := peek_bits_lsb(z, width);
consume_bits_lsb(z, width);
return k;
}
read_bits_no_refill_lsb :: #force_inline proc(z: ^Context, width: u8) -> u32 {
read_bits_lsb :: proc{read_bits_lsb_from_memory, read_bits_lsb_from_stream};
@(optimization_mode="speed")
read_bits_no_refill_lsb_from_memory :: #force_inline proc(z: ^Context_Memory_Input, width: u8) -> u32 {
k := #force_inline peek_bits_no_refill_lsb(z, width);
#force_inline consume_bits_lsb(z, width);
return k;
}
@(optimization_mode="speed")
read_bits_no_refill_lsb_from_stream :: #force_inline proc(z: ^Context_Stream_Input, width: u8) -> u32 {
k := peek_bits_no_refill_lsb(z, width);
consume_bits_lsb(z, width);
return k;
}
discard_to_next_byte_lsb :: proc(z: ^Context) {
read_bits_no_refill_lsb :: proc{read_bits_no_refill_lsb_from_memory, read_bits_no_refill_lsb_from_stream};
@(optimization_mode="speed")
discard_to_next_byte_lsb_from_memory :: proc(z: ^Context_Memory_Input) {
discard := u8(z.num_bits & 7);
#force_inline consume_bits_lsb(z, discard);
}
@(optimization_mode="speed")
discard_to_next_byte_lsb_from_stream :: proc(z: ^Context_Stream_Input) {
discard := u8(z.num_bits & 7);
consume_bits_lsb(z, discard);
}
discard_to_next_byte_lsb :: proc{discard_to_next_byte_lsb_from_memory, discard_to_next_byte_lsb_from_stream};
+30 -11
View File
@@ -1,9 +1,21 @@
//+ignore
package gzip
import "core:compress/gzip"
/*
Copyright 2021 Jeroen van Rijn <nom@duclavier.com>.
Made available under Odin's BSD-2 license.
List of contributors:
Jeroen van Rijn: Initial implementation.
Ginger Bill: Cosmetic changes.
A small GZIP implementation as an example.
*/
import "core:bytes"
import "core:os"
import "core:compress"
import "core:fmt"
// Small GZIP file with fextra, fname and fcomment present.
@private
@@ -20,8 +32,7 @@ TEST: []u8 = {
main :: proc() {
// Set up output buffer.
buf: bytes.Buffer;
defer bytes.buffer_destroy(&buf);
buf := bytes.Buffer{};
stdout :: proc(s: string) {
os.write_string(os.stdout, s);
@@ -34,37 +45,45 @@ main :: proc() {
if len(args) < 2 {
stderr("No input file specified.\n");
err := gzip.load(TEST, &buf);
if gzip.is_kind(err, gzip.E_General.OK) {
err := load(slice=TEST, buf=&buf, known_gzip_size=len(TEST));
if err == nil {
stdout("Displaying test vector: ");
stdout(bytes.buffer_to_string(&buf));
stdout("\n");
} else {
fmt.printf("gzip.load returned %v\n", err);
}
bytes.buffer_destroy(&buf);
os.exit(0);
}
// The rest are all files.
args = args[1:];
err: gzip.Error;
err: Error;
for file in args {
if file == "-" {
// Read from stdin
s := os.stream_from_handle(os.stdin);
err = gzip.load(s, &buf);
ctx := &compress.Context_Stream_Input{
input = s,
};
err = load(ctx, &buf);
} else {
err = gzip.load(file, &buf);
err = load(file, &buf);
}
if !gzip.is_kind(err, gzip.E_General.OK) {
if gzip.is_kind(err, gzip.E_General.File_Not_Found) {
if err != nil {
if err != E_General.File_Not_Found {
stderr("File not found: ");
stderr(file);
stderr("\n");
os.exit(1);
}
stderr("GZIP returned an error.\n");
bytes.buffer_destroy(&buf);
os.exit(2);
}
stdout(bytes.buffer_to_string(&buf));
}
os.exit(0);
bytes.buffer_destroy(&buf);
}
+114 -61
View File
@@ -1,5 +1,19 @@
package gzip
/*
Copyright 2021 Jeroen van Rijn <nom@duclavier.com>.
Made available under Odin's BSD-2 license.
List of contributors:
Jeroen van Rijn: Initial implementation.
This package implements support for the GZIP file format v4.3,
as specified in RFC 1952.
It is implemented in such a way that it lends itself naturally
to be the input to a complementary TAR implementation.
*/
import "core:compress/zlib"
import "core:compress"
import "core:os"
@@ -7,16 +21,6 @@ import "core:io"
import "core:bytes"
import "core:hash"
/*
This package implements support for the GZIP file format v4.3,
as specified in RFC 1952.
It is implemented in such a way that it lends itself naturally
to be the input to a complementary TAR implementation.
*/
Magic :: enum u16le {
GZIP = 0x8b << 8 | 0x1f,
}
@@ -94,42 +98,55 @@ E_General :: compress.General_Error;
E_GZIP :: compress.GZIP_Error;
E_ZLIB :: compress.ZLIB_Error;
E_Deflate :: compress.Deflate_Error;
is_kind :: compress.is_kind;
load_from_slice :: proc(slice: []u8, buf: ^bytes.Buffer, allocator := context.allocator) -> (err: Error) {
GZIP_MAX_PAYLOAD_SIZE :: int(max(u32le));
r := bytes.Reader{};
bytes.reader_init(&r, slice);
stream := bytes.reader_to_stream(&r);
load :: proc{load_from_slice, load_from_file, load_from_context};
err = load_from_stream(stream, buf, allocator);
return err;
}
load_from_file :: proc(filename: string, buf: ^bytes.Buffer, allocator := context.allocator) -> (err: Error) {
load_from_file :: proc(filename: string, buf: ^bytes.Buffer, expected_output_size := -1, allocator := context.allocator) -> (err: Error) {
data, ok := os.read_entire_file(filename, allocator);
defer delete(data);
err = E_General.File_Not_Found;
if ok {
err = load_from_slice(data, buf, allocator);
err = load_from_slice(data, buf, len(data), expected_output_size, allocator);
}
return;
}
load_from_stream :: proc(stream: io.Stream, buf: ^bytes.Buffer, allocator := context.allocator) -> (err: Error) {
ctx := compress.Context{
input = stream,
};
load_from_slice :: proc(slice: []u8, buf: ^bytes.Buffer, known_gzip_size := -1, expected_output_size := -1, allocator := context.allocator) -> (err: Error) {
buf := buf;
ws := bytes.buffer_to_stream(buf);
ctx.output = ws;
header, e := compress.read_data(&ctx, Header);
z := &compress.Context_Memory_Input{
input_data = slice,
output = buf,
};
return load_from_context(z, buf, known_gzip_size, expected_output_size, allocator);
}
load_from_context :: proc(z: ^$C, buf: ^bytes.Buffer, known_gzip_size := -1, expected_output_size := -1, allocator := context.allocator) -> (err: Error) {
buf := buf;
expected_output_size := expected_output_size;
input_data_consumed := 0;
z.output = buf;
if expected_output_size > GZIP_MAX_PAYLOAD_SIZE {
return E_GZIP.Payload_Size_Exceeds_Max_Payload;
}
if expected_output_size > compress.COMPRESS_OUTPUT_ALLOCATE_MAX {
return E_GZIP.Output_Exceeds_COMPRESS_OUTPUT_ALLOCATE_MAX;
}
b: []u8;
header, e := compress.read_data(z, Header);
if e != .None {
return E_General.File_Too_Short;
}
input_data_consumed += size_of(Header);
if header.magic != .GZIP {
return E_GZIP.Invalid_GZIP_Signature;
@@ -154,7 +171,9 @@ load_from_stream :: proc(stream: io.Stream, buf: ^bytes.Buffer, allocator := con
// printf("os: %v\n", OS_Name[header.os]);
if .extra in header.flags {
xlen, e_extra := compress.read_data(&ctx, u16le);
xlen, e_extra := compress.read_data(z, u16le);
input_data_consumed += 2;
if e_extra != .None {
return E_General.Stream_Too_Short;
}
@@ -170,19 +189,21 @@ load_from_stream :: proc(stream: io.Stream, buf: ^bytes.Buffer, allocator := con
for xlen >= 4 {
// println("Parsing Extra field(s).");
field_id, field_error = compress.read_data(&ctx, [2]u8);
field_id, field_error = compress.read_data(z, [2]u8);
if field_error != .None {
// printf("Parsing Extra returned: %v\n", field_error);
return E_General.Stream_Too_Short;
}
xlen -= 2;
input_data_consumed += 2;
field_length, field_error = compress.read_data(&ctx, u16le);
field_length, field_error = compress.read_data(z, u16le);
if field_error != .None {
// printf("Parsing Extra returned: %v\n", field_error);
return E_General.Stream_Too_Short;
}
xlen -= 2;
input_data_consumed += 2;
if xlen <= 0 {
// We're not going to try and recover by scanning for a ZLIB header.
@@ -192,16 +213,16 @@ load_from_stream :: proc(stream: io.Stream, buf: ^bytes.Buffer, allocator := con
// printf(" Field \"%v\" of length %v found: ", string(field_id[:]), field_length);
if field_length > 0 {
field_data := make([]u8, field_length, context.temp_allocator);
_, field_error = ctx.input->impl_read(field_data);
b, field_error = compress.read_slice(z, int(field_length));
if field_error != .None {
// printf("Parsing Extra returned: %v\n", field_error);
return E_General.Stream_Too_Short;
}
xlen -= field_length;
input_data_consumed += int(field_length);
// printf("%v\n", string(field_data));
}
}
if xlen != 0 {
return E_GZIP.Invalid_Extra_Data;
@@ -212,16 +233,16 @@ load_from_stream :: proc(stream: io.Stream, buf: ^bytes.Buffer, allocator := con
if .name in header.flags {
// Should be enough.
name: [1024]u8;
b: [1]u8;
i := 0;
name_error: io.Error;
for i < len(name) {
_, name_error = ctx.input->impl_read(b[:]);
b, name_error = compress.read_slice(z, 1);
if name_error != .None {
return E_General.Stream_Too_Short;
}
if b == 0 {
input_data_consumed += 1;
if b[0] == 0 {
break;
}
name[i] = b[0];
@@ -236,16 +257,16 @@ load_from_stream :: proc(stream: io.Stream, buf: ^bytes.Buffer, allocator := con
if .comment in header.flags {
// Should be enough.
comment: [1024]u8;
b: [1]u8;
i := 0;
comment_error: io.Error;
for i < len(comment) {
_, comment_error = ctx.input->impl_read(b[:]);
b, comment_error = compress.read_slice(z, 1);
if comment_error != .None {
return E_General.Stream_Too_Short;
}
if b == 0 {
input_data_consumed += 1;
if b[0] == 0 {
break;
}
comment[i] = b[0];
@@ -258,9 +279,9 @@ load_from_stream :: proc(stream: io.Stream, buf: ^bytes.Buffer, allocator := con
}
if .header_crc in header.flags {
crc16: [2]u8;
crc_error: io.Error;
_, crc_error = ctx.input->impl_read(crc16[:]);
_, crc_error = compress.read_slice(z, 2);
input_data_consumed += 2;
if crc_error != .None {
return E_General.Stream_Too_Short;
}
@@ -273,42 +294,74 @@ load_from_stream :: proc(stream: io.Stream, buf: ^bytes.Buffer, allocator := con
/*
We should have arrived at the ZLIB payload.
*/
payload_u32le: u32le;
zlib_error := zlib.inflate_raw(&ctx);
// fmt.printf("known_gzip_size: %v | expected_output_size: %v\n", known_gzip_size, expected_output_size);
// fmt.printf("ZLIB returned: %v\n", zlib_error);
if expected_output_size > -1 {
/*
We already checked that it's not larger than the output buffer max,
or GZIP length field's max.
if !is_kind(zlib_error, E_General.OK) || zlib_error == nil {
return zlib_error;
We'll just pass it on to `zlib.inflate_raw`;
*/
} else {
/*
If we know the size of the GZIP file *and* it is fully in memory,
then we can peek at the unpacked size at the end.
We'll still want to ensure there's capacity left in the output buffer when we write, of course.
*/
if known_gzip_size > -1 {
offset := i64(known_gzip_size - input_data_consumed - 4);
size, _ := compress.input_size(z);
if size >= offset + 4 {
length_bytes := z.input_data[offset:][:4];
payload_u32le = (^u32le)(&length_bytes[0])^;
expected_output_size = int(payload_u32le);
}
} else {
/*
TODO(Jeroen): When reading a GZIP from a stream, check if impl_seek is present.
If so, we can seek to the end, grab the size from the footer, and seek back to payload start.
*/
}
}
// fmt.printf("GZIP: Expected Payload Size: %v\n", expected_output_size);
zlib_error := zlib.inflate_raw(z=z, expected_output_size=expected_output_size);
if zlib_error != nil {
return zlib_error;
}
/*
Read CRC32 using the ctx bit reader because zlib may leave bytes in there.
*/
compress.discard_to_next_byte_lsb(&ctx);
compress.discard_to_next_byte_lsb(z);
footer_error: io.Error;
payload_crc_b: [4]u8;
payload_len_b: [4]u8;
for i in 0..3 {
payload_crc_b[i] = u8(compress.read_bits_lsb(&ctx, 8));
for _, i in payload_crc_b {
payload_crc_b[i], footer_error = compress.read_u8_prefer_code_buffer_lsb(z);
}
payload_crc := transmute(u32le)payload_crc_b;
for i in 0..3 {
payload_len_b[i] = u8(compress.read_bits_lsb(&ctx, 8));
}
payload_len := int(transmute(u32le)payload_len_b);
payload := bytes.buffer_to_bytes(buf);
crc32 := u32le(hash.crc32(payload));
crc32 := u32le(hash.crc32(payload));
if crc32 != payload_crc {
return E_GZIP.Payload_CRC_Invalid;
}
if len(payload) != payload_len {
payload_len_b: [4]u8;
for _, i in payload_len_b {
payload_len_b[i], footer_error = compress.read_u8_prefer_code_buffer_lsb(z);
}
payload_len := transmute(u32le)payload_len_b;
if len(payload) != int(payload_len) {
return E_GZIP.Payload_Length_Invalid;
}
return E_General.OK;
return nil;
}
load :: proc{load_from_file, load_from_slice, load_from_stream};
+14 -4
View File
@@ -1,7 +1,16 @@
//+ignore
package zlib
import "core:compress/zlib"
/*
Copyright 2021 Jeroen van Rijn <nom@duclavier.com>.
Made available under Odin's BSD-2 license.
List of contributors:
Jeroen van Rijn: Initial implementation.
An example of how to use `zlib.inflate`.
*/
import "core:bytes"
import "core:fmt"
@@ -26,17 +35,18 @@ main :: proc() {
171, 15, 18, 59, 138, 112, 63, 23, 205, 110, 254, 136, 109, 78, 231,
63, 234, 138, 133, 204,
};
OUTPUT_SIZE :: 438;
buf: bytes.Buffer;
// We can pass ", true" to inflate a raw DEFLATE stream instead of a ZLIB wrapped one.
err := zlib.inflate(ODIN_DEMO, &buf);
err := inflate(input=ODIN_DEMO, buf=&buf, expected_output_size=OUTPUT_SIZE);
defer bytes.buffer_destroy(&buf);
if !zlib.is_kind(err, zlib.E_General.OK) {
if err != nil {
fmt.printf("\nError: %v\n", err);
}
s := bytes.buffer_to_string(&buf);
fmt.printf("Input: %v bytes, output (%v bytes):\n%v\n", len(ODIN_DEMO), len(s), s);
assert(len(s) == 438);
assert(len(s) == OUTPUT_SIZE);
}
+232 -120
View File
@@ -1,17 +1,34 @@
package zlib
/*
Copyright 2021 Jeroen van Rijn <nom@duclavier.com>.
Made available under Odin's BSD-2 license.
List of contributors:
Jeroen van Rijn: Initial implementation, optimization.
Ginger Bill: Cosmetic changes.
*/
import "core:compress"
import "core:mem"
import "core:io"
import "core:bytes"
import "core:hash"
import "core:bytes"
/*
zlib.inflate decompresses a ZLIB stream passed in as a []u8 or io.Stream.
Returns: Error. You can use zlib.is_kind or compress.is_kind to easily test for OK.
Returns: Error.
*/
Context :: compress.Context;
/*
Do we do Adler32 as we write bytes to output?
It used to be faster to do it inline, now it's faster to do it at the end of `inflate`.
We'll see what's faster after more optimization, and might end up removing
`Context.rolling_hash` if not inlining it is still faster.
*/
Compression_Method :: enum u8 {
DEFLATE = 8,
@@ -34,7 +51,6 @@ Error :: compress.Error;
E_General :: compress.General_Error;
E_ZLIB :: compress.ZLIB_Error;
E_Deflate :: compress.Deflate_Error;
is_kind :: compress.is_kind;
DEFLATE_MAX_CHUNK_SIZE :: 65535;
DEFLATE_MAX_LITERAL_SIZE :: 65535;
@@ -103,7 +119,7 @@ Huffman_Table :: struct {
};
// Implementation starts here
@(optimization_mode="speed")
z_bit_reverse :: #force_inline proc(n: u16, bits: u8) -> (r: u16) {
assert(bits <= 16);
// NOTE: Can optimize with llvm.bitreverse.i64 or some bit twiddling
@@ -118,27 +134,114 @@ z_bit_reverse :: #force_inline proc(n: u16, bits: u8) -> (r: u16) {
return;
}
write_byte :: #force_inline proc(z: ^Context, c: u8) -> (err: io.Error) #no_bounds_check {
c := c;
buf := transmute([]u8)mem.Raw_Slice{data=&c, len=1};
z.rolling_hash = hash.adler32(buf, z.rolling_hash);
_, e := z.output->impl_write(buf);
if e != .None {
return e;
@(optimization_mode="speed")
grow_buffer :: proc(buf: ^[dynamic]u8) -> (err: compress.Error) {
/*
That we get here at all means that we didn't pass an expected output size,
or that it was too little.
*/
/*
Double until we reach the maximum allowed.
*/
new_size := min(len(buf) << 1, compress.COMPRESS_OUTPUT_ALLOCATE_MAX);
resize(buf, new_size);
if len(buf) != new_size {
/*
Resize failed.
*/
return .Resize_Failed;
}
z.last[z.bytes_written % z.window_size] = c;
return nil;
}
/*
TODO: Make these return compress.Error.
*/
@(optimization_mode="speed")
write_byte :: #force_inline proc(z: ^$C, c: u8) -> (err: io.Error) #no_bounds_check {
/*
Resize if needed.
*/
if int(z.bytes_written) + 1 >= len(z.output.buf) {
e := grow_buffer(&z.output.buf);
if e != nil {
return .Short_Write;
}
}
#no_bounds_check {
z.output.buf[z.bytes_written] = c;
}
z.bytes_written += 1;
return .None;
}
allocate_huffman_table :: proc(allocator := context.allocator) -> (z: ^Huffman_Table, err: Error) {
@(optimization_mode="speed")
repl_byte :: proc(z: ^$C, count: u16, c: u8) -> (err: io.Error) #no_bounds_check {
/*
TODO(Jeroen): Once we have a magic ring buffer, we can just peek/write into it
without having to worry about wrapping, so no need for a temp allocation to give to
the output stream, just give it _that_ slice.
*/
z = new(Huffman_Table, allocator);
return z, E_General.OK;
/*
Resize if needed.
*/
if int(z.bytes_written) + int(count) >= len(z.output.buf) {
e := grow_buffer(&z.output.buf);
if e != nil {
return .Short_Write;
}
}
#no_bounds_check {
for _ in 0..<count {
z.output.buf[z.bytes_written] = c;
z.bytes_written += 1;
}
}
return .None;
}
@(optimization_mode="speed")
repl_bytes :: proc(z: ^$C, count: u16, distance: u16) -> (err: io.Error) {
/*
TODO(Jeroen): Once we have a magic ring buffer, we can just peek/write into it
without having to worry about wrapping, so no need for a temp allocation to give to
the output stream, just give it _that_ slice.
*/
offset := i64(distance);
if int(z.bytes_written) + int(count) >= len(z.output.buf) {
e := grow_buffer(&z.output.buf);
if e != nil {
return .Short_Write;
}
}
#no_bounds_check {
for _ in 0..<count {
c := z.output.buf[z.bytes_written - offset];
z.output.buf[z.bytes_written] = c;
z.bytes_written += 1;
}
}
return .None;
}
allocate_huffman_table :: proc(allocator := context.allocator) -> (z: ^Huffman_Table, err: Error) {
return new(Huffman_Table, allocator), nil;
}
@(optimization_mode="speed")
build_huffman :: proc(z: ^Huffman_Table, code_lengths: []u8) -> (err: Error) {
sizes: [HUFFMAN_MAX_BITS+1]int;
next_code: [HUFFMAN_MAX_BITS]int;
@@ -148,34 +251,34 @@ build_huffman :: proc(z: ^Huffman_Table, code_lengths: []u8) -> (err: Error) {
mem.zero_slice(sizes[:]);
mem.zero_slice(z.fast[:]);
for v, _ in code_lengths {
for v in code_lengths {
sizes[v] += 1;
}
sizes[0] = 0;
for i in 1..16 {
for i in 1..<(HUFFMAN_MAX_BITS+1) {
if sizes[i] > (1 << uint(i)) {
return E_Deflate.Huffman_Bad_Sizes;
}
}
code := int(0);
for i in 1..<16 {
for i in 1..<HUFFMAN_MAX_BITS {
next_code[i] = code;
z.firstcode[i] = u16(code);
z.firstsymbol[i] = u16(k);
code = code + sizes[i];
if sizes[i] != 0 {
if (code - 1 >= (1 << u16(i))) {
if code - 1 >= (1 << u16(i)) {
return E_Deflate.Huffman_Bad_Code_Lengths;
}
}
z.maxcode[i] = code << (16 - uint(i));
z.maxcode[i] = code << (HUFFMAN_MAX_BITS - uint(i));
code <<= 1;
k += int(sizes[i]);
}
z.maxcode[16] = 0x10000; // Sentinel
z.maxcode[HUFFMAN_MAX_BITS] = 0x10000; // Sentinel
c: int;
for v, ci in code_lengths {
@@ -184,7 +287,7 @@ build_huffman :: proc(z: ^Huffman_Table, code_lengths: []u8) -> (err: Error) {
fastv := u16((u16(v) << 9) | u16(ci));
z.size[c] = u8(v);
z.value[c] = u16(ci);
if (v <= ZFAST_BITS) {
if v <= ZFAST_BITS {
j := z_bit_reverse(u16(next_code[v]), v);
for j < (1 << ZFAST_BITS) {
z.fast[j] = fastv;
@@ -194,28 +297,23 @@ build_huffman :: proc(z: ^Huffman_Table, code_lengths: []u8) -> (err: Error) {
next_code[v] += 1;
}
}
return E_General.OK;
return nil;
}
decode_huffman_slowpath :: proc(z: ^Context, t: ^Huffman_Table) -> (r: u16, err: Error) #no_bounds_check {
@(optimization_mode="speed")
decode_huffman_slowpath :: proc(z: ^$C, t: ^Huffman_Table) -> (r: u16, err: Error) #no_bounds_check {
code := u16(compress.peek_bits_lsb(z,16));
r = 0;
err = E_General.OK;
k: int;
k := int(z_bit_reverse(code, 16));
s: u8;
code := u16(compress.peek_bits_lsb(z, 16));
k = int(z_bit_reverse(code, 16));
#no_bounds_check for s = HUFFMAN_FAST_BITS+1; ; {
if k < t.maxcode[s] {
break;
}
s += 1;
}
if (s >= 16) {
if s >= 16 {
return 0, E_Deflate.Bad_Huffman_Code;
}
// code size is s, so:
@@ -230,17 +328,17 @@ decode_huffman_slowpath :: proc(z: ^Context, t: ^Huffman_Table) -> (r: u16, err:
compress.consume_bits_lsb(z, s);
r = t.value[b];
return r, E_General.OK;
return r, nil;
}
decode_huffman :: proc(z: ^Context, t: ^Huffman_Table) -> (r: u16, err: Error) #no_bounds_check {
@(optimization_mode="speed")
decode_huffman :: proc(z: ^$C, t: ^Huffman_Table) -> (r: u16, err: Error) #no_bounds_check {
if z.num_bits < 16 {
if z.num_bits == -100 {
if z.num_bits > 63 {
return 0, E_ZLIB.Code_Buffer_Malformed;
}
compress.refill_lsb(z);
if z.eof {
if z.num_bits > 63 {
return 0, E_General.Stream_Too_Short;
}
}
@@ -248,15 +346,16 @@ decode_huffman :: proc(z: ^Context, t: ^Huffman_Table) -> (r: u16, err: Error) #
if b != 0 {
s := u8(b >> ZFAST_BITS);
compress.consume_bits_lsb(z, s);
return b & 511, E_General.OK;
return b & 511, nil;
}
return decode_huffman_slowpath(z, t);
}
parse_huffman_block :: proc(z: ^Context, z_repeat, z_offset: ^Huffman_Table) -> (err: Error) #no_bounds_check {
@(optimization_mode="speed")
parse_huffman_block :: proc(z: ^$C, z_repeat, z_offset: ^Huffman_Table) -> (err: Error) #no_bounds_check {
#no_bounds_check for {
value, e := decode_huffman(z, z_repeat);
if !is_kind(e, E_General.OK) {
if e != nil {
return err;
}
if value < 256 {
@@ -266,8 +365,8 @@ parse_huffman_block :: proc(z: ^Context, z_repeat, z_offset: ^Huffman_Table) ->
}
} else {
if value == 256 {
// End of block
return E_General.OK;
// End of block
return nil;
}
value -= 257;
@@ -277,7 +376,7 @@ parse_huffman_block :: proc(z: ^Context, z_repeat, z_offset: ^Huffman_Table) ->
}
value, e = decode_huffman(z, z_offset);
if !is_kind(e, E_General.OK) {
if e != nil {
return E_Deflate.Bad_Huffman_Code;
}
@@ -291,7 +390,6 @@ parse_huffman_block :: proc(z: ^Context, z_repeat, z_offset: ^Huffman_Table) ->
return E_Deflate.Bad_Distance;
}
offset := i64(z.bytes_written - i64(distance));
/*
These might be sped up with a repl_byte call that copies
from the already written output more directly, and that
@@ -304,23 +402,17 @@ parse_huffman_block :: proc(z: ^Context, z_repeat, z_offset: ^Huffman_Table) ->
Replicate the last outputted byte, length times.
*/
if length > 0 {
b, e := compress.peek_back_byte(z, offset);
c := z.output.buf[z.bytes_written - i64(distance)];
e := repl_byte(z, length, c);
if e != .None {
return E_General.Output_Too_Short;
}
#no_bounds_check for _ in 0..<length {
write_byte(z, b);
}
}
} else {
if length > 0 {
#no_bounds_check for _ in 0..<length {
b, e := compress.peek_back_byte(z, offset);
if e != .None {
return E_General.Output_Too_Short;
}
write_byte(z, b);
offset += 1;
e := repl_bytes(z, length, distance);
if e != .None {
return E_General.Output_Too_Short;
}
}
}
@@ -328,22 +420,18 @@ parse_huffman_block :: proc(z: ^Context, z_repeat, z_offset: ^Huffman_Table) ->
}
}
inflate_from_stream :: proc(using ctx: ^Context, raw := false, allocator := context.allocator) -> (err: Error) #no_bounds_check {
@(optimization_mode="speed")
inflate_from_context :: proc(using ctx: ^compress.Context_Memory_Input, raw := false, expected_output_size := -1, allocator := context.allocator) -> (err: Error) #no_bounds_check {
/*
ctx.input must be an io.Stream backed by an implementation that supports:
- read
- size
ctx.output must be an io.Stream backed by an implementation that supports:
- write
ctx.output must be a bytes.Buffer for now. We'll add a separate implementation that writes to a stream.
raw determines whether the ZLIB header is processed, or we're inflating a raw
DEFLATE stream.
*/
if !raw {
data_size := io.size(ctx.input);
if data_size < 6 {
size, size_err := compress.input_size(ctx);
if size < 6 || size_err != nil {
return E_General.Stream_Too_Short;
}
@@ -358,8 +446,6 @@ inflate_from_stream :: proc(using ctx: ^Context, raw := false, allocator := cont
if cinfo > 7 {
return E_ZLIB.Unsupported_Window_Size;
}
ctx.window_size = 1 << (cinfo + 8);
flg, _ := compress.read_u8(ctx);
fcheck := flg & 0x1f;
@@ -384,33 +470,58 @@ inflate_from_stream :: proc(using ctx: ^Context, raw := false, allocator := cont
at the end to compare checksums.
*/
// Seed the Adler32 rolling checksum.
ctx.rolling_hash = 1;
}
// Parse ZLIB stream without header.
err = inflate_raw(ctx);
if !is_kind(err, E_General.OK) {
// Parse ZLIB stream without header.
err = inflate_raw(z=ctx, expected_output_size=expected_output_size);
if err != nil {
return err;
}
if !raw {
compress.discard_to_next_byte_lsb(ctx);
adler32 := compress.read_bits_lsb(ctx, 8) << 24 | compress.read_bits_lsb(ctx, 8) << 16 | compress.read_bits_lsb(ctx, 8) << 8 | compress.read_bits_lsb(ctx, 8);
if ctx.rolling_hash != u32(adler32) {
adler_b: [4]u8;
for _, i in adler_b {
adler_b[i], _ = compress.read_u8_prefer_code_buffer_lsb(ctx);
}
adler := transmute(u32be)adler_b;
output_hash := hash.adler32(ctx.output.buf[:]);
if output_hash != u32(adler) {
return E_General.Checksum_Failed;
}
}
return E_General.OK;
return nil;
}
// @(optimization_mode="speed")
inflate_from_stream_raw :: proc(z: ^Context, allocator := context.allocator) -> (err: Error) #no_bounds_check {
final := u32(0);
type := u32(0);
// TODO: Check alignment of reserve/resize.
z.num_bits = 0;
@(optimization_mode="speed")
inflate_raw :: proc(z: ^$C, expected_output_size := -1, allocator := context.allocator) -> (err: Error) #no_bounds_check {
expected_output_size := expected_output_size;
/*
Always set up a minimum allocation size.
*/
expected_output_size = max(max(expected_output_size, compress.COMPRESS_OUTPUT_ALLOCATE_MIN), 512);
// fmt.printf("\nZLIB: Expected Payload Size: %v\n\n", expected_output_size);
if expected_output_size > 0 && expected_output_size <= compress.COMPRESS_OUTPUT_ALLOCATE_MAX {
/*
Try to pre-allocate the output buffer.
*/
reserve(&z.output.buf, expected_output_size);
resize (&z.output.buf, expected_output_size);
};
if len(z.output.buf) != expected_output_size {
return .Resize_Failed;
}
z.num_bits = 0;
z.code_buffer = 0;
z_repeat: ^Huffman_Table;
@@ -418,35 +529,29 @@ inflate_from_stream_raw :: proc(z: ^Context, allocator := context.allocator) ->
codelength_ht: ^Huffman_Table;
z_repeat, err = allocate_huffman_table(allocator=context.allocator);
if !is_kind(err, E_General.OK) {
if err != nil {
return err;
}
z_offset, err = allocate_huffman_table(allocator=context.allocator);
if !is_kind(err, E_General.OK) {
if err != nil {
return err;
}
codelength_ht, err = allocate_huffman_table(allocator=context.allocator);
if !is_kind(err, E_General.OK) {
if err != nil {
return err;
}
defer free(z_repeat);
defer free(z_offset);
defer free(codelength_ht);
if z.window_size == 0 {
z.window_size = DEFLATE_MAX_DISTANCE;
}
// Allocate rolling window buffer.
last_b := mem.make_dynamic_array_len_cap([dynamic]u8, z.window_size, z.window_size, allocator);
z.last = &last_b;
defer delete(last_b);
final := u32(0);
type := u32(0);
for {
final = compress.read_bits_lsb(z, 1);
type = compress.read_bits_lsb(z, 2);
// log.debugf("Final: %v | Type: %v\n", final, type);
// fmt.printf("Final: %v | Type: %v\n", final, type);
switch type {
case 0:
@@ -455,9 +560,13 @@ inflate_from_stream_raw :: proc(z: ^Context, allocator := context.allocator) ->
// Discard bits until next byte boundary
compress.discard_to_next_byte_lsb(z);
uncompressed_len := int(compress.read_bits_lsb(z, 16));
length_check := int(compress.read_bits_lsb(z, 16));
if uncompressed_len != ~length_check {
uncompressed_len := i16(compress.read_bits_lsb(z, 16));
length_check := i16(compress.read_bits_lsb(z, 16));
// fmt.printf("LEN: %v, ~LEN: %v, NLEN: %v, ~NLEN: %v\n", uncompressed_len, ~uncompressed_len, length_check, ~length_check);
if ~uncompressed_len != length_check {
return E_Deflate.Len_Nlen_Mismatch;
}
@@ -478,11 +587,11 @@ inflate_from_stream_raw :: proc(z: ^Context, allocator := context.allocator) ->
if type == 1 {
// Use fixed code lengths.
err = build_huffman(z_repeat, Z_FIXED_LENGTH[:]);
if !is_kind(err, E_General.OK) {
if err != nil {
return err;
}
err = build_huffman(z_offset, Z_FIXED_DIST[:]);
if !is_kind(err, E_General.OK) {
if err != nil {
return err;
}
} else {
@@ -503,7 +612,7 @@ inflate_from_stream_raw :: proc(z: ^Context, allocator := context.allocator) ->
codelength_sizes[Z_LENGTH_DEZIGZAG[i]] = u8(s);
}
err = build_huffman(codelength_ht, codelength_sizes[:]);
if !is_kind(err, E_General.OK) {
if err != nil {
return err;
}
@@ -512,7 +621,7 @@ inflate_from_stream_raw :: proc(z: ^Context, allocator := context.allocator) ->
for n < ntot {
c, err = decode_huffman(z, codelength_ht);
if !is_kind(err, E_General.OK) {
if err != nil {
return err;
}
@@ -537,7 +646,7 @@ inflate_from_stream_raw :: proc(z: ^Context, allocator := context.allocator) ->
case 18:
c = u16(compress.read_bits_no_refill_lsb(z, 7) + 11);
case:
return E_Deflate.Huffman_Bad_Code_Lengths;
return E_Deflate.Huffman_Bad_Code_Lengths;
}
if ntot - n < u32(c) {
@@ -556,18 +665,18 @@ inflate_from_stream_raw :: proc(z: ^Context, allocator := context.allocator) ->
}
err = build_huffman(z_repeat, lencodes[:hlit]);
if !is_kind(err, E_General.OK) {
if err != nil {
return err;
}
err = build_huffman(z_offset, lencodes[hlit:ntot]);
if !is_kind(err, E_General.OK) {
if err != nil {
return err;
}
}
err = parse_huffman_block(z, z_repeat, z_offset);
// log.debugf("Err: %v | Final: %v | Type: %v\n", err, final, type);
if !is_kind(err, E_General.OK) {
if err != nil {
return err;
}
}
@@ -575,29 +684,32 @@ inflate_from_stream_raw :: proc(z: ^Context, allocator := context.allocator) ->
break;
}
}
return E_General.OK;
if int(z.bytes_written) != len(z.output.buf) {
resize(&z.output.buf, int(z.bytes_written));
}
return nil;
}
inflate_from_byte_array :: proc(input: []u8, buf: ^bytes.Buffer, raw := false) -> (err: Error) {
ctx := Context{};
inflate_from_byte_array :: proc(input: []u8, buf: ^bytes.Buffer, raw := false, expected_output_size := -1) -> (err: Error) {
ctx := compress.Context_Memory_Input{};
r := bytes.Reader{};
bytes.reader_init(&r, input);
rs := bytes.reader_to_stream(&r);
ctx.input = rs;
ctx.input_data = input;
ctx.output = buf;
buf := buf;
ws := bytes.buffer_to_stream(buf);
ctx.output = ws;
err = inflate_from_stream(&ctx, raw);
err = inflate_from_context(ctx=&ctx, raw=raw, expected_output_size=expected_output_size);
return err;
}
inflate_from_byte_array_raw :: proc(input: []u8, buf: ^bytes.Buffer, raw := false) -> (err: Error) {
return inflate_from_byte_array(input, buf, true);
inflate_from_byte_array_raw :: proc(input: []u8, buf: ^bytes.Buffer, raw := false, expected_output_size := -1) -> (err: Error) {
ctx := compress.Context_Memory_Input{};
ctx.input_data = input;
ctx.output = buf;
return inflate_raw(z=&ctx, expected_output_size=expected_output_size);
}
inflate :: proc{inflate_from_stream, inflate_from_byte_array};
inflate_raw :: proc{inflate_from_stream_raw, inflate_from_byte_array_raw};
inflate :: proc{inflate_from_context, inflate_from_byte_array};
+1 -1
View File
@@ -3,7 +3,7 @@ package container
import "core:mem"
import "core:runtime"
Array :: struct(T: typeid) {
Array :: struct($T: typeid) {
data: ^T,
len: int,
cap: int,
+6 -6
View File
@@ -4,12 +4,12 @@ import "intrinsics"
_ :: intrinsics;
Map :: struct(Key, Value: typeid) where intrinsics.type_is_valid_map_key(Key) {
Map :: struct($Key, $Value: typeid) where intrinsics.type_is_valid_map_key(Key) {
hash: Array(int),
entries: Array(Map_Entry(Key, Value)),
}
Map_Entry :: struct(Key, Value: typeid) where intrinsics.type_is_valid_map_key(Key) {
Map_Entry :: struct($Key, $Value: typeid) where intrinsics.type_is_valid_map_key(Key) {
hash: uintptr,
next: int,
key: Key,
@@ -141,7 +141,7 @@ map_clear :: proc(m: ^$M/Map($Key, $Value)) {
multi_map_find_first :: proc(m: $M/Map($Key, $Value), key: Key) -> ^Map_Entry(Value) {
multi_map_find_first :: proc(m: $M/Map($Key, $Value), key: Key) -> ^Map_Entry(Key, Value) {
i := _map_find_or_fail(m, key);
if i < 0 {
return nil;
@@ -149,7 +149,7 @@ multi_map_find_first :: proc(m: $M/Map($Key, $Value), key: Key) -> ^Map_Entry(Va
return array_get_ptr(m.entries, i);
}
multi_map_find_next :: proc(m: $M/Map($Key, $Value), e: ^Map_Entry(Value)) -> ^Map_Entry(Value) {
multi_map_find_next :: proc(m: $M/Map($Key, $Value), e: ^Map_Entry(Key, Value)) -> ^Map_Entry(Key, Value) {
i := e.next;
for i >= 0 {
it := array_get_ptr(m.entries, i);
@@ -220,7 +220,7 @@ multi_map_insert :: proc(m: ^$M/Map($Key, $Value), key: Key, value: Value) {
}
}
multi_map_remove :: proc(m: ^$M/Map($Key, $Value), e: ^Map_Entry(Value)) {
multi_map_remove :: proc(m: ^$M/Map($Key, $Value), e: ^Map_Entry(Key, Value)) {
fr := _map_find_entry(m, e);
if fr.entry_index >= 0 {
_map_erase(m, fr);
@@ -306,7 +306,7 @@ _map_find_key :: proc(m: $M/Map($Key, $Value), key: Key) -> Map_Find_Result wher
return fr;
}
_map_find_entry :: proc(m: ^$M/Map($Key, $Value), e: ^Map_Entry(Value)) -> Map_Find_Result {
_map_find_entry :: proc(m: ^$M/Map($Key, $Value), e: ^Map_Entry(Key, Value)) -> Map_Find_Result {
fr: Map_Find_Result;
fr.hash_index = -1;
fr.entry_prev = -1;
+1 -1
View File
@@ -1,6 +1,6 @@
package container
Priority_Queue :: struct(T: typeid) {
Priority_Queue :: struct($T: typeid) {
data: Array(T),
len: int,
priority: proc(item: T) -> int,
+1 -1
View File
@@ -1,6 +1,6 @@
package container
Queue :: struct(T: typeid) {
Queue :: struct($T: typeid) {
data: Array(T),
len: int,
offset: int,
+3 -2
View File
@@ -1,7 +1,7 @@
package container
Ring :: struct(T: typeid) {
Ring :: struct($T: typeid) {
next, prev: ^Ring(T),
value: T,
}
@@ -26,6 +26,7 @@ ring_prev :: proc(r: ^$R/Ring) -> ^R {
ring_move :: proc(r: ^$R/Ring, n: int) -> ^R {
r := r;
if r.next == nil {
return ring_init(r);
}
@@ -64,7 +65,7 @@ ring_len :: proc(r: ^$R/Ring) -> int {
n := 0;
if r != nil {
n = 1;
for p := ring_next(&p); p != r; p = p.next {
for p := ring_next(r); p != r; p = p.next {
n += 1;
}
}
+1 -1
View File
@@ -1,6 +1,6 @@
package container
Small_Array :: struct(N: int, T: typeid) where N >= 0 {
Small_Array :: struct($N: int, $T: typeid) where N >= 0 {
data: [N]T,
len: int,
}
+4 -4
View File
@@ -201,9 +201,9 @@ next_token :: proc(p: ^Parser) -> Token {
unquote_char :: proc(str: string, quote: byte) -> (r: rune, multiple_bytes: bool, tail_string: string, success: bool) {
hex_to_int :: proc(c: byte) -> int {
switch c {
case '0'..'9': return int(c-'0');
case 'a'..'f': return int(c-'a')+10;
case 'A'..'F': return int(c-'A')+10;
case '0'..='9': return int(c-'0');
case 'a'..='f': return int(c-'a')+10;
case 'A'..='F': return int(c-'A')+10;
}
return -1;
}
@@ -241,7 +241,7 @@ unquote_char :: proc(str: string, quote: byte) -> (r: rune, multiple_bytes: bool
case '"': r = '"';
case '\'': r = '\'';
case '0'..'7':
case '0'..='7':
v := int(c-'0');
if len(s) < 2 {
return;
+5 -5
View File
@@ -232,7 +232,7 @@ get_pos :: proc(t: ^Tokenizer) -> Pos {
is_letter :: proc(r: rune) -> bool {
switch r {
case 'a'..'z', 'A'..'Z', '_':
case 'a'..='z', 'A'..='Z', '_':
return true;
}
return false;
@@ -240,7 +240,7 @@ is_letter :: proc(r: rune) -> bool {
is_digit :: proc(r: rune) -> bool {
switch r {
case '0'..'9':
case '0'..='9':
return true;
}
return false;
@@ -273,9 +273,9 @@ scan_identifier :: proc(t: ^Tokenizer) -> string {
digit_value :: proc(r: rune) -> int {
switch r {
case '0'..'9': return int(r - '0');
case 'a'..'f': return int(r - 'a' + 10);
case 'A'..'F': return int(r - 'A' + 10);
case '0'..='9': return int(r - '0');
case 'a'..='f': return int(r - 'a' + 10);
case 'A'..='F': return int(r - 'A' + 10);
}
return 16;
}
+6 -6
View File
@@ -290,9 +290,9 @@ unquote_string :: proc(token: Token, spec: Specification, allocator := context.a
for c in s[2:4] {
x: rune;
switch c {
case '0'..'9': x = c - '0';
case 'a'..'f': x = c - 'a' + 10;
case 'A'..'F': x = c - 'A' + 10;
case '0'..='9': x = c - '0';
case 'a'..='f': x = c - 'a' + 10;
case 'A'..='F': x = c - 'A' + 10;
case: return -1;
}
r = r*16 + x;
@@ -308,9 +308,9 @@ unquote_string :: proc(token: Token, spec: Specification, allocator := context.a
for c in s[2:6] {
x: rune;
switch c {
case '0'..'9': x = c - '0';
case 'a'..'f': x = c - 'a' + 10;
case 'A'..'F': x = c - 'A' + 10;
case '0'..='9': x = c - '0';
case 'a'..='f': x = c - 'a' + 10;
case 'A'..='F': x = c - 'A' + 10;
case: return -1;
}
r = r*16 + x;
+8 -8
View File
@@ -82,7 +82,7 @@ get_token :: proc(t: ^Tokenizer) -> (token: Token, err: Error) {
for t.offset < len(t.data) {
next_rune(t);
switch t.r {
case '0'..'9', 'a'..'f', 'A'..'F':
case '0'..='9', 'a'..='f', 'A'..='F':
// Okay
case:
return;
@@ -100,7 +100,7 @@ get_token :: proc(t: ^Tokenizer) -> (token: Token, err: Error) {
for i := 0; i < 4; i += 1 {
r := next_rune(t);
switch r {
case '0'..'9', 'a'..'f', 'A'..'F':
case '0'..='9', 'a'..='f', 'A'..='F':
// Okay
case:
return false;
@@ -149,7 +149,7 @@ get_token :: proc(t: ^Tokenizer) -> (token: Token, err: Error) {
skip_alphanum :: proc(t: ^Tokenizer) {
for t.offset < len(t.data) {
switch next_rune(t) {
case 'A'..'Z', 'a'..'z', '0'..'9', '_':
case 'A'..='Z', 'a'..='z', '0'..='9', '_':
continue;
}
@@ -173,7 +173,7 @@ get_token :: proc(t: ^Tokenizer) -> (token: Token, err: Error) {
token.kind = .EOF;
err = .EOF;
case 'A'..'Z', 'a'..'z', '_':
case 'A'..='Z', 'a'..='z', '_':
token.kind = .Ident;
skip_alphanum(t);
@@ -200,7 +200,7 @@ get_token :: proc(t: ^Tokenizer) -> (token: Token, err: Error) {
case '-':
switch t.r {
case '0'..'9':
case '0'..='9':
// Okay
case:
// Illegal use of +/-
@@ -219,7 +219,7 @@ get_token :: proc(t: ^Tokenizer) -> (token: Token, err: Error) {
}
fallthrough;
case '0'..'9':
case '0'..='9':
token.kind = t.parse_integers ? .Integer : .Float;
if t.spec == .JSON5 { // Hexadecimal Numbers
if curr_rune == '0' && (t.r == 'x' || t.r == 'X') {
@@ -361,7 +361,7 @@ is_valid_number :: proc(str: string, spec: Specification) -> bool {
switch s[0] {
case '0':
s = s[1:];
case '1'..'9':
case '1'..='9':
s = s[1:];
for len(s) > 0 && '0' <= s[0] && s[0] <= '9' {
s = s[1:];
@@ -453,7 +453,7 @@ is_valid_string_literal :: proc(str: string, spec: Specification) -> bool {
for j := 0; j < 4; j += 1 {
c2 := hex[j];
switch c2 {
case '0'..'9', 'a'..'z', 'A'..'Z':
case '0'..='9', 'a'..='z', 'A'..='Z':
// Okay
case:
return false;
+141
View File
@@ -0,0 +1,141 @@
/*
package fmt implemented formatted I/O with procedures similar to C's printf and Python's format.
The format 'verbs' are derived from C's but simpler.
Printing
The verbs:
General:
%v the value in a default format
%#v an expanded format of %v with newlines and indentation
%T an Odin-syntax representation of the type of the value
%% a literal percent sign; consumes no value
{{ a literal open brace; consumes no value
}} a literal close brace; consumes no value
{:v} equivalent to %v (Python-like formatting syntax)
Boolean:
%t the word "true" or "false"
Integer:
%b base 2
%c the character represented by the corresponding Unicode code point
%r synonym for %c
%o base 8
%d base 10
%i base 10
%z base 12
%x base 16, with lower-case letters for a-f
%X base 16, with upper-case letters for A-F
%U Unicode format: U+1234; same as "U+%04X"
Floating-point, complex numbers, and quaternions:
%e scientific notation, e.g. -1.23456e+78
%E scientific notation, e.g. -1.23456E+78
%f decimal point but no exponent, e.g. 123.456
%F synonym for %f
%h hexadecimal (lower-case) representation with 0h prefix (0h01234abcd)
%H hexadecimal (upper-case) representation with 0H prefix (0h01234ABCD)
String and slice of bytes
%s the uninterpreted bytes of the string or slice
%q a double-quoted string safely escaped with Odin syntax
%x base 16, lower-case, two characters per byte
%X base 16, upper-case, two characters per byte
Slice and dynamic array:
%p address of the 0th element in base 16 notation (upper-case), with leading 0x
Pointer:
%p base 16 notation (upper-case), with leading 0x
The %b, %d, %o, %z, %x, %X verbs also work with pointers,
treating it as if it was an integer
Enums:
%s prints the name of the enum field
The %i, %d, %f verbs also work with enums,
treating it as if it was a number
For compound values, the elements are printed using these rules recursively; laid out like the following:
struct: {name0 = field0, name1 = field1, ...}
array [elem0, elem1, elem2, ...]
enumerated array [key0 = elem0, key1 = elem1, key2 = elem2, ...]
maps: map[key0 = value0, key1 = value1, ...]
bit sets {key0 = elem0, key1 = elem1, ...}
pointer to above: &{}, &[], &map[]
Width is specified by an optional decimal number immediately preceding the verb.
If not present, the width is whatever is necessary to represent the value.
Precision is specified after the (optional) width followed by a period followed by a decimal number.
If no period is present, a default precision is used.
A period with no following number specifies a precision of 0.
Examples:
%f default width, default precision
%8f width 8, default precision
%.3f default width, precision 2
%8.3f width 8, precision 3
%8.f width 8, precision 0
Width and precision are measured in units of Unicode code points (runes).
n.b. C's printf uses units of bytes
Other flags:
+ always print a sign for numeric values
- pad with spaces on the right rather the left (left-justify the field)
# alternate format:
add leading 0b for binary (%#b)
add leading 0o for octal (%#o)
add leading 0z for dozenal (%#z)
add leading 0x or 0X for hexadecimal (%#x or %#X)
remove leading 0x for %p (%#p)
' ' (space) leave a space for elided sign in numbers (% d)
0 pad with leading zeros rather than spaces
Flags are ignored by verbs that don't expect them
For each printf-like procedure, there is a print function that takes no
format, and is equivalent to doing %v for every value and inserts a separator
between each value (default is a single space).
Another procedure println which has the same functionality as print but appends a newline.
Explicit argument indices:
In printf-like procedures, the default behaviour is for each formatting verb to format successive
arguments passed in the call. However, the notation [n] immediately before the verb indicates that
the nth zero-index argument is to be formatted instead.
The same notation before an '*' for a width or precision selecting the argument index holding the value.
Python-like syntax with argument indices differs for the selecting the argument index: {N:v}
Examples:
fmt.printf("%[1]d %[0]d\n", 13, 37); // C-like syntax
fmt.printf("{1:d} {0:d}\n", 13, 37); // Python-like syntax
prints "37 13", whilst:
fmt.printf("%[2]*.[1]*[0]f\n", 17.0, 2, 6); // C-like syntax
fmt.printf("%{0:[2]*.[1]*f}\n", 17.0, 2, 6); // Python-like syntax
equivalent to:
fmt.printf("%6.2f\n", 17.0, 2, 6); // C-like syntax
fmt.printf("{:6.2f}\n", 17.0, 2, 6); // Python-like syntax
prints "17.00"
Format errors:
If an invalid argument is given for a verb, such as providing a string to %d, the generated string
will contain a description of the problem. For example:
Bad enum value:
%!(BAD ENUM VALUE)
Too many arguments:
%!(EXTRA <value>, <value>, ...)
Too few arguments:
%!(MISSING ARGUMENT)
Invalid width or precision
%!(BAD WIDTH)
%!(BAD PRECISION)
Missing verb:
%!(NO VERB)
Invalid or invalid use of argument index:
%!(BAD ARGUMENT NUMBER)
Missing close brace when using Python-like formatting syntax:
%!(MISSING CLOSE BRACE)
*/
package fmt
+5 -4
View File
@@ -641,9 +641,9 @@ fmt_write_padding :: proc(fi: ^Info, width: int) {
return;
}
pad_byte: byte = '0';
if fi.space {
pad_byte = ' ';
pad_byte: byte = ' ';
if !fi.space {
pad_byte = '0';
}
for i := 0; i < width; i += 1 {
@@ -1013,6 +1013,7 @@ fmt_pointer :: proc(fi: ^Info, p: rawptr, verb: rune) {
case 'b': _fmt_int(fi, u, 2, false, 8*size_of(rawptr), __DIGITS_UPPER);
case 'o': _fmt_int(fi, u, 8, false, 8*size_of(rawptr), __DIGITS_UPPER);
case 'i', 'd': _fmt_int(fi, u, 10, false, 8*size_of(rawptr), __DIGITS_UPPER);
case 'z': _fmt_int(fi, u, 12, false, 8*size_of(rawptr), __DIGITS_UPPER);
case 'x': _fmt_int(fi, u, 16, false, 8*size_of(rawptr), __DIGITS_UPPER);
case 'X': _fmt_int(fi, u, 16, false, 8*size_of(rawptr), __DIGITS_UPPER);
@@ -1082,7 +1083,7 @@ fmt_enum :: proc(fi: ^Info, v: any, verb: rune) {
case 's', 'v':
str, ok := enum_value_to_string(v);
if !ok {
str = "!%(BAD ENUM VALUE)";
str = "%!(BAD ENUM VALUE)";
}
io.write_string(fi.writer, str);
}
+3 -74
View File
@@ -1,86 +1,15 @@
package hash
crc32 :: proc(data: []byte, seed := u32(0)) -> u32 #no_bounds_check {
result := ~u32(seed);
for b in data {
result = result>>8 ~ _crc32_table[(result ~ u32(b)) & 0xff];
}
return ~result;
}
@(optimization_mode="speed")
crc64 :: proc(data: []byte, seed := u32(0)) -> u64 #no_bounds_check {
result := ~u64(seed);
for b in data {
#no_bounds_check for b in data {
result = result>>8 ~ _crc64_table[(result ~ u64(b)) & 0xff];
}
return ~result;
}
@private _crc32_table := [256]u32{
0x00000000, 0x77073096, 0xee0e612c, 0x990951ba,
0x076dc419, 0x706af48f, 0xe963a535, 0x9e6495a3,
0x0edb8832, 0x79dcb8a4, 0xe0d5e91e, 0x97d2d988,
0x09b64c2b, 0x7eb17cbd, 0xe7b82d07, 0x90bf1d91,
0x1db71064, 0x6ab020f2, 0xf3b97148, 0x84be41de,
0x1adad47d, 0x6ddde4eb, 0xf4d4b551, 0x83d385c7,
0x136c9856, 0x646ba8c0, 0xfd62f97a, 0x8a65c9ec,
0x14015c4f, 0x63066cd9, 0xfa0f3d63, 0x8d080df5,
0x3b6e20c8, 0x4c69105e, 0xd56041e4, 0xa2677172,
0x3c03e4d1, 0x4b04d447, 0xd20d85fd, 0xa50ab56b,
0x35b5a8fa, 0x42b2986c, 0xdbbbc9d6, 0xacbcf940,
0x32d86ce3, 0x45df5c75, 0xdcd60dcf, 0xabd13d59,
0x26d930ac, 0x51de003a, 0xc8d75180, 0xbfd06116,
0x21b4f4b5, 0x56b3c423, 0xcfba9599, 0xb8bda50f,
0x2802b89e, 0x5f058808, 0xc60cd9b2, 0xb10be924,
0x2f6f7c87, 0x58684c11, 0xc1611dab, 0xb6662d3d,
0x76dc4190, 0x01db7106, 0x98d220bc, 0xefd5102a,
0x71b18589, 0x06b6b51f, 0x9fbfe4a5, 0xe8b8d433,
0x7807c9a2, 0x0f00f934, 0x9609a88e, 0xe10e9818,
0x7f6a0dbb, 0x086d3d2d, 0x91646c97, 0xe6635c01,
0x6b6b51f4, 0x1c6c6162, 0x856530d8, 0xf262004e,
0x6c0695ed, 0x1b01a57b, 0x8208f4c1, 0xf50fc457,
0x65b0d9c6, 0x12b7e950, 0x8bbeb8ea, 0xfcb9887c,
0x62dd1ddf, 0x15da2d49, 0x8cd37cf3, 0xfbd44c65,
0x4db26158, 0x3ab551ce, 0xa3bc0074, 0xd4bb30e2,
0x4adfa541, 0x3dd895d7, 0xa4d1c46d, 0xd3d6f4fb,
0x4369e96a, 0x346ed9fc, 0xad678846, 0xda60b8d0,
0x44042d73, 0x33031de5, 0xaa0a4c5f, 0xdd0d7cc9,
0x5005713c, 0x270241aa, 0xbe0b1010, 0xc90c2086,
0x5768b525, 0x206f85b3, 0xb966d409, 0xce61e49f,
0x5edef90e, 0x29d9c998, 0xb0d09822, 0xc7d7a8b4,
0x59b33d17, 0x2eb40d81, 0xb7bd5c3b, 0xc0ba6cad,
0xedb88320, 0x9abfb3b6, 0x03b6e20c, 0x74b1d29a,
0xead54739, 0x9dd277af, 0x04db2615, 0x73dc1683,
0xe3630b12, 0x94643b84, 0x0d6d6a3e, 0x7a6a5aa8,
0xe40ecf0b, 0x9309ff9d, 0x0a00ae27, 0x7d079eb1,
0xf00f9344, 0x8708a3d2, 0x1e01f268, 0x6906c2fe,
0xf762575d, 0x806567cb, 0x196c3671, 0x6e6b06e7,
0xfed41b76, 0x89d32be0, 0x10da7a5a, 0x67dd4acc,
0xf9b9df6f, 0x8ebeeff9, 0x17b7be43, 0x60b08ed5,
0xd6d6a3e8, 0xa1d1937e, 0x38d8c2c4, 0x4fdff252,
0xd1bb67f1, 0xa6bc5767, 0x3fb506dd, 0x48b2364b,
0xd80d2bda, 0xaf0a1b4c, 0x36034af6, 0x41047a60,
0xdf60efc3, 0xa867df55, 0x316e8eef, 0x4669be79,
0xcb61b38c, 0xbc66831a, 0x256fd2a0, 0x5268e236,
0xcc0c7795, 0xbb0b4703, 0x220216b9, 0x5505262f,
0xc5ba3bbe, 0xb2bd0b28, 0x2bb45a92, 0x5cb36a04,
0xc2d7ffa7, 0xb5d0cf31, 0x2cd99e8b, 0x5bdeae1d,
0x9b64c2b0, 0xec63f226, 0x756aa39c, 0x026d930a,
0x9c0906a9, 0xeb0e363f, 0x72076785, 0x05005713,
0x95bf4a82, 0xe2b87a14, 0x7bb12bae, 0x0cb61b38,
0x92d28e9b, 0xe5d5be0d, 0x7cdcefb7, 0x0bdbdf21,
0x86d3d2d4, 0xf1d4e242, 0x68ddb3f8, 0x1fda836e,
0x81be16cd, 0xf6b9265b, 0x6fb077e1, 0x18b74777,
0x88085ae6, 0xff0f6a70, 0x66063bca, 0x11010b5c,
0x8f659eff, 0xf862ae69, 0x616bffd3, 0x166ccf45,
0xa00ae278, 0xd70dd2ee, 0x4e048354, 0x3903b3c2,
0xa7672661, 0xd06016f7, 0x4969474d, 0x3e6e77db,
0xaed16a4a, 0xd9d65adc, 0x40df0b66, 0x37d83bf0,
0xa9bcae53, 0xdebb9ec5, 0x47b2cf7f, 0x30b5ffe9,
0xbdbdf21c, 0xcabac28a, 0x53b39330, 0x24b4a3a6,
0xbad03605, 0xcdd70693, 0x54de5729, 0x23d967bf,
0xb3667a2e, 0xc4614ab8, 0x5d681b02, 0x2a6f2b94,
0xb40bbe37, 0xc30c8ea1, 0x5a05df1b, 0x2d02ef8d,
};
@private _crc64_table := [256]u64{
0x0000000000000000, 0x42f0e1eba9ea3693, 0x85e1c3d753d46d26, 0xc711223cfa3e5bb5,
0x493366450e42ecdf, 0x0bc387aea7a8da4c, 0xccd2a5925d9681f9, 0x8e224479f47cb76a,
+401
View File
@@ -0,0 +1,401 @@
package hash
import "intrinsics"
@(optimization_mode="speed")
crc32 :: proc(data: []byte, seed := u32(0)) -> u32 #no_bounds_check {
crc := ~seed;
buffer := raw_data(data);
length := len(data);
for length != 0 && uintptr(buffer) & 7 != 0 {
crc = crc32_table[0][byte(crc) ~ buffer^] ~ (crc >> 8);
buffer = intrinsics.ptr_offset(buffer, 1);
length -= 1;
}
for length >= 8 {
buf := (^[8]byte)(buffer);
word := u32((^u32le)(buffer)^);
crc ~= word;
crc = crc32_table[7][crc & 0xff] ~
crc32_table[6][(crc >> 8) & 0xff] ~
crc32_table[5][(crc >> 16) & 0xff] ~
crc32_table[4][(crc >> 24) & 0xff] ~
crc32_table[3][buf[4]] ~
crc32_table[2][buf[5]] ~
crc32_table[1][buf[6]] ~
crc32_table[0][buf[7]];
buffer = intrinsics.ptr_offset(buffer, 8);
length -= 8;
}
for length != 0 {
crc = crc32_table[0][byte(crc) ~ buffer^] ~ (crc >> 8);
buffer = intrinsics.ptr_offset(buffer, 1);
length -= 1;
}
return ~crc;
}
@(private)
crc32_table := [8][256]u32{
{
0x00000000, 0x77073096, 0xee0e612c, 0x990951ba, 0x076dc419, 0x706af48f, 0xe963a535, 0x9e6495a3,
0x0edb8832, 0x79dcb8a4, 0xe0d5e91e, 0x97d2d988, 0x09b64c2b, 0x7eb17cbd, 0xe7b82d07, 0x90bf1d91,
0x1db71064, 0x6ab020f2, 0xf3b97148, 0x84be41de, 0x1adad47d, 0x6ddde4eb, 0xf4d4b551, 0x83d385c7,
0x136c9856, 0x646ba8c0, 0xfd62f97a, 0x8a65c9ec, 0x14015c4f, 0x63066cd9, 0xfa0f3d63, 0x8d080df5,
0x3b6e20c8, 0x4c69105e, 0xd56041e4, 0xa2677172, 0x3c03e4d1, 0x4b04d447, 0xd20d85fd, 0xa50ab56b,
0x35b5a8fa, 0x42b2986c, 0xdbbbc9d6, 0xacbcf940, 0x32d86ce3, 0x45df5c75, 0xdcd60dcf, 0xabd13d59,
0x26d930ac, 0x51de003a, 0xc8d75180, 0xbfd06116, 0x21b4f4b5, 0x56b3c423, 0xcfba9599, 0xb8bda50f,
0x2802b89e, 0x5f058808, 0xc60cd9b2, 0xb10be924, 0x2f6f7c87, 0x58684c11, 0xc1611dab, 0xb6662d3d,
0x76dc4190, 0x01db7106, 0x98d220bc, 0xefd5102a, 0x71b18589, 0x06b6b51f, 0x9fbfe4a5, 0xe8b8d433,
0x7807c9a2, 0x0f00f934, 0x9609a88e, 0xe10e9818, 0x7f6a0dbb, 0x086d3d2d, 0x91646c97, 0xe6635c01,
0x6b6b51f4, 0x1c6c6162, 0x856530d8, 0xf262004e, 0x6c0695ed, 0x1b01a57b, 0x8208f4c1, 0xf50fc457,
0x65b0d9c6, 0x12b7e950, 0x8bbeb8ea, 0xfcb9887c, 0x62dd1ddf, 0x15da2d49, 0x8cd37cf3, 0xfbd44c65,
0x4db26158, 0x3ab551ce, 0xa3bc0074, 0xd4bb30e2, 0x4adfa541, 0x3dd895d7, 0xa4d1c46d, 0xd3d6f4fb,
0x4369e96a, 0x346ed9fc, 0xad678846, 0xda60b8d0, 0x44042d73, 0x33031de5, 0xaa0a4c5f, 0xdd0d7cc9,
0x5005713c, 0x270241aa, 0xbe0b1010, 0xc90c2086, 0x5768b525, 0x206f85b3, 0xb966d409, 0xce61e49f,
0x5edef90e, 0x29d9c998, 0xb0d09822, 0xc7d7a8b4, 0x59b33d17, 0x2eb40d81, 0xb7bd5c3b, 0xc0ba6cad,
0xedb88320, 0x9abfb3b6, 0x03b6e20c, 0x74b1d29a, 0xead54739, 0x9dd277af, 0x04db2615, 0x73dc1683,
0xe3630b12, 0x94643b84, 0x0d6d6a3e, 0x7a6a5aa8, 0xe40ecf0b, 0x9309ff9d, 0x0a00ae27, 0x7d079eb1,
0xf00f9344, 0x8708a3d2, 0x1e01f268, 0x6906c2fe, 0xf762575d, 0x806567cb, 0x196c3671, 0x6e6b06e7,
0xfed41b76, 0x89d32be0, 0x10da7a5a, 0x67dd4acc, 0xf9b9df6f, 0x8ebeeff9, 0x17b7be43, 0x60b08ed5,
0xd6d6a3e8, 0xa1d1937e, 0x38d8c2c4, 0x4fdff252, 0xd1bb67f1, 0xa6bc5767, 0x3fb506dd, 0x48b2364b,
0xd80d2bda, 0xaf0a1b4c, 0x36034af6, 0x41047a60, 0xdf60efc3, 0xa867df55, 0x316e8eef, 0x4669be79,
0xcb61b38c, 0xbc66831a, 0x256fd2a0, 0x5268e236, 0xcc0c7795, 0xbb0b4703, 0x220216b9, 0x5505262f,
0xc5ba3bbe, 0xb2bd0b28, 0x2bb45a92, 0x5cb36a04, 0xc2d7ffa7, 0xb5d0cf31, 0x2cd99e8b, 0x5bdeae1d,
0x9b64c2b0, 0xec63f226, 0x756aa39c, 0x026d930a, 0x9c0906a9, 0xeb0e363f, 0x72076785, 0x05005713,
0x95bf4a82, 0xe2b87a14, 0x7bb12bae, 0x0cb61b38, 0x92d28e9b, 0xe5d5be0d, 0x7cdcefb7, 0x0bdbdf21,
0x86d3d2d4, 0xf1d4e242, 0x68ddb3f8, 0x1fda836e, 0x81be16cd, 0xf6b9265b, 0x6fb077e1, 0x18b74777,
0x88085ae6, 0xff0f6a70, 0x66063bca, 0x11010b5c, 0x8f659eff, 0xf862ae69, 0x616bffd3, 0x166ccf45,
0xa00ae278, 0xd70dd2ee, 0x4e048354, 0x3903b3c2, 0xa7672661, 0xd06016f7, 0x4969474d, 0x3e6e77db,
0xaed16a4a, 0xd9d65adc, 0x40df0b66, 0x37d83bf0, 0xa9bcae53, 0xdebb9ec5, 0x47b2cf7f, 0x30b5ffe9,
0xbdbdf21c, 0xcabac28a, 0x53b39330, 0x24b4a3a6, 0xbad03605, 0xcdd70693, 0x54de5729, 0x23d967bf,
0xb3667a2e, 0xc4614ab8, 0x5d681b02, 0x2a6f2b94, 0xb40bbe37, 0xc30c8ea1, 0x5a05df1b, 0x2d02ef8d,
},
{
0x00000000, 0x191b3141, 0x32366282, 0x2b2d53c3, 0x646cc504, 0x7d77f445, 0x565aa786, 0x4f4196c7,
0xc8d98a08, 0xd1c2bb49, 0xfaefe88a, 0xe3f4d9cb, 0xacb54f0c, 0xb5ae7e4d, 0x9e832d8e, 0x87981ccf,
0x4ac21251, 0x53d92310, 0x78f470d3, 0x61ef4192, 0x2eaed755, 0x37b5e614, 0x1c98b5d7, 0x05838496,
0x821b9859, 0x9b00a918, 0xb02dfadb, 0xa936cb9a, 0xe6775d5d, 0xff6c6c1c, 0xd4413fdf, 0xcd5a0e9e,
0x958424a2, 0x8c9f15e3, 0xa7b24620, 0xbea97761, 0xf1e8e1a6, 0xe8f3d0e7, 0xc3de8324, 0xdac5b265,
0x5d5daeaa, 0x44469feb, 0x6f6bcc28, 0x7670fd69, 0x39316bae, 0x202a5aef, 0x0b07092c, 0x121c386d,
0xdf4636f3, 0xc65d07b2, 0xed705471, 0xf46b6530, 0xbb2af3f7, 0xa231c2b6, 0x891c9175, 0x9007a034,
0x179fbcfb, 0x0e848dba, 0x25a9de79, 0x3cb2ef38, 0x73f379ff, 0x6ae848be, 0x41c51b7d, 0x58de2a3c,
0xf0794f05, 0xe9627e44, 0xc24f2d87, 0xdb541cc6, 0x94158a01, 0x8d0ebb40, 0xa623e883, 0xbf38d9c2,
0x38a0c50d, 0x21bbf44c, 0x0a96a78f, 0x138d96ce, 0x5ccc0009, 0x45d73148, 0x6efa628b, 0x77e153ca,
0xbabb5d54, 0xa3a06c15, 0x888d3fd6, 0x91960e97, 0xded79850, 0xc7cca911, 0xece1fad2, 0xf5facb93,
0x7262d75c, 0x6b79e61d, 0x4054b5de, 0x594f849f, 0x160e1258, 0x0f152319, 0x243870da, 0x3d23419b,
0x65fd6ba7, 0x7ce65ae6, 0x57cb0925, 0x4ed03864, 0x0191aea3, 0x188a9fe2, 0x33a7cc21, 0x2abcfd60,
0xad24e1af, 0xb43fd0ee, 0x9f12832d, 0x8609b26c, 0xc94824ab, 0xd05315ea, 0xfb7e4629, 0xe2657768,
0x2f3f79f6, 0x362448b7, 0x1d091b74, 0x04122a35, 0x4b53bcf2, 0x52488db3, 0x7965de70, 0x607eef31,
0xe7e6f3fe, 0xfefdc2bf, 0xd5d0917c, 0xcccba03d, 0x838a36fa, 0x9a9107bb, 0xb1bc5478, 0xa8a76539,
0x3b83984b, 0x2298a90a, 0x09b5fac9, 0x10aecb88, 0x5fef5d4f, 0x46f46c0e, 0x6dd93fcd, 0x74c20e8c,
0xf35a1243, 0xea412302, 0xc16c70c1, 0xd8774180, 0x9736d747, 0x8e2de606, 0xa500b5c5, 0xbc1b8484,
0x71418a1a, 0x685abb5b, 0x4377e898, 0x5a6cd9d9, 0x152d4f1e, 0x0c367e5f, 0x271b2d9c, 0x3e001cdd,
0xb9980012, 0xa0833153, 0x8bae6290, 0x92b553d1, 0xddf4c516, 0xc4eff457, 0xefc2a794, 0xf6d996d5,
0xae07bce9, 0xb71c8da8, 0x9c31de6b, 0x852aef2a, 0xca6b79ed, 0xd37048ac, 0xf85d1b6f, 0xe1462a2e,
0x66de36e1, 0x7fc507a0, 0x54e85463, 0x4df36522, 0x02b2f3e5, 0x1ba9c2a4, 0x30849167, 0x299fa026,
0xe4c5aeb8, 0xfdde9ff9, 0xd6f3cc3a, 0xcfe8fd7b, 0x80a96bbc, 0x99b25afd, 0xb29f093e, 0xab84387f,
0x2c1c24b0, 0x350715f1, 0x1e2a4632, 0x07317773, 0x4870e1b4, 0x516bd0f5, 0x7a468336, 0x635db277,
0xcbfad74e, 0xd2e1e60f, 0xf9ccb5cc, 0xe0d7848d, 0xaf96124a, 0xb68d230b, 0x9da070c8, 0x84bb4189,
0x03235d46, 0x1a386c07, 0x31153fc4, 0x280e0e85, 0x674f9842, 0x7e54a903, 0x5579fac0, 0x4c62cb81,
0x8138c51f, 0x9823f45e, 0xb30ea79d, 0xaa1596dc, 0xe554001b, 0xfc4f315a, 0xd7626299, 0xce7953d8,
0x49e14f17, 0x50fa7e56, 0x7bd72d95, 0x62cc1cd4, 0x2d8d8a13, 0x3496bb52, 0x1fbbe891, 0x06a0d9d0,
0x5e7ef3ec, 0x4765c2ad, 0x6c48916e, 0x7553a02f, 0x3a1236e8, 0x230907a9, 0x0824546a, 0x113f652b,
0x96a779e4, 0x8fbc48a5, 0xa4911b66, 0xbd8a2a27, 0xf2cbbce0, 0xebd08da1, 0xc0fdde62, 0xd9e6ef23,
0x14bce1bd, 0x0da7d0fc, 0x268a833f, 0x3f91b27e, 0x70d024b9, 0x69cb15f8, 0x42e6463b, 0x5bfd777a,
0xdc656bb5, 0xc57e5af4, 0xee530937, 0xf7483876, 0xb809aeb1, 0xa1129ff0, 0x8a3fcc33, 0x9324fd72,
},
{
0x00000000, 0x01c26a37, 0x0384d46e, 0x0246be59, 0x0709a8dc, 0x06cbc2eb, 0x048d7cb2, 0x054f1685,
0x0e1351b8, 0x0fd13b8f, 0x0d9785d6, 0x0c55efe1, 0x091af964, 0x08d89353, 0x0a9e2d0a, 0x0b5c473d,
0x1c26a370, 0x1de4c947, 0x1fa2771e, 0x1e601d29, 0x1b2f0bac, 0x1aed619b, 0x18abdfc2, 0x1969b5f5,
0x1235f2c8, 0x13f798ff, 0x11b126a6, 0x10734c91, 0x153c5a14, 0x14fe3023, 0x16b88e7a, 0x177ae44d,
0x384d46e0, 0x398f2cd7, 0x3bc9928e, 0x3a0bf8b9, 0x3f44ee3c, 0x3e86840b, 0x3cc03a52, 0x3d025065,
0x365e1758, 0x379c7d6f, 0x35dac336, 0x3418a901, 0x3157bf84, 0x3095d5b3, 0x32d36bea, 0x331101dd,
0x246be590, 0x25a98fa7, 0x27ef31fe, 0x262d5bc9, 0x23624d4c, 0x22a0277b, 0x20e69922, 0x2124f315,
0x2a78b428, 0x2bbade1f, 0x29fc6046, 0x283e0a71, 0x2d711cf4, 0x2cb376c3, 0x2ef5c89a, 0x2f37a2ad,
0x709a8dc0, 0x7158e7f7, 0x731e59ae, 0x72dc3399, 0x7793251c, 0x76514f2b, 0x7417f172, 0x75d59b45,
0x7e89dc78, 0x7f4bb64f, 0x7d0d0816, 0x7ccf6221, 0x798074a4, 0x78421e93, 0x7a04a0ca, 0x7bc6cafd,
0x6cbc2eb0, 0x6d7e4487, 0x6f38fade, 0x6efa90e9, 0x6bb5866c, 0x6a77ec5b, 0x68315202, 0x69f33835,
0x62af7f08, 0x636d153f, 0x612bab66, 0x60e9c151, 0x65a6d7d4, 0x6464bde3, 0x662203ba, 0x67e0698d,
0x48d7cb20, 0x4915a117, 0x4b531f4e, 0x4a917579, 0x4fde63fc, 0x4e1c09cb, 0x4c5ab792, 0x4d98dda5,
0x46c49a98, 0x4706f0af, 0x45404ef6, 0x448224c1, 0x41cd3244, 0x400f5873, 0x4249e62a, 0x438b8c1d,
0x54f16850, 0x55330267, 0x5775bc3e, 0x56b7d609, 0x53f8c08c, 0x523aaabb, 0x507c14e2, 0x51be7ed5,
0x5ae239e8, 0x5b2053df, 0x5966ed86, 0x58a487b1, 0x5deb9134, 0x5c29fb03, 0x5e6f455a, 0x5fad2f6d,
0xe1351b80, 0xe0f771b7, 0xe2b1cfee, 0xe373a5d9, 0xe63cb35c, 0xe7fed96b, 0xe5b86732, 0xe47a0d05,
0xef264a38, 0xeee4200f, 0xeca29e56, 0xed60f461, 0xe82fe2e4, 0xe9ed88d3, 0xebab368a, 0xea695cbd,
0xfd13b8f0, 0xfcd1d2c7, 0xfe976c9e, 0xff5506a9, 0xfa1a102c, 0xfbd87a1b, 0xf99ec442, 0xf85cae75,
0xf300e948, 0xf2c2837f, 0xf0843d26, 0xf1465711, 0xf4094194, 0xf5cb2ba3, 0xf78d95fa, 0xf64fffcd,
0xd9785d60, 0xd8ba3757, 0xdafc890e, 0xdb3ee339, 0xde71f5bc, 0xdfb39f8b, 0xddf521d2, 0xdc374be5,
0xd76b0cd8, 0xd6a966ef, 0xd4efd8b6, 0xd52db281, 0xd062a404, 0xd1a0ce33, 0xd3e6706a, 0xd2241a5d,
0xc55efe10, 0xc49c9427, 0xc6da2a7e, 0xc7184049, 0xc25756cc, 0xc3953cfb, 0xc1d382a2, 0xc011e895,
0xcb4dafa8, 0xca8fc59f, 0xc8c97bc6, 0xc90b11f1, 0xcc440774, 0xcd866d43, 0xcfc0d31a, 0xce02b92d,
0x91af9640, 0x906dfc77, 0x922b422e, 0x93e92819, 0x96a63e9c, 0x976454ab, 0x9522eaf2, 0x94e080c5,
0x9fbcc7f8, 0x9e7eadcf, 0x9c381396, 0x9dfa79a1, 0x98b56f24, 0x99770513, 0x9b31bb4a, 0x9af3d17d,
0x8d893530, 0x8c4b5f07, 0x8e0de15e, 0x8fcf8b69, 0x8a809dec, 0x8b42f7db, 0x89044982, 0x88c623b5,
0x839a6488, 0x82580ebf, 0x801eb0e6, 0x81dcdad1, 0x8493cc54, 0x8551a663, 0x8717183a, 0x86d5720d,
0xa9e2d0a0, 0xa820ba97, 0xaa6604ce, 0xaba46ef9, 0xaeeb787c, 0xaf29124b, 0xad6fac12, 0xacadc625,
0xa7f18118, 0xa633eb2f, 0xa4755576, 0xa5b73f41, 0xa0f829c4, 0xa13a43f3, 0xa37cfdaa, 0xa2be979d,
0xb5c473d0, 0xb40619e7, 0xb640a7be, 0xb782cd89, 0xb2cddb0c, 0xb30fb13b, 0xb1490f62, 0xb08b6555,
0xbbd72268, 0xba15485f, 0xb853f606, 0xb9919c31, 0xbcde8ab4, 0xbd1ce083, 0xbf5a5eda, 0xbe9834ed,
},
{
0x00000000, 0xb8bc6765, 0xaa09c88b, 0x12b5afee, 0x8f629757, 0x37def032, 0x256b5fdc, 0x9dd738b9,
0xc5b428ef, 0x7d084f8a, 0x6fbde064, 0xd7018701, 0x4ad6bfb8, 0xf26ad8dd, 0xe0df7733, 0x58631056,
0x5019579f, 0xe8a530fa, 0xfa109f14, 0x42acf871, 0xdf7bc0c8, 0x67c7a7ad, 0x75720843, 0xcdce6f26,
0x95ad7f70, 0x2d111815, 0x3fa4b7fb, 0x8718d09e, 0x1acfe827, 0xa2738f42, 0xb0c620ac, 0x087a47c9,
0xa032af3e, 0x188ec85b, 0x0a3b67b5, 0xb28700d0, 0x2f503869, 0x97ec5f0c, 0x8559f0e2, 0x3de59787,
0x658687d1, 0xdd3ae0b4, 0xcf8f4f5a, 0x7733283f, 0xeae41086, 0x525877e3, 0x40edd80d, 0xf851bf68,
0xf02bf8a1, 0x48979fc4, 0x5a22302a, 0xe29e574f, 0x7f496ff6, 0xc7f50893, 0xd540a77d, 0x6dfcc018,
0x359fd04e, 0x8d23b72b, 0x9f9618c5, 0x272a7fa0, 0xbafd4719, 0x0241207c, 0x10f48f92, 0xa848e8f7,
0x9b14583d, 0x23a83f58, 0x311d90b6, 0x89a1f7d3, 0x1476cf6a, 0xaccaa80f, 0xbe7f07e1, 0x06c36084,
0x5ea070d2, 0xe61c17b7, 0xf4a9b859, 0x4c15df3c, 0xd1c2e785, 0x697e80e0, 0x7bcb2f0e, 0xc377486b,
0xcb0d0fa2, 0x73b168c7, 0x6104c729, 0xd9b8a04c, 0x446f98f5, 0xfcd3ff90, 0xee66507e, 0x56da371b,
0x0eb9274d, 0xb6054028, 0xa4b0efc6, 0x1c0c88a3, 0x81dbb01a, 0x3967d77f, 0x2bd27891, 0x936e1ff4,
0x3b26f703, 0x839a9066, 0x912f3f88, 0x299358ed, 0xb4446054, 0x0cf80731, 0x1e4da8df, 0xa6f1cfba,
0xfe92dfec, 0x462eb889, 0x549b1767, 0xec277002, 0x71f048bb, 0xc94c2fde, 0xdbf98030, 0x6345e755,
0x6b3fa09c, 0xd383c7f9, 0xc1366817, 0x798a0f72, 0xe45d37cb, 0x5ce150ae, 0x4e54ff40, 0xf6e89825,
0xae8b8873, 0x1637ef16, 0x048240f8, 0xbc3e279d, 0x21e91f24, 0x99557841, 0x8be0d7af, 0x335cb0ca,
0xed59b63b, 0x55e5d15e, 0x47507eb0, 0xffec19d5, 0x623b216c, 0xda874609, 0xc832e9e7, 0x708e8e82,
0x28ed9ed4, 0x9051f9b1, 0x82e4565f, 0x3a58313a, 0xa78f0983, 0x1f336ee6, 0x0d86c108, 0xb53aa66d,
0xbd40e1a4, 0x05fc86c1, 0x1749292f, 0xaff54e4a, 0x322276f3, 0x8a9e1196, 0x982bbe78, 0x2097d91d,
0x78f4c94b, 0xc048ae2e, 0xd2fd01c0, 0x6a4166a5, 0xf7965e1c, 0x4f2a3979, 0x5d9f9697, 0xe523f1f2,
0x4d6b1905, 0xf5d77e60, 0xe762d18e, 0x5fdeb6eb, 0xc2098e52, 0x7ab5e937, 0x680046d9, 0xd0bc21bc,
0x88df31ea, 0x3063568f, 0x22d6f961, 0x9a6a9e04, 0x07bda6bd, 0xbf01c1d8, 0xadb46e36, 0x15080953,
0x1d724e9a, 0xa5ce29ff, 0xb77b8611, 0x0fc7e174, 0x9210d9cd, 0x2aacbea8, 0x38191146, 0x80a57623,
0xd8c66675, 0x607a0110, 0x72cfaefe, 0xca73c99b, 0x57a4f122, 0xef189647, 0xfdad39a9, 0x45115ecc,
0x764dee06, 0xcef18963, 0xdc44268d, 0x64f841e8, 0xf92f7951, 0x41931e34, 0x5326b1da, 0xeb9ad6bf,
0xb3f9c6e9, 0x0b45a18c, 0x19f00e62, 0xa14c6907, 0x3c9b51be, 0x842736db, 0x96929935, 0x2e2efe50,
0x2654b999, 0x9ee8defc, 0x8c5d7112, 0x34e11677, 0xa9362ece, 0x118a49ab, 0x033fe645, 0xbb838120,
0xe3e09176, 0x5b5cf613, 0x49e959fd, 0xf1553e98, 0x6c820621, 0xd43e6144, 0xc68bceaa, 0x7e37a9cf,
0xd67f4138, 0x6ec3265d, 0x7c7689b3, 0xc4caeed6, 0x591dd66f, 0xe1a1b10a, 0xf3141ee4, 0x4ba87981,
0x13cb69d7, 0xab770eb2, 0xb9c2a15c, 0x017ec639, 0x9ca9fe80, 0x241599e5, 0x36a0360b, 0x8e1c516e,
0x866616a7, 0x3eda71c2, 0x2c6fde2c, 0x94d3b949, 0x090481f0, 0xb1b8e695, 0xa30d497b, 0x1bb12e1e,
0x43d23e48, 0xfb6e592d, 0xe9dbf6c3, 0x516791a6, 0xccb0a91f, 0x740cce7a, 0x66b96194, 0xde0506f1,
},
{
0x00000000, 0x3d6029b0, 0x7ac05360, 0x47a07ad0, 0xf580a6c0, 0xc8e08f70, 0x8f40f5a0, 0xb220dc10,
0x30704bc1, 0x0d106271, 0x4ab018a1, 0x77d03111, 0xc5f0ed01, 0xf890c4b1, 0xbf30be61, 0x825097d1,
0x60e09782, 0x5d80be32, 0x1a20c4e2, 0x2740ed52, 0x95603142, 0xa80018f2, 0xefa06222, 0xd2c04b92,
0x5090dc43, 0x6df0f5f3, 0x2a508f23, 0x1730a693, 0xa5107a83, 0x98705333, 0xdfd029e3, 0xe2b00053,
0xc1c12f04, 0xfca106b4, 0xbb017c64, 0x866155d4, 0x344189c4, 0x0921a074, 0x4e81daa4, 0x73e1f314,
0xf1b164c5, 0xccd14d75, 0x8b7137a5, 0xb6111e15, 0x0431c205, 0x3951ebb5, 0x7ef19165, 0x4391b8d5,
0xa121b886, 0x9c419136, 0xdbe1ebe6, 0xe681c256, 0x54a11e46, 0x69c137f6, 0x2e614d26, 0x13016496,
0x9151f347, 0xac31daf7, 0xeb91a027, 0xd6f18997, 0x64d15587, 0x59b17c37, 0x1e1106e7, 0x23712f57,
0x58f35849, 0x659371f9, 0x22330b29, 0x1f532299, 0xad73fe89, 0x9013d739, 0xd7b3ade9, 0xead38459,
0x68831388, 0x55e33a38, 0x124340e8, 0x2f236958, 0x9d03b548, 0xa0639cf8, 0xe7c3e628, 0xdaa3cf98,
0x3813cfcb, 0x0573e67b, 0x42d39cab, 0x7fb3b51b, 0xcd93690b, 0xf0f340bb, 0xb7533a6b, 0x8a3313db,
0x0863840a, 0x3503adba, 0x72a3d76a, 0x4fc3feda, 0xfde322ca, 0xc0830b7a, 0x872371aa, 0xba43581a,
0x9932774d, 0xa4525efd, 0xe3f2242d, 0xde920d9d, 0x6cb2d18d, 0x51d2f83d, 0x167282ed, 0x2b12ab5d,
0xa9423c8c, 0x9422153c, 0xd3826fec, 0xeee2465c, 0x5cc29a4c, 0x61a2b3fc, 0x2602c92c, 0x1b62e09c,
0xf9d2e0cf, 0xc4b2c97f, 0x8312b3af, 0xbe729a1f, 0x0c52460f, 0x31326fbf, 0x7692156f, 0x4bf23cdf,
0xc9a2ab0e, 0xf4c282be, 0xb362f86e, 0x8e02d1de, 0x3c220dce, 0x0142247e, 0x46e25eae, 0x7b82771e,
0xb1e6b092, 0x8c869922, 0xcb26e3f2, 0xf646ca42, 0x44661652, 0x79063fe2, 0x3ea64532, 0x03c66c82,
0x8196fb53, 0xbcf6d2e3, 0xfb56a833, 0xc6368183, 0x74165d93, 0x49767423, 0x0ed60ef3, 0x33b62743,
0xd1062710, 0xec660ea0, 0xabc67470, 0x96a65dc0, 0x248681d0, 0x19e6a860, 0x5e46d2b0, 0x6326fb00,
0xe1766cd1, 0xdc164561, 0x9bb63fb1, 0xa6d61601, 0x14f6ca11, 0x2996e3a1, 0x6e369971, 0x5356b0c1,
0x70279f96, 0x4d47b626, 0x0ae7ccf6, 0x3787e546, 0x85a73956, 0xb8c710e6, 0xff676a36, 0xc2074386,
0x4057d457, 0x7d37fde7, 0x3a978737, 0x07f7ae87, 0xb5d77297, 0x88b75b27, 0xcf1721f7, 0xf2770847,
0x10c70814, 0x2da721a4, 0x6a075b74, 0x576772c4, 0xe547aed4, 0xd8278764, 0x9f87fdb4, 0xa2e7d404,
0x20b743d5, 0x1dd76a65, 0x5a7710b5, 0x67173905, 0xd537e515, 0xe857cca5, 0xaff7b675, 0x92979fc5,
0xe915e8db, 0xd475c16b, 0x93d5bbbb, 0xaeb5920b, 0x1c954e1b, 0x21f567ab, 0x66551d7b, 0x5b3534cb,
0xd965a31a, 0xe4058aaa, 0xa3a5f07a, 0x9ec5d9ca, 0x2ce505da, 0x11852c6a, 0x562556ba, 0x6b457f0a,
0x89f57f59, 0xb49556e9, 0xf3352c39, 0xce550589, 0x7c75d999, 0x4115f029, 0x06b58af9, 0x3bd5a349,
0xb9853498, 0x84e51d28, 0xc34567f8, 0xfe254e48, 0x4c059258, 0x7165bbe8, 0x36c5c138, 0x0ba5e888,
0x28d4c7df, 0x15b4ee6f, 0x521494bf, 0x6f74bd0f, 0xdd54611f, 0xe03448af, 0xa794327f, 0x9af41bcf,
0x18a48c1e, 0x25c4a5ae, 0x6264df7e, 0x5f04f6ce, 0xed242ade, 0xd044036e, 0x97e479be, 0xaa84500e,
0x4834505d, 0x755479ed, 0x32f4033d, 0x0f942a8d, 0xbdb4f69d, 0x80d4df2d, 0xc774a5fd, 0xfa148c4d,
0x78441b9c, 0x4524322c, 0x028448fc, 0x3fe4614c, 0x8dc4bd5c, 0xb0a494ec, 0xf704ee3c, 0xca64c78c,
},
{
0x00000000, 0xcb5cd3a5, 0x4dc8a10b, 0x869472ae, 0x9b914216, 0x50cd91b3, 0xd659e31d, 0x1d0530b8,
0xec53826d, 0x270f51c8, 0xa19b2366, 0x6ac7f0c3, 0x77c2c07b, 0xbc9e13de, 0x3a0a6170, 0xf156b2d5,
0x03d6029b, 0xc88ad13e, 0x4e1ea390, 0x85427035, 0x9847408d, 0x531b9328, 0xd58fe186, 0x1ed33223,
0xef8580f6, 0x24d95353, 0xa24d21fd, 0x6911f258, 0x7414c2e0, 0xbf481145, 0x39dc63eb, 0xf280b04e,
0x07ac0536, 0xccf0d693, 0x4a64a43d, 0x81387798, 0x9c3d4720, 0x57619485, 0xd1f5e62b, 0x1aa9358e,
0xebff875b, 0x20a354fe, 0xa6372650, 0x6d6bf5f5, 0x706ec54d, 0xbb3216e8, 0x3da66446, 0xf6fab7e3,
0x047a07ad, 0xcf26d408, 0x49b2a6a6, 0x82ee7503, 0x9feb45bb, 0x54b7961e, 0xd223e4b0, 0x197f3715,
0xe82985c0, 0x23755665, 0xa5e124cb, 0x6ebdf76e, 0x73b8c7d6, 0xb8e41473, 0x3e7066dd, 0xf52cb578,
0x0f580a6c, 0xc404d9c9, 0x4290ab67, 0x89cc78c2, 0x94c9487a, 0x5f959bdf, 0xd901e971, 0x125d3ad4,
0xe30b8801, 0x28575ba4, 0xaec3290a, 0x659ffaaf, 0x789aca17, 0xb3c619b2, 0x35526b1c, 0xfe0eb8b9,
0x0c8e08f7, 0xc7d2db52, 0x4146a9fc, 0x8a1a7a59, 0x971f4ae1, 0x5c439944, 0xdad7ebea, 0x118b384f,
0xe0dd8a9a, 0x2b81593f, 0xad152b91, 0x6649f834, 0x7b4cc88c, 0xb0101b29, 0x36846987, 0xfdd8ba22,
0x08f40f5a, 0xc3a8dcff, 0x453cae51, 0x8e607df4, 0x93654d4c, 0x58399ee9, 0xdeadec47, 0x15f13fe2,
0xe4a78d37, 0x2ffb5e92, 0xa96f2c3c, 0x6233ff99, 0x7f36cf21, 0xb46a1c84, 0x32fe6e2a, 0xf9a2bd8f,
0x0b220dc1, 0xc07ede64, 0x46eaacca, 0x8db67f6f, 0x90b34fd7, 0x5bef9c72, 0xdd7beedc, 0x16273d79,
0xe7718fac, 0x2c2d5c09, 0xaab92ea7, 0x61e5fd02, 0x7ce0cdba, 0xb7bc1e1f, 0x31286cb1, 0xfa74bf14,
0x1eb014d8, 0xd5ecc77d, 0x5378b5d3, 0x98246676, 0x852156ce, 0x4e7d856b, 0xc8e9f7c5, 0x03b52460,
0xf2e396b5, 0x39bf4510, 0xbf2b37be, 0x7477e41b, 0x6972d4a3, 0xa22e0706, 0x24ba75a8, 0xefe6a60d,
0x1d661643, 0xd63ac5e6, 0x50aeb748, 0x9bf264ed, 0x86f75455, 0x4dab87f0, 0xcb3ff55e, 0x006326fb,
0xf135942e, 0x3a69478b, 0xbcfd3525, 0x77a1e680, 0x6aa4d638, 0xa1f8059d, 0x276c7733, 0xec30a496,
0x191c11ee, 0xd240c24b, 0x54d4b0e5, 0x9f886340, 0x828d53f8, 0x49d1805d, 0xcf45f2f3, 0x04192156,
0xf54f9383, 0x3e134026, 0xb8873288, 0x73dbe12d, 0x6eded195, 0xa5820230, 0x2316709e, 0xe84aa33b,
0x1aca1375, 0xd196c0d0, 0x5702b27e, 0x9c5e61db, 0x815b5163, 0x4a0782c6, 0xcc93f068, 0x07cf23cd,
0xf6999118, 0x3dc542bd, 0xbb513013, 0x700de3b6, 0x6d08d30e, 0xa65400ab, 0x20c07205, 0xeb9ca1a0,
0x11e81eb4, 0xdab4cd11, 0x5c20bfbf, 0x977c6c1a, 0x8a795ca2, 0x41258f07, 0xc7b1fda9, 0x0ced2e0c,
0xfdbb9cd9, 0x36e74f7c, 0xb0733dd2, 0x7b2fee77, 0x662adecf, 0xad760d6a, 0x2be27fc4, 0xe0beac61,
0x123e1c2f, 0xd962cf8a, 0x5ff6bd24, 0x94aa6e81, 0x89af5e39, 0x42f38d9c, 0xc467ff32, 0x0f3b2c97,
0xfe6d9e42, 0x35314de7, 0xb3a53f49, 0x78f9ecec, 0x65fcdc54, 0xaea00ff1, 0x28347d5f, 0xe368aefa,
0x16441b82, 0xdd18c827, 0x5b8cba89, 0x90d0692c, 0x8dd55994, 0x46898a31, 0xc01df89f, 0x0b412b3a,
0xfa1799ef, 0x314b4a4a, 0xb7df38e4, 0x7c83eb41, 0x6186dbf9, 0xaada085c, 0x2c4e7af2, 0xe712a957,
0x15921919, 0xdececabc, 0x585ab812, 0x93066bb7, 0x8e035b0f, 0x455f88aa, 0xc3cbfa04, 0x089729a1,
0xf9c19b74, 0x329d48d1, 0xb4093a7f, 0x7f55e9da, 0x6250d962, 0xa90c0ac7, 0x2f987869, 0xe4c4abcc,
},
{
0x00000000, 0xa6770bb4, 0x979f1129, 0x31e81a9d, 0xf44f2413, 0x52382fa7, 0x63d0353a, 0xc5a73e8e,
0x33ef4e67, 0x959845d3, 0xa4705f4e, 0x020754fa, 0xc7a06a74, 0x61d761c0, 0x503f7b5d, 0xf64870e9,
0x67de9cce, 0xc1a9977a, 0xf0418de7, 0x56368653, 0x9391b8dd, 0x35e6b369, 0x040ea9f4, 0xa279a240,
0x5431d2a9, 0xf246d91d, 0xc3aec380, 0x65d9c834, 0xa07ef6ba, 0x0609fd0e, 0x37e1e793, 0x9196ec27,
0xcfbd399c, 0x69ca3228, 0x582228b5, 0xfe552301, 0x3bf21d8f, 0x9d85163b, 0xac6d0ca6, 0x0a1a0712,
0xfc5277fb, 0x5a257c4f, 0x6bcd66d2, 0xcdba6d66, 0x081d53e8, 0xae6a585c, 0x9f8242c1, 0x39f54975,
0xa863a552, 0x0e14aee6, 0x3ffcb47b, 0x998bbfcf, 0x5c2c8141, 0xfa5b8af5, 0xcbb39068, 0x6dc49bdc,
0x9b8ceb35, 0x3dfbe081, 0x0c13fa1c, 0xaa64f1a8, 0x6fc3cf26, 0xc9b4c492, 0xf85cde0f, 0x5e2bd5bb,
0x440b7579, 0xe27c7ecd, 0xd3946450, 0x75e36fe4, 0xb044516a, 0x16335ade, 0x27db4043, 0x81ac4bf7,
0x77e43b1e, 0xd19330aa, 0xe07b2a37, 0x460c2183, 0x83ab1f0d, 0x25dc14b9, 0x14340e24, 0xb2430590,
0x23d5e9b7, 0x85a2e203, 0xb44af89e, 0x123df32a, 0xd79acda4, 0x71edc610, 0x4005dc8d, 0xe672d739,
0x103aa7d0, 0xb64dac64, 0x87a5b6f9, 0x21d2bd4d, 0xe47583c3, 0x42028877, 0x73ea92ea, 0xd59d995e,
0x8bb64ce5, 0x2dc14751, 0x1c295dcc, 0xba5e5678, 0x7ff968f6, 0xd98e6342, 0xe86679df, 0x4e11726b,
0xb8590282, 0x1e2e0936, 0x2fc613ab, 0x89b1181f, 0x4c162691, 0xea612d25, 0xdb8937b8, 0x7dfe3c0c,
0xec68d02b, 0x4a1fdb9f, 0x7bf7c102, 0xdd80cab6, 0x1827f438, 0xbe50ff8c, 0x8fb8e511, 0x29cfeea5,
0xdf879e4c, 0x79f095f8, 0x48188f65, 0xee6f84d1, 0x2bc8ba5f, 0x8dbfb1eb, 0xbc57ab76, 0x1a20a0c2,
0x8816eaf2, 0x2e61e146, 0x1f89fbdb, 0xb9fef06f, 0x7c59cee1, 0xda2ec555, 0xebc6dfc8, 0x4db1d47c,
0xbbf9a495, 0x1d8eaf21, 0x2c66b5bc, 0x8a11be08, 0x4fb68086, 0xe9c18b32, 0xd82991af, 0x7e5e9a1b,
0xefc8763c, 0x49bf7d88, 0x78576715, 0xde206ca1, 0x1b87522f, 0xbdf0599b, 0x8c184306, 0x2a6f48b2,
0xdc27385b, 0x7a5033ef, 0x4bb82972, 0xedcf22c6, 0x28681c48, 0x8e1f17fc, 0xbff70d61, 0x198006d5,
0x47abd36e, 0xe1dcd8da, 0xd034c247, 0x7643c9f3, 0xb3e4f77d, 0x1593fcc9, 0x247be654, 0x820cede0,
0x74449d09, 0xd23396bd, 0xe3db8c20, 0x45ac8794, 0x800bb91a, 0x267cb2ae, 0x1794a833, 0xb1e3a387,
0x20754fa0, 0x86024414, 0xb7ea5e89, 0x119d553d, 0xd43a6bb3, 0x724d6007, 0x43a57a9a, 0xe5d2712e,
0x139a01c7, 0xb5ed0a73, 0x840510ee, 0x22721b5a, 0xe7d525d4, 0x41a22e60, 0x704a34fd, 0xd63d3f49,
0xcc1d9f8b, 0x6a6a943f, 0x5b828ea2, 0xfdf58516, 0x3852bb98, 0x9e25b02c, 0xafcdaab1, 0x09baa105,
0xfff2d1ec, 0x5985da58, 0x686dc0c5, 0xce1acb71, 0x0bbdf5ff, 0xadcafe4b, 0x9c22e4d6, 0x3a55ef62,
0xabc30345, 0x0db408f1, 0x3c5c126c, 0x9a2b19d8, 0x5f8c2756, 0xf9fb2ce2, 0xc813367f, 0x6e643dcb,
0x982c4d22, 0x3e5b4696, 0x0fb35c0b, 0xa9c457bf, 0x6c636931, 0xca146285, 0xfbfc7818, 0x5d8b73ac,
0x03a0a617, 0xa5d7ada3, 0x943fb73e, 0x3248bc8a, 0xf7ef8204, 0x519889b0, 0x6070932d, 0xc6079899,
0x304fe870, 0x9638e3c4, 0xa7d0f959, 0x01a7f2ed, 0xc400cc63, 0x6277c7d7, 0x539fdd4a, 0xf5e8d6fe,
0x647e3ad9, 0xc209316d, 0xf3e12bf0, 0x55962044, 0x90311eca, 0x3646157e, 0x07ae0fe3, 0xa1d90457,
0x579174be, 0xf1e67f0a, 0xc00e6597, 0x66796e23, 0xa3de50ad, 0x05a95b19, 0x34414184, 0x92364a30,
},
{
0x00000000, 0xccaa009e, 0x4225077d, 0x8e8f07e3, 0x844a0efa, 0x48e00e64, 0xc66f0987, 0x0ac50919,
0xd3e51bb5, 0x1f4f1b2b, 0x91c01cc8, 0x5d6a1c56, 0x57af154f, 0x9b0515d1, 0x158a1232, 0xd92012ac,
0x7cbb312b, 0xb01131b5, 0x3e9e3656, 0xf23436c8, 0xf8f13fd1, 0x345b3f4f, 0xbad438ac, 0x767e3832,
0xaf5e2a9e, 0x63f42a00, 0xed7b2de3, 0x21d12d7d, 0x2b142464, 0xe7be24fa, 0x69312319, 0xa59b2387,
0xf9766256, 0x35dc62c8, 0xbb53652b, 0x77f965b5, 0x7d3c6cac, 0xb1966c32, 0x3f196bd1, 0xf3b36b4f,
0x2a9379e3, 0xe639797d, 0x68b67e9e, 0xa41c7e00, 0xaed97719, 0x62737787, 0xecfc7064, 0x205670fa,
0x85cd537d, 0x496753e3, 0xc7e85400, 0x0b42549e, 0x01875d87, 0xcd2d5d19, 0x43a25afa, 0x8f085a64,
0x562848c8, 0x9a824856, 0x140d4fb5, 0xd8a74f2b, 0xd2624632, 0x1ec846ac, 0x9047414f, 0x5ced41d1,
0x299dc2ed, 0xe537c273, 0x6bb8c590, 0xa712c50e, 0xadd7cc17, 0x617dcc89, 0xeff2cb6a, 0x2358cbf4,
0xfa78d958, 0x36d2d9c6, 0xb85dde25, 0x74f7debb, 0x7e32d7a2, 0xb298d73c, 0x3c17d0df, 0xf0bdd041,
0x5526f3c6, 0x998cf358, 0x1703f4bb, 0xdba9f425, 0xd16cfd3c, 0x1dc6fda2, 0x9349fa41, 0x5fe3fadf,
0x86c3e873, 0x4a69e8ed, 0xc4e6ef0e, 0x084cef90, 0x0289e689, 0xce23e617, 0x40ace1f4, 0x8c06e16a,
0xd0eba0bb, 0x1c41a025, 0x92cea7c6, 0x5e64a758, 0x54a1ae41, 0x980baedf, 0x1684a93c, 0xda2ea9a2,
0x030ebb0e, 0xcfa4bb90, 0x412bbc73, 0x8d81bced, 0x8744b5f4, 0x4beeb56a, 0xc561b289, 0x09cbb217,
0xac509190, 0x60fa910e, 0xee7596ed, 0x22df9673, 0x281a9f6a, 0xe4b09ff4, 0x6a3f9817, 0xa6959889,
0x7fb58a25, 0xb31f8abb, 0x3d908d58, 0xf13a8dc6, 0xfbff84df, 0x37558441, 0xb9da83a2, 0x7570833c,
0x533b85da, 0x9f918544, 0x111e82a7, 0xddb48239, 0xd7718b20, 0x1bdb8bbe, 0x95548c5d, 0x59fe8cc3,
0x80de9e6f, 0x4c749ef1, 0xc2fb9912, 0x0e51998c, 0x04949095, 0xc83e900b, 0x46b197e8, 0x8a1b9776,
0x2f80b4f1, 0xe32ab46f, 0x6da5b38c, 0xa10fb312, 0xabcaba0b, 0x6760ba95, 0xe9efbd76, 0x2545bde8,
0xfc65af44, 0x30cfafda, 0xbe40a839, 0x72eaa8a7, 0x782fa1be, 0xb485a120, 0x3a0aa6c3, 0xf6a0a65d,
0xaa4de78c, 0x66e7e712, 0xe868e0f1, 0x24c2e06f, 0x2e07e976, 0xe2ade9e8, 0x6c22ee0b, 0xa088ee95,
0x79a8fc39, 0xb502fca7, 0x3b8dfb44, 0xf727fbda, 0xfde2f2c3, 0x3148f25d, 0xbfc7f5be, 0x736df520,
0xd6f6d6a7, 0x1a5cd639, 0x94d3d1da, 0x5879d144, 0x52bcd85d, 0x9e16d8c3, 0x1099df20, 0xdc33dfbe,
0x0513cd12, 0xc9b9cd8c, 0x4736ca6f, 0x8b9ccaf1, 0x8159c3e8, 0x4df3c376, 0xc37cc495, 0x0fd6c40b,
0x7aa64737, 0xb60c47a9, 0x3883404a, 0xf42940d4, 0xfeec49cd, 0x32464953, 0xbcc94eb0, 0x70634e2e,
0xa9435c82, 0x65e95c1c, 0xeb665bff, 0x27cc5b61, 0x2d095278, 0xe1a352e6, 0x6f2c5505, 0xa386559b,
0x061d761c, 0xcab77682, 0x44387161, 0x889271ff, 0x825778e6, 0x4efd7878, 0xc0727f9b, 0x0cd87f05,
0xd5f86da9, 0x19526d37, 0x97dd6ad4, 0x5b776a4a, 0x51b26353, 0x9d1863cd, 0x1397642e, 0xdf3d64b0,
0x83d02561, 0x4f7a25ff, 0xc1f5221c, 0x0d5f2282, 0x079a2b9b, 0xcb302b05, 0x45bf2ce6, 0x89152c78,
0x50353ed4, 0x9c9f3e4a, 0x121039a9, 0xdeba3937, 0xd47f302e, 0x18d530b0, 0x965a3753, 0x5af037cd,
0xff6b144a, 0x33c114d4, 0xbd4e1337, 0x71e413a9, 0x7b211ab0, 0xb78b1a2e, 0x39041dcd, 0xf5ae1d53,
0x2c8e0fff, 0xe0240f61, 0x6eab0882, 0xa201081c, 0xa8c40105, 0x646e019b, 0xeae10678, 0x264b06e6,
},
};
/*
@(optimization_mode="speed")
crc32 :: proc(data: []byte, seed := u32(0)) -> u32 {
result := ~u32(seed);
#no_bounds_check for b in data {
result = result>>8 ~ _crc32_table[(result ~ u32(b)) & 0xff];
}
return ~result;
}
@private _crc32_table := [256]u32{
0x00000000, 0x77073096, 0xee0e612c, 0x990951ba,
0x076dc419, 0x706af48f, 0xe963a535, 0x9e6495a3,
0x0edb8832, 0x79dcb8a4, 0xe0d5e91e, 0x97d2d988,
0x09b64c2b, 0x7eb17cbd, 0xe7b82d07, 0x90bf1d91,
0x1db71064, 0x6ab020f2, 0xf3b97148, 0x84be41de,
0x1adad47d, 0x6ddde4eb, 0xf4d4b551, 0x83d385c7,
0x136c9856, 0x646ba8c0, 0xfd62f97a, 0x8a65c9ec,
0x14015c4f, 0x63066cd9, 0xfa0f3d63, 0x8d080df5,
0x3b6e20c8, 0x4c69105e, 0xd56041e4, 0xa2677172,
0x3c03e4d1, 0x4b04d447, 0xd20d85fd, 0xa50ab56b,
0x35b5a8fa, 0x42b2986c, 0xdbbbc9d6, 0xacbcf940,
0x32d86ce3, 0x45df5c75, 0xdcd60dcf, 0xabd13d59,
0x26d930ac, 0x51de003a, 0xc8d75180, 0xbfd06116,
0x21b4f4b5, 0x56b3c423, 0xcfba9599, 0xb8bda50f,
0x2802b89e, 0x5f058808, 0xc60cd9b2, 0xb10be924,
0x2f6f7c87, 0x58684c11, 0xc1611dab, 0xb6662d3d,
0x76dc4190, 0x01db7106, 0x98d220bc, 0xefd5102a,
0x71b18589, 0x06b6b51f, 0x9fbfe4a5, 0xe8b8d433,
0x7807c9a2, 0x0f00f934, 0x9609a88e, 0xe10e9818,
0x7f6a0dbb, 0x086d3d2d, 0x91646c97, 0xe6635c01,
0x6b6b51f4, 0x1c6c6162, 0x856530d8, 0xf262004e,
0x6c0695ed, 0x1b01a57b, 0x8208f4c1, 0xf50fc457,
0x65b0d9c6, 0x12b7e950, 0x8bbeb8ea, 0xfcb9887c,
0x62dd1ddf, 0x15da2d49, 0x8cd37cf3, 0xfbd44c65,
0x4db26158, 0x3ab551ce, 0xa3bc0074, 0xd4bb30e2,
0x4adfa541, 0x3dd895d7, 0xa4d1c46d, 0xd3d6f4fb,
0x4369e96a, 0x346ed9fc, 0xad678846, 0xda60b8d0,
0x44042d73, 0x33031de5, 0xaa0a4c5f, 0xdd0d7cc9,
0x5005713c, 0x270241aa, 0xbe0b1010, 0xc90c2086,
0x5768b525, 0x206f85b3, 0xb966d409, 0xce61e49f,
0x5edef90e, 0x29d9c998, 0xb0d09822, 0xc7d7a8b4,
0x59b33d17, 0x2eb40d81, 0xb7bd5c3b, 0xc0ba6cad,
0xedb88320, 0x9abfb3b6, 0x03b6e20c, 0x74b1d29a,
0xead54739, 0x9dd277af, 0x04db2615, 0x73dc1683,
0xe3630b12, 0x94643b84, 0x0d6d6a3e, 0x7a6a5aa8,
0xe40ecf0b, 0x9309ff9d, 0x0a00ae27, 0x7d079eb1,
0xf00f9344, 0x8708a3d2, 0x1e01f268, 0x6906c2fe,
0xf762575d, 0x806567cb, 0x196c3671, 0x6e6b06e7,
0xfed41b76, 0x89d32be0, 0x10da7a5a, 0x67dd4acc,
0xf9b9df6f, 0x8ebeeff9, 0x17b7be43, 0x60b08ed5,
0xd6d6a3e8, 0xa1d1937e, 0x38d8c2c4, 0x4fdff252,
0xd1bb67f1, 0xa6bc5767, 0x3fb506dd, 0x48b2364b,
0xd80d2bda, 0xaf0a1b4c, 0x36034af6, 0x41047a60,
0xdf60efc3, 0xa867df55, 0x316e8eef, 0x4669be79,
0xcb61b38c, 0xbc66831a, 0x256fd2a0, 0x5268e236,
0xcc0c7795, 0xbb0b4703, 0x220216b9, 0x5505262f,
0xc5ba3bbe, 0xb2bd0b28, 0x2bb45a92, 0x5cb36a04,
0xc2d7ffa7, 0xb5d0cf31, 0x2cd99e8b, 0x5bdeae1d,
0x9b64c2b0, 0xec63f226, 0x756aa39c, 0x026d930a,
0x9c0906a9, 0xeb0e363f, 0x72076785, 0x05005713,
0x95bf4a82, 0xe2b87a14, 0x7bb12bae, 0x0cb61b38,
0x92d28e9b, 0xe5d5be0d, 0x7cdcefb7, 0x0bdbdf21,
0x86d3d2d4, 0xf1d4e242, 0x68ddb3f8, 0x1fda836e,
0x81be16cd, 0xf6b9265b, 0x6fb077e1, 0x18b74777,
0x88085ae6, 0xff0f6a70, 0x66063bca, 0x11010b5c,
0x8f659eff, 0xf862ae69, 0x616bffd3, 0x166ccf45,
0xa00ae278, 0xd70dd2ee, 0x4e048354, 0x3903b3c2,
0xa7672661, 0xd06016f7, 0x4969474d, 0x3e6e77db,
0xaed16a4a, 0xd9d65adc, 0x40df0b66, 0x37d83bf0,
0xa9bcae53, 0xdebb9ec5, 0x47b2cf7f, 0x30b5ffe9,
0xbdbdf21c, 0xcabac28a, 0x53b39330, 0x24b4a3a6,
0xbad03605, 0xcdd70693, 0x54de5729, 0x23d967bf,
0xb3667a2e, 0xc4614ab8, 0x5d681b02, 0x2a6f2b94,
0xb40bbe37, 0xc30c8ea1, 0x5a05df1b, 0x2d02ef8d,
};
*/
+49 -7
View File
@@ -1,17 +1,52 @@
package hash
import "core:mem"
import "intrinsics"
@(optimization_mode="speed")
adler32 :: proc(data: []byte, seed := u32(1)) -> u32 #no_bounds_check {
adler32 :: proc(data: []byte, seed := u32(1)) -> u32 {
ADLER_CONST :: 65521;
a, b: u32 = seed & 0xFFFF, seed >> 16;
for x in data {
a = (a + u32(x)) % ADLER_CONST;
b = (b + a) % ADLER_CONST;
buffer := raw_data(data);
a, b: u64 = u64(seed) & 0xFFFF, u64(seed) >> 16;
buf := data[:];
for len(buf) != 0 && uintptr(buffer) & 7 != 0 {
a = (a + u64(buf[0]));
b = (b + a);
buffer = intrinsics.ptr_offset(buffer, 1);
buf = buf[1:];
}
return (b << 16) | a;
for len(buf) > 7 {
count := min(len(buf), 5552);
for count > 7 {
a += u64(buf[0]); b += a;
a += u64(buf[1]); b += a;
a += u64(buf[2]); b += a;
a += u64(buf[3]); b += a;
a += u64(buf[4]); b += a;
a += u64(buf[5]); b += a;
a += u64(buf[6]); b += a;
a += u64(buf[7]); b += a;
buf = buf[8:];
count -= 8;
}
a %= ADLER_CONST;
b %= ADLER_CONST;
}
for len(buf) != 0 {
a = (a + u64(buf[0])) % ADLER_CONST;
b = (b + a) % ADLER_CONST;
buf = buf[1:];
}
return (u32(b) << 16) | u32(a);
}
@(optimization_mode="speed")
djb2 :: proc(data: []byte) -> u32 {
hash: u32 = 5381;
for b in data {
@@ -20,6 +55,7 @@ djb2 :: proc(data: []byte) -> u32 {
return hash;
}
@(optimization_mode="speed")
fnv32 :: proc(data: []byte) -> u32 {
h: u32 = 0x811c9dc5;
for b in data {
@@ -28,6 +64,7 @@ fnv32 :: proc(data: []byte) -> u32 {
return h;
}
@(optimization_mode="speed")
fnv64 :: proc(data: []byte) -> u64 {
h: u64 = 0xcbf29ce484222325;
for b in data {
@@ -36,6 +73,7 @@ fnv64 :: proc(data: []byte) -> u64 {
return h;
}
@(optimization_mode="speed")
fnv32a :: proc(data: []byte) -> u32 {
h: u32 = 0x811c9dc5;
for b in data {
@@ -44,6 +82,7 @@ fnv32a :: proc(data: []byte) -> u32 {
return h;
}
@(optimization_mode="speed")
fnv64a :: proc(data: []byte) -> u64 {
h: u64 = 0xcbf29ce484222325;
for b in data {
@@ -52,6 +91,7 @@ fnv64a :: proc(data: []byte) -> u64 {
return h;
}
@(optimization_mode="speed")
jenkins :: proc(data: []byte) -> u32 {
hash: u32 = 0;
for b in data {
@@ -65,6 +105,7 @@ jenkins :: proc(data: []byte) -> u32 {
return hash;
}
@(optimization_mode="speed")
murmur32 :: proc(data: []byte) -> u32 {
c1_32: u32 : 0xcc9e2d51;
c2_32: u32 : 0x1b873593;
@@ -114,6 +155,7 @@ murmur32 :: proc(data: []byte) -> u32 {
return h1;
}
@(optimization_mode="speed")
murmur64 :: proc(data: []byte) -> u64 {
SEED :: 0x9747b28c;
@@ -219,7 +261,7 @@ murmur64 :: proc(data: []byte) -> u64 {
}
}
@(optimization_mode="speed")
sdbm :: proc(data: []byte) -> u32 {
hash: u32 = 0;
for b in data {
+134 -27
View File
@@ -1,65 +1,97 @@
package image
/*
Copyright 2021 Jeroen van Rijn <nom@duclavier.com>.
Made available under Odin's BSD-2 license.
List of contributors:
Jeroen van Rijn: Initial implementation, optimization.
Ginger Bill: Cosmetic changes.
*/
import "core:bytes"
import "core:mem"
Image :: struct {
width: int,
height: int,
channels: int,
depth: u8,
pixels: bytes.Buffer,
width: int,
height: int,
channels: int,
depth: int,
pixels: bytes.Buffer,
/*
Some image loaders/writers can return/take an optional background color.
For convenience, we return them as u16 so we don't need to switch on the type
in our viewer, and can just test against nil.
*/
background: Maybe([3]u16),
sidecar: any,
background: Maybe([3]u16),
metadata_ptr: rawptr,
metadata_type: typeid,
}
/*
IMPORTANT: `.do_not_expand_*` options currently skip handling of the `alpha_*` options,
therefore Gray+Alpha will be returned as such even if you add `.alpha_drop_if_present`,
and `.alpha_add_if_missing` and keyed transparency will likewise be ignored.
The same goes for indexed images. This will be remedied in a near future update.
*/
/*
Image_Option:
`.info`
This option behaves as `return_ihdr` and `do_not_decompress_image` and can be used
This option behaves as `.return_ihdr` and `.do_not_decompress_image` and can be used
to gather an image's dimensions and color information.
`.return_header`
Fill out img.sidecar.header with the image's format-specific header struct.
If we only care about the image specs, we can set `return_header` +
`do_not_decompress_image`, or `.info`, which works as if both of these were set.
If we only care about the image specs, we can set `.return_header` +
`.do_not_decompress_image`, or `.info`, which works as if both of these were set.
`.return_metadata`
Returns all chunks not needed to decode the data.
It also returns the header as if `.return_header` is set.
It also returns the header as if `.return_header` was set.
`do_not_decompress_image`
`.do_not_decompress_image`
Skip decompressing IDAT chunk, defiltering and the rest.
`alpha_add_if_missing`
`.do_not_expand_grayscale`
Do not turn grayscale (+ Alpha) images into RGB(A).
Returns just the 1 or 2 channels present, although 1, 2 and 4 bit are still scaled to 8-bit.
`.do_not_expand_indexed`
Do not turn indexed (+ Alpha) images into RGB(A).
Returns just the 1 or 2 (with `tRNS`) channels present.
Make sure to use `return_metadata` to also return the palette chunk so you can recolor it yourself.
`.do_not_expand_channels`
Applies both `.do_not_expand_grayscale` and `.do_not_expand_indexed`.
`.alpha_add_if_missing`
If the image has no alpha channel, it'll add one set to max(type).
Turns RGB into RGBA and Gray into Gray+Alpha
`alpha_drop_if_present`
`.alpha_drop_if_present`
If the image has an alpha channel, drop it.
You may want to use `alpha_premultiply` in this case.
You may want to use `.alpha_premultiply` in this case.
NOTE: For PNG, this also skips handling of the tRNS chunk, if present,
unless you select `alpha_premultiply`.
In this case it'll premultiply the specified pixels in question only,
as the others are implicitly fully opaque.
NOTE: For PNG, this also skips handling of the tRNS chunk, if present,
unless you select `alpha_premultiply`.
In this case it'll premultiply the specified pixels in question only,
as the others are implicitly fully opaque.
`alpha_premultiply`
`.alpha_premultiply`
If the image has an alpha channel, returns image data as follows:
RGB *= A, Gray = Gray *= A
`blend_background`
`.blend_background`
If a bKGD chunk is present in a PNG, we normally just set `img.background`
with its value and leave it up to the application to decide how to display the image,
as per the PNG specification.
With `blend_background` selected, we blend the image against the background
With `.blend_background` selected, we blend the image against the background
color. As this negates the use for an alpha channel, we'll drop it _unless_
you also specify `alpha_add_if_missing`.
you also specify `.alpha_add_if_missing`.
Options that don't apply to an image format will be ignored by their loader.
*/
@@ -73,10 +105,14 @@ Option :: enum {
alpha_drop_if_present,
alpha_premultiply,
blend_background,
// Unimplemented
do_not_expand_grayscale,
do_not_expand_indexed,
do_not_expand_channels,
}
Options :: distinct bit_set[Option];
PNG_Error :: enum {
Error :: enum {
Invalid_PNG_Signature,
IHDR_Not_First_Chunk,
IHDR_Corrupt,
@@ -93,15 +129,86 @@ PNG_Error :: enum {
Invalid_Color_Bit_Depth_Combo,
Unknown_Filter_Method,
Unknown_Interlace_Method,
Requested_Channel_Not_Present,
Post_Processing_Error,
}
/*
Functions to help with image buffer calculations
*/
compute_buffer_size :: proc(width, height, channels, depth: int, extra_row_bytes := int(0)) -> (size: int) {
size = ((((channels * width * depth) + 7) >> 3) + extra_row_bytes) * height;
return;
}
}
/*
For when you have an RGB(A) image, but want a particular channel.
*/
Channel :: enum u8 {
R = 1,
G = 2,
B = 3,
A = 4,
}
return_single_channel :: proc(img: ^Image, channel: Channel) -> (res: ^Image, ok: bool) {
ok = false;
t: bytes.Buffer;
idx := int(channel);
if img.channels == 2 && idx == 4 {
// Alpha requested, which in a two channel image is index 2: G.
idx = 2;
}
if idx > img.channels {
return {}, false;
}
switch img.depth {
case 8:
buffer_size := compute_buffer_size(img.width, img.height, 1, 8);
t = bytes.Buffer{};
resize(&t.buf, buffer_size);
i := bytes.buffer_to_bytes(&img.pixels);
o := bytes.buffer_to_bytes(&t);
for len(i) > 0 {
o[0] = i[idx];
i = i[img.channels:];
o = o[1:];
}
case 16:
buffer_size := compute_buffer_size(img.width, img.height, 2, 8);
t = bytes.Buffer{};
resize(&t.buf, buffer_size);
i := mem.slice_data_cast([]u16, img.pixels.buf[:]);
o := mem.slice_data_cast([]u16, t.buf[:]);
for len(i) > 0 {
o[0] = i[idx];
i = i[img.channels:];
o = o[1:];
}
case 1, 2, 4:
// We shouldn't see this case, as the loader already turns these into 8-bit.
return {}, false;
}
res = new(Image);
res.width = img.width;
res.height = img.height;
res.channels = 1;
res.depth = img.depth;
res.pixels = t;
res.background = img.background;
res.metadata_ptr = img.metadata_ptr;
res.metadata_type = img.metadata_type;
return res, true;
}
+115 -88
View File
@@ -1,9 +1,20 @@
//+ignore
package png
/*
Copyright 2021 Jeroen van Rijn <nom@duclavier.com>.
Made available under Odin's BSD-2 license.
List of contributors:
Jeroen van Rijn: Initial implementation.
Ginger Bill: Cosmetic changes.
An example of how to use `load`.
*/
import "core:compress"
import "core:image"
import "core:image/png"
// import "core:image/png"
import "core:bytes"
import "core:fmt"
@@ -12,115 +23,131 @@ import "core:mem"
import "core:os"
main :: proc() {
track := mem.Tracking_Allocator{};
mem.tracking_allocator_init(&track, context.allocator);
context.allocator = mem.tracking_allocator(&track);
demo();
if len(track.allocation_map) > 0 {
fmt.println("Leaks:");
for _, v in track.allocation_map {
fmt.printf("\t%v\n\n", v);
}
}
}
demo :: proc() {
file: string;
options := image.Options{};
options := image.Options{}; // {.return_metadata};
err: compress.Error;
img: ^image.Image;
file = "../../../misc/logo-slim.png";
img, err = png.load(file, options);
defer png.destroy(img);
img, err = load(file, options);
defer destroy(img);
if !png.is_kind(err, png.E_General.OK) {
if err != nil {
fmt.printf("Trying to read PNG file %v returned %v\n", file, err);
} else {
v: png.Info;
ok: bool;
v: ^Info;
fmt.printf("Image: %vx%vx%v, %v-bit.\n", img.width, img.height, img.channels, img.depth);
if img.metadata_ptr != nil && img.metadata_type == Info {
v = (^Info)(img.metadata_ptr);
if v, ok = img.sidecar.(png.Info); ok {
// Handle ancillary chunks as you wish.
// We provide helper functions for a few types.
for c in v.chunks {
#partial switch (c.header.type) {
case .tIME:
t, _ := png.core_time(c);
fmt.printf("[tIME]: %v\n", t);
case .gAMA:
fmt.printf("[gAMA]: %v\n", png.gamma(c));
case .pHYs:
phys := png.phys(c);
if phys.unit == .Meter {
xm := f32(img.width) / f32(phys.ppu_x);
ym := f32(img.height) / f32(phys.ppu_y);
dpi_x, dpi_y := png.phys_to_dpi(phys);
fmt.printf("[pHYs] Image resolution is %v x %v pixels per meter.\n", phys.ppu_x, phys.ppu_y);
fmt.printf("[pHYs] Image resolution is %v x %v DPI.\n", dpi_x, dpi_y);
fmt.printf("[pHYs] Image dimensions are %v x %v meters.\n", xm, ym);
#partial switch c.header.type {
case .tIME:
t, _ := core_time(c);
fmt.printf("[tIME]: %v\n", t);
case .gAMA:
fmt.printf("[gAMA]: %v\n", gamma(c));
case .pHYs:
phys := phys(c);
if phys.unit == .Meter {
xm := f32(img.width) / f32(phys.ppu_x);
ym := f32(img.height) / f32(phys.ppu_y);
dpi_x, dpi_y := phys_to_dpi(phys);
fmt.printf("[pHYs] Image resolution is %v x %v pixels per meter.\n", phys.ppu_x, phys.ppu_y);
fmt.printf("[pHYs] Image resolution is %v x %v DPI.\n", dpi_x, dpi_y);
fmt.printf("[pHYs] Image dimensions are %v x %v meters.\n", xm, ym);
} else {
fmt.printf("[pHYs] x: %v, y: %v pixels per unknown unit.\n", phys.ppu_x, phys.ppu_y);
}
case .iTXt, .zTXt, .tEXt:
res, ok_text := text(c);
if ok_text {
if c.header.type == .iTXt {
fmt.printf("[iTXt] %v (%v:%v): %v\n", res.keyword, res.language, res.keyword_localized, res.text);
} else {
fmt.printf("[pHYs] x: %v, y: %v pixels per unknown unit.\n", phys.ppu_x, phys.ppu_y);
fmt.printf("[tEXt/zTXt] %v: %v\n", res.keyword, res.text);
}
case .iTXt, .zTXt, .tEXt:
res, ok_text := png.text(c);
if ok_text {
if c.header.type == .iTXt {
fmt.printf("[iTXt] %v (%v:%v): %v\n", res.keyword, res.language, res.keyword_localized, res.text);
} else {
fmt.printf("[tEXt/zTXt] %v: %v\n", res.keyword, res.text);
}
}
defer png.text_destroy(res);
case .bKGD:
fmt.printf("[bKGD] %v\n", img.background);
case .eXIf:
res, ok_exif := png.exif(c);
if ok_exif {
/*
Other than checking the signature and byte order, we don't handle Exif data.
If you wish to interpret it, pass it to an Exif parser.
*/
fmt.printf("[eXIf] %v\n", res);
}
case .PLTE:
plte, plte_ok := png.plte(c);
if plte_ok {
fmt.printf("[PLTE] %v\n", plte);
} else {
fmt.printf("[PLTE] Error\n");
}
case .hIST:
res, ok_hist := png.hist(c);
if ok_hist {
fmt.printf("[hIST] %v\n", res);
}
case .cHRM:
res, ok_chrm := png.chrm(c);
if ok_chrm {
fmt.printf("[cHRM] %v\n", res);
}
case .sPLT:
res, ok_splt := png.splt(c);
if ok_splt {
fmt.printf("[sPLT] %v\n", res);
}
png.splt_destroy(res);
case .sBIT:
if res, ok_sbit := png.sbit(c); ok_sbit {
fmt.printf("[sBIT] %v\n", res);
}
case .iCCP:
res, ok_iccp := png.iccp(c);
if ok_iccp {
fmt.printf("[iCCP] %v\n", res);
}
png.iccp_destroy(res);
case .sRGB:
if res, ok_srgb := png.srgb(c); ok_srgb {
fmt.printf("[sRGB] Rendering intent: %v\n", res);
}
case:
type := c.header.type;
name := png.chunk_type_to_name(&type);
fmt.printf("[%v]: %v\n", name, c.data);
}
defer text_destroy(res);
case .bKGD:
fmt.printf("[bKGD] %v\n", img.background);
case .eXIf:
res, ok_exif := exif(c);
if ok_exif {
/*
Other than checking the signature and byte order, we don't handle Exif data.
If you wish to interpret it, pass it to an Exif parser.
*/
fmt.printf("[eXIf] %v\n", res);
}
case .PLTE:
plte, plte_ok := plte(c);
if plte_ok {
fmt.printf("[PLTE] %v\n", plte);
} else {
fmt.printf("[PLTE] Error\n");
}
case .hIST:
res, ok_hist := hist(c);
if ok_hist {
fmt.printf("[hIST] %v\n", res);
}
case .cHRM:
res, ok_chrm := chrm(c);
if ok_chrm {
fmt.printf("[cHRM] %v\n", res);
}
case .sPLT:
res, ok_splt := splt(c);
if ok_splt {
fmt.printf("[sPLT] %v\n", res);
}
splt_destroy(res);
case .sBIT:
if res, ok_sbit := sbit(c); ok_sbit {
fmt.printf("[sBIT] %v\n", res);
}
case .iCCP:
res, ok_iccp := iccp(c);
if ok_iccp {
fmt.printf("[iCCP] %v\n", res);
}
iccp_destroy(res);
case .sRGB:
if res, ok_srgb := srgb(c); ok_srgb {
fmt.printf("[sRGB] Rendering intent: %v\n", res);
}
case:
type := c.header.type;
name := chunk_type_to_name(&type);
fmt.printf("[%v]: %v\n", name, c.data);
}
}
}
}
if is_kind(err, E_General.OK) && .do_not_decompress_image not_in options && .info not_in options {
if err == nil && .do_not_decompress_image not_in options && .info not_in options {
if ok := write_image_as_ppm("out.ppm", img); ok {
fmt.println("Saved decoded image.");
} else {
@@ -195,7 +222,7 @@ write_image_as_ppm :: proc(filename: string, image: ^image.Image) -> (success: b
defer close(fd);
write_string(fd,
fmt.tprintf("P6\n%v %v\n%v\n", width, height, (1 << depth -1)),
fmt.tprintf("P6\n%v %v\n%v\n", width, height, (1 << uint(depth) - 1)),
);
if channels == 3 {
+19 -10
View File
@@ -1,5 +1,16 @@
package png
/*
Copyright 2021 Jeroen van Rijn <nom@duclavier.com>.
Made available under Odin's BSD-2 license.
List of contributors:
Jeroen van Rijn: Initial implementation.
Ginger Bill: Cosmetic changes.
These are a few useful utility functions to work with PNG images.
*/
import "core:image"
import "core:compress/zlib"
import coretime "core:time"
@@ -7,10 +18,6 @@ import "core:strings"
import "core:bytes"
import "core:mem"
/*
These are a few useful utility functions to work with PNG images.
*/
/*
Cleanup of image-specific data.
There are other helpers for cleanup of PNG-specific data.
@@ -27,6 +34,8 @@ destroy :: proc(img: ^Image) {
}
bytes.buffer_destroy(&img.pixels);
// Clean up Info.
free(img.metadata_ptr);
/*
We don't need to do anything for the individual chunks.
@@ -81,7 +90,7 @@ core_time :: proc(c: Chunk) -> (t: coretime.Time, ok: bool) {
}
text :: proc(c: Chunk) -> (res: Text, ok: bool) {
#partial switch c.header.type {
#partial switch c.header.type {
case .tEXt:
ok = true;
@@ -107,7 +116,7 @@ text :: proc(c: Chunk) -> (res: Text, ok: bool) {
buf: bytes.Buffer;
zlib_error := zlib.inflate_from_byte_array(fields[2], &buf);
defer bytes.buffer_destroy(&buf);
if !is_kind(zlib_error, E_General.OK) {
if zlib_error != nil {
ok = false; return;
}
@@ -161,7 +170,7 @@ text :: proc(c: Chunk) -> (res: Text, ok: bool) {
buf: bytes.Buffer;
zlib_error := zlib.inflate_from_byte_array(rest, &buf);
defer bytes.buffer_destroy(&buf);
if !is_kind(zlib_error, E_General.OK) {
if zlib_error != nil {
ok = false; return;
}
@@ -200,7 +209,7 @@ iccp :: proc(c: Chunk) -> (res: iCCP, ok: bool) {
// Set up ZLIB context and decompress iCCP payload
buf: bytes.Buffer;
zlib_error := zlib.inflate_from_byte_array(fields[2], &buf);
if !is_kind(zlib_error, E_General.OK) {
if zlib_error != nil {
bytes.buffer_destroy(&buf);
ok = false; return;
}
@@ -498,7 +507,7 @@ when false {
err = zlib.write_zlib_stream_from_memory(&ctx);
b: []u8;
if is_kind(err, E_General, E_General.OK) {
if err == nil {
b = ctx.out_buf[:];
} else {
return err;
@@ -511,6 +520,6 @@ when false {
iend := make_chunk([]u8{}, .IEND);
write_chunk(fd, iend);
return E_General.OK;
return nil;
}
}
+345 -335
View File
@@ -1,5 +1,14 @@
package png
/*
Copyright 2021 Jeroen van Rijn <nom@duclavier.com>.
Made available under Odin's BSD-2 license.
List of contributors:
Jeroen van Rijn: Initial implementation.
Ginger Bill: Cosmetic changes.
*/
import "core:compress"
import "core:compress/zlib"
import "core:image"
@@ -14,9 +23,8 @@ import "core:intrinsics"
Error :: compress.Error;
E_General :: compress.General_Error;
E_PNG :: image.PNG_Error;
E_PNG :: image.Error;
E_Deflate :: compress.Deflate_Error;
is_kind :: compress.is_kind;
Image :: image.Image;
Options :: image.Options;
@@ -116,11 +124,11 @@ Interlace_Method :: enum u8 {
}
Row_Filter :: enum u8 {
None = 0,
Sub = 1,
Up = 2,
Average = 3,
Paeth = 4,
None = 0,
Sub = 1,
Up = 2,
Average = 3,
Paeth = 4,
};
PLTE_Entry :: [3]u8;
@@ -167,18 +175,18 @@ CIE_1931 :: struct #packed {
}
cHRM_Raw :: struct #packed {
w: CIE_1931_Raw,
r: CIE_1931_Raw,
g: CIE_1931_Raw,
b: CIE_1931_Raw,
w: CIE_1931_Raw,
r: CIE_1931_Raw,
g: CIE_1931_Raw,
b: CIE_1931_Raw,
}
#assert(size_of(cHRM_Raw) == 32);
cHRM :: struct #packed {
w: CIE_1931,
r: CIE_1931,
g: CIE_1931,
b: CIE_1931,
w: CIE_1931,
r: CIE_1931,
g: CIE_1931,
b: CIE_1931,
}
#assert(size_of(cHRM) == 32);
@@ -237,27 +245,22 @@ ADAM7_Y_SPACING := []int{ 8,8,8,4,4,2,2 };
// Implementation starts here
read_chunk :: proc(ctx: ^compress.Context) -> (Chunk, Error) {
chunk := Chunk{};
read_chunk :: proc(ctx: ^$C) -> (chunk: Chunk, err: Error) {
ch, e := compress.read_data(ctx, Chunk_Header);
if e != .None {
return {}, E_General.Stream_Too_Short;
}
chunk.header = ch;
data := make([]u8, ch.length, context.temp_allocator);
_, e2 := ctx.input->impl_read(data);
if e2 != .None {
chunk.data, e = compress.read_slice(ctx, int(ch.length));
if e != .None {
return {}, E_General.Stream_Too_Short;
}
chunk.data = data;
// Compute CRC over chunk type + data
type := (^[4]byte)(&ch.type)^;
computed_crc := hash.crc32(type[:]);
computed_crc = hash.crc32(data, computed_crc);
computed_crc = hash.crc32(chunk.data, computed_crc);
crc, e3 := compress.read_data(ctx, u32be);
if e3 != .None {
@@ -268,13 +271,12 @@ read_chunk :: proc(ctx: ^compress.Context) -> (Chunk, Error) {
if chunk.crc != u32be(computed_crc) {
return {}, E_General.Checksum_Failed;
}
return chunk, E_General.OK;
return chunk, nil;
}
read_header :: proc(ctx: ^compress.Context) -> (IHDR, Error) {
read_header :: proc(ctx: ^$C) -> (IHDR, Error) {
c, e := read_chunk(ctx);
if !is_kind(e, E_General.OK) {
if e != nil {
return {}, e;
}
@@ -298,51 +300,51 @@ read_header :: proc(ctx: ^compress.Context) -> (IHDR, Error) {
}
switch (transmute(u8)color_type) {
case 0:
/*
Grayscale.
Allowed bit depths: 1, 2, 4, 8 and 16.
*/
allowed := false;
for i in ([]u8{1, 2, 4, 8, 16}) {
if bit_depth == i {
allowed = true;
break;
}
switch transmute(u8)color_type {
case 0:
/*
Grayscale.
Allowed bit depths: 1, 2, 4, 8 and 16.
*/
allowed := false;
for i in ([]u8{1, 2, 4, 8, 16}) {
if bit_depth == i {
allowed = true;
break;
}
if !allowed {
return {}, E_PNG.Invalid_Color_Bit_Depth_Combo;
}
case 2, 4, 6:
/*
RGB, Grayscale+Alpha, RGBA.
Allowed bit depths: 8 and 16
*/
if bit_depth != 8 && bit_depth != 16 {
return {}, E_PNG.Invalid_Color_Bit_Depth_Combo;
}
case 3:
/*
Paletted. PLTE chunk must appear.
Allowed bit depths: 1, 2, 4 and 8.
*/
allowed := false;
for i in ([]u8{1, 2, 4, 8}) {
if bit_depth == i {
allowed = true;
break;
}
}
if !allowed {
return {}, E_PNG.Invalid_Color_Bit_Depth_Combo;
}
if !allowed {
return {}, E_PNG.Invalid_Color_Bit_Depth_Combo;
}
case 2, 4, 6:
/*
RGB, Grayscale+Alpha, RGBA.
Allowed bit depths: 8 and 16
*/
if bit_depth != 8 && bit_depth != 16 {
return {}, E_PNG.Invalid_Color_Bit_Depth_Combo;
}
case 3:
/*
Paletted. PLTE chunk must appear.
Allowed bit depths: 1, 2, 4 and 8.
*/
allowed := false;
for i in ([]u8{1, 2, 4, 8}) {
if bit_depth == i {
allowed = true;
break;
}
}
if !allowed {
return {}, E_PNG.Invalid_Color_Bit_Depth_Combo;
}
case:
return {}, E_PNG.Unknown_Color_Type;
case:
return {}, E_PNG.Unknown_Color_Type;
}
return header, E_General.OK;
return header, nil;
}
chunk_type_to_name :: proc(type: ^Chunk_Type) -> string {
@@ -351,16 +353,16 @@ chunk_type_to_name :: proc(type: ^Chunk_Type) -> string {
}
load_from_slice :: proc(slice: []u8, options := Options{}, allocator := context.allocator) -> (img: ^Image, err: Error) {
r := bytes.Reader{};
bytes.reader_init(&r, slice);
stream := bytes.reader_to_stream(&r);
ctx := &compress.Context_Memory_Input{
input_data = slice,
};
/*
TODO: Add a flag to tell the PNG loader that the stream is backed by a slice.
This way the stream reader could avoid the copy into the temp memory returned by it,
and instead return a slice into the original memory that's already owned by the caller.
*/
img, err = load_from_stream(stream, options, allocator);
img, err = load_from_context(ctx, options, allocator);
return img, err;
}
@@ -378,28 +380,30 @@ load_from_file :: proc(filename: string, options := Options{}, allocator := cont
}
}
load_from_stream :: proc(stream: io.Stream, options := Options{}, allocator := context.allocator) -> (img: ^Image, err: Error) {
load_from_context :: proc(ctx: ^$C, options := Options{}, allocator := context.allocator) -> (img: ^Image, err: Error) {
options := options;
if .info in options {
options |= {.return_metadata, .do_not_decompress_image};
options ~= {.info};
options -= {.info};
}
if .alpha_drop_if_present in options && .alpha_add_if_missing in options {
return {}, E_General.Incompatible_Options;
}
if .do_not_expand_channels in options {
options |= {.do_not_expand_grayscale, .do_not_expand_indexed};
}
if img == nil {
img = new(Image);
}
img.sidecar = nil;
info := new(Info, context.allocator);
img.metadata_ptr = info;
img.metadata_type = typeid_of(Info);
ctx := compress.Context{
input = stream,
};
signature, io_error := compress.read_data(&ctx, Signature);
signature, io_error := compress.read_data(ctx, Signature);
if io_error != .None || signature != .PNG {
return img, E_PNG.Invalid_PNG_Signature;
}
@@ -414,7 +418,7 @@ load_from_stream :: proc(stream: io.Stream, options := Options{}, allocator := c
e: io.Error;
header: IHDR;
info: Info;
info.chunks.allocator = context.temp_allocator;
// State to ensure correct chunk ordering.
@@ -432,275 +436,273 @@ load_from_stream :: proc(stream: io.Stream, options := Options{}, allocator := c
read_error: io.Error;
// 12 bytes is the size of a chunk with a zero-length payload.
for (read_error == .None && !seen_iend) {
for read_error == .None && !seen_iend {
// Peek at next chunk's length and type.
// TODO: Some streams may not provide seek/read_at
ch, e = compress.peek_data(&ctx, Chunk_Header);
ch, e = compress.peek_data(ctx, Chunk_Header);
if e != .None {
return img, E_General.Stream_Too_Short;
}
// name := chunk_type_to_name(&ch.type); // Only used for debug prints during development.
#partial switch(ch.type) {
case .IHDR:
if seen_ihdr || !first {
return {}, E_PNG.IHDR_Not_First_Chunk;
}
seen_ihdr = true;
#partial switch ch.type {
case .IHDR:
if seen_ihdr || !first {
return {}, E_PNG.IHDR_Not_First_Chunk;
}
seen_ihdr = true;
header, err = read_header(&ctx);
if !is_kind(err, E_General.OK) {
return img, err;
}
header, err = read_header(ctx);
if err != nil {
return img, err;
}
if .Paletted in header.color_type {
// Color type 3
img.channels = 1;
final_image_channels = 3;
img.depth = 8;
} else if .Color in header.color_type {
// Color image without a palette
img.channels = 3;
final_image_channels = 3;
img.depth = header.bit_depth;
} else {
// Grayscale
img.channels = 1;
final_image_channels = 1;
img.depth = header.bit_depth;
}
if .Alpha in header.color_type {
img.channels += 1;
final_image_channels += 1;
}
if img.channels == 0 || img.depth == 0 {
return {}, E_PNG.IHDR_Corrupt;
}
img.width = int(header.width);
img.height = int(header.height);
using header;
h := IHDR{
width = width,
height = height,
bit_depth = bit_depth,
color_type = color_type,
compression_method = compression_method,
filter_method = filter_method,
interlace_method = interlace_method,
};
info.header = h;
case .PLTE:
seen_plte = true;
// PLTE must appear before IDAT and can't appear for color types 0, 4.
ct := transmute(u8)info.header.color_type;
if seen_idat || ct == 0 || ct == 4 {
return img, E_PNG.PLTE_Encountered_Unexpectedly;
}
c, err = read_chunk(&ctx);
if !is_kind(err, E_General.OK) {
return img, err;
}
if c.header.length % 3 != 0 || c.header.length > 768 {
return img, E_PNG.PLTE_Invalid_Length;
}
plte_ok: bool;
_plte, plte_ok = plte(c);
if !plte_ok {
return img, E_PNG.PLTE_Invalid_Length;
}
if .return_metadata in options {
append(&info.chunks, c);
}
case .IDAT:
// If we only want image metadata and don't want the pixel data, we can early out.
if .return_metadata not_in options && .do_not_decompress_image in options {
img.channels = final_image_channels;
img.sidecar = info;
return img, E_General.OK;
}
// There must be at least 1 IDAT, contiguous if more.
if seen_idat {
return img, E_PNG.IDAT_Must_Be_Contiguous;
}
if idat_length > 0 {
return img, E_PNG.IDAT_Must_Be_Contiguous;
}
next := ch.type;
for next == .IDAT {
c, err = read_chunk(&ctx);
if !is_kind(err, E_General.OK) {
return img, err;
}
bytes.buffer_write(&idat_b, c.data);
idat_length += c.header.length;
ch, e = compress.peek_data(&ctx, Chunk_Header);
if e != .None {
return img, E_General.Stream_Too_Short;
}
next = ch.type;
}
idat = bytes.buffer_to_bytes(&idat_b);
if int(idat_length) != len(idat) {
return {}, E_PNG.IDAT_Corrupt;
}
seen_idat = true;
case .IEND:
c, err = read_chunk(&ctx);
if !is_kind(err, E_General.OK) {
return img, err;
}
seen_iend = true;
case .bKGD:
// TODO: Make sure that 16-bit bKGD + tRNS chunks return u16 instead of u16be
c, err = read_chunk(&ctx);
if !is_kind(err, E_General.OK) {
return img, err;
}
seen_bkgd = true;
if .return_metadata in options {
append(&info.chunks, c);
}
ct := transmute(u8)info.header.color_type;
switch(ct) {
case 3: // Indexed color
if c.header.length != 1 {
return {}, E_PNG.BKGD_Invalid_Length;
}
col := _plte.entries[c.data[0]];
img.background = [3]u16{
u16(col[0]) << 8 | u16(col[0]),
u16(col[1]) << 8 | u16(col[1]),
u16(col[2]) << 8 | u16(col[2]),
};
case 0, 4: // Grayscale, with and without Alpha
if c.header.length != 2 {
return {}, E_PNG.BKGD_Invalid_Length;
}
col := u16(mem.slice_data_cast([]u16be, c.data[:])[0]);
img.background = [3]u16{col, col, col};
case 2, 6: // Color, with and without Alpha
if c.header.length != 6 {
return {}, E_PNG.BKGD_Invalid_Length;
}
col := mem.slice_data_cast([]u16be, c.data[:]);
img.background = [3]u16{u16(col[0]), u16(col[1]), u16(col[2])};
}
case .tRNS:
c, err = read_chunk(&ctx);
if !is_kind(err, E_General.OK) {
return img, err;
}
if .Alpha in info.header.color_type {
return img, E_PNG.TRNS_Encountered_Unexpectedly;
}
if .return_metadata in options {
append(&info.chunks, c);
}
/*
This makes the image one with transparency, so set it to +1 here,
even if we need we leave img.channels alone for the defilterer's
sake. If we early because the user just cares about metadata,
we'll set it to 'final_image_channels'.
*/
if .Paletted in header.color_type {
// Color type 3
img.channels = 1;
final_image_channels = 3;
img.depth = 8;
} else if .Color in header.color_type {
// Color image without a palette
img.channels = 3;
final_image_channels = 3;
img.depth = int(header.bit_depth);
} else {
// Grayscale
img.channels = 1;
final_image_channels = 1;
img.depth = int(header.bit_depth);
}
if .Alpha in header.color_type {
img.channels += 1;
final_image_channels += 1;
}
seen_trns = true;
if info.header.bit_depth < 8 && .Paletted not_in info.header.color_type {
// Rescale tRNS data so key matches intensity
dsc := depth_scale_table;
scale := dsc[info.header.bit_depth];
if scale != 1 {
key := mem.slice_data_cast([]u16be, c.data)[0] * u16be(scale);
c.data = []u8{0, u8(key & 255)};
}
}
trns = c;
case .iDOT, .CbGI:
/*
iPhone PNG bastardization that doesn't adhere to spec with broken IDAT chunk.
We're not going to add support for it. If you have the misfortunte of coming
across one of these files, use a utility to defry it.s
*/
return img, E_PNG.PNG_Does_Not_Adhere_to_Spec;
case:
// Unhandled type
c, err = read_chunk(&ctx);
if !is_kind(err, E_General.OK) {
if img.channels == 0 || img.depth == 0 {
return {}, E_PNG.IHDR_Corrupt;
}
img.width = int(header.width);
img.height = int(header.height);
using header;
h := IHDR{
width = width,
height = height,
bit_depth = bit_depth,
color_type = color_type,
compression_method = compression_method,
filter_method = filter_method,
interlace_method = interlace_method,
};
info.header = h;
case .PLTE:
seen_plte = true;
// PLTE must appear before IDAT and can't appear for color types 0, 4.
ct := transmute(u8)info.header.color_type;
if seen_idat || ct == 0 || ct == 4 {
return img, E_PNG.PLTE_Encountered_Unexpectedly;
}
c, err = read_chunk(ctx);
if err != nil {
return img, err;
}
if c.header.length % 3 != 0 || c.header.length > 768 {
return img, E_PNG.PLTE_Invalid_Length;
}
plte_ok: bool;
_plte, plte_ok = plte(c);
if !plte_ok {
return img, E_PNG.PLTE_Invalid_Length;
}
if .return_metadata in options {
append(&info.chunks, c);
}
case .IDAT:
// If we only want image metadata and don't want the pixel data, we can early out.
if .return_metadata not_in options && .do_not_decompress_image in options {
img.channels = final_image_channels;
return img, nil;
}
// There must be at least 1 IDAT, contiguous if more.
if seen_idat {
return img, E_PNG.IDAT_Must_Be_Contiguous;
}
if idat_length > 0 {
return img, E_PNG.IDAT_Must_Be_Contiguous;
}
next := ch.type;
for next == .IDAT {
c, err = read_chunk(ctx);
if err != nil {
return img, err;
}
if .return_metadata in options {
// NOTE: Chunk cata is currently allocated on the temp allocator.
append(&info.chunks, c);
bytes.buffer_write(&idat_b, c.data);
idat_length += c.header.length;
ch, e = compress.peek_data(ctx, Chunk_Header);
if e != .None {
return img, E_General.Stream_Too_Short;
}
next = ch.type;
}
idat = bytes.buffer_to_bytes(&idat_b);
if int(idat_length) != len(idat) {
return {}, E_PNG.IDAT_Corrupt;
}
seen_idat = true;
case .IEND:
c, err = read_chunk(ctx);
if err != nil {
return img, err;
}
seen_iend = true;
case .bKGD:
// TODO: Make sure that 16-bit bKGD + tRNS chunks return u16 instead of u16be
c, err = read_chunk(ctx);
if err != nil {
return img, err;
}
seen_bkgd = true;
if .return_metadata in options {
append(&info.chunks, c);
}
ct := transmute(u8)info.header.color_type;
switch ct {
case 3: // Indexed color
if c.header.length != 1 {
return {}, E_PNG.BKGD_Invalid_Length;
}
col := _plte.entries[c.data[0]];
img.background = [3]u16{
u16(col[0]) << 8 | u16(col[0]),
u16(col[1]) << 8 | u16(col[1]),
u16(col[2]) << 8 | u16(col[2]),
};
case 0, 4: // Grayscale, with and without Alpha
if c.header.length != 2 {
return {}, E_PNG.BKGD_Invalid_Length;
}
col := u16(mem.slice_data_cast([]u16be, c.data[:])[0]);
img.background = [3]u16{col, col, col};
case 2, 6: // Color, with and without Alpha
if c.header.length != 6 {
return {}, E_PNG.BKGD_Invalid_Length;
}
col := mem.slice_data_cast([]u16be, c.data[:]);
img.background = [3]u16{u16(col[0]), u16(col[1]), u16(col[2])};
}
case .tRNS:
c, err = read_chunk(ctx);
if err != nil {
return img, err;
}
if .Alpha in info.header.color_type {
return img, E_PNG.TRNS_Encountered_Unexpectedly;
}
if .return_metadata in options {
append(&info.chunks, c);
}
/*
This makes the image one with transparency, so set it to +1 here,
even if we need we leave img.channels alone for the defilterer's
sake. If we early because the user just cares about metadata,
we'll set it to 'final_image_channels'.
*/
final_image_channels += 1;
seen_trns = true;
if info.header.bit_depth < 8 && .Paletted not_in info.header.color_type {
// Rescale tRNS data so key matches intensity
dsc := depth_scale_table;
scale := dsc[info.header.bit_depth];
if scale != 1 {
key := mem.slice_data_cast([]u16be, c.data)[0] * u16be(scale);
c.data = []u8{0, u8(key & 255)};
}
}
trns = c;
case .iDOT, .CbGI:
/*
iPhone PNG bastardization that doesn't adhere to spec with broken IDAT chunk.
We're not going to add support for it. If you have the misfortunte of coming
across one of these files, use a utility to defry it.s
*/
return img, E_PNG.PNG_Does_Not_Adhere_to_Spec;
case:
// Unhandled type
c, err = read_chunk(ctx);
if err != nil {
return img, err;
}
if .return_metadata in options {
// NOTE: Chunk cata is currently allocated on the temp allocator.
append(&info.chunks, c);
}
first = false;
}
}
if .return_header in options || .return_metadata in options {
img.sidecar = info;
}
if .do_not_decompress_image in options {
img.channels = final_image_channels;
return img, E_General.OK;
return img, nil;
}
if !seen_idat {
return img, E_PNG.IDAT_Missing;
}
buf: bytes.Buffer;
zlib_error := zlib.inflate(idat, &buf);
defer bytes.buffer_destroy(&buf);
/*
Calculate the expected output size, to help `inflate` make better decisions about the output buffer.
We'll also use it to check the returned buffer size is what we expected it to be.
if !is_kind(zlib_error, E_General.OK) {
return {}, zlib_error;
Let's calcalate the expected size of the IDAT based on its dimensions, and whether or not it's interlaced.
*/
expected_size: int;
if header.interlace_method != .Adam7 {
expected_size = compute_buffer_size(int(header.width), int(header.height), int(img.channels), int(header.bit_depth), 1);
} else {
/*
Let's calcalate the expected size of the IDAT based on its dimensions,
and whether or not it's interlaced
Because Adam7 divides the image up into sub-images, and each scanline must start
with a filter byte, Adam7 interlaced images can have a larger raw size.
*/
expected_size: int;
buf_len := len(buf.buf);
if header.interlace_method != .Adam7 {
expected_size = compute_buffer_size(int(header.width), int(header.height), int(img.channels), int(header.bit_depth), 1);
} else {
/*
Because Adam7 divides the image up into sub-images, and each scanline must start
with a filter byte, Adam7 interlaced images can have a larger raw size.
*/
for p := 0; p < 7; p += 1 {
x := (int(header.width) - ADAM7_X_ORIG[p] + ADAM7_X_SPACING[p] - 1) / ADAM7_X_SPACING[p];
y := (int(header.height) - ADAM7_Y_ORIG[p] + ADAM7_Y_SPACING[p] - 1) / ADAM7_Y_SPACING[p];
if (x > 0 && y > 0) {
expected_size += compute_buffer_size(int(x), int(y), int(img.channels), int(header.bit_depth), 1);
}
for p := 0; p < 7; p += 1 {
x := (int(header.width) - ADAM7_X_ORIG[p] + ADAM7_X_SPACING[p] - 1) / ADAM7_X_SPACING[p];
y := (int(header.height) - ADAM7_Y_ORIG[p] + ADAM7_Y_SPACING[p] - 1) / ADAM7_Y_SPACING[p];
if x > 0 && y > 0 {
expected_size += compute_buffer_size(int(x), int(y), int(img.channels), int(header.bit_depth), 1);
}
}
}
if expected_size != buf_len {
return {}, E_PNG.IDAT_Corrupt;
}
buf: bytes.Buffer;
zlib_error := zlib.inflate(idat, &buf, false, expected_size);
defer bytes.buffer_destroy(&buf);
if zlib_error != nil {
return {}, zlib_error;
}
buf_len := len(buf.buf);
if expected_size != buf_len {
return {}, E_PNG.IDAT_Corrupt;
}
/*
@@ -709,7 +711,7 @@ load_from_stream :: proc(stream: io.Stream, options := Options{}, allocator := c
as metadata, and set it instead to the raw number of channels.
*/
defilter_error := defilter(img, &buf, &header, options);
if !is_kind(defilter_error, E_General.OK) {
if defilter_error != nil {
bytes.buffer_destroy(&img.pixels);
return {}, defilter_error;
}
@@ -723,6 +725,14 @@ load_from_stream :: proc(stream: io.Stream, options := Options{}, allocator := c
will become the default.
*/
if .Paletted in header.color_type && .do_not_expand_indexed in options {
return img, nil;
}
if .Color not_in header.color_type && .do_not_expand_grayscale in options {
return img, nil;
}
raw_image_channels := img.channels;
out_image_channels := 3;
@@ -828,7 +838,7 @@ load_from_stream :: proc(stream: io.Stream, options := Options{}, allocator := c
// If we have 3 in and 3 out, or 4 in and 4 out without premultiplication...
if raw_image_channels == 4 && .alpha_premultiply not_in options && !seen_bkgd {
// Then we're done.
return img, E_General.OK;
return img, nil;
}
}
@@ -843,7 +853,7 @@ load_from_stream :: proc(stream: io.Stream, options := Options{}, allocator := c
p16 := mem.slice_data_cast([]u16, temp.buf[:]);
o16 := mem.slice_data_cast([]u16, t.buf[:]);
switch (raw_image_channels) {
switch raw_image_channels {
case 1:
// Gray without Alpha. Might have tRNS alpha.
key := u16(0);
@@ -1025,7 +1035,7 @@ load_from_stream :: proc(stream: io.Stream, options := Options{}, allocator := c
// If we have 3 in and 3 out, or 4 in and 4 out without premultiplication...
if !premultiply {
// Then we're done.
return img, E_General.OK;
return img, nil;
}
}
@@ -1040,7 +1050,7 @@ load_from_stream :: proc(stream: io.Stream, options := Options{}, allocator := c
p := mem.slice_data_cast([]u8, temp.buf[:]);
o := mem.slice_data_cast([]u8, t.buf[:]);
switch (raw_image_channels) {
switch raw_image_channels {
case 1:
// Gray without Alpha. Might have tRNS alpha.
key := u8(0);
@@ -1218,7 +1228,7 @@ load_from_stream :: proc(stream: io.Stream, options := Options{}, allocator := c
unreachable("We should never see bit depths other than 8, 16 and 'Paletted' here.");
}
return img, E_General.OK;
return img, nil;
}
@@ -1265,7 +1275,7 @@ defilter_8 :: proc(params: ^Filter_Params) -> (ok: bool) {
nk := row_stride - channels;
filter := Row_Filter(src[0]); src = src[1:];
switch(filter) {
switch filter {
case .None:
copy(dest, src[:row_stride]);
case .Sub:
@@ -1337,41 +1347,41 @@ defilter_less_than_8 :: proc(params: ^Filter_Params) -> (ok: bool) #no_bounds_ch
case .None:
copy(dest, src[:row_stride_in]);
case .Sub:
for i in 0..channels {
for i in 0..=channels {
dest[i] = src[i];
}
for k in 0..nk {
for k in 0..=nk {
dest[channels+k] = (src[channels+k] + dest[k]) & 255;
}
case .Up:
for k in 0..row_stride_in {
for k in 0..=row_stride_in {
dest[k] = (src[k] + up[k]) & 255;
}
case .Average:
for i in 0..channels {
for i in 0..=channels {
avg := up[i] >> 1;
dest[i] = (src[i] + avg) & 255;
}
for k in 0..nk {
for k in 0..=nk {
avg := u8((u16(up[channels+k]) + u16(dest[k])) >> 1);
dest[channels+k] = (src[channels+k] + avg) & 255;
}
case .Paeth:
for i in 0..channels {
for i in 0..=channels {
paeth := filter_paeth(0, up[i], 0);
dest[i] = (src[i] + paeth) & 255;
}
for k in 0..nk {
paeth := filter_paeth(dest[k], up[channels], up[k]);
for k in 0..=nk {
paeth := filter_paeth(dest[k], up[channels+k], up[k]);
dest[channels+k] = (src[channels+k] + paeth) & 255;
}
case:
return false;
}
src = src [row_stride_in:];
up = dest;
dest = dest[row_stride_in:];
src = src[row_stride_in:];
up = dest;
dest = dest[row_stride_in:];
}
// Let's expand the bits
@@ -1579,7 +1589,7 @@ defilter :: proc(img: ^Image, filter_bytes: ^bytes.Buffer, header: ^IHDR, option
i,j,x,y: int;
x = (width - ADAM7_X_ORIG[p] + ADAM7_X_SPACING[p] - 1) / ADAM7_X_SPACING[p];
y = (height - ADAM7_Y_ORIG[p] + ADAM7_Y_SPACING[p] - 1) / ADAM7_Y_SPACING[p];
if (x > 0 && y > 0) {
if x > 0 && y > 0 {
temp: bytes.Buffer;
temp_len := compute_buffer_size(x, y, channels, depth == 16 ? 16 : 8);
resize(&temp.buf, temp_len);
@@ -1640,7 +1650,7 @@ defilter :: proc(img: ^Image, filter_bytes: ^bytes.Buffer, header: ^IHDR, option
}
}
return E_General.OK;
return nil;
}
load :: proc{load_from_file, load_from_slice, load_from_stream};
load :: proc{load_from_file, load_from_slice, load_from_context};
+9
View File
@@ -31,6 +31,13 @@ overflow_add :: proc(lhs, rhs: $T) -> (T, bool) #optional_ok ---
overflow_sub :: proc(lhs, rhs: $T) -> (T, bool) #optional_ok ---
overflow_mul :: proc(lhs, rhs: $T) -> (T, bool) #optional_ok ---
sqrt :: proc(x: $T) -> T where type_is_float(T) ---
mem_copy :: proc(dst, src: rawptr, len: int) ---
mem_copy_non_overlapping :: proc(dst, src: rawptr, len: int) ---
mem_zero :: proc(ptr: rawptr, len: int) ---
fixed_point_mul :: proc(lhs, rhs: $T, #const scale: uint) -> T where type_is_integer(T) ---
fixed_point_div :: proc(lhs, rhs: $T, #const scale: uint) -> T where type_is_integer(T) ---
fixed_point_mul_sat :: proc(lhs, rhs: $T, #const scale: uint) -> T where type_is_integer(T) ---
@@ -129,6 +136,7 @@ type_is_string :: proc($T: typeid) -> bool ---
type_is_typeid :: proc($T: typeid) -> bool ---
type_is_any :: proc($T: typeid) -> bool ---
type_is_endian_platform :: proc($T: typeid) -> bool ---
type_is_endian_little :: proc($T: typeid) -> bool ---
type_is_endian_big :: proc($T: typeid) -> bool ---
type_is_unsigned :: proc($T: typeid) -> bool ---
@@ -159,6 +167,7 @@ type_is_simd_vector :: proc($T: typeid) -> bool ---
type_has_nil :: proc($T: typeid) -> bool ---
type_is_specialization_of :: proc($T, $S: typeid) -> bool ---
type_is_variant_of :: proc($U, $V: typeid) -> bool where type_is_union(U) ---
type_has_field :: proc($T: typeid, $name: string) -> bool ---
+1 -1
View File
@@ -6,7 +6,7 @@ import "core:strconv"
import "intrinsics"
_ :: intrinsics;
Fixed :: struct($Backing: typeid, Fraction_Width: uint)
Fixed :: struct($Backing: typeid, $Fraction_Width: uint)
where
intrinsics.type_is_integer(Backing),
0 <= Fraction_Width,
+10 -16
View File
@@ -41,7 +41,7 @@ scalar_dot :: proc(a, b: $T) -> T where IS_FLOAT(T), !IS_ARRAY(T) {
return a * b;
}
vector_dot :: proc(a, b: $T/[$N]$E) -> (c: E) where IS_NUMERIC(E) {
vector_dot :: proc(a, b: $T/[$N]$E) -> (c: E) where IS_NUMERIC(E) #no_bounds_check {
for i in 0..<N {
c += a[i] * b[i];
}
@@ -60,7 +60,7 @@ quaternion256_dot :: proc(a, b: $T/quaternion256) -> (c: f64) {
dot :: proc{scalar_dot, vector_dot, quaternion64_dot, quaternion128_dot, quaternion256_dot};
inner_product :: dot;
outer_product :: proc(a: $A/[$M]$E, b: $B/[$N]E) -> (out: [M][N]E) where IS_NUMERIC(E) {
outer_product :: proc(a: $A/[$M]$E, b: $B/[$N]E) -> (out: [M][N]E) where IS_NUMERIC(E) #no_bounds_check {
for i in 0..<M {
for j in 0..<N {
out[i][j] = a[i]*b[j];
@@ -156,7 +156,7 @@ projection :: proc(x, normal: $T/[$N]$E) -> T where IS_NUMERIC(E) {
return dot(x, normal) / dot(normal, normal) * normal;
}
identity :: proc($T: typeid/[$N][N]$E) -> (m: T) {
identity :: proc($T: typeid/[$N][N]$E) -> (m: T) #no_bounds_check {
for i in 0..<N {
m[i][i] = E(1);
}
@@ -170,8 +170,7 @@ trace :: proc(m: $T/[$N][N]$E) -> (tr: E) {
return;
}
transpose :: proc(a: $T/[$N][$M]$E) -> (m: T) {
transpose :: proc(a: $T/[$N][$M]$E) -> (m: (T when N == M else [M][N]E)) #no_bounds_check {
for j in 0..<M {
for i in 0..<N {
m[j][i] = a[i][j];
@@ -181,8 +180,7 @@ transpose :: proc(a: $T/[$N][$M]$E) -> (m: T) {
}
matrix_mul :: proc(a, b: $M/[$N][N]$E) -> (c: M)
where !IS_ARRAY(E),
IS_NUMERIC(E) {
where !IS_ARRAY(E), IS_NUMERIC(E) #no_bounds_check {
for i in 0..<N {
for k in 0..<N {
for j in 0..<N {
@@ -194,8 +192,7 @@ matrix_mul :: proc(a, b: $M/[$N][N]$E) -> (c: M)
}
matrix_comp_mul :: proc(a, b: $M/[$J][$I]$E) -> (c: M)
where !IS_ARRAY(E),
IS_NUMERIC(E) {
where !IS_ARRAY(E), IS_NUMERIC(E) #no_bounds_check {
for j in 0..<J {
for i in 0..<I {
c[j][i] = a[j][i] * b[j][i];
@@ -205,9 +202,7 @@ matrix_comp_mul :: proc(a, b: $M/[$J][$I]$E) -> (c: M)
}
matrix_mul_differ :: proc(a: $A/[$J][$I]$E, b: $B/[$K][J]E) -> (c: [K][I]E)
where !IS_ARRAY(E),
IS_NUMERIC(E),
I != K {
where !IS_ARRAY(E), IS_NUMERIC(E), I != K #no_bounds_check {
for k in 0..<K {
for j in 0..<J {
for i in 0..<I {
@@ -220,8 +215,7 @@ matrix_mul_differ :: proc(a: $A/[$J][$I]$E, b: $B/[$K][J]E) -> (c: [K][I]E)
matrix_mul_vector :: proc(a: $A/[$I][$J]$E, b: $B/[I]E) -> (c: B)
where !IS_ARRAY(E),
IS_NUMERIC(E) {
where !IS_ARRAY(E), IS_NUMERIC(E) #no_bounds_check {
for i in 0..<I {
for j in 0..<J {
c[j] += a[i][j] * b[i];
@@ -329,14 +323,14 @@ cubic :: proc(v1, v2, v3, v4: $T/[$N]$E, s: E) -> T {
array_cast :: proc(v: $A/[$N]$T, $Elem_Type: typeid) -> (w: [N]Elem_Type) {
array_cast :: proc(v: $A/[$N]$T, $Elem_Type: typeid) -> (w: [N]Elem_Type) #no_bounds_check {
for i in 0..<N {
w[i] = Elem_Type(v[i]);
}
return;
}
matrix_cast :: proc(v: $A/[$M][$N]$T, $Elem_Type: typeid) -> (w: [M][N]Elem_Type) {
matrix_cast :: proc(v: $A/[$M][$N]$T, $Elem_Type: typeid) -> (w: [M][N]Elem_Type) #no_bounds_check {
for i in 0..<M {
for j in 0..<N {
w[i][j] = Elem_Type(v[i][j]);
+6 -6
View File
@@ -1284,13 +1284,13 @@ matrix3_from_quaternion :: proc{
matrix3_inverse_f16 :: proc(m: Matrix3f16) -> Matrix3f16 {
return transpose(matrix3_inverse_transpose(m));
return auto_cast transpose(matrix3_inverse_transpose(m));
}
matrix3_inverse_f32 :: proc(m: Matrix3f32) -> Matrix3f32 {
return transpose(matrix3_inverse_transpose(m));
return auto_cast transpose(matrix3_inverse_transpose(m));
}
matrix3_inverse_f64 :: proc(m: Matrix3f64) -> Matrix3f64 {
return transpose(matrix3_inverse_transpose(m));
return auto_cast transpose(matrix3_inverse_transpose(m));
}
matrix3_inverse :: proc{
matrix3_inverse_f16,
@@ -1655,13 +1655,13 @@ matrix4_from_trs :: proc{
matrix4_inverse_f16 :: proc(m: Matrix4f16) -> Matrix4f16 {
return transpose(matrix4_inverse_transpose(m));
return auto_cast transpose(matrix4_inverse_transpose(m));
}
matrix4_inverse_f32 :: proc(m: Matrix4f32) -> Matrix4f32 {
return transpose(matrix4_inverse_transpose(m));
return auto_cast transpose(matrix4_inverse_transpose(m));
}
matrix4_inverse_f64 :: proc(m: Matrix4f64) -> Matrix4f64 {
return transpose(matrix4_inverse_transpose(m));
return auto_cast transpose(matrix4_inverse_transpose(m));
}
matrix4_inverse :: proc{
matrix4_inverse_f16,
+540 -165
View File
@@ -96,45 +96,186 @@ foreign _ {
ldexp_f64 :: proc(val: f64, exp: i32) -> f64 ---;
}
sqrt :: proc{sqrt_f16, sqrt_f32, sqrt_f64};
sin :: proc{sin_f16, sin_f32, sin_f64};
cos :: proc{cos_f16, cos_f32, cos_f64};
pow :: proc{pow_f16, pow_f32, pow_f64};
fmuladd :: proc{fmuladd_f16, fmuladd_f32, fmuladd_f64};
ln :: proc{ln_f16, ln_f32, ln_f64};
exp :: proc{exp_f16, exp_f32, exp_f64};
sqrt_f16le :: proc(x: f16le) -> f16le { return #force_inline f16le(sqrt_f16(f16(x))); }
sqrt_f16be :: proc(x: f16be) -> f16be { return #force_inline f16be(sqrt_f16(f16(x))); }
sqrt_f32le :: proc(x: f32le) -> f32le { return #force_inline f32le(sqrt_f32(f32(x))); }
sqrt_f32be :: proc(x: f32be) -> f32be { return #force_inline f32be(sqrt_f32(f32(x))); }
sqrt_f64le :: proc(x: f64le) -> f64le { return #force_inline f64le(sqrt_f64(f64(x))); }
sqrt_f64be :: proc(x: f64be) -> f64be { return #force_inline f64be(sqrt_f64(f64(x))); }
sqrt :: proc{
sqrt_f16, sqrt_f16le, sqrt_f16be,
sqrt_f32, sqrt_f32le, sqrt_f32be,
sqrt_f64, sqrt_f64le, sqrt_f64be,
};
ldexp :: proc{ldexp_f16, ldexp_f32, ldexp_f64};
sin_f16le :: proc(θ: f16le) -> f16le { return #force_inline f16le(sin_f16(f16(θ))); }
sin_f16be :: proc(θ: f16be) -> f16be { return #force_inline f16be(sin_f16(f16(θ))); }
sin_f32le :: proc(θ: f32le) -> f32le { return #force_inline f32le(sin_f32(f32(θ))); }
sin_f32be :: proc(θ: f32be) -> f32be { return #force_inline f32be(sin_f32(f32(θ))); }
sin_f64le :: proc(θ: f64le) -> f64le { return #force_inline f64le(sin_f64(f64(θ))); }
sin_f64be :: proc(θ: f64be) -> f64be { return #force_inline f64be(sin_f64(f64(θ))); }
sin :: proc{
sin_f16, sin_f16le, sin_f16be,
sin_f32, sin_f32le, sin_f32be,
sin_f64, sin_f64le, sin_f64be,
};
log_f16 :: proc(x, base: f16) -> f16 { return ln(x) / ln(base); }
log_f32 :: proc(x, base: f32) -> f32 { return ln(x) / ln(base); }
log_f64 :: proc(x, base: f64) -> f64 { return ln(x) / ln(base); }
log :: proc{log_f16, log_f32, log_f64};
cos_f16le :: proc(θ: f16le) -> f16le { return #force_inline f16le(cos_f16(f16(θ))); }
cos_f16be :: proc(θ: f16be) -> f16be { return #force_inline f16be(cos_f16(f16(θ))); }
cos_f32le :: proc(θ: f32le) -> f32le { return #force_inline f32le(cos_f32(f32(θ))); }
cos_f32be :: proc(θ: f32be) -> f32be { return #force_inline f32be(cos_f32(f32(θ))); }
cos_f64le :: proc(θ: f64le) -> f64le { return #force_inline f64le(cos_f64(f64(θ))); }
cos_f64be :: proc(θ: f64be) -> f64be { return #force_inline f64be(cos_f64(f64(θ))); }
cos :: proc{
cos_f16, cos_f16le, cos_f16be,
cos_f32, cos_f32le, cos_f32be,
cos_f64, cos_f64le, cos_f64be,
};
log2_f16 :: proc(x: f16) -> f16 { return ln(x)/LN2; }
log2_f32 :: proc(x: f32) -> f32 { return ln(x)/LN2; }
log2_f64 :: proc(x: f64) -> f64 { return ln(x)/LN2; }
log2 :: proc{log2_f16, log2_f32, log2_f64};
pow_f16le :: proc(x, power: f16le) -> f16le { return #force_inline f16le(pow_f16(f16(x), f16(power))); }
pow_f16be :: proc(x, power: f16be) -> f16be { return #force_inline f16be(pow_f16(f16(x), f16(power))); }
pow_f32le :: proc(x, power: f32le) -> f32le { return #force_inline f32le(pow_f32(f32(x), f32(power))); }
pow_f32be :: proc(x, power: f32be) -> f32be { return #force_inline f32be(pow_f32(f32(x), f32(power))); }
pow_f64le :: proc(x, power: f64le) -> f64le { return #force_inline f64le(pow_f64(f64(x), f64(power))); }
pow_f64be :: proc(x, power: f64be) -> f64be { return #force_inline f64be(pow_f64(f64(x), f64(power))); }
pow :: proc{
pow_f16, pow_f16le, pow_f16be,
pow_f32, pow_f32le, pow_f32be,
pow_f64, pow_f64le, pow_f64be,
};
log10_f16 :: proc(x: f16) -> f16 { return ln(x)/LN10; }
log10_f32 :: proc(x: f32) -> f32 { return ln(x)/LN10; }
log10_f64 :: proc(x: f64) -> f64 { return ln(x)/LN10; }
log10 :: proc{log10_f16, log10_f32, log10_f64};
fmuladd_f16le :: proc(a, b, c: f16le) -> f16le { return #force_inline f16le(fmuladd_f16(f16(a), f16(b), f16(c))); }
fmuladd_f16be :: proc(a, b, c: f16be) -> f16be { return #force_inline f16be(fmuladd_f16(f16(a), f16(b), f16(c))); }
fmuladd_f32le :: proc(a, b, c: f32le) -> f32le { return #force_inline f32le(fmuladd_f32(f32(a), f32(b), f32(c))); }
fmuladd_f32be :: proc(a, b, c: f32be) -> f32be { return #force_inline f32be(fmuladd_f32(f32(a), f32(b), f32(c))); }
fmuladd_f64le :: proc(a, b, c: f64le) -> f64le { return #force_inline f64le(fmuladd_f64(f64(a), f64(b), f64(c))); }
fmuladd_f64be :: proc(a, b, c: f64be) -> f64be { return #force_inline f64be(fmuladd_f64(f64(a), f64(b), f64(c))); }
fmuladd :: proc{
fmuladd_f16, fmuladd_f16le, fmuladd_f16be,
fmuladd_f32, fmuladd_f32le, fmuladd_f32be,
fmuladd_f64, fmuladd_f64le, fmuladd_f64be,
};
ln_f16le :: proc(x: f16le) -> f16le { return #force_inline f16le(ln_f16(f16(x))); }
ln_f16be :: proc(x: f16be) -> f16be { return #force_inline f16be(ln_f16(f16(x))); }
ln_f32le :: proc(x: f32le) -> f32le { return #force_inline f32le(ln_f32(f32(x))); }
ln_f32be :: proc(x: f32be) -> f32be { return #force_inline f32be(ln_f32(f32(x))); }
ln_f64le :: proc(x: f64le) -> f64le { return #force_inline f64le(ln_f64(f64(x))); }
ln_f64be :: proc(x: f64be) -> f64be { return #force_inline f64be(ln_f64(f64(x))); }
ln :: proc{
ln_f16, ln_f16le, ln_f16be,
ln_f32, ln_f32le, ln_f32be,
ln_f64, ln_f64le, ln_f64be,
};
exp_f16le :: proc(x: f16le) -> f16le { return #force_inline f16le(exp_f16(f16(x))); }
exp_f16be :: proc(x: f16be) -> f16be { return #force_inline f16be(exp_f16(f16(x))); }
exp_f32le :: proc(x: f32le) -> f32le { return #force_inline f32le(exp_f32(f32(x))); }
exp_f32be :: proc(x: f32be) -> f32be { return #force_inline f32be(exp_f32(f32(x))); }
exp_f64le :: proc(x: f64le) -> f64le { return #force_inline f64le(exp_f64(f64(x))); }
exp_f64be :: proc(x: f64be) -> f64be { return #force_inline f64be(exp_f64(f64(x))); }
exp :: proc{
exp_f16, exp_f16le, exp_f16be,
exp_f32, exp_f32le, exp_f32be,
exp_f64, exp_f64le, exp_f64be,
};
ldexp_f16le :: proc(val: f16le, exp: i32) -> f16le { return #force_inline f16le(ldexp_f16(f16(val), exp)); }
ldexp_f16be :: proc(val: f16be, exp: i32) -> f16be { return #force_inline f16be(ldexp_f16(f16(val), exp)); }
ldexp_f32le :: proc(val: f32le, exp: i32) -> f32le { return #force_inline f32le(ldexp_f32(f32(val), exp)); }
ldexp_f32be :: proc(val: f32be, exp: i32) -> f32be { return #force_inline f32be(ldexp_f32(f32(val), exp)); }
ldexp_f64le :: proc(val: f64le, exp: i32) -> f64le { return #force_inline f64le(ldexp_f64(f64(val), exp)); }
ldexp_f64be :: proc(val: f64be, exp: i32) -> f64be { return #force_inline f64be(ldexp_f64(f64(val), exp)); }
ldexp :: proc{
ldexp_f16, ldexp_f16le, ldexp_f16be,
ldexp_f32, ldexp_f32le, ldexp_f32be,
ldexp_f64, ldexp_f64le, ldexp_f64be,
};
tan_f16 :: proc(θ: f16) -> f16 { return sin(θ)/cos(θ); }
tan_f32 :: proc(θ: f32) -> f32 { return sin(θ)/cos(θ); }
tan_f64 :: proc(θ: f64) -> f64 { return sin(θ)/cos(θ); }
tan :: proc{tan_f16, tan_f32, tan_f64};
log_f16 :: proc(x, base: f16) -> f16 { return ln(x) / ln(base); }
log_f16le :: proc(x, base: f16le) -> f16le { return f16le(log_f16(f16(x), f16(base))); }
log_f16be :: proc(x, base: f16be) -> f16be { return f16be(log_f16(f16(x), f16(base))); }
log_f32 :: proc(x, base: f32) -> f32 { return ln(x) / ln(base); }
log_f32le :: proc(x, base: f32le) -> f32le { return f32le(log_f32(f32(x), f32(base))); }
log_f32be :: proc(x, base: f32be) -> f32be { return f32be(log_f32(f32(x), f32(base))); }
log_f64 :: proc(x, base: f64) -> f64 { return ln(x) / ln(base); }
log_f64le :: proc(x, base: f64le) -> f64le { return f64le(log_f64(f64(x), f64(base))); }
log_f64be :: proc(x, base: f64be) -> f64be { return f64be(log_f64(f64(x), f64(base))); }
log :: proc{
log_f16, log_f16le, log_f16be,
log_f32, log_f32le, log_f32be,
log_f64, log_f64le, log_f64be,
};
log2_f16 :: proc(x: f16) -> f16 { return ln(x)/LN2; }
log2_f16le :: proc(x: f16le) -> f16le { return f16le(log2_f16(f16(x))); }
log2_f16be :: proc(x: f16be) -> f16be { return f16be(log2_f16(f16(x))); }
log2_f32 :: proc(x: f32) -> f32 { return ln(x)/LN2; }
log2_f32le :: proc(x: f32le) -> f32le { return f32le(log2_f32(f32(x))); }
log2_f32be :: proc(x: f32be) -> f32be { return f32be(log2_f32(f32(x))); }
log2_f64 :: proc(x: f64) -> f64 { return ln(x)/LN2; }
log2_f64le :: proc(x: f64le) -> f64le { return f64le(log2_f64(f64(x))); }
log2_f64be :: proc(x: f64be) -> f64be { return f64be(log2_f64(f64(x))); }
log2 :: proc{
log2_f16, log2_f16le, log2_f16be,
log2_f32, log2_f32le, log2_f32be,
log2_f64, log2_f64le, log2_f64be,
};
log10_f16 :: proc(x: f16) -> f16 { return ln(x)/LN10; }
log10_f16le :: proc(x: f16le) -> f16le { return f16le(log10_f16(f16(x))); }
log10_f16be :: proc(x: f16be) -> f16be { return f16be(log10_f16(f16(x))); }
log10_f32 :: proc(x: f32) -> f32 { return ln(x)/LN10; }
log10_f32le :: proc(x: f32le) -> f32le { return f32le(log10_f32(f32(x))); }
log10_f32be :: proc(x: f32be) -> f32be { return f32be(log10_f32(f32(x))); }
log10_f64 :: proc(x: f64) -> f64 { return ln(x)/LN10; }
log10_f64le :: proc(x: f64le) -> f64le { return f64le(log10_f64(f64(x))); }
log10_f64be :: proc(x: f64be) -> f64be { return f64be(log10_f64(f64(x))); }
log10 :: proc{
log10_f16, log10_f16le, log10_f16be,
log10_f32, log10_f32le, log10_f32be,
log10_f64, log10_f64le, log10_f64be,
};
tan_f16 :: proc(θ: f16) -> f16 { return sin(θ)/cos(θ); }
tan_f16le :: proc(θ: f16le) -> f16le { return f16le(tan_f16(f16(θ))); }
tan_f16be :: proc(θ: f16be) -> f16be { return f16be(tan_f16(f16(θ))); }
tan_f32 :: proc(θ: f32) -> f32 { return sin(θ)/cos(θ); }
tan_f32le :: proc(θ: f32le) -> f32le { return f32le(tan_f32(f32(θ))); }
tan_f32be :: proc(θ: f32be) -> f32be { return f32be(tan_f32(f32(θ))); }
tan_f64 :: proc(θ: f64) -> f64 { return sin(θ)/cos(θ); }
tan_f64le :: proc(θ: f64le) -> f64le { return f64le(tan_f64(f64(θ))); }
tan_f64be :: proc(θ: f64be) -> f64be { return f64be(tan_f64(f64(θ))); }
tan :: proc{
tan_f16, tan_f16le, tan_f16be,
tan_f32, tan_f32le, tan_f32be,
tan_f64, tan_f64le, tan_f64be,
};
lerp :: proc(a, b: $T, t: $E) -> (x: T) { return a*(1-t) + b*t; }
saturate :: proc(a: $T) -> (x: T) { return clamp(a, 0, 1); };
unlerp_f16 :: proc(a, b, x: f16) -> (t: f16) { return (x-a)/(b-a); }
unlerp_f32 :: proc(a, b, x: f32) -> (t: f32) { return (x-a)/(b-a); }
unlerp_f64 :: proc(a, b, x: f64) -> (t: f64) { return (x-a)/(b-a); }
unlerp :: proc{unlerp_f16, unlerp_f32, unlerp_f64};
unlerp :: proc(a, b, x: $T) -> (t: T) where intrinsics.type_is_float(T), !intrinsics.type_is_array(T) {
return (x-a)/(b-a);
}
remap :: proc(old_value, old_min, old_max, new_min, new_max: $T) -> (x: T) where intrinsics.type_is_numeric(T), !intrinsics.type_is_array(T) {
old_range := old_max - old_min;
new_range := new_max - new_min;
if old_range == 0 {
return new_range / 2;
}
return ((old_value - old_min) / old_range) * new_range + new_min;
}
wrap :: proc(x, y: $T) -> T where intrinsics.type_is_numeric(T), !intrinsics.type_is_array(T) {
tmp := mod(x, y);
@@ -170,57 +311,105 @@ gain :: proc(t, g: $T) -> T where intrinsics.type_is_numeric(T) {
}
sign_f16 :: proc(x: f16) -> f16 { return f16(int(0 < x) - int(x < 0)); }
sign_f32 :: proc(x: f32) -> f32 { return f32(int(0 < x) - int(x < 0)); }
sign_f64 :: proc(x: f64) -> f64 { return f64(int(0 < x) - int(x < 0)); }
sign :: proc{sign_f16, sign_f32, sign_f64};
sign_f16 :: proc(x: f16) -> f16 { return f16(int(0 < x) - int(x < 0)); }
sign_f16le :: proc(x: f16le) -> f16le { return f16le(int(0 < x) - int(x < 0)); }
sign_f16be :: proc(x: f16be) -> f16be { return f16be(int(0 < x) - int(x < 0)); }
sign_f32 :: proc(x: f32) -> f32 { return f32(int(0 < x) - int(x < 0)); }
sign_f32le :: proc(x: f32le) -> f32le { return f32le(int(0 < x) - int(x < 0)); }
sign_f32be :: proc(x: f32be) -> f32be { return f32be(int(0 < x) - int(x < 0)); }
sign_f64 :: proc(x: f64) -> f64 { return f64(int(0 < x) - int(x < 0)); }
sign_f64le :: proc(x: f64le) -> f64le { return f64le(int(0 < x) - int(x < 0)); }
sign_f64be :: proc(x: f64be) -> f64be { return f64be(int(0 < x) - int(x < 0)); }
sign :: proc{
sign_f16, sign_f16le, sign_f16be,
sign_f32, sign_f32le, sign_f32be,
sign_f64, sign_f64le, sign_f64be,
};
sign_bit_f16 :: proc(x: f16) -> bool {
sign_bit_f16 :: proc(x: f16) -> bool {
return (transmute(u16)x) & (1<<15) != 0;
}
sign_bit_f32 :: proc(x: f32) -> bool {
sign_bit_f16le :: proc(x: f16le) -> bool { return #force_inline sign_bit_f16(f16(x)); }
sign_bit_f16be :: proc(x: f16be) -> bool { return #force_inline sign_bit_f16(f16(x)); }
sign_bit_f32 :: proc(x: f32) -> bool {
return (transmute(u32)x) & (1<<31) != 0;
}
sign_bit_f64 :: proc(x: f64) -> bool {
sign_bit_f32le :: proc(x: f32le) -> bool { return #force_inline sign_bit_f32(f32(x)); }
sign_bit_f32be :: proc(x: f32be) -> bool { return #force_inline sign_bit_f32(f32(x)); }
sign_bit_f64 :: proc(x: f64) -> bool {
return (transmute(u64)x) & (1<<63) != 0;
}
sign_bit :: proc{sign_bit_f16, sign_bit_f32, sign_bit_f64};
sign_bit_f64le :: proc(x: f64le) -> bool { return #force_inline sign_bit_f64(f64(x)); }
sign_bit_f64be :: proc(x: f64be) -> bool { return #force_inline sign_bit_f64(f64(x)); }
sign_bit :: proc{
sign_bit_f16, sign_bit_f16le, sign_bit_f16be,
sign_bit_f32, sign_bit_f32le, sign_bit_f32be,
sign_bit_f64, sign_bit_f64le, sign_bit_f64be,
};
copy_sign_f16 :: proc(x, y: f16) -> f16 {
copy_sign_f16 :: proc(x, y: f16) -> f16 {
ix := transmute(u16)x;
iy := transmute(u16)y;
ix &= 0x7fff;
ix |= iy & 0x8000;
return transmute(f16)ix;
}
copy_sign_f32 :: proc(x, y: f32) -> f32 {
copy_sign_f16le :: proc(x, y: f16le) -> f16le { return #force_inline f16le(copy_sign_f16(f16(x), f16(y))); }
copy_sign_f16be :: proc(x, y: f16be) -> f16be { return #force_inline f16be(copy_sign_f16(f16(x), f16(y))); }
copy_sign_f32 :: proc(x, y: f32) -> f32 {
ix := transmute(u32)x;
iy := transmute(u32)y;
ix &= 0x7fff_ffff;
ix |= iy & 0x8000_0000;
return transmute(f32)ix;
}
copy_sign_f64 :: proc(x, y: f64) -> f64 {
copy_sign_f32le :: proc(x, y: f32le) -> f32le { return #force_inline f32le(copy_sign_f32(f32(x), f32(y))); }
copy_sign_f32be :: proc(x, y: f32be) -> f32be { return #force_inline f32be(copy_sign_f32(f32(x), f32(y))); }
copy_sign_f64 :: proc(x, y: f64) -> f64 {
ix := transmute(u64)x;
iy := transmute(u64)y;
ix &= 0x7fff_ffff_ffff_ffff;
ix |= iy & 0x8000_0000_0000_0000;
return transmute(f64)ix;
}
copy_sign :: proc{copy_sign_f16, copy_sign_f32, copy_sign_f64};
copy_sign_f64le :: proc(x, y: f64le) -> f64le { return #force_inline f64le(copy_sign_f64(f64(x), f64(y))); }
copy_sign_f64be :: proc(x, y: f64be) -> f64be { return #force_inline f64be(copy_sign_f64(f64(x), f64(y))); }
copy_sign :: proc{
copy_sign_f16, copy_sign_f16le, copy_sign_f16be,
copy_sign_f32, copy_sign_f32le, copy_sign_f32be,
copy_sign_f64, copy_sign_f64le, copy_sign_f64be,
};
to_radians_f16 :: proc(degrees: f16) -> f16 { return degrees * RAD_PER_DEG; }
to_radians_f16le :: proc(degrees: f16le) -> f16le { return degrees * RAD_PER_DEG; }
to_radians_f16be :: proc(degrees: f16be) -> f16be { return degrees * RAD_PER_DEG; }
to_radians_f32 :: proc(degrees: f32) -> f32 { return degrees * RAD_PER_DEG; }
to_radians_f32le :: proc(degrees: f32le) -> f32le { return degrees * RAD_PER_DEG; }
to_radians_f32be :: proc(degrees: f32be) -> f32be { return degrees * RAD_PER_DEG; }
to_radians_f64 :: proc(degrees: f64) -> f64 { return degrees * RAD_PER_DEG; }
to_radians_f64le :: proc(degrees: f64le) -> f64le { return degrees * RAD_PER_DEG; }
to_radians_f64be :: proc(degrees: f64be) -> f64be { return degrees * RAD_PER_DEG; }
to_degrees_f16 :: proc(radians: f16) -> f16 { return radians * DEG_PER_RAD; }
to_degrees_f16le :: proc(radians: f16le) -> f16le { return radians * DEG_PER_RAD; }
to_degrees_f16be :: proc(radians: f16be) -> f16be { return radians * DEG_PER_RAD; }
to_degrees_f32 :: proc(radians: f32) -> f32 { return radians * DEG_PER_RAD; }
to_degrees_f32le :: proc(radians: f32le) -> f32le { return radians * DEG_PER_RAD; }
to_degrees_f32be :: proc(radians: f32be) -> f32be { return radians * DEG_PER_RAD; }
to_degrees_f64 :: proc(radians: f64) -> f64 { return radians * DEG_PER_RAD; }
to_degrees_f64le :: proc(radians: f64le) -> f64le { return radians * DEG_PER_RAD; }
to_degrees_f64be :: proc(radians: f64be) -> f64be { return radians * DEG_PER_RAD; }
to_radians :: proc{
to_radians_f16, to_radians_f16le, to_radians_f16be,
to_radians_f32, to_radians_f32le, to_radians_f32be,
to_radians_f64, to_radians_f64le, to_radians_f64be,
};
to_degrees :: proc{
to_degrees_f16, to_degrees_f16le, to_degrees_f16be,
to_degrees_f32, to_degrees_f32le, to_degrees_f32be,
to_degrees_f64, to_degrees_f64le, to_degrees_f64be,
};
to_radians_f16 :: proc(degrees: f16) -> f16 { return degrees * RAD_PER_DEG; }
to_radians_f32 :: proc(degrees: f32) -> f32 { return degrees * RAD_PER_DEG; }
to_radians_f64 :: proc(degrees: f64) -> f64 { return degrees * RAD_PER_DEG; }
to_degrees_f16 :: proc(radians: f16) -> f16 { return radians * DEG_PER_RAD; }
to_degrees_f32 :: proc(radians: f32) -> f32 { return radians * DEG_PER_RAD; }
to_degrees_f64 :: proc(radians: f64) -> f64 { return radians * DEG_PER_RAD; }
to_radians :: proc{to_radians_f16, to_radians_f32, to_radians_f64};
to_degrees :: proc{to_degrees_f16, to_degrees_f32, to_degrees_f64};
trunc_f16 :: proc(x: f16) -> f16 {
trunc_f16 :: proc(x: f16) -> f16 {
trunc_internal :: proc(f: f16) -> f16 {
mask :: 0x1f;
shift :: 16 - 6;
@@ -249,8 +438,10 @@ trunc_f16 :: proc(x: f16) -> f16 {
}
return trunc_internal(x);
}
trunc_f16le :: proc(x: f16le) -> f16le { return #force_inline f16le(trunc_f16(f16(x))); }
trunc_f16be :: proc(x: f16be) -> f16be { return #force_inline f16be(trunc_f16(f16(x))); }
trunc_f32 :: proc(x: f32) -> f32 {
trunc_f32 :: proc(x: f32) -> f32 {
trunc_internal :: proc(f: f32) -> f32 {
mask :: 0xff;
shift :: 32 - 9;
@@ -279,8 +470,10 @@ trunc_f32 :: proc(x: f32) -> f32 {
}
return trunc_internal(x);
}
trunc_f32le :: proc(x: f32le) -> f32le { return #force_inline f32le(trunc_f32(f32(x))); }
trunc_f32be :: proc(x: f32be) -> f32be { return #force_inline f32be(trunc_f32(f32(x))); }
trunc_f64 :: proc(x: f64) -> f64 {
trunc_f64 :: proc(x: f64) -> f64 {
trunc_internal :: proc(f: f64) -> f64 {
mask :: 0x7ff;
shift :: 64 - 12;
@@ -309,27 +502,68 @@ trunc_f64 :: proc(x: f64) -> f64 {
}
return trunc_internal(x);
}
trunc_f64le :: proc(x: f64le) -> f64le { return #force_inline f64le(trunc_f64(f64(x))); }
trunc_f64be :: proc(x: f64be) -> f64be { return #force_inline f64be(trunc_f64(f64(x))); }
trunc :: proc{
trunc_f16, trunc_f16le, trunc_f16be,
trunc_f32, trunc_f32le, trunc_f32be,
trunc_f64, trunc_f64le, trunc_f64be,
};
trunc :: proc{trunc_f16, trunc_f32, trunc_f64};
round_f16 :: proc(x: f16) -> f16 {
round_f16 :: proc(x: f16) -> f16 {
return ceil(x - 0.5) if x < 0 else floor(x + 0.5);
}
round_f32 :: proc(x: f32) -> f32 {
round_f16le :: proc(x: f16le) -> f16le {
return ceil(x - 0.5) if x < 0 else floor(x + 0.5);
}
round_f64 :: proc(x: f64) -> f64 {
round_f16be :: proc(x: f16be) -> f16be {
return ceil(x - 0.5) if x < 0 else floor(x + 0.5);
}
round :: proc{round_f16, round_f32, round_f64};
round_f32 :: proc(x: f32) -> f32 {
return ceil(x - 0.5) if x < 0 else floor(x + 0.5);
}
round_f32le :: proc(x: f32le) -> f32le {
return ceil(x - 0.5) if x < 0 else floor(x + 0.5);
}
round_f32be :: proc(x: f32be) -> f32be {
return ceil(x - 0.5) if x < 0 else floor(x + 0.5);
}
round_f64 :: proc(x: f64) -> f64 {
return ceil(x - 0.5) if x < 0 else floor(x + 0.5);
}
round_f64le :: proc(x: f64le) -> f64le {
return ceil(x - 0.5) if x < 0 else floor(x + 0.5);
}
round_f64be :: proc(x: f64be) -> f64be {
return ceil(x - 0.5) if x < 0 else floor(x + 0.5);
}
round :: proc{
round_f16, round_f16le, round_f16be,
round_f32, round_f32le, round_f32be,
round_f64, round_f64le, round_f64be,
};
ceil_f16 :: proc(x: f16) -> f16 { return -floor(-x); }
ceil_f32 :: proc(x: f32) -> f32 { return -floor(-x); }
ceil_f64 :: proc(x: f64) -> f64 { return -floor(-x); }
ceil :: proc{ceil_f16, ceil_f32, ceil_f64};
ceil_f16 :: proc(x: f16) -> f16 { return -floor(-x); }
ceil_f16le :: proc(x: f16le) -> f16le { return -floor(-x); }
ceil_f16be :: proc(x: f16be) -> f16be { return -floor(-x); }
floor_f16 :: proc(x: f16) -> f16 {
ceil_f32 :: proc(x: f32) -> f32 { return -floor(-x); }
ceil_f32le :: proc(x: f32le) -> f32le { return -floor(-x); }
ceil_f32be :: proc(x: f32be) -> f32be { return -floor(-x); }
ceil_f64 :: proc(x: f64) -> f64 { return -floor(-x); }
ceil_f64le :: proc(x: f64le) -> f64le { return -floor(-x); }
ceil_f64be :: proc(x: f64be) -> f64be { return -floor(-x); }
ceil :: proc{
ceil_f16, ceil_f16le, ceil_f16be,
ceil_f32, ceil_f32le, ceil_f32be,
ceil_f64, ceil_f64le, ceil_f64be,
};
floor_f16 :: proc(x: f16) -> f16 {
if x == 0 || is_nan(x) || is_inf(x) {
return x;
}
@@ -343,7 +577,9 @@ floor_f16 :: proc(x: f16) -> f16 {
d, _ := modf(x);
return d;
}
floor_f32 :: proc(x: f32) -> f32 {
floor_f16le :: proc(x: f16le) -> f16le { return #force_inline f16le(floor_f16(f16(x))); }
floor_f16be :: proc(x: f16be) -> f16be { return #force_inline f16be(floor_f16(f16(x))); }
floor_f32 :: proc(x: f32) -> f32 {
if x == 0 || is_nan(x) || is_inf(x) {
return x;
}
@@ -357,7 +593,9 @@ floor_f32 :: proc(x: f32) -> f32 {
d, _ := modf(x);
return d;
}
floor_f64 :: proc(x: f64) -> f64 {
floor_f32le :: proc(x: f32le) -> f32le { return #force_inline f32le(floor_f32(f32(x))); }
floor_f32be :: proc(x: f32be) -> f32be { return #force_inline f32be(floor_f32(f32(x))); }
floor_f64 :: proc(x: f64) -> f64 {
if x == 0 || is_nan(x) || is_inf(x) {
return x;
}
@@ -371,7 +609,13 @@ floor_f64 :: proc(x: f64) -> f64 {
d, _ := modf(x);
return d;
}
floor :: proc{floor_f16, floor_f32, floor_f64};
floor_f64le :: proc(x: f64le) -> f64le { return #force_inline f64le(floor_f64(f64(x))); }
floor_f64be :: proc(x: f64be) -> f64be { return #force_inline f64be(floor_f64(f64(x))); }
floor :: proc{
floor_f16, floor_f16le, floor_f16be,
floor_f32, floor_f32le, floor_f32be,
floor_f64, floor_f64le, floor_f64be,
};
floor_div :: proc(x, y: $T) -> T
@@ -393,7 +637,7 @@ floor_mod :: proc(x, y: $T) -> T
return r;
}
modf_f16 :: proc(x: f16) -> (int: f16, frac: f16) {
modf_f16 :: proc(x: f16) -> (int: f16, frac: f16) {
shift :: 16 - 5 - 1;
mask :: 0x1f;
bias :: 15;
@@ -419,7 +663,15 @@ modf_f16 :: proc(x: f16) -> (int: f16, frac: f16) {
frac = x - int;
return;
}
modf_f32 :: proc(x: f32) -> (int: f32, frac: f32) {
modf_f16le :: proc(x: f16le) -> (int: f16le, frac: f16le) {
i, f := #force_inline modf_f16(f16(x));
return f16le(i), f16le(f);
}
modf_f16be :: proc(x: f16be) -> (int: f16be, frac: f16be) {
i, f := #force_inline modf_f16(f16(x));
return f16be(i), f16be(f);
}
modf_f32 :: proc(x: f32) -> (int: f32, frac: f32) {
shift :: 32 - 8 - 1;
mask :: 0xff;
bias :: 127;
@@ -445,7 +697,15 @@ modf_f32 :: proc(x: f32) -> (int: f32, frac: f32) {
frac = x - int;
return;
}
modf_f64 :: proc(x: f64) -> (int: f64, frac: f64) {
modf_f32le :: proc(x: f32le) -> (int: f32le, frac: f32le) {
i, f := #force_inline modf_f32(f32(x));
return f32le(i), f32le(f);
}
modf_f32be :: proc(x: f32be) -> (int: f32be, frac: f32be) {
i, f := #force_inline modf_f32(f32(x));
return f32be(i), f32be(f);
}
modf_f64 :: proc(x: f64) -> (int: f64, frac: f64) {
shift :: 64 - 11 - 1;
mask :: 0x7ff;
bias :: 1023;
@@ -471,10 +731,22 @@ modf_f64 :: proc(x: f64) -> (int: f64, frac: f64) {
frac = x - int;
return;
}
modf :: proc{modf_f16, modf_f32, modf_f64};
modf_f64le :: proc(x: f64le) -> (int: f64le, frac: f64le) {
i, f := #force_inline modf_f64(f64(x));
return f64le(i), f64le(f);
}
modf_f64be :: proc(x: f64be) -> (int: f64be, frac: f64be) {
i, f := #force_inline modf_f64(f64(x));
return f64be(i), f64be(f);
}
modf :: proc{
modf_f16, modf_f16le, modf_f16be,
modf_f32, modf_f32le, modf_f32be,
modf_f64, modf_f64le, modf_f64be,
};
split_decimal :: modf;
mod_f16 :: proc(x, y: f16) -> (n: f16) {
mod_f16 :: proc(x, y: f16) -> (n: f16) {
z := abs(y);
n = remainder(abs(x), z);
if sign(n) < 0 {
@@ -482,7 +754,9 @@ mod_f16 :: proc(x, y: f16) -> (n: f16) {
}
return copy_sign(n, x);
}
mod_f32 :: proc(x, y: f32) -> (n: f32) {
mod_f16le :: proc(x, y: f16le) -> (n: f16le) { return #force_inline f16le(mod_f16(f16(x), f16(y))); }
mod_f16be :: proc(x, y: f16be) -> (n: f16be) { return #force_inline f16be(mod_f16(f16(x), f16(y))); }
mod_f32 :: proc(x, y: f32) -> (n: f32) {
z := abs(y);
n = remainder(abs(x), z);
if sign(n) < 0 {
@@ -490,7 +764,9 @@ mod_f32 :: proc(x, y: f32) -> (n: f32) {
}
return copy_sign(n, x);
}
mod_f64 :: proc(x, y: f64) -> (n: f64) {
mod_f32le :: proc(x, y: f32le) -> (n: f32le) { return #force_inline f32le(mod_f32(f32(x), f32(y))); }
mod_f32be :: proc(x, y: f32be) -> (n: f32be) { return #force_inline f32be(mod_f32(f32(x), f32(y))); }
mod_f64 :: proc(x, y: f64) -> (n: f64) {
z := abs(y);
n = remainder(abs(x), z);
if sign(n) < 0 {
@@ -498,14 +774,28 @@ mod_f64 :: proc(x, y: f64) -> (n: f64) {
}
return copy_sign(n, x);
}
mod :: proc{mod_f16, mod_f32, mod_f64};
remainder_f16 :: proc(x, y: f16) -> f16 { return x - round(x/y) * y; }
remainder_f32 :: proc(x, y: f32) -> f32 { return x - round(x/y) * y; }
remainder_f64 :: proc(x, y: f64) -> f64 { return x - round(x/y) * y; }
remainder :: proc{remainder_f16, remainder_f32, remainder_f64};
mod_f64le :: proc(x, y: f64le) -> (n: f64le) { return #force_inline f64le(mod_f64(f64(x), f64(y))); }
mod_f64be :: proc(x, y: f64be) -> (n: f64be) { return #force_inline f64be(mod_f64(f64(x), f64(y))); }
mod :: proc{
mod_f16, mod_f16le, mod_f16be,
mod_f32, mod_f32le, mod_f32be,
mod_f64, mod_f64le, mod_f64be,
};
remainder_f16 :: proc(x, y: f16 ) -> f16 { return x - round(x/y) * y; }
remainder_f16le :: proc(x, y: f16le) -> f16le { return x - round(x/y) * y; }
remainder_f16be :: proc(x, y: f16be) -> f16be { return x - round(x/y) * y; }
remainder_f32 :: proc(x, y: f32 ) -> f32 { return x - round(x/y) * y; }
remainder_f32le :: proc(x, y: f32le) -> f32le { return x - round(x/y) * y; }
remainder_f32be :: proc(x, y: f32be) -> f32be { return x - round(x/y) * y; }
remainder_f64 :: proc(x, y: f64 ) -> f64 { return x - round(x/y) * y; }
remainder_f64le :: proc(x, y: f64le) -> f64le { return x - round(x/y) * y; }
remainder_f64be :: proc(x, y: f64be) -> f64be { return x - round(x/y) * y; }
remainder :: proc{
remainder_f16, remainder_f16le, remainder_f16be,
remainder_f32, remainder_f32le, remainder_f32be,
remainder_f64, remainder_f64le, remainder_f64be,
};
gcd :: proc(x, y: $T) -> T
where intrinsics.type_is_ordered_numeric(T) {
@@ -522,14 +812,30 @@ lcm :: proc(x, y: $T) -> T
return x / gcd(x, y) * y;
}
frexp_f16 :: proc(x: f16) -> (significand: f16, exponent: int) {
frexp_f16 :: proc(x: f16) -> (significand: f16, exponent: int) {
f, e := frexp_f64(f64(x));
return f16(f), e;
}
frexp_f32 :: proc(x: f32) -> (significand: f32, exponent: int) {
frexp_f16le :: proc(x: f16le) -> (significand: f16le, exponent: int) {
f, e := frexp_f64(f64(x));
return f16le(f), e;
}
frexp_f16be :: proc(x: f16be) -> (significand: f16be, exponent: int) {
f, e := frexp_f64(f64(x));
return f16be(f), e;
}
frexp_f32 :: proc(x: f32) -> (significand: f32, exponent: int) {
f, e := frexp_f64(f64(x));
return f32(f), e;
}
frexp_f32le :: proc(x: f32le) -> (significand: f32le, exponent: int) {
f, e := frexp_f64(f64(x));
return f32le(f), e;
}
frexp_f32be :: proc(x: f32be) -> (significand: f32be, exponent: int) {
f, e := frexp_f64(f64(x));
return f32be(f), e;
}
frexp_f64 :: proc(x: f64) -> (significand: f64, exponent: int) {
switch {
case x == 0:
@@ -550,7 +856,19 @@ frexp_f64 :: proc(x: f64) -> (significand: f64, exponent: int) {
}
return;
}
frexp :: proc{frexp_f16, frexp_f32, frexp_f64};
frexp_f64le :: proc(x: f64le) -> (significand: f64le, exponent: int) {
f, e := frexp_f64(f64(x));
return f64le(f), e;
}
frexp_f64be :: proc(x: f64be) -> (significand: f64be, exponent: int) {
f, e := frexp_f64(f64(x));
return f64be(f), e;
}
frexp :: proc{
frexp_f16, frexp_f16le, frexp_f16be,
frexp_f32, frexp_f32le, frexp_f32be,
frexp_f64, frexp_f64le, frexp_f64be,
};
@@ -616,7 +934,7 @@ factorial :: proc(n: int) -> int {
return table[n];
}
classify_f16 :: proc(x: f16) -> Float_Class {
classify_f16 :: proc(x: f16) -> Float_Class {
switch {
case x == 0:
i := transmute(i16)x;
@@ -640,7 +958,9 @@ classify_f16 :: proc(x: f16) -> Float_Class {
}
return .Normal;
}
classify_f32 :: proc(x: f32) -> Float_Class {
classify_f16le :: proc(x: f16le) -> Float_Class { return #force_inline classify_f16(f16(x)); }
classify_f16be :: proc(x: f16be) -> Float_Class { return #force_inline classify_f16(f16(x)); }
classify_f32 :: proc(x: f32) -> Float_Class {
switch {
case x == 0:
i := transmute(i32)x;
@@ -664,7 +984,9 @@ classify_f32 :: proc(x: f32) -> Float_Class {
}
return .Normal;
}
classify_f64 :: proc(x: f64) -> Float_Class {
classify_f32le :: proc(x: f32le) -> Float_Class { return #force_inline classify_f32(f32(x)); }
classify_f32be :: proc(x: f32be) -> Float_Class { return #force_inline classify_f32(f32(x)); }
classify_f64 :: proc(x: f64) -> Float_Class {
switch {
case x == 0:
i := transmute(i64)x;
@@ -687,13 +1009,28 @@ classify_f64 :: proc(x: f64) -> Float_Class {
}
return .Normal;
}
classify :: proc{classify_f16, classify_f32, classify_f64};
is_nan_f16 :: proc(x: f16) -> bool { return classify(x) == .NaN; }
is_nan_f32 :: proc(x: f32) -> bool { return classify(x) == .NaN; }
is_nan_f64 :: proc(x: f64) -> bool { return classify(x) == .NaN; }
is_nan :: proc{is_nan_f16, is_nan_f32, is_nan_f64};
classify_f64le :: proc(x: f64le) -> Float_Class { return #force_inline classify_f64(f64(x)); }
classify_f64be :: proc(x: f64be) -> Float_Class { return #force_inline classify_f64(f64(x)); }
classify :: proc{
classify_f16, classify_f16le, classify_f16be,
classify_f32, classify_f32le, classify_f32be,
classify_f64, classify_f64le, classify_f64be,
};
is_nan_f16 :: proc(x: f16) -> bool { return classify(x) == .NaN; }
is_nan_f16le :: proc(x: f16le) -> bool { return classify(x) == .NaN; }
is_nan_f16be :: proc(x: f16be) -> bool { return classify(x) == .NaN; }
is_nan_f32 :: proc(x: f32) -> bool { return classify(x) == .NaN; }
is_nan_f32le :: proc(x: f32le) -> bool { return classify(x) == .NaN; }
is_nan_f32be :: proc(x: f32be) -> bool { return classify(x) == .NaN; }
is_nan_f64 :: proc(x: f64) -> bool { return classify(x) == .NaN; }
is_nan_f64le :: proc(x: f64le) -> bool { return classify(x) == .NaN; }
is_nan_f64be :: proc(x: f64be) -> bool { return classify(x) == .NaN; }
is_nan :: proc{
is_nan_f16, is_nan_f16le, is_nan_f16be,
is_nan_f32, is_nan_f32le, is_nan_f32be,
is_nan_f64, is_nan_f64le, is_nan_f64be,
};
// is_inf reports whether f is an infinity, according to sign.
// If sign > 0, is_inf reports whether f is positive infinity.
@@ -709,6 +1046,13 @@ is_inf_f16 :: proc(x: f16, sign: int = 0) -> bool {
}
return class == .Inf || class == .Neg_Inf;
}
is_inf_f16le :: proc(x: f16le, sign: int = 0) -> bool {
return #force_inline is_inf_f16(f16(x), sign);
}
is_inf_f16be :: proc(x: f16be, sign: int = 0) -> bool {
return #force_inline is_inf_f16(f16(x), sign);
}
is_inf_f32 :: proc(x: f32, sign: int = 0) -> bool {
class := classify(abs(x));
switch {
@@ -719,6 +1063,13 @@ is_inf_f32 :: proc(x: f32, sign: int = 0) -> bool {
}
return class == .Inf || class == .Neg_Inf;
}
is_inf_f32le :: proc(x: f32le, sign: int = 0) -> bool {
return #force_inline is_inf_f32(f32(x), sign);
}
is_inf_f32be :: proc(x: f32be, sign: int = 0) -> bool {
return #force_inline is_inf_f32(f32(x), sign);
}
is_inf_f64 :: proc(x: f64, sign: int = 0) -> bool {
class := classify(abs(x));
switch {
@@ -729,16 +1080,37 @@ is_inf_f64 :: proc(x: f64, sign: int = 0) -> bool {
}
return class == .Inf || class == .Neg_Inf;
}
is_inf :: proc{is_inf_f16, is_inf_f32, is_inf_f64};
inf_f16 :: proc(sign: int) -> f16 {
return f16(inf_f16(sign));
is_inf_f64le :: proc(x: f64le, sign: int = 0) -> bool {
return #force_inline is_inf_f64(f64(x), sign);
}
inf_f32 :: proc(sign: int) -> f32 {
is_inf_f64be :: proc(x: f64be, sign: int = 0) -> bool {
return #force_inline is_inf_f64(f64(x), sign);
}
is_inf :: proc{
is_inf_f16, is_inf_f16le, is_inf_f16be,
is_inf_f32, is_inf_f32le, is_inf_f32be,
is_inf_f64, is_inf_f64le, is_inf_f64be,
};
inf_f16 :: proc(sign: int) -> f16 {
return f16(inf_f64(sign));
}
inf_f16le :: proc(sign: int) -> f16le {
return f16le(inf_f64(sign));
}
inf_f16be :: proc(sign: int) -> f16be {
return f16be(inf_f64(sign));
}
inf_f32 :: proc(sign: int) -> f32 {
return f32(inf_f64(sign));
}
inf_f64 :: proc(sign: int) -> f64 {
inf_f32le :: proc(sign: int) -> f32le {
return f32le(inf_f64(sign));
}
inf_f32be :: proc(sign: int) -> f32be {
return f32be(inf_f64(sign));
}
inf_f64 :: proc(sign: int) -> f64 {
v: u64;
if sign >= 0 {
v = 0x7ff00000_00000000;
@@ -747,19 +1119,41 @@ inf_f64 :: proc(sign: int) -> f64 {
}
return transmute(f64)v;
}
inf_f64le :: proc(sign: int) -> f64le {
return f64le(inf_f64(sign));
}
inf_f64be :: proc(sign: int) -> f64be {
return f64be(inf_f64(sign));
}
nan_f16 :: proc() -> f16 {
nan_f16 :: proc() -> f16 {
return f16(nan_f64());
}
nan_f32 :: proc() -> f32 {
nan_f16le :: proc() -> f16le {
return f16le(nan_f64());
}
nan_f16be :: proc() -> f16be {
return f16be(nan_f64());
}
nan_f32 :: proc() -> f32 {
return f32(nan_f64());
}
nan_f64 :: proc() -> f64 {
nan_f32le :: proc() -> f32le {
return f32le(nan_f64());
}
nan_f32be :: proc() -> f32be {
return f32be(nan_f64());
}
nan_f64 :: proc() -> f64 {
v: u64 = 0x7ff80000_00000001;
return transmute(f64)v;
}
nan_f64le :: proc() -> f64le {
return f64le(nan_f64());
}
nan_f64be :: proc() -> f64be {
return f64be(nan_f64());
}
is_power_of_two :: proc(x: int) -> bool {
return x > 0 && (x & (x-1)) == 0;
@@ -816,14 +1210,30 @@ cumsum :: proc(dst, src: $T/[]$E) -> T
}
atan2_f16 :: proc(y, x: f16) -> f16 {
atan2_f16 :: proc(y, x: f16) -> f16 {
// TODO(bill): Better atan2_f16
return f16(atan2_f64(f64(y), f64(x)));
}
atan2_f32 :: proc(y, x: f32) -> f32 {
atan2_f16le :: proc(y, x: f16le) -> f16le {
// TODO(bill): Better atan2_f16
return f16le(atan2_f64(f64(y), f64(x)));
}
atan2_f16be :: proc(y, x: f16be) -> f16be {
// TODO(bill): Better atan2_f16
return f16be(atan2_f64(f64(y), f64(x)));
}
atan2_f32 :: proc(y, x: f32) -> f32 {
// TODO(bill): Better atan2_f32
return f32(atan2_f64(f64(y), f64(x)));
}
atan2_f32le :: proc(y, x: f32le) -> f32le {
// TODO(bill): Better atan2_f32
return f32le(atan2_f64(f64(y), f64(x)));
}
atan2_f32be :: proc(y, x: f32be) -> f32be {
// TODO(bill): Better atan2_f32
return f32be(atan2_f64(f64(y), f64(x)));
}
atan2_f64 :: proc(y, x: f64) -> f64 {
// TODO(bill): Faster atan2_f64 if possible
@@ -910,80 +1320,45 @@ atan2_f64 :: proc(y, x: f64) -> f64 {
}
return q;
}
atan2_f64le :: proc(y, x: f64le) -> f64le {
// TODO(bill): Better atan2_f32
return f64le(atan2_f64(f64(y), f64(x)));
}
atan2_f64be :: proc(y, x: f64be) -> f64be {
// TODO(bill): Better atan2_f32
return f64be(atan2_f64(f64(y), f64(x)));
}
atan2 :: proc{
atan2_f16, atan2_f16le, atan2_f16be,
atan2_f32, atan2_f32le, atan2_f32be,
atan2_f64, atan2_f64le, atan2_f64be,
};
atan2 :: proc{atan2_f16, atan2_f32, atan2_f64};
atan :: proc(x: $T) -> T where intrinsics.type_is_float(T) {
return atan2(x, 1);
}
atan_f16 :: proc(x: f16) -> f16 {
return atan2_f16(x, 1);
asin :: proc(x: $T) -> T where intrinsics.type_is_float(T) {
return atan2(x, 1 + sqrt(1 - x*x));
}
atan_f32 :: proc(x: f32) -> f32 {
return atan2_f32(x, 1);
}
atan_f64 :: proc(x: f64) -> f64 {
return atan2_f64(x, 1);
}
atan :: proc{atan_f16, atan_f32, atan_f64};
asin_f16 :: proc(x: f16) -> f16 {
return atan2_f16(x, 1 + sqrt_f16(1 - x*x));
acos :: proc(x: $T) -> T where intrinsics.type_is_float(T) {
return 2 * atan2(sqrt(1 - x), sqrt(1 + x));
}
asin_f32 :: proc(x: f32) -> f32 {
return atan2_f32(x, 1 + sqrt_f32(1 - x*x));
}
asin_f64 :: proc(x: f64) -> f64 {
return atan2_f64(x, 1 + sqrt_f64(1 - x*x));
}
asin :: proc{asin_f16, asin_f32, asin_f64};
acos_f16 :: proc(x: f16) -> f16 {
return 2 * atan2_f16(sqrt_f16(1 - x), sqrt_f16(1 + x));
}
acos_f32 :: proc(x: f32) -> f32 {
return 2 * atan2_f32(sqrt_f32(1 - x), sqrt_f32(1 + x));
}
acos_f64 :: proc(x: f64) -> f64 {
return 2 * atan2_f64(sqrt_f64(1 - x), sqrt_f64(1 + x));
}
acos :: proc{acos_f16, acos_f32, acos_f64};
sinh_f16 :: proc(x: f16) -> f16 {
sinh :: proc(x: $T) -> T where intrinsics.type_is_float(T) {
return (exp(x) - exp(-x))*0.5;
}
sinh_f32 :: proc(x: f32) -> f32 {
return (exp(x) - exp(-x))*0.5;
}
sinh_f64 :: proc(x: f64) -> f64 {
return (exp(x) - exp(-x))*0.5;
}
sinh :: proc{sinh_f16, sinh_f32, sinh_f64};
cosh_f16 :: proc(x: f16) -> f16 {
cosh :: proc(x: $T) -> T where intrinsics.type_is_float(T) {
return (exp(x) + exp(-x))*0.5;
}
cosh_f32 :: proc(x: f32) -> f32 {
return (exp(x) + exp(-x))*0.5;
}
cosh_f64 :: proc(x: f64) -> f64 {
return (exp(x) + exp(-x))*0.5;
}
cosh :: proc{cosh_f16, cosh_f32, cosh_f64};
tanh_f16 :: proc(x: f16) -> f16 {
tanh :: proc(x: $T) -> T where intrinsics.type_is_float(T) {
t := exp(2*x);
return (t - 1) / (t + 1);
}
tanh_f32 :: proc(x: f32) -> f32 {
t := exp(2*x);
return (t - 1) / (t + 1);
}
tanh_f64 :: proc(x: f64) -> f64 {
t := exp(2*x);
return (t - 1) / (t + 1);
}
tanh :: proc{tanh_f16, tanh_f32, tanh_f64};
F16_DIG :: 3;
F16_EPSILON :: 0.00097656;
+2 -2
View File
@@ -6,9 +6,9 @@ Rand :: struct {
}
@(private, static)
@(private)
_GLOBAL_SEED_DATA := 1234567890;
@(private, static)
@(private)
global_rand := create(u64(uintptr(&_GLOBAL_SEED_DATA)));
set_global_seed :: proc(seed: u64) {
+1 -1
View File
@@ -22,7 +22,7 @@ Allocator_Mode_Set :: distinct bit_set[Allocator_Mode];
Allocator_Query_Info :: runtime.Allocator_Query_Info;
/*
Allocator_Query_Info :: struct {
pointer: Maybe(rawptr),
pointer: rawptr,
size: Maybe(int),
alignment: Maybe(int),
}
+1 -1
View File
@@ -919,7 +919,7 @@ tracking_allocator_proc :: proc(allocator_data: rawptr, mode: Allocator_Mode,
// Small_Allocator primary allocates memory from its local buffer of size BUFFER_SIZE
// If that buffer's memory is exhausted, it will use the backing allocator (a scratch allocator is recommended)
// Memory allocated with Small_Allocator cannot be freed individually using 'free' and must be freed using 'free_all'
Small_Allocator :: struct(BUFFER_SIZE: int)
Small_Allocator :: struct($BUFFER_SIZE: int)
where
BUFFER_SIZE >= 2*size_of(uintptr),
BUFFER_SIZE & (BUFFER_SIZE-1) == 0 {
+1
View File
@@ -142,6 +142,7 @@ slice_ptr :: proc(ptr: ^$T, len: int) -> []T {
byte_slice :: #force_inline proc "contextless" (data: rawptr, len: int) -> []byte {
return transmute([]u8)Raw_Slice{data=data, len=max(len, 0)};
}
@(deprecated="use byte_slice")
slice_ptr_to_bytes :: proc(data: rawptr, len: int) -> []byte {
return transmute([]u8)Raw_Slice{data=data, len=max(len, 0)};
}
+7 -11
View File
@@ -16,16 +16,12 @@ Proc_Inlining :: enum u32 {
No_Inline = 2,
}
Proc_Calling_Convention :: enum i32 {
Invalid = 0,
Odin,
Contextless,
C_Decl,
Std_Call,
Fast_Call,
None,
Foreign_Block_Default = -1,
Proc_Calling_Convention_Extra :: enum i32 {
Foreign_Block_Default,
}
Proc_Calling_Convention :: union {
string,
Proc_Calling_Convention_Extra,
}
Node_State_Flag :: enum {
@@ -69,7 +65,7 @@ File :: struct {
pkg: ^Package,
fullpath: string,
src: []byte,
src: string,
docs: ^Comment_Group,
+84 -23
View File
@@ -28,11 +28,11 @@ Magic_String :: "odindoc\x00";
Header_Base :: struct {
magic: [8]byte,
_: u32le,
_: u32le, // padding
version: Version_Type,
total_size: u32le,
header_size: u32le,
hash: u32le,
total_size: u32le, // in bytes
header_size: u32le, // in bytes
hash: u32le, // hash of the data after the header (header_size)
}
Header :: struct {
@@ -95,16 +95,17 @@ Entity_Flag :: enum u32le {
Foreign = 0,
Export = 1,
Param_Using = 2,
Param_Const = 3,
Param_Auto_Cast = 4,
Param_Ellipsis = 5,
Param_CVararg = 6,
Param_No_Alias = 7,
Param_Using = 2, // using
Param_Const = 3, // #const
Param_Auto_Cast = 4, // auto_cast
Param_Ellipsis = 5, // Variadic parameter
Param_CVararg = 6, // #c_vararg
Param_No_Alias = 7, // #no_alias
Type_Alias = 8,
Var_Thread_Local = 9,
Var_Static = 10,
}
Entity_Flags :: distinct bit_set[Entity_Flag; u32le];
@@ -116,14 +117,25 @@ Entity :: struct {
name: String,
type: Type_Index,
init_string: String,
_: u32le,
_: u32le, // reserved for init
comment: String,
docs: String,
// May used by:
// .Variable
// .Procedure
foreign_library: Entity_Index,
// May used by:
// .Variable
// .Procedure
link_name: String,
attributes: Array(Attribute),
grouped_entities: Array(Entity_Index), // Procedure Groups
where_clauses: Array(String), // Procedures
// Used by: .Proc_Group
grouped_entities: Array(Entity_Index),
// May used by: .Procedure
where_clauses: Array(String),
}
Attribute :: struct {
@@ -159,21 +171,70 @@ Type_Kind :: enum u32le {
Type_Elems_Cap :: 4;
Type :: struct {
kind: Type_Kind,
flags: u32le, // Type_Kind specific
name: String,
kind: Type_Kind,
// Type_Kind specific used by some types
// Underlying flag types:
// .Basic - Type_Flags_Basic
// .Struct - Type_Flags_Struct
// .Union - Type_Flags_Union
// .Proc - Type_Flags_Proc
// .Bit_Set - Type_Flags_Bit_Set
flags: u32le,
// Used by:
// .Basic
// .Named
// .Generic
name: String,
// Used By: .Struct, .Union
custom_align: String,
// Used by some types
// Used by:
// .Array - 1 count: 0=len
// .Enumerated_Array - 1 count: 0=len
// .SOA_Struct_Fixed - 1 count: 0=len
// .Bit_Set - 2 count: 0=lower, 1=upper
// .Simd_Vector - 1 count: 0=len
elem_count_len: u32le,
elem_counts: [Type_Elems_Cap]i64le,
// Each of these is esed by some types, not all
calling_convention: String, // Procedures
types: Array(Type_Index),
entities: Array(Entity_Index),
polymorphic_params: Type_Index, // Struct, Union
where_clauses: Array(String), // Struct, Union
// Used by: .Procedures
// blank implies the "odin" calling convention
calling_convention: String,
// Used by:
// .Named - 1 type: 0=base type
// .Generic - <1 type: 0=specialization
// .Pointer - 1 type: 0=element
// .Array - 1 type: 0=element
// .Enumerated_Array - 2 types: 0=index and 1=element
// .Slice - 1 type: 0=element
// .Dynamic_Array - 1 type: 0=element
// .Map - 2 types: 0=key, 1=value
// .SOA_Struct_Fixed - 1 type: underlying SOA struct element
// .SOA_Struct_Slice - 1 type: underlying SOA struct element
// .SOA_Struct_Dynamic - 1 type: underlying SOA struct element
// .Union - 0+ types: variants
// .Enum - <1 type: 0=base type
// .Proc - 2 types: 0=parameters, 1=results
// .Bit_Set - <=2 types: 0=element type, 1=underlying type (Underlying_Type flag will be set)
// .Simd_Vector - 1 type: 0=element
// .Relative_Pointer - 2 types: 0=pointer type, 1=base integer
// .Relative_Slice - 2 types: 0=slice type, 1=base integer
types: Array(Type_Index),
// Used by:
// .Named - 1 field for the definition
// .Struct - fields
// .Enum - fields
// .Tuple - parameters (procedures only)
entities: Array(Entity_Index),
// Used By: .Struct, .Union
polymorphic_params: Type_Index,
// Used By: .Struct, .Union
where_clauses: Array(String),
}
Type_Flags_Basic :: distinct bit_set[Type_Flag_Basic; u32le];
+41
View File
@@ -0,0 +1,41 @@
package odin_format
import "core:odin/printer"
import "core:odin/parser"
import "core:odin/ast"
default_style := printer.default_style;
simplify :: proc(file: ^ast.File) {
}
format :: proc(filepath: string, source: string, config: printer.Config, parser_flags := parser.Flags{}, allocator := context.allocator) -> (string, bool) {
config := config;
pkg := ast.Package {
kind = .Normal,
};
file := ast.File {
pkg = &pkg,
src = source,
fullpath = filepath,
};
config.newline_limit = clamp(config.newline_limit, 0, 16);
config.spaces = clamp(config.spaces, 1, 16);
config.align_length_break = clamp(config.align_length_break, 0, 64);
p := parser.default_parser(parser_flags);
ok := parser.parse_file(&p, &file);
if !ok || file.syntax_error_count > 0 {
return {}, false;
}
prnt := printer.make_printer(config, allocator);
return printer.print(&prnt, &file), true;
}
+1 -1
View File
@@ -39,7 +39,7 @@ collect_package :: proc(path: string) -> (pkg: ^ast.Package, success: bool) {
}
file := ast.new(ast.File, NO_POS, NO_POS);
file.pkg = pkg;
file.src = src;
file.src = string(src);
file.fullpath = fullpath;
pkg.files[fullpath] = file;
}
+79 -50
View File
@@ -8,10 +8,21 @@ import "core:fmt"
Warning_Handler :: #type proc(pos: tokenizer.Pos, fmt: string, args: ..any);
Error_Handler :: #type proc(pos: tokenizer.Pos, fmt: string, args: ..any);
Flag :: enum u32 {
Optional_Semicolons,
}
Flags :: distinct bit_set[Flag; u32];
Parser :: struct {
file: ^ast.File,
tok: tokenizer.Tokenizer,
// If .Optional_Semicolons is true, semicolons are completely as statement terminators
// different to .Insert_Semicolon in tok.flags
flags: Flags,
warn: Warning_Handler,
err: Error_Handler,
@@ -100,8 +111,9 @@ end_pos :: proc(tok: tokenizer.Token) -> tokenizer.Pos {
return pos;
}
default_parser :: proc() -> Parser {
default_parser :: proc(flags := Flags{}) -> Parser {
return Parser {
flags = flags,
err = default_error_handler,
warn = default_warning_handler,
};
@@ -128,6 +140,10 @@ parse_file :: proc(p: ^Parser, file: ^ast.File) -> bool {
p.line_comment = nil;
}
if .Optional_Semicolons in p.flags {
p.tok.flags += {.Insert_Semicolon};
}
p.file = file;
tokenizer.init(&p.tok, file.src, file.fullpath, p.err);
if p.tok.ch <= 0 {
@@ -400,6 +416,11 @@ is_semicolon_optional_for_node :: proc(p: ^Parser, node: ^ast.Node) -> bool {
if node == nil {
return false;
}
if .Optional_Semicolons in p.flags {
return true;
}
switch n in node.derived {
case ast.Empty_Stmt, ast.Block_Stmt:
return true;
@@ -439,14 +460,34 @@ is_semicolon_optional_for_node :: proc(p: ^Parser, node: ^ast.Node) -> bool {
return false;
}
expect_semicolon_newline_error :: proc(p: ^Parser, token: tokenizer.Token, s: ^ast.Node) {
if .Optional_Semicolons not_in p.flags && .Insert_Semicolon in p.tok.flags && token.text == "\n" {
#partial switch token.kind {
case .Close_Brace:
case .Close_Paren:
case .Else:
return;
}
if is_semicolon_optional_for_node(p, s) {
return;
}
tok := token;
tok.pos.column -= 1;
error(p, tok.pos, "expected ';', got newline");
}
}
expect_semicolon :: proc(p: ^Parser, node: ^ast.Node) -> bool {
if allow_token(p, .Semicolon) {
expect_semicolon_newline_error(p, p.prev_tok, node);
return true;
}
prev := p.prev_tok;
if prev.kind == .Semicolon {
expect_semicolon_newline_error(p, p.prev_tok, node);
return true;
}
@@ -615,7 +656,7 @@ parse_if_stmt :: proc(p: ^Parser) -> ^ast.If_Stmt {
cond = parse_expr(p, false);
} else {
init = parse_simple_stmt(p, nil);
if allow_token(p, .Semicolon) {
if parse_control_statement_semicolon_separator(p) {
cond = parse_expr(p, false);
} else {
cond = convert_stmt_to_expr(p, init, "boolean expression");
@@ -668,6 +709,18 @@ parse_if_stmt :: proc(p: ^Parser) -> ^ast.If_Stmt {
return if_stmt;
}
parse_control_statement_semicolon_separator :: proc(p: ^Parser) -> bool {
tok := peek_token(p);
if tok.kind != .Open_Brace {
return allow_token(p, .Semicolon);
}
if tok.text == ";" {
return allow_token(p, .Semicolon);
}
return false;
}
parse_for_stmt :: proc(p: ^Parser) -> ^ast.Stmt {
if p.curr_proc == nil {
error(p, p.curr_tok.pos, "you cannot use a for statement in the file scope");
@@ -716,7 +769,7 @@ parse_for_stmt :: proc(p: ^Parser) -> ^ast.Stmt {
}
}
if !is_range && allow_token(p, .Semicolon) {
if !is_range && parse_control_statement_semicolon_separator(p) {
init = cond;
cond = nil;
if p.curr_tok.kind != .Semicolon {
@@ -820,7 +873,7 @@ parse_switch_stmt :: proc(p: ^Parser) -> ^ast.Stmt {
tag = parse_simple_stmt(p, {Stmt_Allow_Flag.In});
if as, ok := tag.derived.(ast.Assign_Stmt); ok && as.op.kind == .In {
is_type_switch = true;
} else if allow_token(p, .Semicolon) {
} else if parse_control_statement_semicolon_separator(p) {
init = tag;
tag = nil;
if p.curr_tok.kind != .Open_Brace {
@@ -831,6 +884,7 @@ parse_switch_stmt :: proc(p: ^Parser) -> ^ast.Stmt {
}
skip_possible_newline(p);
open := expect_token(p, .Open_Brace);
for p.curr_tok.kind == .Case {
@@ -958,6 +1012,7 @@ parse_foreign_block :: proc(p: ^Parser, tok: tokenizer.Token) -> ^ast.Foreign_Bl
defer p.in_foreign_block = prev_in_foreign_block;
p.in_foreign_block = true;
skip_possible_newline_for_literal(p);
open := expect_token(p, .Open_Brace);
for p.curr_tok.kind != .Close_Brace && p.curr_tok.kind != .EOF {
decl := parse_foreign_block_decl(p);
@@ -1287,7 +1342,7 @@ token_precedence :: proc(p: ^Parser, kind: tokenizer.Token_Kind) -> int {
#partial switch kind {
case .Question, .If, .When:
return 1;
case .Ellipsis, .Range_Half:
case .Ellipsis, .Range_Half, .Range_Full:
if !p.allow_range {
return 0;
}
@@ -1884,24 +1939,12 @@ parse_results :: proc(p: ^Parser) -> (list: ^ast.Field_List, diverging: bool) {
string_to_calling_convention :: proc(s: string) -> ast.Proc_Calling_Convention {
if s[0] != '"' && s[0] != '`' {
return .Invalid;
return nil;
}
switch s[1:len(s)-1] {
case "odin":
return .Odin;
case "contextless":
return .Contextless;
case "cdecl", "c":
return .C_Decl;
case "stdcall", "std":
return .Std_Call;
case "fast", "fastcall":
return .Fast_Call;
case "none":
return .None;
if len(s) == 2 {
return nil;
}
return .Invalid;
return s;
}
parse_proc_tags :: proc(p: ^Parser) -> (tags: ast.Proc_Tags) {
@@ -1926,21 +1969,17 @@ parse_proc_tags :: proc(p: ^Parser) -> (tags: ast.Proc_Tags) {
}
parse_proc_type :: proc(p: ^Parser, tok: tokenizer.Token) -> ^ast.Proc_Type {
cc := ast.Proc_Calling_Convention.Invalid;
cc: ast.Proc_Calling_Convention;
if p.curr_tok.kind == .String {
str := expect_token(p, .String);
cc = string_to_calling_convention(str.text);
if cc == ast.Proc_Calling_Convention.Invalid {
if cc == nil {
error(p, str.pos, "unknown calling convention '%s'", str.text);
}
}
if cc == ast.Proc_Calling_Convention.Invalid {
if p.in_foreign_block {
cc = ast.Proc_Calling_Convention.Foreign_Block_Default;
} else {
cc = ast.Proc_Calling_Convention.Odin;
}
if cc == nil && p.in_foreign_block {
cc = .Foreign_Block_Default;
}
expect_token(p, .Open_Paren);
@@ -1976,23 +2015,6 @@ parse_proc_type :: proc(p: ^Parser, tok: tokenizer.Token) -> ^ast.Proc_Type {
return pt;
}
check_poly_params_for_type :: proc(p: ^Parser, poly_params: ^ast.Field_List, tok: tokenizer.Token) {
if poly_params == nil {
return;
}
for field in poly_params.list {
for name in field.names {
if name == nil {
continue;
}
if _, ok := name.derived.(ast.Poly_Type); ok {
error(p, name.pos, "polymorphic names are not needed for %s parameters", tok.text);
return;
}
}
}
}
parse_inlining_operand :: proc(p: ^Parser, lhs: bool, tok: tokenizer.Token) -> ^ast.Expr {
expr := parse_unary_expr(p, lhs);
@@ -2224,6 +2246,7 @@ parse_operand :: proc(p: ^Parser, lhs: bool) -> ^ast.Expr {
p.expr_level = -1;
where_clauses = parse_rhs_expr_list(p);
p.expr_level = prev_level;
tags = parse_proc_tags(p);
}
if p.allow_type && p.expr_level < 0 {
if where_token.kind != .Invalid {
@@ -2233,6 +2256,8 @@ parse_operand :: proc(p: ^Parser, lhs: bool) -> ^ast.Expr {
}
body: ^ast.Stmt;
skip_possible_newline_for_literal(p);
if allow_token(p, .Undef) {
body = nil;
if where_token.kind != .Invalid {
@@ -2358,7 +2383,6 @@ parse_operand :: proc(p: ^Parser, lhs: bool) -> ^ast.Expr {
poly_params = nil;
}
expect_token_after(p, .Close_Paren, "parameter list");
check_poly_params_for_type(p, poly_params, tok);
}
prev_level := p.expr_level;
@@ -2405,6 +2429,7 @@ parse_operand :: proc(p: ^Parser, lhs: bool) -> ^ast.Expr {
p.expr_level = where_prev_level;
}
skip_possible_newline_for_literal(p);
expect_token(p, .Open_Brace);
fields, name_count = parse_field_list(p, .Close_Brace, ast.Field_Flags_Struct);
close := expect_token(p, .Close_Brace);
@@ -2434,7 +2459,6 @@ parse_operand :: proc(p: ^Parser, lhs: bool) -> ^ast.Expr {
poly_params = nil;
}
expect_token_after(p, .Close_Paren, "parameter list");
check_poly_params_for_type(p, poly_params, tok);
}
prev_level := p.expr_level;
@@ -2473,6 +2497,7 @@ parse_operand :: proc(p: ^Parser, lhs: bool) -> ^ast.Expr {
variants: [dynamic]^ast.Expr;
skip_possible_newline_for_literal(p);
expect_token_after(p, .Open_Brace, "union");
for p.curr_tok.kind != .Close_Brace && p.curr_tok.kind != .EOF {
@@ -2503,6 +2528,8 @@ parse_operand :: proc(p: ^Parser, lhs: bool) -> ^ast.Expr {
if p.curr_tok.kind != .Open_Brace {
base_type = parse_type(p);
}
skip_possible_newline_for_literal(p);
open := expect_token(p, .Open_Brace);
fields := parse_elem_list(p);
close := expect_token(p, .Close_Brace);
@@ -2601,6 +2628,7 @@ parse_operand :: proc(p: ^Parser, lhs: bool) -> ^ast.Expr {
}
}
skip_possible_newline_for_literal(p);
open := expect_token(p, .Open_Brace);
asm_string := parse_expr(p, false);
expect_token(p, .Comma);
@@ -2811,7 +2839,7 @@ parse_atom_expr :: proc(p: ^Parser, value: ^ast.Expr, lhs: bool) -> (operand: ^a
open := expect_token(p, .Open_Bracket);
#partial switch p.curr_tok.kind {
case .Colon, .Ellipsis, .Range_Half:
case .Colon, .Ellipsis, .Range_Half, .Range_Full:
// NOTE(bill): Do not err yet
break;
case:
@@ -2819,7 +2847,7 @@ parse_atom_expr :: proc(p: ^Parser, value: ^ast.Expr, lhs: bool) -> (operand: ^a
}
#partial switch p.curr_tok.kind {
case .Ellipsis, .Range_Half:
case .Ellipsis, .Range_Half, .Range_Full:
error(p, p.curr_tok.pos, "expected a colon, not a range");
fallthrough;
case .Colon:
@@ -3150,6 +3178,7 @@ parse_simple_stmt :: proc(p: ^Parser, flags: Stmt_Allow_Flags) -> ^ast.Stmt {
case ast.For_Stmt: n.label = label;
case ast.Switch_Stmt: n.label = label;
case ast.Type_Switch_Stmt: n.label = label;
case ast.Range_Stmt: n.label = label;
}
}
+924
View File
@@ -0,0 +1,924 @@
package odin_printer
import "core:odin/ast"
import "core:odin/tokenizer"
import "core:strings"
import "core:runtime"
import "core:fmt"
import "core:unicode/utf8"
import "core:mem"
Type_Enum :: enum {Line_Comment, Value_Decl, Switch_Stmt, Struct, Assign, Call, Enum, If, For, Proc_Lit};
Line_Type :: bit_set[Type_Enum];
/*
Represents an unwrapped line
*/
Line :: struct {
format_tokens: [dynamic]Format_Token,
finalized: bool,
used: bool,
depth: int,
types: Line_Type, //for performance, so you don't have to verify what types are in it by going through the tokens - might give problems when adding linebreaking
}
/*
Represents a singular token in a unwrapped line
*/
Format_Token :: struct {
kind: tokenizer.Token_Kind,
text: string,
type: Type_Enum,
spaces_before: int,
parameter_count: int,
}
Printer :: struct {
string_builder: strings.Builder,
config: Config,
depth: int, //the identation depth
comments: [dynamic]^ast.Comment_Group,
latest_comment_index: int,
allocator: mem.Allocator,
file: ^ast.File,
source_position: tokenizer.Pos,
last_source_position: tokenizer.Pos,
lines: [dynamic]Line, //need to look into a better data structure, one that can handle inserting lines rather than appending
skip_semicolon: bool,
current_line: ^Line,
current_line_index: int,
last_line_index: int,
last_token: ^Format_Token,
merge_next_token: bool,
space_next_token: bool,
debug: bool,
}
Config :: struct {
spaces: int, //Spaces per indentation
newline_limit: int, //The limit of newlines between statements and declarations.
tabs: bool, //Enable or disable tabs
convert_do: bool, //Convert all do statements to brace blocks
semicolons: bool, //Enable semicolons
split_multiple_stmts: bool,
align_switch: bool,
brace_style: Brace_Style,
align_assignments: bool,
align_structs: bool,
align_style: Alignment_Style,
align_enums: bool,
align_length_break: int,
indent_cases: bool,
newline_style: Newline_Style,
}
Brace_Style :: enum {
_1TBS,
Allman,
Stroustrup,
K_And_R,
}
Block_Type :: enum {
None,
If_Stmt,
Proc,
Generic,
Comp_Lit,
Switch_Stmt,
}
Alignment_Style :: enum {
Align_On_Type_And_Equals,
Align_On_Colon_And_Equals,
}
Newline_Style :: enum {
CRLF,
LF,
}
default_style := Config {
spaces = 4,
newline_limit = 2,
convert_do = false,
semicolons = true,
tabs = true,
brace_style = ._1TBS,
split_multiple_stmts = true,
align_assignments = true,
align_style = .Align_On_Type_And_Equals,
indent_cases = false,
align_switch = true,
align_structs = true,
align_enums = true,
newline_style = .CRLF,
align_length_break = 9,
};
make_printer :: proc(config: Config, allocator := context.allocator) -> Printer {
return {
config = config,
allocator = allocator,
debug = false,
};
}
print :: proc(p: ^Printer, file: ^ast.File) -> string {
p.comments = file.comments;
if len(file.decls) > 0 {
p.lines = make([dynamic]Line, 0, (file.decls[len(file.decls) - 1].end.line - file.decls[0].pos.line) * 2, context.temp_allocator);
}
set_source_position(p, file.pkg_token.pos);
p.last_source_position.line = 1;
set_line(p, 0);
push_generic_token(p, .Package, 0);
push_ident_token(p, file.pkg_name, 1);
for decl in file.decls {
visit_decl(p, cast(^ast.Decl)decl);
}
if len(p.comments) > 0 {
infinite := p.comments[len(p.comments) - 1].end;
infinite.offset = 9999999;
push_comments(p, infinite);
}
fix_lines(p);
builder := strings.make_builder(0, mem.megabytes(5), p.allocator);
last_line := 0;
newline: string;
if p.config.newline_style == .LF {
newline = "\n";
} else {
newline = "\r\n";
}
for line, line_index in p.lines {
diff_line := line_index - last_line;
for i := 0; i < diff_line; i += 1 {
strings.write_string(&builder, newline);
}
if p.config.tabs {
for i := 0; i < line.depth; i += 1 {
strings.write_byte(&builder, '\t');
}
} else {
for i := 0; i < line.depth * p.config.spaces; i += 1 {
strings.write_byte(&builder, ' ');
}
}
if p.debug {
strings.write_string(&builder, fmt.tprintf("line %v: ", line_index));
}
for format_token in line.format_tokens {
for i := 0; i < format_token.spaces_before; i += 1 {
strings.write_byte(&builder, ' ');
}
strings.write_string(&builder, format_token.text);
}
last_line = line_index;
}
strings.write_string(&builder, newline);
return strings.to_string(builder);
}
fix_lines :: proc(p: ^Printer) {
align_var_decls(p);
format_generic(p);
align_comments(p); //align them last since they rely on the other alignments
}
format_value_decl :: proc(p: ^Printer, index: int) {
eq_found := false;
eq_token: Format_Token;
eq_line: int;
largest := 0;
found_eq: for line, line_index in p.lines[index:] {
for format_token in line.format_tokens {
largest += len(format_token.text) + format_token.spaces_before;
if format_token.kind == .Eq {
eq_token = format_token;
eq_line = line_index + index;
eq_found = true;
break found_eq;
}
}
}
if !eq_found {
return;
}
align_next := false;
//check to see if there is a binary operator in the last token(this is guaranteed by the ast visit), otherwise it's not multilined
for line, line_index in p.lines[eq_line:] {
if len(line.format_tokens) == 0 {
break;
}
if align_next {
line.format_tokens[0].spaces_before = largest + 1;
align_next = false;
}
kind := find_last_token(line.format_tokens).kind;
if tokenizer.Token_Kind.B_Operator_Begin < kind && kind <= tokenizer.Token_Kind.Cmp_Or {
align_next = true;
}
if !align_next {
break;
}
}
}
find_last_token :: proc(format_tokens: [dynamic]Format_Token) -> Format_Token {
for i := len(format_tokens) - 1; i >= 0; i -= 1 {
if format_tokens[i].kind != .Comment {
return format_tokens[i];
}
}
panic("not possible");
}
format_assignment :: proc(p: ^Printer, index: int) {
}
format_call :: proc(p: ^Printer, line_index: int, format_index: int) {
paren_found := false;
paren_token: Format_Token;
paren_line: int;
paren_token_index: int;
largest := 0;
found_paren: for line, i in p.lines[line_index:] {
for format_token, j in line.format_tokens {
largest += len(format_token.text) + format_token.spaces_before;
if i == 0 && j < format_index {
continue;
}
if format_token.kind == .Open_Paren && format_token.type == .Call {
paren_token = format_token;
paren_line = line_index + i;
paren_found = true;
paren_token_index = j;
break found_paren;
}
}
}
if !paren_found {
panic("Should not be possible");
}
paren_count := 1;
done := false;
for line, line_index in p.lines[paren_line:] {
if len(line.format_tokens) == 0 {
continue;
}
for format_token, i in line.format_tokens {
if format_token.kind == .Comment {
continue;
}
if line_index == 0 && i <= paren_token_index {
continue;
}
if format_token.kind == .Open_Paren {
paren_count += 1;
} else if format_token.kind == .Close_Paren {
paren_count -= 1;
}
if paren_count == 0 {
done = true;
}
}
if line_index != 0 {
line.format_tokens[0].spaces_before = largest;
}
if done {
return;
}
}
}
format_keyword_to_brace :: proc(p: ^Printer, line_index: int, format_index: int, keyword: tokenizer.Token_Kind) {
keyword_found := false;
keyword_token: Format_Token;
keyword_line: int;
largest := 0;
brace_count := 0;
done := false;
found_keyword: for line, i in p.lines[line_index:] {
for format_token in line.format_tokens {
largest += len(format_token.text) + format_token.spaces_before;
if format_token.kind == keyword {
keyword_token = format_token;
keyword_line = line_index + i;
keyword_found = true;
break found_keyword;
}
}
}
if !keyword_found {
panic("Should not be possible");
}
for line, line_index in p.lines[keyword_line:] {
if len(line.format_tokens) == 0 {
continue;
}
for format_token, i in line.format_tokens {
if format_token.kind == .Comment {
break;
} else if format_token.kind == .Undef {
return;
}
if line_index == 0 && i <= format_index {
continue;
}
if format_token.kind == .Open_Brace {
brace_count += 1;
} else if format_token.kind == .Close_Brace {
brace_count -= 1;
}
if brace_count == 1 {
done = true;
}
}
if line_index != 0 {
line.format_tokens[0].spaces_before = largest + 1;
}
if done {
return;
}
}
}
format_generic :: proc(p: ^Printer) {
next_struct_line := 0;
for line, line_index in p.lines {
if len(line.format_tokens) <= 0 {
continue;
}
for format_token, token_index in line.format_tokens {
#partial switch format_token.kind {
case .For, .If, .When, .Switch:
format_keyword_to_brace(p, line_index, token_index, format_token.kind);
case .Proc:
if format_token.type == .Proc_Lit {
format_keyword_to_brace(p, line_index, token_index, format_token.kind);
}
case:
if format_token.type == .Call {
format_call(p, line_index, token_index);
}
}
}
if .Switch_Stmt in line.types && p.config.align_switch {
align_switch_stmt(p, line_index);
}
if .Enum in line.types && p.config.align_enums {
align_enum(p, line_index);
}
if .Struct in line.types && p.config.align_structs && next_struct_line <= 0 {
next_struct_line = align_struct(p, line_index);
}
if .Value_Decl in line.types {
format_value_decl(p, line_index);
}
if .Assign in line.types {
format_assignment(p, line_index);
}
next_struct_line -= 1;
}
}
align_var_decls :: proc(p: ^Printer) {
current_line: int;
current_typed: bool;
current_not_mutable: bool;
largest_lhs := 0;
largest_rhs := 0;
TokenAndLength :: struct {
format_token: ^Format_Token,
length: int,
};
colon_tokens := make([dynamic]TokenAndLength, 0, 10, context.temp_allocator);
type_tokens := make([dynamic]TokenAndLength, 0, 10, context.temp_allocator);
equal_tokens := make([dynamic]TokenAndLength, 0, 10, context.temp_allocator);
for line, line_index in p.lines {
//It is only possible to align value decls that are one one line, otherwise just ignore them
if .Value_Decl not_in line.types {
continue;
}
typed := true;
not_mutable := false;
continue_flag := false;
for i := 0; i < len(line.format_tokens); i += 1 {
if line.format_tokens[i].kind == .Colon && line.format_tokens[min(i + 1, len(line.format_tokens) - 1)].kind == .Eq {
typed = false;
}
if line.format_tokens[i].kind == .Colon && line.format_tokens[min(i + 1, len(line.format_tokens) - 1)].kind == .Colon {
not_mutable = true;
}
if line.format_tokens[i].kind == .Union ||
line.format_tokens[i].kind == .Enum ||
line.format_tokens[i].kind == .Struct ||
line.format_tokens[i].kind == .For ||
line.format_tokens[i].kind == .If ||
line.format_tokens[i].kind == .Comment {
continue_flag = true;
}
//enforced undef is always on the last line, if it exists
if line.format_tokens[i].kind == .Proc && line.format_tokens[len(line.format_tokens)-1].kind != .Undef {
continue_flag = true;
}
}
if continue_flag {
continue;
}
if line_index != current_line + 1 || typed != current_typed || not_mutable != current_not_mutable {
if p.config.align_style == .Align_On_Colon_And_Equals || !current_typed || current_not_mutable {
for colon_token in colon_tokens {
colon_token.format_token.spaces_before = largest_lhs - colon_token.length + 1;
}
} else if p.config.align_style == .Align_On_Type_And_Equals {
for type_token in type_tokens {
type_token.format_token.spaces_before = largest_lhs - type_token.length + 1;
}
}
if current_typed {
for equal_token in equal_tokens {
equal_token.format_token.spaces_before = largest_rhs - equal_token.length + 1;
}
} else {
for equal_token in equal_tokens {
equal_token.format_token.spaces_before = 0;
}
}
clear(&colon_tokens);
clear(&type_tokens);
clear(&equal_tokens);
largest_rhs = 0;
largest_lhs = 0;
current_typed = typed;
current_not_mutable = not_mutable;
}
current_line = line_index;
current_token_index := 0;
lhs_length := 0;
rhs_length := 0;
//calcuate the length of lhs of a value decl i.e. `a, b:`
for; current_token_index < len(line.format_tokens); current_token_index += 1 {
lhs_length += len(line.format_tokens[current_token_index].text) + line.format_tokens[current_token_index].spaces_before;
if line.format_tokens[current_token_index].kind == .Colon {
append(&colon_tokens, TokenAndLength {format_token = &line.format_tokens[current_token_index], length = lhs_length});
if len(line.format_tokens) > current_token_index && line.format_tokens[current_token_index + 1].kind != .Eq {
append(&type_tokens, TokenAndLength {format_token = &line.format_tokens[current_token_index + 1], length = lhs_length});
}
current_token_index += 1;
largest_lhs = max(largest_lhs, lhs_length);
break;
}
}
//calcuate the length of the rhs i.e. `[dynamic]int = 123123`
for; current_token_index < len(line.format_tokens); current_token_index += 1 {
rhs_length += len(line.format_tokens[current_token_index].text) + line.format_tokens[current_token_index].spaces_before;
if line.format_tokens[current_token_index].kind == .Eq {
append(&equal_tokens, TokenAndLength {format_token = &line.format_tokens[current_token_index], length = rhs_length});
largest_rhs = max(largest_rhs, rhs_length);
break;
}
}
}
//repeating myself, move to sub procedure
if p.config.align_style == .Align_On_Colon_And_Equals || !current_typed || current_not_mutable {
for colon_token in colon_tokens {
colon_token.format_token.spaces_before = largest_lhs - colon_token.length + 1;
}
} else if p.config.align_style == .Align_On_Type_And_Equals {
for type_token in type_tokens {
type_token.format_token.spaces_before = largest_lhs - type_token.length + 1;
}
}
if current_typed {
for equal_token in equal_tokens {
equal_token.format_token.spaces_before = largest_rhs - equal_token.length + 1;
}
} else {
for equal_token in equal_tokens {
equal_token.format_token.spaces_before = 0;
}
}
}
align_switch_stmt :: proc(p: ^Printer, index: int) {
switch_found := false;
brace_token: Format_Token;
brace_line: int;
found_switch_brace: for line, line_index in p.lines[index:] {
for format_token in line.format_tokens {
if format_token.kind == .Open_Brace && switch_found {
brace_token = format_token;
brace_line = line_index + index;
break found_switch_brace;
} else if format_token.kind == .Open_Brace {
break;
} else if format_token.kind == .Switch {
switch_found = true;
}
}
}
if !switch_found {
return;
}
largest := 0;
case_count := 0;
TokenAndLength :: struct {
format_token: ^Format_Token,
length: int,
};
format_tokens := make([dynamic]TokenAndLength, 0, brace_token.parameter_count, context.temp_allocator);
//find all the switch cases that are one lined
for line, line_index in p.lines[brace_line + 1:] {
case_found := false;
colon_found := false;
length := 0;
for format_token, i in line.format_tokens {
if format_token.kind == .Comment {
break;
}
//this will only happen if the case is one lined
if case_found && colon_found {
append(&format_tokens, TokenAndLength {format_token = &line.format_tokens[i], length = length});
largest = max(length, largest);
break;
}
if format_token.kind == .Case {
case_found = true;
case_count += 1;
} else if format_token.kind == .Colon {
colon_found = true;
}
length += len(format_token.text) + format_token.spaces_before;
}
if case_count >= brace_token.parameter_count {
break;
}
}
for token in format_tokens {
token.format_token.spaces_before = largest - token.length + 1;
}
}
align_enum :: proc(p: ^Printer, index: int) {
enum_found := false;
brace_token: Format_Token;
brace_line: int;
found_enum_brace: for line, line_index in p.lines[index:] {
for format_token in line.format_tokens {
if format_token.kind == .Open_Brace && enum_found {
brace_token = format_token;
brace_line = line_index + index;
break found_enum_brace;
} else if format_token.kind == .Open_Brace {
break;
} else if format_token.kind == .Enum {
enum_found = true;
}
}
}
if !enum_found {
return;
}
largest := 0;
comma_count := 0;
TokenAndLength :: struct {
format_token: ^Format_Token,
length: int,
};
format_tokens := make([dynamic]TokenAndLength, 0, brace_token.parameter_count, context.temp_allocator);
for line, line_index in p.lines[brace_line + 1:] {
length := 0;
for format_token, i in line.format_tokens {
if format_token.kind == .Comment {
break;
}
if format_token.kind == .Eq {
append(&format_tokens, TokenAndLength {format_token = &line.format_tokens[i], length = length});
largest = max(length, largest);
break;
} else if format_token.kind == .Comma {
comma_count += 1;
}
length += len(format_token.text) + format_token.spaces_before;
}
if comma_count >= brace_token.parameter_count {
break;
}
}
for token in format_tokens {
token.format_token.spaces_before = largest - token.length + 1;
}
}
align_struct :: proc(p: ^Printer, index: int) -> int {
struct_found := false;
brace_token: Format_Token;
brace_line: int;
found_struct_brace: for line, line_index in p.lines[index:] {
for format_token in line.format_tokens {
if format_token.kind == .Open_Brace && struct_found {
brace_token = format_token;
brace_line = line_index + index;
break found_struct_brace;
} else if format_token.kind == .Open_Brace {
break;
} else if format_token.kind == .Struct {
struct_found = true;
}
}
}
if !struct_found {
return 0;
}
largest := 0;
colon_count := 0;
nested := false;
seen_brace := false;
TokenAndLength :: struct {
format_token: ^Format_Token,
length: int,
};
format_tokens := make([]TokenAndLength, brace_token.parameter_count, context.temp_allocator);
if brace_token.parameter_count == 0 {
return 0;
}
end_line_index := 0;
for line, line_index in p.lines[brace_line + 1:] {
length := 0;
for format_token, i in line.format_tokens {
//give up on nested structs
if format_token.kind == .Comment {
break;
} else if format_token.kind == .Open_Paren {
break;
} else if format_token.kind == .Open_Brace {
seen_brace = true;
} else if format_token.kind == .Close_Brace {
seen_brace = false;
} else if seen_brace {
continue;
}
if format_token.kind == .Colon {
format_tokens[colon_count] = {format_token = &line.format_tokens[i + 1], length = length};
if format_tokens[colon_count].format_token.kind == .Struct {
nested = true;
}
colon_count += 1;
largest = max(length, largest);
}
length += len(format_token.text) + format_token.spaces_before;
}
if nested {
end_line_index = line_index + brace_line + 1;
}
if colon_count >= brace_token.parameter_count {
break;
}
}
//give up aligning nested, it never looks good
if nested {
for line, line_index in p.lines[end_line_index:] {
for format_token in line.format_tokens {
if format_token.kind == .Close_Brace {
return end_line_index + line_index - index;
}
}
}
}
for token in format_tokens {
token.format_token.spaces_before = largest - token.length + 1;
}
return 0;
}
align_comments :: proc(p: ^Printer) {
Comment_Align_Info :: struct {
length: int,
begin: int,
end: int,
depth: int,
};
comment_infos := make([dynamic]Comment_Align_Info, 0, context.temp_allocator);
current_info: Comment_Align_Info;
for line, line_index in p.lines {
if len(line.format_tokens) <= 0 {
continue;
}
if .Line_Comment in line.types {
if current_info.end + 1 != line_index || current_info.depth != line.depth ||
(current_info.begin == current_info.end && current_info.length == 0) {
if (current_info.begin != 0 && current_info.end != 0) || current_info.length > 0 {
append(&comment_infos, current_info);
}
current_info.begin = line_index;
current_info.end = line_index;
current_info.depth = line.depth;
current_info.length = 0;
}
length := 0;
for format_token, i in line.format_tokens {
if format_token.kind == .Comment {
current_info.length = max(current_info.length, length);
current_info.end = line_index;
}
length += format_token.spaces_before + len(format_token.text);
}
}
}
if (current_info.begin != 0 && current_info.end != 0) || current_info.length > 0 {
append(&comment_infos, current_info);
}
for info in comment_infos {
if info.begin == info.end || info.length == 0 {
continue;
}
for i := info.begin; i <= info.end; i += 1 {
l := p.lines[i];
length := 0;
for format_token, i in l.format_tokens {
if format_token.kind == .Comment {
if len(l.format_tokens) == 1 {
l.format_tokens[i].spaces_before = info.length + 1;
} else {
l.format_tokens[i].spaces_before = info.length - length + 1;
}
}
length += format_token.spaces_before + len(format_token.text);
}
}
}
}
File diff suppressed because it is too large Load Diff
+2
View File
@@ -107,6 +107,7 @@ Token_Kind :: enum u32 {
Comma, // ,
Ellipsis, // ..
Range_Half, // ..<
Range_Full, // ..=
Back_Slash, // \
B_Operator_End,
@@ -233,6 +234,7 @@ tokens := [Token_Kind.COUNT]string {
",",
"..",
"..<",
"..=",
"\\",
"",
+7 -4
View File
@@ -14,7 +14,7 @@ Flags :: distinct bit_set[Flag; u32];
Tokenizer :: struct {
// Immutable data
path: string,
src: []byte,
src: string,
err: Error_Handler,
flags: Flags,
@@ -31,7 +31,7 @@ Tokenizer :: struct {
error_count: int,
}
init :: proc(t: ^Tokenizer, src: []byte, path: string, err: Error_Handler = default_error_handler) {
init :: proc(t: ^Tokenizer, src: string, path: string, err: Error_Handler = default_error_handler) {
t.src = src;
t.err = err;
t.ch = ' ';
@@ -87,7 +87,7 @@ advance_rune :: proc(using t: ^Tokenizer) {
case r == 0:
error(t, t.offset, "illegal character NUL");
case r >= utf8.RUNE_SELF:
r, w = utf8.decode_rune(src[read_offset:]);
r, w = utf8.decode_rune_in_string(src[read_offset:]);
if r == utf8.RUNE_ERROR && w == 1 {
error(t, t.offset, "illegal UTF-8 encoding");
} else if r == utf8.RUNE_BOM && offset > 0 {
@@ -608,7 +608,7 @@ scan :: proc(t: ^Tokenizer) -> Token {
kind = switch3(t, .And, .And_Eq, '&', .Cmp_And);
}
case '|': kind = switch3(t, .Or, .Or_Eq, '|', .Cmp_Or);
case '~': kind = .Xor;
case '~': kind = switch2(t, .Xor, .Xor_Eq);
case '<': kind = switch4(t, .Lt, .Lt_Eq, '<', .Shl, .Shl_Eq);
case '>': kind = switch4(t, .Gt, .Gt_Eq, '>', .Shr,.Shr_Eq);
@@ -623,6 +623,9 @@ scan :: proc(t: ^Tokenizer) -> Token {
if t.ch == '<' {
advance_rune(t);
kind = .Range_Half;
} else if t.ch == '=' {
advance_rune(t);
kind = .Range_Full;
}
}
}
+3 -3
View File
@@ -273,7 +273,7 @@ is_file :: proc(path: string) -> bool {
attribs := win32.GetFileAttributesW(wpath);
if i32(attribs) != win32.INVALID_FILE_ATTRIBUTES {
return attribs & win32.FILE_ATTRIBUTE_DIRECTORY == win32.FILE_ATTRIBUTE_DIRECTORY;
return attribs & win32.FILE_ATTRIBUTE_DIRECTORY == 0;
}
return false;
}
@@ -283,7 +283,7 @@ is_dir :: proc(path: string) -> bool {
attribs := win32.GetFileAttributesW(wpath);
if i32(attribs) != win32.INVALID_FILE_ATTRIBUTES {
return attribs & win32.FILE_ATTRIBUTE_DIRECTORY != win32.FILE_ATTRIBUTE_DIRECTORY;
return attribs & win32.FILE_ATTRIBUTE_DIRECTORY != 0;
}
return false;
}
@@ -347,7 +347,7 @@ is_abs :: proc(path: string) -> bool {
when ODIN_OS == "windows" {
if len(path) > 2 {
switch path[0] {
case 'A'..'Z', 'a'..'z':
case 'A'..='Z', 'a'..='z':
return path[1] == ':' && is_path_separator(path[2]);
}
}
+17 -49
View File
@@ -1,11 +1,8 @@
package os2
Platform_Error_Min_Bits :: 32;
import "core:io"
Error :: enum u64 {
None = 0,
// General Errors
General_Error :: enum u32 {
Invalid_Argument,
Permission_Denied,
@@ -13,43 +10,20 @@ Error :: enum u64 {
Not_Exist,
Closed,
// Timeout Errors
Timeout,
// I/O Errors
// EOF is the error returned by `read` when no more input is available
EOF,
// Unexpected_EOF means that EOF was encountered in the middle of reading a fixed-sized block of data
Unexpected_EOF,
// Short_Write means that a write accepted fewer bytes than requested but failed to return an explicit error
Short_Write,
// Invalid_Write means that a write returned an impossible count
Invalid_Write,
// Short_Buffer means that a read required a longer buffer than was provided
Short_Buffer,
// No_Progress is returned by some implementations of `io.Reader` when many calls
// to `read` have failed to return any data or error.
// This is usually a signed of a broken `io.Reader` implementation
No_Progress,
Invalid_Whence,
Invalid_Offset,
Invalid_Unread,
Negative_Read,
Negative_Write,
Negative_Count,
Buffer_Full,
// Platform Specific Errors
Platform_Minimum = 1<<Platform_Error_Min_Bits,
}
Platform_Error :: struct {
err: i32,
}
Error :: union {
General_Error,
io.Error,
Platform_Error,
}
#assert(size_of(Error) == size_of(u64));
Path_Error :: struct {
op: string,
path: string,
@@ -83,20 +57,14 @@ link_error_delete :: proc(lerr: Maybe(Link_Error)) {
is_platform_error :: proc(ferr: Error) -> (err: i32, ok: bool) {
if ferr >= .Platform_Minimum {
err = i32(u64(ferr)>>Platform_Error_Min_Bits);
ok = true;
}
return;
v := or_else(ferr.(Platform_Error), {});
return v.err, v.err != 0;
}
error_from_platform_error :: proc(errno: i32) -> Error {
return Error(u64(errno) << Platform_Error_Min_Bits);
}
error_string :: proc(ferr: Error) -> string {
#partial switch ferr {
case .None: return "";
switch ferr {
case nil: return "";
case .Invalid_Argument: return "invalid argument";
case .Permission_Denied: return "permission denied";
case .Exist: return "file already exists";
+3 -16
View File
@@ -10,23 +10,10 @@ file_to_stream :: proc(fd: Handle) -> (s: io.Stream) {
@(private)
error_to_io_error :: proc(ferr: Error) -> io.Error {
#partial switch ferr {
case .None: return .None;
case .EOF: return .EOF;
case .Unexpected_EOF: return .Unexpected_EOF;
case .Short_Write: return .Short_Write;
case .Invalid_Write: return .Invalid_Write;
case .Short_Buffer: return .Short_Buffer;
case .No_Progress: return .No_Progress;
case .Invalid_Whence: return .Invalid_Whence;
case .Invalid_Offset: return .Invalid_Offset;
case .Invalid_Unread: return .Invalid_Unread;
case .Negative_Read: return .Negative_Read;
case .Negative_Write: return .Negative_Write;
case .Negative_Count: return .Negative_Count;
case .Buffer_Full: return .Buffer_Full;
if ferr == nil {
return .None;
}
return .Unknown;
return or_else(ferr.(io.Error), .Unknown);
}
+1
View File
@@ -1,6 +1,7 @@
package os2
import "core:mem"
import "core:io"
import "core:strconv"
import "core:unicode/utf8"
+10 -10
View File
@@ -5,19 +5,19 @@ import "core:io"
import "core:time"
_create :: proc(name: string) -> (Handle, Error) {
return 0, .None;
return 0, nil;
}
_open :: proc(name: string) -> (Handle, Error) {
return 0, .None;
return 0, nil;
}
_open_file :: proc(name: string, flag: int, perm: File_Mode) -> (Handle, Error) {
return 0, .None;
return 0, nil;
}
_close :: proc(fd: Handle) -> Error {
return .None;
return nil;
}
_name :: proc(fd: Handle, allocator := context.allocator) -> string {
@@ -58,11 +58,11 @@ _file_size :: proc(fd: Handle) -> (n: i64, err: Error) {
_sync :: proc(fd: Handle) -> Error {
return .None;
return nil;
}
_flush :: proc(fd: Handle) -> Error {
return .None;
return nil;
}
_truncate :: proc(fd: Handle, size: i64) -> Maybe(Path_Error) {
@@ -92,20 +92,20 @@ _read_link :: proc(name: string) -> (string, Maybe(Path_Error)) {
_chdir :: proc(fd: Handle) -> Error {
return .None;
return nil;
}
_chmod :: proc(fd: Handle, mode: File_Mode) -> Error {
return .None;
return nil;
}
_chown :: proc(fd: Handle, uid, gid: int) -> Error {
return .None;
return nil;
}
_lchown :: proc(name: string, uid, gid: int) -> Error {
return .None;
return nil;
}
+1 -1
View File
@@ -6,7 +6,7 @@ import win32 "core:sys/windows"
_pipe :: proc() -> (r, w: Handle, err: Error) {
p: [2]win32.HANDLE;
if !win32.CreatePipe(&p[0], &p[1], nil, 0) {
return 0, 0, error_from_platform_error(i32(win32.GetLastError()));
return 0, 0, Platform_Error{i32(win32.GetLastError())};
}
return Handle(p[0]), Handle(p[1]), nil;
}
+4 -4
View File
@@ -40,7 +40,7 @@ _same_file :: proc(fi1, fi2: File_Info) -> bool {
_stat_errno :: proc(errno: win32.DWORD) -> Path_Error {
return Path_Error{err = error_from_platform_error(i32(errno))};
return Path_Error{err = Platform_Error{i32(errno)}};
}
@@ -89,7 +89,7 @@ internal_stat :: proc(name: string, create_file_attributes: u32, allocator := co
fd: win32.WIN32_FIND_DATAW;
sh := win32.FindFirstFileW(wname, &fd);
if sh == win32.INVALID_HANDLE_VALUE {
e = Path_Error{err = error_from_platform_error(i32(win32.GetLastError()))};
e = Path_Error{err = Platform_Error{i32(win32.GetLastError())}};
return;
}
win32.FindClose(sh);
@@ -99,7 +99,7 @@ internal_stat :: proc(name: string, create_file_attributes: u32, allocator := co
h := win32.CreateFileW(wname, 0, 0, nil, win32.OPEN_EXISTING, create_file_attributes, nil);
if h == win32.INVALID_HANDLE_VALUE {
e = Path_Error{err = error_from_platform_error(i32(win32.GetLastError()))};
e = Path_Error{err = Platform_Error{i32(win32.GetLastError())}};
return;
}
defer win32.CloseHandle(h);
@@ -322,7 +322,7 @@ _is_abs :: proc(path: string) -> bool {
}
if len(path) > 2 {
switch path[0] {
case 'A'..'Z', 'a'..'z':
case 'A'..='Z', 'a'..='z':
return path[1] == ':' && is_path_separator(path[2]);
}
}
+2 -2
View File
@@ -4,11 +4,11 @@ package os2
import win32 "core:sys/windows"
_create_temp :: proc(dir, pattern: string) -> (Handle, Error) {
return 0, .None;
return 0, nil;
}
_mkdir_temp :: proc(dir, pattern: string, allocator := context.allocator) -> (string, Error) {
return "", .None;
return "", nil;
}
_temp_dir :: proc(allocator := context.allocator) -> string {
+2 -2
View File
@@ -302,7 +302,7 @@ foreign libc {
@(link_name="chdir") _unix_chdir :: proc(buf: cstring) -> c.int ---;
@(link_name="realpath") _unix_realpath :: proc(path: cstring, resolved_path: rawptr) -> rawptr ---;
@(link_name="exit") _unix_exit :: proc(status: int) ---;
@(link_name="exit") _unix_exit :: proc(status: int) -> ! ---;
}
foreign dl {
@@ -337,7 +337,7 @@ write :: proc(fd: Handle, data: []u8) -> (int, Errno) {
return 0, 0;
}
bytes_written := _unix_write(fd, raw_data(data), len(data));
if(bytes_written == -1) {
if bytes_written == -1 {
return 0, 1;
}
return bytes_written, 0;
+1 -1
View File
@@ -10,7 +10,7 @@ import "core:c"
Handle :: distinct i32;
File_Time :: distinct u64;
Errno :: distinct i32;
Syscall :: distinct int;
Syscall :: distinct i32;
INVALID_HANDLE :: ~Handle(0);
+3 -2
View File
@@ -35,8 +35,9 @@ open :: proc(path: string, mode: int = O_RDONLY, perm: int = 0) -> (Handle, Errn
close :: proc(fd: Handle) -> Errno {
return 0;
}
seek :: proc(fd: Handle, offset: i64, whence: int) -> (i64, Errno) {
return 0, 0;
}
current_thread_id :: proc "contextless" () -> int {
return 0;
}
+3 -3
View File
@@ -11,7 +11,7 @@ import "core:strconv"
Handle :: distinct i32;
File_Time :: distinct u64;
Errno :: distinct i32;
Syscall :: distinct int;
Syscall :: distinct i32;
INVALID_HANDLE :: ~Handle(0);
@@ -269,7 +269,7 @@ SYS_GETTID: Syscall : 186;
foreign libc {
@(link_name="__errno_location") __errno_location :: proc() -> ^int ---;
@(link_name="syscall") syscall :: proc(number: Syscall, #c_vararg args: ..any) -> int ---;
@(link_name="syscall") syscall :: proc(number: Syscall, #c_vararg args: ..any) -> i32 ---;
@(link_name="open") _unix_open :: proc(path: cstring, flags: c.int, mode: c.int) -> Handle ---;
@(link_name="close") _unix_close :: proc(fd: Handle) -> c.int ---;
@@ -595,7 +595,7 @@ exit :: proc "contextless" (code: int) -> ! {
}
current_thread_id :: proc "contextless" () -> int {
return syscall(SYS_GETTID);
return cast(int)syscall(SYS_GETTID);
}
dlopen :: proc(filename: string, flags: int) -> rawptr {
+2 -2
View File
@@ -11,14 +11,14 @@ full_path_from_name :: proc(name: string, allocator := context.allocator) -> (pa
}
p := win32.utf8_to_utf16(name, context.temp_allocator);
buf := make([dynamic]u16, 100, allocator);
defer delete(buf);
for {
n := win32.GetFullPathNameW(raw_data(p), u32(len(buf)), raw_data(buf), nil);
if n == 0 {
delete(buf);
return "", Errno(win32.GetLastError());
}
if n <= u32(len(buf)) {
return win32.utf16_to_utf8(buf[:n]), ERROR_NONE;
return win32.utf16_to_utf8(buf[:n], allocator), ERROR_NONE;
}
resize(&buf, len(buf)*2);
}
+14 -8
View File
@@ -39,7 +39,7 @@ volume_name_len :: proc(path: string) -> int {
c := path[0];
if path[1] == ':' {
switch c {
case 'a'..'z', 'A'..'Z':
case 'a'..='z', 'A'..='Z':
return 2;
}
}
@@ -170,6 +170,7 @@ clean :: proc(path: string, allocator := context.allocator) -> string {
cleaned, new_allocation := from_slash(s);
if new_allocation {
delete(s);
lazy_buffer_destroy(out);
}
return cleaned;
}
@@ -206,23 +207,23 @@ Relative_Error :: enum {
rel :: proc(base_path, target_path: string, allocator := context.allocator) -> (string, Relative_Error) {
context.allocator = allocator;
base_vol, target_vol := volume_name(base_path), volume_name(target_path);
base, target := clean(base_path), clean(target_path);
base_clean, target_clean := clean(base_path), clean(target_path);
delete_target := true;
defer {
if delete_target {
delete(target);
delete(target_clean);
}
delete(base);
delete(base_clean);
}
if strings.equal_fold(target, base) {
if strings.equal_fold(target_clean, base_clean) {
return strings.clone("."), .None;
}
base = base[len(base_vol):];
target = target[len(target_vol):];
base_vol, target_vol := volume_name(base_path), volume_name(target_path);
base := base_clean[len(base_vol):];
target := target_clean[len(target_vol):];
if base == "." {
base = "";
}
@@ -395,3 +396,8 @@ lazy_buffer_string :: proc(lb: ^Lazy_Buffer) -> string {
copy(z[len(x):], y);
return string(z);
}
@(private)
lazy_buffer_destroy :: proc(lb: ^Lazy_Buffer) {
delete(lb.b);
lb^ = {};
}
+1 -1
View File
@@ -5,7 +5,7 @@ import "core:mem"
_ :: runtime;
_ :: mem;
Map_Entry_Info :: struct(Key, Value: typeid) {
Map_Entry_Info :: struct($Key, $Value: typeid) {
hash: uintptr,
key: Key,
value: Value,
+63 -21
View File
@@ -33,6 +33,8 @@ Type_Info_Simd_Vector :: runtime.Type_Info_Simd_Vector;
Type_Info_Relative_Pointer :: runtime.Type_Info_Relative_Pointer;
Type_Info_Relative_Slice :: runtime.Type_Info_Relative_Slice;
Type_Info_Enum_Value :: runtime.Type_Info_Enum_Value;
Type_Kind :: enum {
Invalid,
@@ -111,7 +113,7 @@ backing_type_kind :: proc(T: typeid) -> Type_Kind {
}
type_info_base :: proc(info: ^runtime.Type_Info) -> ^runtime.Type_Info {
type_info_base :: proc(info: ^Type_Info) -> ^Type_Info {
if info == nil { return nil; }
base := info;
@@ -125,7 +127,7 @@ type_info_base :: proc(info: ^runtime.Type_Info) -> ^runtime.Type_Info {
}
type_info_core :: proc(info: ^runtime.Type_Info) -> ^runtime.Type_Info {
type_info_core :: proc(info: ^Type_Info) -> ^Type_Info {
if info == nil { return nil; }
base := info;
@@ -344,7 +346,7 @@ Struct_Tag :: distinct string;
Struct_Field :: struct {
name: string,
type: typeid,
type: ^Type_Info,
tag: Struct_Tag,
offset: uintptr,
is_using: bool,
@@ -355,7 +357,7 @@ struct_field_at :: proc(T: typeid, i: int) -> (field: Struct_Field) {
if s, ok := ti.variant.(runtime.Type_Info_Struct); ok {
if 0 <= i && i < len(s.names) {
field.name = s.names[i];
field.type = s.types[i].id;
field.type = s.types[i];
field.tag = Struct_Tag(s.tags[i]);
field.offset = s.offsets[i];
field.is_using = s.usings[i];
@@ -370,7 +372,7 @@ struct_field_by_name :: proc(T: typeid, name: string) -> (field: Struct_Field) {
for fname, i in s.names {
if fname == name {
field.name = s.names[i];
field.type = s.types[i].id;
field.type = s.types[i];
field.tag = Struct_Tag(s.tags[i]);
field.offset = s.offsets[i];
field.is_using = s.usings[i];
@@ -381,7 +383,7 @@ struct_field_by_name :: proc(T: typeid, name: string) -> (field: Struct_Field) {
return;
}
struct_field_value_by_name :: proc(a: any, field: string, recurse := false) -> any {
struct_field_value_by_name :: proc(a: any, field: string, allow_using := false) -> any {
if a == nil { return nil; }
ti := runtime.type_info_base(type_info_of(a.id));
@@ -395,13 +397,13 @@ struct_field_value_by_name :: proc(a: any, field: string, recurse := false) -> a
};
}
if recurse && s.usings[i] {
if allow_using && s.usings[i] {
f := any{
rawptr(uintptr(a.data) + s.offsets[i]),
s.types[i].id,
};
if res := struct_field_value_by_name(f, field, recurse); res != nil {
if res := struct_field_value_by_name(f, field, allow_using); res != nil {
return res;
}
}
@@ -420,7 +422,7 @@ struct_field_names :: proc(T: typeid) -> []string {
return nil;
}
struct_field_types :: proc(T: typeid) -> []^runtime.Type_Info {
struct_field_types :: proc(T: typeid) -> []^Type_Info {
ti := runtime.type_info_base(type_info_of(T));
if s, ok := ti.variant.(runtime.Type_Info_Struct); ok {
return s.types;
@@ -445,6 +447,20 @@ struct_field_offsets :: proc(T: typeid) -> []uintptr {
return nil;
}
struct_fields_zipped :: proc(T: typeid) -> (fields: #soa[]Struct_Field) {
ti := runtime.type_info_base(type_info_of(T));
if s, ok := ti.variant.(runtime.Type_Info_Struct); ok {
return soa_zip(
name = s.names,
type = s.types,
tag = transmute([]Struct_Tag)s.tags,
offset = s.offsets,
is_using = s.usings,
);
}
return nil;
}
struct_tag_get :: proc(tag: Struct_Tag, key: string) -> (value: Struct_Tag) {
@@ -468,7 +484,7 @@ struct_tag_lookup :: proc(tag: Struct_Tag, key: string) -> (value: Struct_Tag, o
switch t[i] {
case ':', '"':
break loop;
case 0x00 ..< ' ', 0x7f .. 0x9f: // break if control character is found
case 0x00 ..< ' ', 0x7f ..= 0x9f: // break if control character is found
break loop;
}
i += 1;
@@ -516,7 +532,7 @@ enum_string :: proc(a: any) -> string {
if e, ok := ti.variant.(runtime.Type_Info_Enum); ok {
v, _ := as_i64(a);
for value, i in e.values {
if value == runtime.Type_Info_Enum_Value(v) {
if value == Type_Info_Enum_Value(v) {
return e.names[i];
}
}
@@ -528,26 +544,24 @@ enum_string :: proc(a: any) -> string {
}
// Given a enum type and a value name, get the enum value.
enum_from_name :: proc($EnumType: typeid, name: string) -> (value: EnumType, ok: bool) {
ti := type_info_base(type_info_of(EnumType));
enum_from_name :: proc($Enum_Type: typeid, name: string) -> (value: Enum_Type, ok: bool) {
ti := type_info_base(type_info_of(Enum_Type));
if eti, eti_ok := ti.variant.(runtime.Type_Info_Enum); eti_ok {
for value_name, i in eti.names {
if value_name != name {
continue;
}
v := eti.values[i];
value = EnumType(v);
value = Enum_Type(v);
ok = true;
return;
}
} else {
panic("expected enum type to reflect.enum_from_name");
}
return;
}
enum_from_name_any :: proc(EnumType: typeid, name: string) -> (value: runtime.Type_Info_Enum_Value, ok: bool) {
ti := runtime.type_info_base(type_info_of(EnumType));
enum_from_name_any :: proc(Enum_Type: typeid, name: string) -> (value: Type_Info_Enum_Value, ok: bool) {
ti := runtime.type_info_base(type_info_of(Enum_Type));
if eti, eti_ok := ti.variant.(runtime.Type_Info_Enum); eti_ok {
for value_name, i in eti.names {
if value_name != name {
@@ -557,14 +571,42 @@ enum_from_name_any :: proc(EnumType: typeid, name: string) -> (value: runtime.Ty
ok = true;
return;
}
} else {
panic("expected enum type to reflect.enum_from_name_any");
}
return;
}
union_variant_type_info :: proc(a: any) -> ^runtime.Type_Info {
enum_field_names :: proc(Enum_Type: typeid) -> []string {
ti := runtime.type_info_base(type_info_of(Enum_Type));
if eti, eti_ok := ti.variant.(runtime.Type_Info_Enum); eti_ok {
return eti.names;
}
return nil;
}
enum_field_values :: proc(Enum_Type: typeid) -> []Type_Info_Enum_Value {
ti := runtime.type_info_base(type_info_of(Enum_Type));
if eti, eti_ok := ti.variant.(runtime.Type_Info_Enum); eti_ok {
return eti.values;
}
return nil;
}
Enum_Field :: struct {
name: string,
value: Type_Info_Enum_Value,
}
enum_fields_zipped :: proc(Enum_Type: typeid) -> (fields: #soa[]Enum_Field) {
ti := runtime.type_info_base(type_info_of(Enum_Type));
if eti, eti_ok := ti.variant.(runtime.Type_Info_Enum); eti_ok {
return soa_zip(name=eti.names, value=eti.values);
}
return nil;
}
union_variant_type_info :: proc(a: any) -> ^Type_Info {
id := union_variant_typeid(a);
return type_info_of(id);
}
+9 -9
View File
@@ -121,6 +121,9 @@ Type_Info_Union :: struct {
variants: []^Type_Info,
tag_offset: uintptr,
tag_type: ^Type_Info,
equal: Equal_Proc, // set only when the struct has .Comparable set but does not have .Simple_Compare set
custom_align: bool,
no_nil: bool,
maybe: bool,
@@ -252,7 +255,7 @@ Source_Code_Location :: struct {
procedure: string,
}
Assertion_Failure_Proc :: #type proc(prefix, message: string, loc: Source_Code_Location);
Assertion_Failure_Proc :: #type proc(prefix, message: string, loc: Source_Code_Location) -> !;
// Allocation Stuff
Allocator_Mode :: enum byte {
@@ -326,8 +329,6 @@ Context :: struct {
assertion_failure_proc: Assertion_Failure_Proc,
logger: Logger,
thread_id: int,
user_data: any,
user_ptr: rawptr,
user_index: int,
@@ -410,7 +411,7 @@ type_info_core :: proc "contextless" (info: ^Type_Info) -> ^Type_Info {
}
type_info_base_without_enum :: type_info_core;
__type_info_of :: proc "contextless" (id: typeid) -> ^Type_Info {
__type_info_of :: proc "contextless" (id: typeid) -> ^Type_Info #no_bounds_check {
MASK :: 1<<(8*size_of(typeid) - 8) - 1;
data := transmute(uintptr)id;
n := int(data & MASK);
@@ -426,7 +427,7 @@ typeid_base :: proc "contextless" (id: typeid) -> typeid {
return ti.id;
}
typeid_core :: proc "contextless" (id: typeid) -> typeid {
ti := type_info_base_without_enum(type_info_of(id));
ti := type_info_core(type_info_of(id));
return ti.id;
}
typeid_base_without_enum :: typeid_core;
@@ -476,14 +477,13 @@ __init_context :: proc "contextless" (c: ^Context) {
c.temp_allocator.procedure = default_temp_allocator_proc;
c.temp_allocator.data = &global_default_temp_allocator_data;
c.thread_id = current_thread_id(); // NOTE(bill): This is "contextless" so it is okay to call
c.assertion_failure_proc = default_assertion_failure_proc;
c.logger.procedure = default_logger_proc;
c.logger.data = nil;
}
default_assertion_failure_proc :: proc(prefix, message: string, loc: Source_Code_Location) {
default_assertion_failure_proc :: proc(prefix, message: string, loc: Source_Code_Location) -> ! {
print_caller_location(loc);
print_string(" ");
print_string(prefix);
@@ -492,6 +492,6 @@ default_assertion_failure_proc :: proc(prefix, message: string, loc: Source_Code
print_string(message);
}
print_byte('\n');
debug_trap();
// trap();
// intrinsics.debug_trap();
intrinsics.trap();
}
+31 -24
View File
@@ -3,7 +3,7 @@ package runtime
import "intrinsics"
@builtin
Maybe :: union(T: typeid) #maybe {T};
Maybe :: union($T: typeid) #maybe {T};
@thread_local global_default_temp_allocator_data: Default_Temp_Allocator;
@@ -17,7 +17,7 @@ init_global_temporary_allocator :: proc(size: int, backup_allocator := context.a
copy_slice :: proc "contextless" (dst, src: $T/[]$E) -> int {
n := max(0, min(len(dst), len(src)));
if n > 0 {
mem_copy(raw_data(dst), raw_data(src), n*size_of(E));
intrinsics.mem_copy(raw_data(dst), raw_data(src), n*size_of(E));
}
return n;
}
@@ -25,7 +25,7 @@ copy_slice :: proc "contextless" (dst, src: $T/[]$E) -> int {
copy_from_string :: proc "contextless" (dst: $T/[]$E/u8, src: $S/string) -> int {
n := max(0, min(len(dst), len(src)));
if n > 0 {
mem_copy(raw_data(dst), raw_data(src), n);
intrinsics.mem_copy(raw_data(dst), raw_data(src), n);
}
return n;
}
@@ -35,26 +35,26 @@ copy :: proc{copy_slice, copy_from_string};
@builtin
unordered_remove :: proc(array: ^$D/[dynamic]$T, index: int, loc := #caller_location) {
unordered_remove :: proc(array: ^$D/[dynamic]$T, index: int, loc := #caller_location) #no_bounds_check {
bounds_check_error_loc(loc, index, len(array));
n := len(array)-1;
if index != n {
array[index] = array[n];
}
pop(array);
(^Raw_Dynamic_Array)(array).len -= 1;
}
@builtin
ordered_remove :: proc(array: ^$D/[dynamic]$T, index: int, loc := #caller_location) {
ordered_remove :: proc(array: ^$D/[dynamic]$T, index: int, loc := #caller_location) #no_bounds_check {
bounds_check_error_loc(loc, index, len(array));
if index+1 < len(array) {
copy(array[index:], array[index+1:]);
}
pop(array);
(^Raw_Dynamic_Array)(array).len -= 1;
}
@builtin
remove_range :: proc(array: ^$D/[dynamic]$T, lo, hi: int, loc := #caller_location) {
remove_range :: proc(array: ^$D/[dynamic]$T, lo, hi: int, loc := #caller_location) #no_bounds_check {
slice_expr_error_lo_hi_loc(loc, lo, hi, len(array));
n := max(hi-lo, 0);
if n > 0 {
@@ -270,11 +270,22 @@ reserve_map :: proc(m: ^$T/map[$K]$V, capacity: int) {
// The delete_key built-in procedure deletes the element with the specified key (m[key]) from the map.
// If m is nil, or there is no such element, this procedure is a no-op
@builtin
delete_key :: proc(m: ^$T/map[$K]$V, key: K) {
delete_key :: proc(m: ^$T/map[$K]$V, key: K) -> (deleted_key: K, deleted_value: V) {
if m != nil {
key := key;
__dynamic_map_delete_key(__get_map_header(m), __get_map_hash(&key));
h := __get_map_header(m);
hash := __get_map_hash(&key);
fr := __dynamic_map_find(h, hash);
if fr.entry_index >= 0 {
entry := __dynamic_map_get_entry(h, fr.entry_index);
deleted_key = (^K)(uintptr(entry)+h.key_offset)^;
deleted_value = (^V)(uintptr(entry)+h.value_offset)^;
__dynamic_map_erase(h, fr);
}
}
return;
}
@@ -285,22 +296,18 @@ append_elem :: proc(array: ^$T/[dynamic]$E, arg: E, loc := #caller_location) {
return;
}
arg_len := 1;
if cap(array) < len(array)+arg_len {
cap := 2 * cap(array) + max(8, arg_len);
if cap(array) < len(array)+1 {
cap := 2 * cap(array) + max(8, 1);
_ = reserve(array, cap, loc);
}
arg_len = min(cap(array)-len(array), arg_len);
if arg_len > 0 {
if cap(array)-len(array) > 0 {
a := (^Raw_Dynamic_Array)(array);
if size_of(E) != 0 {
when size_of(E) != 0 {
data := (^E)(a.data);
assert(data != nil);
val := arg;
mem_copy(ptr_offset(data, a.len), &val, size_of(E));
assert(condition=data != nil, loc=loc);
intrinsics.ptr_offset(data, a.len)^ = arg;
}
a.len += arg_len;
a.len += 1;
}
}
@@ -323,10 +330,10 @@ append_elems :: proc(array: ^$T/[dynamic]$E, args: ..E, loc := #caller_location)
arg_len = min(cap(array)-len(array), arg_len);
if arg_len > 0 {
a := (^Raw_Dynamic_Array)(array);
if size_of(E) != 0 {
when size_of(E) != 0 {
data := (^E)(a.data);
assert(data != nil);
mem_copy(ptr_offset(data, a.len), &args[0], size_of(E) * arg_len);
assert(condition=data != nil, loc=loc);
intrinsics.mem_copy(intrinsics.ptr_offset(data, a.len), &args[0], size_of(E) * arg_len);
}
a.len += arg_len;
}
-205
View File
@@ -1,205 +0,0 @@
package runtime
when ODIN_DEFAULT_TO_NIL_ALLOCATOR || ODIN_OS == "freestanding" {
// mem.nil_allocator reimplementation
default_allocator_proc :: proc(allocator_data: rawptr, mode: mem.Allocator_Mode,
size, alignment: int,
old_memory: rawptr, old_size: int, loc := #caller_location) -> ([]byte, Allocator_Error) {
return nil, .None;
}
default_allocator :: proc() -> Allocator {
return Allocator{
procedure = default_allocator_proc,
data = nil,
};
}
} else when ODIN_OS != "windows" {
// TODO(bill): reimplement these procedures in the os_specific stuff
import "core:os"
default_allocator_proc :: os.heap_allocator_proc;
default_allocator :: proc() -> Allocator {
return os.heap_allocator();
}
}
@(private)
byte_slice :: #force_inline proc "contextless" (data: rawptr, len: int) -> []byte {
return transmute([]u8)Raw_Slice{data=data, len=max(len, 0)};
}
DEFAULT_TEMP_ALLOCATOR_BACKING_SIZE: int : #config(DEFAULT_TEMP_ALLOCATOR_BACKING_SIZE, 1<<22);
Default_Temp_Allocator :: struct {
data: []byte,
curr_offset: int,
prev_allocation: rawptr,
backup_allocator: Allocator,
leaked_allocations: [dynamic][]byte,
}
default_temp_allocator_init :: proc(s: ^Default_Temp_Allocator, size: int, backup_allocator := context.allocator) {
s.data = make_aligned([]byte, size, 2*align_of(rawptr), backup_allocator);
s.curr_offset = 0;
s.prev_allocation = nil;
s.backup_allocator = backup_allocator;
s.leaked_allocations.allocator = backup_allocator;
}
default_temp_allocator_destroy :: proc(s: ^Default_Temp_Allocator) {
if s == nil {
return;
}
for ptr in s.leaked_allocations {
free(raw_data(ptr), s.backup_allocator);
}
delete(s.leaked_allocations);
delete(s.data, s.backup_allocator);
s^ = {};
}
default_temp_allocator_proc :: proc(allocator_data: rawptr, mode: Allocator_Mode,
size, alignment: int,
old_memory: rawptr, old_size: int, loc := #caller_location) -> ([]byte, Allocator_Error) {
s := (^Default_Temp_Allocator)(allocator_data);
if s.data == nil {
default_temp_allocator_init(s, DEFAULT_TEMP_ALLOCATOR_BACKING_SIZE, default_allocator());
}
size := size;
switch mode {
case .Alloc:
size = align_forward_int(size, alignment);
switch {
case s.curr_offset+size <= len(s.data):
start := uintptr(raw_data(s.data));
ptr := start + uintptr(s.curr_offset);
ptr = align_forward_uintptr(ptr, uintptr(alignment));
mem_zero(rawptr(ptr), size);
s.prev_allocation = rawptr(ptr);
offset := int(ptr - start);
s.curr_offset = offset + size;
return byte_slice(rawptr(ptr), size), .None;
case size <= len(s.data):
start := uintptr(raw_data(s.data));
ptr := align_forward_uintptr(start, uintptr(alignment));
mem_zero(rawptr(ptr), size);
s.prev_allocation = rawptr(ptr);
offset := int(ptr - start);
s.curr_offset = offset + size;
return byte_slice(rawptr(ptr), size), .None;
}
a := s.backup_allocator;
if a.procedure == nil {
a = context.allocator;
s.backup_allocator = a;
}
data, err := mem_alloc_bytes(size, alignment, a, loc);
if err != nil {
return data, err;
}
if s.leaked_allocations == nil {
s.leaked_allocations = make([dynamic][]byte, a);
}
append(&s.leaked_allocations, data);
// TODO(bill): Should leaks be notified about?
if logger := context.logger; logger.lowest_level <= .Warning {
if logger.procedure != nil {
logger.procedure(logger.data, .Warning, "default temp allocator resorted to backup_allocator" , logger.options, loc);
}
}
return data, .None;
case .Free:
if old_memory == nil {
return nil, .None;
}
start := uintptr(raw_data(s.data));
end := start + uintptr(len(s.data));
old_ptr := uintptr(old_memory);
if s.prev_allocation == old_memory {
s.curr_offset = int(uintptr(s.prev_allocation) - start);
s.prev_allocation = nil;
return nil, .None;
}
if start <= old_ptr && old_ptr < end {
// NOTE(bill): Cannot free this pointer but it is valid
return nil, .None;
}
if len(s.leaked_allocations) != 0 {
for data, i in s.leaked_allocations {
ptr := raw_data(data);
if ptr == old_memory {
free(ptr, s.backup_allocator);
ordered_remove(&s.leaked_allocations, i);
return nil, .None;
}
}
}
return nil, .Invalid_Pointer;
// panic("invalid pointer passed to default_temp_allocator");
case .Free_All:
s.curr_offset = 0;
s.prev_allocation = nil;
for data in s.leaked_allocations {
free(raw_data(data), s.backup_allocator);
}
clear(&s.leaked_allocations);
case .Resize:
begin := uintptr(raw_data(s.data));
end := begin + uintptr(len(s.data));
old_ptr := uintptr(old_memory);
if old_memory == s.prev_allocation && old_ptr & uintptr(alignment)-1 == 0 {
if old_ptr+uintptr(size) < end {
s.curr_offset = int(old_ptr-begin)+size;
return byte_slice(old_memory, size), .None;
}
}
ptr, err := default_temp_allocator_proc(allocator_data, .Alloc, size, alignment, old_memory, old_size, loc);
if err == .None {
copy(ptr, byte_slice(old_memory, old_size));
_, err = default_temp_allocator_proc(allocator_data, .Free, 0, alignment, old_memory, old_size, loc);
}
return ptr, err;
case .Query_Features:
set := (^Allocator_Mode_Set)(old_memory);
if set != nil {
set^ = {.Alloc, .Free, .Free_All, .Resize, .Query_Features};
}
return nil, nil;
case .Query_Info:
return nil, .None;
}
return nil, .None;
}
default_temp_allocator :: proc(allocator: ^Default_Temp_Allocator) -> Allocator {
return Allocator{
procedure = default_temp_allocator_proc,
data = allocator,
};
}
@@ -0,0 +1,29 @@
//+build !windows
//+build !freestanding
package runtime
when ODIN_DEFAULT_TO_NIL_ALLOCATOR {
// mem.nil_allocator reimplementation
default_allocator_proc :: proc(allocator_data: rawptr, mode: Allocator_Mode,
size, alignment: int,
old_memory: rawptr, old_size: int, loc := #caller_location) -> ([]byte, Allocator_Error) {
return nil, .None;
}
default_allocator :: proc() -> Allocator {
return Allocator{
procedure = default_allocator_proc,
data = nil,
};
}
} else {
// TODO(bill): reimplement these procedures in the os_specific stuff
import "core:os"
default_allocator_proc :: os.heap_allocator_proc;
default_allocator :: proc() -> Allocator {
return os.heap_allocator();
}
}
+17
View File
@@ -0,0 +1,17 @@
//+build freestanding
package runtime
// mem.nil_allocator reimplementation
default_allocator_proc :: proc(allocator_data: rawptr, mode: Allocator_Mode,
size, alignment: int,
old_memory: rawptr, old_size: int, loc := #caller_location) -> ([]byte, Allocator_Error) {
return nil, .None;
}
default_allocator :: proc() -> Allocator {
return Allocator{
procedure = default_allocator_proc,
data = nil,
};
}
@@ -0,0 +1,38 @@
//+build windows
package runtime
default_allocator_proc :: proc(allocator_data: rawptr, mode: Allocator_Mode,
size, alignment: int,
old_memory: rawptr, old_size: int, loc := #caller_location) -> (data: []byte, err: Allocator_Error) {
switch mode {
case .Alloc:
data, err = _windows_default_alloc(size, alignment);
case .Free:
_windows_default_free(old_memory);
case .Free_All:
// NOTE(tetra): Do nothing.
case .Resize:
data, err = _windows_default_resize(old_memory, old_size, size, alignment);
case .Query_Features:
set := (^Allocator_Mode_Set)(old_memory);
if set != nil {
set^ = {.Alloc, .Free, .Resize, .Query_Features};
}
case .Query_Info:
// Do nothing
}
return;
}
default_allocator :: proc() -> Allocator {
return Allocator{
procedure = default_allocator_proc,
data = nil,
};
}
@@ -0,0 +1,196 @@
package runtime
@(private)
byte_slice :: #force_inline proc "contextless" (data: rawptr, len: int) -> []byte {
return transmute([]u8)Raw_Slice{data=data, len=max(len, 0)};
}
DEFAULT_TEMP_ALLOCATOR_BACKING_SIZE: int : #config(DEFAULT_TEMP_ALLOCATOR_BACKING_SIZE, 1<<22);
Default_Temp_Allocator :: struct {
data: []byte,
curr_offset: int,
prev_allocation: rawptr,
backup_allocator: Allocator,
leaked_allocations: [dynamic][]byte,
}
default_temp_allocator_init :: proc(s: ^Default_Temp_Allocator, size: int, backup_allocator := context.allocator) {
s.data = make_aligned([]byte, size, 2*align_of(rawptr), backup_allocator);
s.curr_offset = 0;
s.prev_allocation = nil;
s.backup_allocator = backup_allocator;
s.leaked_allocations.allocator = backup_allocator;
}
default_temp_allocator_destroy :: proc(s: ^Default_Temp_Allocator) {
if s == nil {
return;
}
for ptr in s.leaked_allocations {
free(raw_data(ptr), s.backup_allocator);
}
delete(s.leaked_allocations);
delete(s.data, s.backup_allocator);
s^ = {};
}
@(private)
default_temp_allocator_alloc :: proc(s: ^Default_Temp_Allocator, size, alignment: int, loc := #caller_location) -> ([]byte, Allocator_Error) {
size := size;
size = align_forward_int(size, alignment);
switch {
case s.curr_offset+size <= len(s.data):
start := uintptr(raw_data(s.data));
ptr := start + uintptr(s.curr_offset);
ptr = align_forward_uintptr(ptr, uintptr(alignment));
mem_zero(rawptr(ptr), size);
s.prev_allocation = rawptr(ptr);
offset := int(ptr - start);
s.curr_offset = offset + size;
return byte_slice(rawptr(ptr), size), .None;
case size <= len(s.data):
start := uintptr(raw_data(s.data));
ptr := align_forward_uintptr(start, uintptr(alignment));
mem_zero(rawptr(ptr), size);
s.prev_allocation = rawptr(ptr);
offset := int(ptr - start);
s.curr_offset = offset + size;
return byte_slice(rawptr(ptr), size), .None;
}
a := s.backup_allocator;
if a.procedure == nil {
a = context.allocator;
s.backup_allocator = a;
}
data, err := mem_alloc_bytes(size, alignment, a, loc);
if err != nil {
return data, err;
}
if s.leaked_allocations == nil {
s.leaked_allocations = make([dynamic][]byte, a);
}
append(&s.leaked_allocations, data);
// TODO(bill): Should leaks be notified about?
if logger := context.logger; logger.lowest_level <= .Warning {
if logger.procedure != nil {
logger.procedure(logger.data, .Warning, "default temp allocator resorted to backup_allocator" , logger.options, loc);
}
}
return data, .None;
}
@(private)
default_temp_allocator_free :: proc(s: ^Default_Temp_Allocator, old_memory: rawptr, loc := #caller_location) -> Allocator_Error {
if old_memory == nil {
return .None;
}
start := uintptr(raw_data(s.data));
end := start + uintptr(len(s.data));
old_ptr := uintptr(old_memory);
if s.prev_allocation == old_memory {
s.curr_offset = int(uintptr(s.prev_allocation) - start);
s.prev_allocation = nil;
return .None;
}
if start <= old_ptr && old_ptr < end {
// NOTE(bill): Cannot free this pointer but it is valid
return .None;
}
if len(s.leaked_allocations) != 0 {
for data, i in s.leaked_allocations {
ptr := raw_data(data);
if ptr == old_memory {
free(ptr, s.backup_allocator);
ordered_remove(&s.leaked_allocations, i);
return .None;
}
}
}
return .Invalid_Pointer;
// panic("invalid pointer passed to default_temp_allocator");
}
@(private)
default_temp_allocator_free_all :: proc(s: ^Default_Temp_Allocator, loc := #caller_location) {
s.curr_offset = 0;
s.prev_allocation = nil;
for data in s.leaked_allocations {
free(raw_data(data), s.backup_allocator);
}
clear(&s.leaked_allocations);
}
@(private)
default_temp_allocator_resize :: proc(s: ^Default_Temp_Allocator, old_memory: rawptr, old_size, size, alignment: int, loc := #caller_location) -> ([]byte, Allocator_Error) {
begin := uintptr(raw_data(s.data));
end := begin + uintptr(len(s.data));
old_ptr := uintptr(old_memory);
if old_memory == s.prev_allocation && old_ptr & uintptr(alignment)-1 == 0 {
if old_ptr+uintptr(size) < end {
s.curr_offset = int(old_ptr-begin)+size;
return byte_slice(old_memory, size), .None;
}
}
data, err := default_temp_allocator_alloc(s, size, alignment, loc);
if err == .None {
copy(data, byte_slice(old_memory, old_size));
err = default_temp_allocator_free(s, old_memory, loc);
}
return data, err;
}
default_temp_allocator_proc :: proc(allocator_data: rawptr, mode: Allocator_Mode,
size, alignment: int,
old_memory: rawptr, old_size: int, loc := #caller_location) -> (data: []byte, err: Allocator_Error) {
s := (^Default_Temp_Allocator)(allocator_data);
if s.data == nil {
default_temp_allocator_init(s, DEFAULT_TEMP_ALLOCATOR_BACKING_SIZE, default_allocator());
}
switch mode {
case .Alloc:
data, err = default_temp_allocator_alloc(s, size, alignment, loc);
case .Free:
err = default_temp_allocator_free(s, old_memory, loc);
case .Free_All:
default_temp_allocator_free_all(s, loc);
case .Resize:
data, err = default_temp_allocator_resize(s, old_memory, old_size, size, alignment, loc);
case .Query_Features:
set := (^Allocator_Mode_Set)(old_memory);
if set != nil {
set^ = {.Alloc, .Free, .Free_All, .Resize, .Query_Features};
}
case .Query_Info:
// Nothing to give
}
return;
}
default_temp_allocator :: proc(allocator: ^Default_Temp_Allocator) -> Allocator {
return Allocator{
procedure = default_temp_allocator_proc,
data = allocator,
};
}
+1 -1
View File
@@ -362,7 +362,7 @@ __dynamic_map_erase :: proc(using h: Map_Header, fr: Map_Find_Result) #no_bounds
curr := __dynamic_map_get_entry(h, fr.entry_index);
prev.next = curr.next;
}
if (fr.entry_index == m.entries.len-1) {
if fr.entry_index == m.entries.len-1 {
// NOTE(bill): No need to do anything else, just pop
} else {
old := __dynamic_map_get_entry(h, fr.entry_index);
-8
View File
@@ -22,7 +22,6 @@ bounds_check_error :: proc "contextless" (file: string, line, column: i32, index
return;
}
handle_error :: proc "contextless" (file: string, line, column: i32, index, count: int) {
context = default_context();
print_caller_location(Source_Code_Location{file, line, column, ""});
print_string(" Index ");
print_i64(i64(index));
@@ -35,7 +34,6 @@ bounds_check_error :: proc "contextless" (file: string, line, column: i32, index
}
slice_handle_error :: proc "contextless" (file: string, line, column: i32, lo, hi: int, len: int) -> ! {
context = default_context();
print_caller_location(Source_Code_Location{file, line, column, ""});
print_string(" Invalid slice indices: ");
print_i64(i64(lo));
@@ -66,7 +64,6 @@ dynamic_array_expr_error :: proc "contextless" (file: string, line, column: i32,
return;
}
handle_error :: proc "contextless" (file: string, line, column: i32, low, high, max: int) {
context = default_context();
print_caller_location(Source_Code_Location{file, line, column, ""});
print_string(" Invalid dynamic array values: ");
print_i64(i64(low));
@@ -86,7 +83,6 @@ type_assertion_check :: proc "contextless" (ok: bool, file: string, line, column
return;
}
handle_error :: proc "contextless" (file: string, line, column: i32, from, to: typeid) {
context = default_context();
print_caller_location(Source_Code_Location{file, line, column, ""});
print_string(" Invalid type assertion from ");
print_typeid(from);
@@ -131,7 +127,6 @@ type_assertion_check2 :: proc "contextless" (ok: bool, file: string, line, colum
}
handle_error :: proc "contextless" (file: string, line, column: i32, from, to: typeid, from_data: rawptr) {
context = default_context();
actual := variant_type(from, from_data);
@@ -156,7 +151,6 @@ make_slice_error_loc :: #force_inline proc "contextless" (loc := #caller_locatio
return;
}
handle_error :: proc "contextless" (loc: Source_Code_Location, len: int) {
context = default_context();
print_caller_location(loc);
print_string(" Invalid slice length for make: ");
print_i64(i64(len));
@@ -171,7 +165,6 @@ make_dynamic_array_error_loc :: #force_inline proc "contextless" (using loc := #
return;
}
handle_error :: proc "contextless" (loc: Source_Code_Location, len, cap: int) {
context = default_context();
print_caller_location(loc);
print_string(" Invalid dynamic array parameters for make: ");
print_i64(i64(len));
@@ -188,7 +181,6 @@ make_map_expr_error_loc :: #force_inline proc "contextless" (loc := #caller_loca
return;
}
handle_error :: proc "contextless" (loc: Source_Code_Location, cap: int) {
context = default_context();
print_caller_location(loc);
print_string(" Invalid map capacity for make: ");
print_i64(i64(cap));
+116 -46
View File
@@ -97,7 +97,7 @@ mem_zero :: proc "contextless" (data: rawptr, len: int) -> rawptr {
if len < 0 {
return data;
}
memset(data, 0, len);
intrinsics.mem_zero(data, len);
return data;
}
@@ -105,17 +105,9 @@ mem_copy :: proc "contextless" (dst, src: rawptr, len: int) -> rawptr {
if src == nil {
return dst;
}
// NOTE(bill): This _must_ be implemented like C's memmove
foreign _ {
when size_of(rawptr) == 8 {
@(link_name="llvm.memmove.p0i8.p0i8.i64")
llvm_memmove :: proc "none" (dst, src: rawptr, len: int, is_volatile: bool = false) ---;
} else {
@(link_name="llvm.memmove.p0i8.p0i8.i32")
llvm_memmove :: proc "none" (dst, src: rawptr, len: int, is_volatile: bool = false) ---;
}
}
llvm_memmove(dst, src, len);
intrinsics.mem_copy(dst, src, len);
return dst;
}
@@ -123,17 +115,9 @@ mem_copy_non_overlapping :: proc "contextless" (dst, src: rawptr, len: int) -> r
if src == nil {
return dst;
}
// NOTE(bill): This _must_ be implemented like C's memcpy
foreign _ {
when size_of(rawptr) == 8 {
@(link_name="llvm.memcpy.p0i8.p0i8.i64")
llvm_memcpy :: proc "none" (dst, src: rawptr, len: int, is_volatile: bool = false) ---;
} else {
@(link_name="llvm.memcpy.p0i8.p0i8.i32")
llvm_memcpy :: proc "none" (dst, src: rawptr, len: int, is_volatile: bool = false) ---;
}
}
llvm_memcpy(dst, src, len);
intrinsics.mem_copy_non_overlapping(dst, src, len);
return dst;
}
@@ -316,6 +300,9 @@ cstring_to_string :: proc "contextless" (s: cstring) -> string {
}
complex32_eq :: #force_inline proc "contextless" (a, b: complex32) -> bool { return real(a) == real(b) && imag(a) == imag(b); }
complex32_ne :: #force_inline proc "contextless" (a, b: complex32) -> bool { return real(a) != real(b) || imag(a) != imag(b); }
complex64_eq :: #force_inline proc "contextless" (a, b: complex64) -> bool { return real(a) == real(b) && imag(a) == imag(b); }
complex64_ne :: #force_inline proc "contextless" (a, b: complex64) -> bool { return real(a) != real(b) || imag(a) != imag(b); }
@@ -323,6 +310,9 @@ complex128_eq :: #force_inline proc "contextless" (a, b: complex128) -> bool { r
complex128_ne :: #force_inline proc "contextless" (a, b: complex128) -> bool { return real(a) != real(b) || imag(a) != imag(b); }
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); }
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); }
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); }
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); }
@@ -409,11 +399,6 @@ string_decode_rune :: #force_inline proc "contextless" (s: string) -> (rune, int
return rune(s0&MASK4)<<18 | rune(b1&MASKX)<<12 | rune(b2&MASKX)<<6 | rune(b3&MASKX), 4;
}
@(default_calling_convention = "none")
foreign {
@(link_name="llvm.sqrt.f32") _sqrt_f32 :: proc(x: f32) -> f32 ---
@(link_name="llvm.sqrt.f64") _sqrt_f64 :: proc(x: f64) -> f64 ---
}
abs_f16 :: #force_inline proc "contextless" (x: f16) -> f16 {
return -x if x < 0 else x;
}
@@ -424,48 +409,48 @@ abs_f64 :: #force_inline proc "contextless" (x: f64) -> f64 {
return -x if x < 0 else x;
}
min_f16 :: proc(a, b: f16) -> f16 {
min_f16 :: #force_inline proc "contextless" (a, b: f16) -> f16 {
return a if a < b else b;
}
min_f32 :: proc(a, b: f32) -> f32 {
min_f32 :: #force_inline proc "contextless" (a, b: f32) -> f32 {
return a if a < b else b;
}
min_f64 :: proc(a, b: f64) -> f64 {
min_f64 :: #force_inline proc "contextless" (a, b: f64) -> f64 {
return a if a < b else b;
}
max_f16 :: proc(a, b: f16) -> f16 {
max_f16 :: #force_inline proc "contextless" (a, b: f16) -> f16 {
return a if a > b else b;
}
max_f32 :: proc(a, b: f32) -> f32 {
max_f32 :: #force_inline proc "contextless" (a, b: f32) -> f32 {
return a if a > b else b;
}
max_f64 :: proc(a, b: f64) -> f64 {
max_f64 :: #force_inline proc "contextless" (a, b: f64) -> f64 {
return a if a > b else b;
}
abs_complex32 :: #force_inline proc "contextless" (x: complex32) -> f16 {
r, i := real(x), imag(x);
return f16(_sqrt_f32(f32(r*r + i*i)));
return f16(intrinsics.sqrt(f32(r*r + i*i)));
}
abs_complex64 :: #force_inline proc "contextless" (x: complex64) -> f32 {
r, i := real(x), imag(x);
return _sqrt_f32(r*r + i*i);
return intrinsics.sqrt(r*r + i*i);
}
abs_complex128 :: #force_inline proc "contextless" (x: complex128) -> f64 {
r, i := real(x), imag(x);
return _sqrt_f64(r*r + i*i);
return intrinsics.sqrt(r*r + i*i);
}
abs_quaternion64 :: #force_inline proc "contextless" (x: quaternion64) -> f16 {
r, i, j, k := real(x), imag(x), jmag(x), kmag(x);
return f16(_sqrt_f32(f32(r*r + i*i + j*j + k*k)));
return f16(intrinsics.sqrt(f32(r*r + i*i + j*j + k*k)));
}
abs_quaternion128 :: #force_inline proc "contextless" (x: quaternion128) -> f32 {
r, i, j, k := real(x), imag(x), jmag(x), kmag(x);
return _sqrt_f32(r*r + i*i + j*j + k*k);
return intrinsics.sqrt(r*r + i*i + j*j + k*k);
}
abs_quaternion256 :: #force_inline proc "contextless" (x: quaternion256) -> f64 {
r, i, j, k := real(x), imag(x), jmag(x), kmag(x);
return _sqrt_f64(r*r + i*i + j*j + k*k);
return intrinsics.sqrt(r*r + i*i + j*j + k*k);
}
@@ -615,19 +600,19 @@ truncsfhf2 :: proc "c" (value: f32) -> u16 {
m = i & 0x007fffff;
if (e <= 0) {
if (e < -10) {
if e <= 0 {
if e < -10 {
return u16(s);
}
m = (m | 0x00800000) >> u32(1 - e);
if (m & 0x00001000) != 0 {
if m & 0x00001000 != 0 {
m += 0x00002000;
}
return u16(s | (m >> 13));
} else if (e == 0xff - (127 - 15)) {
if (m == 0) {
} else if e == 0xff - (127 - 15) {
if m == 0 {
return u16(s | 0x7c00); /* NOTE(bill): infinity */
} else {
/* NOTE(bill): NAN */
@@ -635,7 +620,7 @@ truncsfhf2 :: proc "c" (value: f32) -> u16 {
return u16(s | 0x7c00 | m | i32(m == 0));
}
} else {
if (m & 0x00001000) != 0 {
if m & 0x00001000 != 0 {
m += 0x00002000;
if (m & 0x00800000) != 0 {
m = 0;
@@ -643,8 +628,8 @@ truncsfhf2 :: proc "c" (value: f32) -> u16 {
}
}
if (e > 30) {
f := 1e12;
if e > 30 {
f := i64(1e12);
for j := 0; j < 10; j += 1 {
/* NOTE(bill): Cause overflow */
g := intrinsics.volatile_load(&f);
@@ -693,3 +678,88 @@ gnu_f2h_ieee :: proc "c" (value: f32) -> u16 {
extendhfsf2 :: proc "c" (value: u16) -> f32 {
return gnu_h2f_ieee(value);
}
@(link_name="__floattidf")
floattidf :: proc(a: i128) -> f64 {
DBL_MANT_DIG :: 53;
if a == 0 {
return 0.0;
}
a := a;
N :: size_of(i128) * 8;
s := a >> (N-1);
a = (a ~ s) - s;
sd: = N - intrinsics.count_leading_zeros(a); // number of significant digits
e := u32(sd - 1); // exponent
if sd > DBL_MANT_DIG {
switch sd {
case DBL_MANT_DIG + 1:
a <<= 1;
case DBL_MANT_DIG + 2:
// okay
case:
a = i128(u128(a) >> u128(sd - (DBL_MANT_DIG+2))) |
i128(u128(a) & (~u128(0) >> u128(N + DBL_MANT_DIG+2 - sd)) != 0);
};
a |= i128((a & 4) != 0);
a += 1;
a >>= 2;
if a & (1 << DBL_MANT_DIG) != 0 {
a >>= 1;
e += 1;
}
} else {
a <<= u128(DBL_MANT_DIG - sd);
}
fb: [2]u32;
fb[0] = (u32(s) & 0x80000000) | // sign
((e + 1023) << 20) | // exponent
u32((u64(a) >> 32) & 0x000FFFFF); // mantissa-high
fb[1] = u32(a); // mantissa-low
return transmute(f64)fb;
}
@(link_name="__floattidf_unsigned")
floattidf_unsigned :: proc(a: u128) -> f64 {
DBL_MANT_DIG :: 53;
if a == 0 {
return 0.0;
}
a := a;
N :: size_of(u128) * 8;
sd: = N - intrinsics.count_leading_zeros(a); // number of significant digits
e := u32(sd - 1); // exponent
if sd > DBL_MANT_DIG {
switch sd {
case DBL_MANT_DIG + 1:
a <<= 1;
case DBL_MANT_DIG + 2:
// okay
case:
a = u128(u128(a) >> u128(sd - (DBL_MANT_DIG+2))) |
u128(u128(a) & (~u128(0) >> u128(N + DBL_MANT_DIG+2 - sd)) != 0);
};
a |= u128((a & 4) != 0);
a += 1;
a >>= 2;
if a & (1 << DBL_MANT_DIG) != 0 {
a >>= 1;
e += 1;
}
} else {
a <<= u128(DBL_MANT_DIG - sd);
}
fb: [2]u32;
fb[0] = (0) | // sign
((e + 1023) << 20) | // exponent
u32((u64(a) >> 32) & 0x000FFFFF); // mantissa-high
fb[1] = u32(a); // mantissa-low
return transmute(f64)fb;
}
-42
View File
@@ -87,45 +87,3 @@ fixdfti :: proc(a: u64) -> i128 {
}
}
@(link_name="__floattidf")
floattidf :: proc(a: i128) -> f64 {
DBL_MANT_DIG :: 53;
if a == 0 {
return 0.0;
}
a := a;
N :: size_of(i128) * 8;
s := a >> (N-1);
a = (a ~ s) - s;
sd: = N - intrinsics.count_leading_zeros(a); // number of significant digits
e := u32(sd - 1); // exponent
if sd > DBL_MANT_DIG {
switch sd {
case DBL_MANT_DIG + 1:
a <<= 1;
case DBL_MANT_DIG + 2:
// okay
case:
a = i128(u128(a) >> u128(sd - (DBL_MANT_DIG+2))) |
i128(u128(a) & (~u128(0) >> u128(N + DBL_MANT_DIG+2 - sd)) != 0);
};
a |= i128((a & 4) != 0);
a += 1;
a >>= 2;
if a & (1 << DBL_MANT_DIG) != 0 {
a >>= 1;
e += 1;
}
} else {
a <<= u128(DBL_MANT_DIG - sd);
}
fb: [2]u32;
fb[1] = (u32(s) & 0x80000000) | // sign
((e + 1023) << 20) | // exponent
u32((u64(a) >> 32) & 0x000FFFFF); // mantissa-high
fb[1] = u32(a); // mantissa-low
return transmute(f64)fb;
}
-42
View File
@@ -87,45 +87,3 @@ fixdfti :: proc(a: u64) -> i128 {
}
}
@(link_name="__floattidf")
floattidf :: proc(a: i128) -> f64 {
DBL_MANT_DIG :: 53;
if a == 0 {
return 0.0;
}
a := a;
N :: size_of(i128) * 8;
s := a >> (N-1);
a = (a ~ s) - s;
sd: = N - intrinsics.count_leading_zeros(a); // number of significant digits
e := u32(sd - 1); // exponent
if sd > DBL_MANT_DIG {
switch sd {
case DBL_MANT_DIG + 1:
a <<= 1;
case DBL_MANT_DIG + 2:
// okay
case:
a = i128(u128(a) >> u128(sd - (DBL_MANT_DIG+2))) |
i128(u128(a) & (~u128(0) >> u128(N + DBL_MANT_DIG+2 - sd)) != 0);
};
a |= i128((a & 4) != 0);
a += 1;
a >>= 2;
if a & (1 << DBL_MANT_DIG) != 0 {
a >>= 1;
e += 1;
}
} else {
a <<= u128(DBL_MANT_DIG - sd);
}
fb: [2]u32;
fb[1] = (u32(s) & 0x80000000) | // sign
((e + 1023) << 20) | // exponent
u32((u64(a) >> 32) & 0x000FFFFF); // mantissa-high
fb[1] = u32(a); // mantissa-low
return transmute(f64)fb;
}
+4
View File
@@ -1,3 +1,7 @@
package runtime
_OS_Errno :: distinct int;
os_write :: proc "contextless" (data: []byte) -> (int, _OS_Errno) {
return _os_write(data);
}
+1 -5
View File
@@ -6,12 +6,8 @@ import "core:os"
// TODO(bill): reimplement `os.write` so that it does not rely on package os
// NOTE: Use os_specific_linux.odin, os_specific_darwin.odin, etc
os_write :: proc "contextless" (data: []byte) -> (int, _OS_Errno) {
_os_write :: proc "contextless" (data: []byte) -> (int, _OS_Errno) {
context = default_context();
n, err := os.write(os.stderr, data);
return int(n), _OS_Errno(err);
}
current_thread_id :: proc "contextless" () -> int {
return os.current_thread_id();
}
+1 -5
View File
@@ -2,10 +2,6 @@
package runtime
// TODO(bill): reimplement `os.write`
os_write :: proc "contextless" (data: []byte) -> (int, _OS_Errno) {
_os_write :: proc "contextless" (data: []byte) -> (int, _OS_Errno) {
return 0, -1;
}
current_thread_id :: proc "contextless" () -> int {
return 0;
}
+49 -85
View File
@@ -1,6 +1,9 @@
//+private
//+build windows
package runtime
import "intrinsics"
foreign import kernel32 "system:Kernel32.lib"
@(private="file")
@@ -14,9 +17,6 @@ foreign kernel32 {
WriteFile :: proc(hFile: rawptr, lpBuffer: rawptr, nNumberOfBytesToWrite: u32, lpNumberOfBytesWritten: ^u32, lpOverlapped: rawptr) -> b32 ---
GetLastError :: proc() -> u32 ---
// current_thread_id
GetCurrentThreadId :: proc() -> u32 ---
// default_allocator
GetProcessHeap :: proc() -> rawptr ---
HeapAlloc :: proc(hHeap: rawptr, dwFlags: u32, dwBytes: uint) -> rawptr ---
@@ -24,7 +24,7 @@ foreign kernel32 {
HeapFree :: proc(hHeap: rawptr, dwFlags: u32, lpMem: rawptr) -> b32 ---
}
os_write :: proc "contextless" (data: []byte) -> (n: int, err: _OS_Errno) {
_os_write :: proc "contextless" (data: []byte) -> (n: int, err: _OS_Errno) #no_bounds_check {
if len(data) == 0 {
return 0, 0;
}
@@ -58,11 +58,6 @@ os_write :: proc "contextless" (data: []byte) -> (n: int, err: _OS_Errno) {
return;
}
current_thread_id :: proc "contextless" () -> int {
return int(GetCurrentThreadId());
}
heap_alloc :: proc "contextless" (size: int) -> rawptr {
HEAP_ZERO_MEMORY :: 0x00000008;
return HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, uint(size));
@@ -86,89 +81,58 @@ heap_free :: proc "contextless" (ptr: rawptr) {
HeapFree(GetProcessHeap(), 0, ptr);
}
default_allocator_proc :: proc(allocator_data: rawptr, mode: Allocator_Mode,
size, alignment: int,
old_memory: rawptr, old_size: int, loc := #caller_location) -> ([]byte, Allocator_Error) {
//
// NOTE(tetra, 2020-01-14): The heap doesn't respect alignment.
// Instead, we overallocate by `alignment + size_of(rawptr) - 1`, and insert
// padding. We also store the original pointer returned by heap_alloc right before
// the pointer we return to the user.
//
//
// NOTE(tetra, 2020-01-14): The heap doesn't respect alignment.
// Instead, we overallocate by `alignment + size_of(rawptr) - 1`, and insert
// padding. We also store the original pointer returned by heap_alloc right before
// the pointer we return to the user.
//
aligned_alloc :: proc "contextless" (size, alignment: int, old_ptr: rawptr = nil) -> ([]byte, Allocator_Error) {
a := max(alignment, align_of(rawptr));
space := size + a - 1;
allocated_mem: rawptr;
if old_ptr != nil {
original_old_ptr := ptr_offset((^rawptr)(old_ptr), -1)^;
allocated_mem = heap_resize(original_old_ptr, space+size_of(rawptr));
} else {
allocated_mem = heap_alloc(space+size_of(rawptr));
}
aligned_mem := rawptr(ptr_offset((^u8)(allocated_mem), size_of(rawptr)));
ptr := uintptr(aligned_mem);
aligned_ptr := (ptr - 1 + uintptr(a)) & -uintptr(a);
diff := int(aligned_ptr - ptr);
if (size + diff) > space {
return nil, .Out_Of_Memory;
}
aligned_mem = rawptr(aligned_ptr);
ptr_offset((^rawptr)(aligned_mem), -1)^ = allocated_mem;
return byte_slice(aligned_mem, size), nil;
}
aligned_free :: proc "contextless" (p: rawptr) {
if p != nil {
heap_free(ptr_offset((^rawptr)(p), -1)^);
}
}
aligned_resize :: proc "contextless" (p: rawptr, old_size: int, new_size: int, new_alignment: int) -> ([]byte, Allocator_Error) {
if p == nil {
return nil, nil;
}
return aligned_alloc(new_size, new_alignment, p);
}
switch mode {
case .Alloc:
return aligned_alloc(size, alignment);
case .Free:
aligned_free(old_memory);
case .Free_All:
// NOTE(tetra): Do nothing.
case .Resize:
if old_memory == nil {
return aligned_alloc(size, alignment);
}
return aligned_resize(old_memory, old_size, size, alignment);
case .Query_Features:
set := (^Allocator_Mode_Set)(old_memory);
if set != nil {
set^ = {.Alloc, .Free, .Resize, .Query_Features};
}
return nil, nil;
case .Query_Info:
_windows_default_alloc_or_resize :: proc "contextless" (size, alignment: int, old_ptr: rawptr = nil) -> ([]byte, Allocator_Error) {
if size == 0 {
_windows_default_free(old_ptr);
return nil, nil;
}
return nil, nil;
a := max(alignment, align_of(rawptr));
space := size + a - 1;
allocated_mem: rawptr;
if old_ptr != nil {
original_old_ptr := intrinsics.ptr_offset((^rawptr)(old_ptr), -1)^;
allocated_mem = heap_resize(original_old_ptr, space+size_of(rawptr));
} else {
allocated_mem = heap_alloc(space+size_of(rawptr));
}
aligned_mem := rawptr(intrinsics.ptr_offset((^u8)(allocated_mem), size_of(rawptr)));
ptr := uintptr(aligned_mem);
aligned_ptr := (ptr - 1 + uintptr(a)) & -uintptr(a);
diff := int(aligned_ptr - ptr);
if (size + diff) > space {
return nil, .Out_Of_Memory;
}
aligned_mem = rawptr(aligned_ptr);
intrinsics.ptr_offset((^rawptr)(aligned_mem), -1)^ = allocated_mem;
return byte_slice(aligned_mem, size), nil;
}
default_allocator :: proc() -> Allocator {
return Allocator{
procedure = default_allocator_proc,
data = nil,
};
_windows_default_alloc :: proc "contextless" (size, alignment: int) -> ([]byte, Allocator_Error) {
return _windows_default_alloc_or_resize(size, alignment, nil);
}
_windows_default_free :: proc "contextless" (ptr: rawptr) {
if ptr != nil {
heap_free(intrinsics.ptr_offset((^rawptr)(ptr), -1)^);
}
}
_windows_default_resize :: proc "contextless" (p: rawptr, old_size: int, new_size: int, new_alignment: int) -> ([]byte, Allocator_Error) {
return _windows_default_alloc_or_resize(new_size, new_alignment, p);
}
+3 -3
View File
@@ -86,7 +86,7 @@ print_encoded_rune :: proc "contextless" (r: rune) {
print_byte('\'');
}
print_rune :: proc "contextless" (r: rune) -> (int, _OS_Errno) {
print_rune :: proc "contextless" (r: rune) -> (int, _OS_Errno) #no_bounds_check {
RUNE_SELF :: 0x80;
if r < RUNE_SELF {
@@ -98,7 +98,7 @@ print_rune :: proc "contextless" (r: rune) -> (int, _OS_Errno) {
}
print_u64 :: proc "contextless" (x: u64) {
print_u64 :: proc "contextless" (x: u64) #no_bounds_check {
digits := _INTEGER_DIGITS;
a: [129]byte;
@@ -115,7 +115,7 @@ print_u64 :: proc "contextless" (x: u64) {
}
print_i64 :: proc "contextless" (x: i64) {
print_i64 :: proc "contextless" (x: i64) #no_bounds_check {
digits := _INTEGER_DIGITS;
b :: i64(10);
+9 -5
View File
@@ -1,18 +1,22 @@
package runtime
import "core:sys/es"
@(link_name="memset")
memset :: proc "c" (ptr: rawptr, val: i32, len: int) -> rawptr {
return es.CRTmemset(ptr, val, len);
addr := 0x1000 + 196 * size_of(int);
fp := (rawptr(((^uintptr)(uintptr(addr)))^));
return ((proc "c" (rawptr, i32, int) -> rawptr)(fp))(ptr, val, len);
}
@(link_name="memmove")
memmove :: proc "c" (dst, src: rawptr, len: int) -> rawptr {
return es.CRTmemmove(dst, src, len);
addr := 0x1000 + 195 * size_of(int);
fp := (rawptr(((^uintptr)(uintptr(addr)))^));
return ((proc "c" (rawptr, rawptr, int) -> rawptr)(fp))(dst, src, len);
}
@(link_name="memcpy")
memcpy :: proc "c" (dst, src: rawptr, len: int) -> rawptr {
return es.CRTmemcpy(dst, src, len);
addr := 0x1000 + 194 * size_of(int);
fp := (rawptr(((^uintptr)(uintptr(addr)))^));
return ((proc "c" (rawptr, rawptr, int) -> rawptr)(fp))(dst, src, len);
}
+1 -1
View File
@@ -11,7 +11,7 @@ udivmod128 :: proc "c" (a, b: u128, rem: ^u128) -> u128 {
q, r: [2]u64 = ---, ---;
sr: u32 = 0;
low :: ODIN_ENDIAN == "big" ? 1 : 0;
low :: 1 when ODIN_ENDIAN == "big" else 0;
high :: 1 - low;
U64_BITS :: 8*size_of(u64);
U128_BITS :: 8*size_of(u128);
+2 -2
View File
@@ -27,12 +27,12 @@ map_values :: proc(m: $M/map[$K]$V, allocator := context.allocator) -> (values:
return;
}
Map_Entry :: struct(Key, Value: typeid) {
Map_Entry :: struct($Key, $Value: typeid) {
key: Key,
value: Value,
}
Map_Entry_Info :: struct(Key, Value: typeid) {
Map_Entry_Info :: struct($Key, $Value: typeid) {
hash: uintptr,
key: Key,
value: Value,
+24
View File
@@ -1,10 +1,12 @@
package slice
import "intrinsics"
import "builtin"
import "core:math/bits"
import "core:mem"
_ :: intrinsics;
_ :: builtin;
_ :: bits;
_ :: mem;
@@ -292,6 +294,28 @@ filter :: proc(s: $S/[]$U, f: proc(U) -> bool, allocator := context.allocator) -
min :: proc(s: $S/[]$T) -> (res: T, ok: bool) where intrinsics.type_is_ordered(T) #optional_ok {
if len(s) != 0 {
res = s[0];
ok = true;
for v in s[1:] {
res = builtin.min(res, v);
}
}
return;
}
max :: proc(s: $S/[]$T) -> (res: T, ok: bool) where intrinsics.type_is_ordered(T) #optional_ok {
if len(s) != 0 {
res = s[0];
ok = true;
for v in s[1:] {
res = builtin.max(res, v);
}
}
return;
}
dot_product :: proc(a, b: $S/[]$T) -> T
where intrinsics.type_is_numeric(T) {
if len(a) != len(b) {
+229 -8
View File
@@ -5,6 +5,33 @@ _ :: intrinsics;
ORD :: intrinsics.type_is_ordered;
Ordering :: enum {
Less = -1,
Equal = 0,
Greater = +1,
}
cmp :: proc(a, b: $E) -> Ordering where ORD(E) {
switch {
case a < b:
return .Less;
case a > b:
return .Greater;
}
return .Equal;
}
cmp_proc :: proc($E: typeid) -> (proc(E, E) -> Ordering) where ORD(E) {
return proc(a, b: E) -> Ordering {
switch {
case a < b:
return .Less;
case a > b:
return .Greater;
}
return .Equal;
};
}
// sort sorts a slice
// This sort is not guaranteed to be stable
@@ -21,7 +48,15 @@ sort :: proc(data: $T/[]$E) where ORD(E) {
sort_by :: proc(data: $T/[]$E, less: proc(i, j: E) -> bool) {
when size_of(E) != 0 {
if n := len(data); n > 1 {
_quick_sort_proc(data, 0, n, _max_depth(n), less);
_quick_sort_less(data, 0, n, _max_depth(n), less);
}
}
}
sort_by_cmp :: proc(data: $T/[]$E, cmp: proc(i, j: E) -> Ordering) {
when size_of(E) != 0 {
if n := len(data); n > 1 {
_quick_sort_cmp(data, 0, n, _max_depth(n), cmp);
}
}
}
@@ -44,6 +79,16 @@ is_sorted_by :: proc(array: $T/[]$E, less: proc(i, j: E) -> bool) -> bool {
return true;
}
is_sorted_cmp :: proc(array: $T/[]$E, cmp: proc(i, j: E) -> Ordering) -> bool {
for i := len(array)-1; i > 0; i -= 1 {
if cmp(array[i], array[i-1]) == .Equal {
return false;
}
}
return true;
}
reverse_sort :: proc(data: $T/[]$E) where ORD(E) {
sort_by(data, proc(i, j: E) -> bool {
@@ -52,6 +97,23 @@ reverse_sort :: proc(data: $T/[]$E) where ORD(E) {
}
reverse_sort_by :: proc(data: $T/[]$E, less: proc(i, j: E) -> bool) where ORD(E) {
context._internal = rawptr(less);
sort_by(data, proc(i, j: E) -> bool {
k := (proc(i, j: E) -> bool)(context._internal);
return k(j, i);
});
}
reverse_sort_by_cmp :: proc(data: $T/[]$E, cmp: proc(i, j: E) -> Ordering) where ORD(E) {
context._internal = rawptr(cmp);
sort_by_cmp(data, proc(i, j: E) -> Ordering {
k := (proc(i, j: E) -> Ordering)(context._internal);
return k(j, i);
});
}
// TODO(bill): Should `sort_by_key` exist or is `sort_by` more than enough?
sort_by_key :: proc(data: $T/[]$E, key: proc(E) -> $K) where ORD(K) {
context._internal = rawptr(key);
@@ -250,7 +312,7 @@ _heap_sort :: proc(data: $T/[]$E, a, b: int) where ORD(E) {
@(private)
_quick_sort_proc :: proc(data: $T/[]$E, a, b, max_depth: int, less: proc(i, j: E) -> bool) {
_quick_sort_less :: proc(data: $T/[]$E, a, b, max_depth: int, less: proc(i, j: E) -> bool) {
median3 :: proc(data: T, m1, m0, m2: int, less: proc(i, j: E) -> bool) {
if less(data[m1], data[m0]) {
swap(data, m1, m0);
@@ -337,16 +399,16 @@ _quick_sort_proc :: proc(data: $T/[]$E, a, b, max_depth: int, less: proc(i, j: E
if b-a > 12 { // only use shell sort for lengths <= 12
if max_depth == 0 {
_heap_sort_proc(data, a, b, less);
_heap_sort_less(data, a, b, less);
return;
}
max_depth -= 1;
mlo, mhi := do_pivot(data, a, b, less);
if mlo-a < b-mhi {
_quick_sort_proc(data, a, mlo, max_depth, less);
_quick_sort_less(data, a, mlo, max_depth, less);
a = mhi;
} else {
_quick_sort_proc(data, mhi, b, max_depth, less);
_quick_sort_less(data, mhi, b, max_depth, less);
b = mlo;
}
}
@@ -357,12 +419,12 @@ _quick_sort_proc :: proc(data: $T/[]$E, a, b, max_depth: int, less: proc(i, j: E
swap(data, i, i-6);
}
}
_insertion_sort_proc(data, a, b, less);
_insertion_sort_less(data, a, b, less);
}
}
@(private)
_insertion_sort_proc :: proc(data: $T/[]$E, a, b: int, less: proc(i, j: E) -> bool) {
_insertion_sort_less :: proc(data: $T/[]$E, a, b: int, less: proc(i, j: E) -> bool) {
for i in a+1..<b {
for j := i; j > a && less(data[j], data[j-1]); j -= 1 {
swap(data, j, j-1);
@@ -371,7 +433,7 @@ _insertion_sort_proc :: proc(data: $T/[]$E, a, b: int, less: proc(i, j: E) -> bo
}
@(private)
_heap_sort_proc :: proc(data: $T/[]$E, a, b: int, less: proc(i, j: E) -> bool) {
_heap_sort_less :: proc(data: $T/[]$E, a, b: int, less: proc(i, j: E) -> bool) {
sift_down :: proc(data: T, lo, hi, first: int, less: proc(i, j: E) -> bool) {
root := lo;
for {
@@ -405,3 +467,162 @@ _heap_sort_proc :: proc(data: $T/[]$E, a, b: int, less: proc(i, j: E) -> bool) {
@(private)
_quick_sort_cmp :: proc(data: $T/[]$E, a, b, max_depth: int, cmp: proc(i, j: E) -> Ordering) {
median3 :: proc(data: T, m1, m0, m2: int, cmp: proc(i, j: E) -> Ordering) {
if cmp(data[m1], data[m0]) == .Less {
swap(data, m1, m0);
}
if cmp(data[m2], data[m1]) == .Less {
swap(data, m2, m1);
if cmp(data[m1], data[m0]) == .Less {
swap(data, m1, m0);
}
}
}
do_pivot :: proc(data: T, lo, hi: int, cmp: proc(i, j: E) -> Ordering) -> (midlo, midhi: int) {
m := int(uint(lo+hi)>>1);
if hi-lo > 40 {
s := (hi-lo)/8;
median3(data, lo, lo+s, lo+s*2, cmp);
median3(data, m, m-s, m+s, cmp);
median3(data, hi-1, hi-1-s, hi-1-s*2, cmp);
}
median3(data, lo, m, hi-1, cmp);
pivot := lo;
a, c := lo+1, hi-1;
for ; a < c && cmp(data[a], data[pivot]) == .Less; a += 1 {
}
b := a;
for {
for ; b < c && cmp(data[pivot], data[b]) >= .Equal; b += 1 { // data[b] <= pivot
}
for ; b < c && cmp(data[pivot], data[c-1]) == .Less; c -=1 { // data[c-1] > pivot
}
if b >= c {
break;
}
swap(data, b, c-1);
b += 1;
c -= 1;
}
protect := hi-c < 5;
if !protect && hi-c < (hi-lo)/4 {
dups := 0;
if cmp(data[pivot], data[hi-1]) != .Less {
swap(data, c, hi-1);
c += 1;
dups += 1;
}
if cmp(data[b-1], data[pivot]) != .Less {
b -= 1;
dups += 1;
}
if cmp(data[m], data[pivot]) != .Less {
swap(data, m, b-1);
b -= 1;
dups += 1;
}
protect = dups > 1;
}
if protect {
for {
for ; a < b && cmp(data[b-1], data[pivot]) >= .Equal; b -= 1 {
}
for ; a < b && cmp(data[a], data[pivot]) == .Less; a += 1 {
}
if a >= b {
break;
}
swap(data, a, b-1);
a += 1;
b -= 1;
}
}
swap(data, pivot, b-1);
return b-1, c;
}
a, b, max_depth := a, b, max_depth;
if b-a > 12 { // only use shell sort for lengths <= 12
if max_depth == 0 {
_heap_sort_cmp(data, a, b, cmp);
return;
}
max_depth -= 1;
mlo, mhi := do_pivot(data, a, b, cmp);
if mlo-a < b-mhi {
_quick_sort_cmp(data, a, mlo, max_depth, cmp);
a = mhi;
} else {
_quick_sort_cmp(data, mhi, b, max_depth, cmp);
b = mlo;
}
}
if b-a > 1 {
// Shell short with gap 6
for i in a+6..<b {
if cmp(data[i], data[i-6]) == .Less {
swap(data, i, i-6);
}
}
_insertion_sort_cmp(data, a, b, cmp);
}
}
@(private)
_insertion_sort_cmp :: proc(data: $T/[]$E, a, b: int, cmp: proc(i, j: E) -> Ordering) {
for i in a+1..<b {
for j := i; j > a && cmp(data[j], data[j-1]) == .Less; j -= 1 {
swap(data, j, j-1);
}
}
}
@(private)
_heap_sort_cmp :: proc(data: $T/[]$E, a, b: int, cmp: proc(i, j: E) -> Ordering) {
sift_down :: proc(data: T, lo, hi, first: int, cmp: proc(i, j: E) -> Ordering) {
root := lo;
for {
child := 2*root + 1;
if child >= hi {
break;
}
if child+1 < hi && cmp(data[first+child], data[first+child+1]) == .Less {
child += 1;
}
if cmp(data[first+root], data[first+child]) >= .Equal {
return;
}
swap(data, first+root, first+child);
root = child;
}
}
first, lo, hi := a, 0, b-a;
for i := (hi-1)/2; i >= 0; i -= 1 {
sift_down(data, i, hi, first, cmp);
}
for i := hi-1; i >= 0; i -= 1 {
swap(data, first, first+i);
sift_down(data, lo, i, first, cmp);
}
}
+9 -54
View File
@@ -1,6 +1,7 @@
package sort
import "core:mem"
import _slice "core:slice"
import "intrinsics"
_ :: intrinsics;
@@ -29,9 +30,11 @@ sort :: proc(it: Interface) {
}
@(deprecated="use slice.sort")
slice :: proc(array: $T/[]$E) where ORD(E) {
s := array;
sort(slice_interface(&s));
_slice.sort(array);
// s := array;
// sort(slice_interface(&s));
}
slice_interface :: proc(s: ^$T/[]$E) -> Interface where ORD(E) {
@@ -76,7 +79,10 @@ reverse_sort :: proc(it: Interface) {
sort(reverse_interface(&it));
}
@(deprecated="use slice.reverse")
reverse_slice :: proc(array: $T/[]$E) where ORD(E) {
_slice.reverse(array);
/*
s := array;
sort(Interface{
collection = rawptr(&s),
@@ -93,6 +99,7 @@ reverse_slice :: proc(array: $T/[]$E) where ORD(E) {
s[i], s[j] = s[j], s[i];
},
});
*/
}
@@ -678,55 +685,3 @@ compare_strings :: proc(a, b: string) -> int {
y := transmute(mem.Raw_String)b;
return mem.compare_byte_ptrs(x.data, y.data, min(x.len, y.len));
}
@(deprecated="use slice.binary_search")
binary_search :: proc(array: $A/[]$T, key: T) -> (index: int, found: bool)
where intrinsics.type_is_ordered(T) #no_bounds_check {
n := len(array);
switch n {
case 0:
return -1, false;
case 1:
if array[0] == key {
return 0, true;
}
return -1, false;
}
lo, hi := 0, n-1;
for array[hi] != array[lo] && key >= array[lo] && key <= array[hi] {
when intrinsics.type_is_ordered_numeric(T) {
// NOTE(bill): This is technically interpolation search
m := lo + int((key - array[lo]) * T(hi - lo) / (array[hi] - array[lo]));
} else {
m := (lo + hi)/2;
}
switch {
case array[m] < key:
lo = m + 1;
case key < array[m]:
hi = m - 1;
case:
return m, true;
}
}
if key == array[lo] {
return lo, true;
}
return -1, false;
}
@(deprecated="use slice.linear_search")
linear_search :: proc(array: $A/[]$T, key: T) -> (index: int, found: bool)
where intrinsics.type_is_comparable(T) #no_bounds_check {
for x, i in array {
if x == key {
return i, true;
}
}
return -1, false;
}
+7 -7
View File
@@ -16,9 +16,9 @@ _digit_value :: proc(r: rune) -> int {
ri := int(r);
v: int = 16;
switch r {
case '0'..'9': v = ri-'0';
case 'a'..'z': v = ri-'a'+10;
case 'A'..'Z': v = ri-'A'+10;
case '0'..='9': v = ri-'0';
case 'a'..='z': v = ri-'a'+10;
case 'A'..='Z': v = ri-'A'+10;
}
return v;
}
@@ -557,9 +557,9 @@ quote_rune :: proc(buf: []byte, r: rune) -> string {
unquote_char :: proc(str: string, quote: byte) -> (r: rune, multiple_bytes: bool, tail_string: string, success: bool) {
hex_to_int :: proc(c: byte) -> int {
switch c {
case '0'..'9': return int(c-'0');
case 'a'..'f': return int(c-'a')+10;
case 'A'..'F': return int(c-'A')+10;
case '0'..='9': return int(c-'0');
case 'a'..='f': return int(c-'a')+10;
case 'A'..='F': return int(c-'A')+10;
}
return -1;
}
@@ -597,7 +597,7 @@ unquote_char :: proc(str: string, quote: byte) -> (r: rune, multiple_bytes: bool
case '"': r = '"';
case '\'': r = '\'';
case '0'..'7':
case '0'..='7':
v := int(c-'0');
if len(s) < 2 {
return;
+3 -3
View File
@@ -221,7 +221,7 @@ pop_rune :: proc(b: ^Builder) -> (r: rune, width: int) {
}
@(private, static)
@(private)
DIGITS_LOWER := "0123456789abcdefx";
write_quoted_string :: proc{
@@ -314,9 +314,9 @@ write_escaped_rune_writer :: proc(w: io.Writer, r: rune, quote: byte, html_safe
is_printable :: proc(r: rune) -> bool {
if r <= 0xff {
switch r {
case 0x20..0x7e:
case 0x20..=0x7e:
return true;
case 0xa1..0xff: // ¡ through ÿ except for the soft hyphen
case 0xa1..=0xff: // ¡ through ÿ except for the soft hyphen
return r != 0xad; //
}
}
+3 -3
View File
@@ -85,9 +85,9 @@ is_delimiter :: proc(c: rune) -> bool {
is_separator :: proc(r: rune) -> bool {
if r <= 0x7f {
switch r {
case '0'..'9': return false;
case 'a'..'z': return false;
case 'A'..'Z': return false;
case '0'..='9': return false;
case 'a'..='z': return false;
case 'A'..='Z': return false;
case '_': return false;
}
return true;
+22
View File
@@ -21,6 +21,28 @@ reader_to_stream :: proc(r: ^Reader) -> (s: io.Stream) {
return;
}
to_reader :: proc(r: ^Reader, s: string) -> io.Reader {
reader_init(r, s);
rr, _ := io.to_reader(reader_to_stream(r));
return rr;
}
to_reader_at :: proc(r: ^Reader, s: string) -> io.Reader_At {
reader_init(r, s);
rr, _ := io.to_reader_at(reader_to_stream(r));
return rr;
}
to_byte_reader :: proc(r: ^Reader, s: string) -> io.Byte_Reader {
reader_init(r, s);
rr, _ := io.to_byte_reader(reader_to_stream(r));
return rr;
}
to_rune_reader :: proc(r: ^Reader, s: string) -> io.Rune_Reader {
reader_init(r, s);
rr, _ := io.to_rune_reader(reader_to_stream(r));
return rr;
}
reader_length :: proc(r: ^Reader) -> int {
if r.i >= i64(len(r.s)) {
return 0;
+43 -1
View File
@@ -104,7 +104,7 @@ equal_fold :: proc(u, v: string) -> bool {
if tr < utf8.RUNE_SELF {
switch sr {
case 'A'..'Z':
case 'A'..='Z':
if tr == (sr+'a')-'A' {
continue loop;
}
@@ -165,6 +165,40 @@ concatenate :: proc(a: []string, allocator := context.allocator) -> string {
return string(b);
}
/*
`rune_offset` and `rune_length` are in runes, not bytes.
If `rune_length` <= 0, then it'll return the remainder of the string starting with `rune_offset`.
*/
cut :: proc(s: string, rune_offset := int(0), rune_length := int(0), allocator := context.allocator) -> (res: string) {
s := s; rune_length := rune_length;
l := utf8.rune_count_in_string(s);
if rune_offset >= l { return ""; }
if rune_offset == 0 && rune_length <= 0 {
return clone(s, allocator);
}
if rune_length == 0 { rune_length = l; }
bytes_needed := min(rune_length * 4, len(s));
buf := make([]u8, bytes_needed, allocator);
byte_offset := 0;
for i := 0; i < l; i += 1 {
_, w := utf8.decode_rune_in_string(s);
if i >= rune_offset {
for j := 0; j < w; j += 1 {
buf[byte_offset+j] = s[j];
}
byte_offset += w;
}
if rune_length > 0 {
if i == rune_offset + rune_length - 1 { break; }
}
s = s[w:];
}
return string(buf[:byte_offset]);
}
@private
_split :: proc(s_, sep: string, sep_save, n_: int, allocator := context.allocator) -> []string {
s, n := s_, n_;
@@ -541,6 +575,14 @@ replace :: proc(s, old, new: string, n: int, allocator := context.allocator) ->
return;
}
remove :: proc(s, key: string, n: int, allocator := context.allocator) -> (output: string, was_allocation: bool) {
return replace(s, key, "", n, allocator);
}
remove_all :: proc(s, key: string, allocator := context.allocator) -> (output: string, was_allocation: bool) {
return remove(s, key, -1, allocator);
}
@(private) _ascii_space := [256]u8{'\t' = 1, '\n' = 1, '\v' = 1, '\f' = 1, '\r' = 1, ' ' = 1};
+1 -1
View File
@@ -13,7 +13,7 @@ Channel_Direction :: enum i8 {
Recv = -1,
}
Channel :: struct(T: typeid, Direction := Channel_Direction.Both) {
Channel :: struct($T: typeid, $Direction := Channel_Direction.Both) {
using _internal: ^Raw_Channel,
}

Some files were not shown because too many files have changed in this diff Show More