mirror of
https://github.com/Ed94/Odin.git
synced 2026-08-06 15:48:51 +00:00
Merge remote-tracking branch 'offical/master'
# Conflicts: # vendor/raylib/raylib.odin
This commit is contained in:
@@ -63,11 +63,13 @@ jobs:
|
|||||||
cp -r core dist
|
cp -r core dist
|
||||||
cp -r vendor dist
|
cp -r vendor dist
|
||||||
cp -r examples dist
|
cp -r examples dist
|
||||||
|
# Zipping so executable permissions are retained, see https://github.com/actions/upload-artifact/issues/38
|
||||||
|
zip -r dist.zip dist
|
||||||
- name: Upload artifact
|
- name: Upload artifact
|
||||||
uses: actions/upload-artifact@v1
|
uses: actions/upload-artifact@v1
|
||||||
with:
|
with:
|
||||||
name: ubuntu_artifacts
|
name: ubuntu_artifacts
|
||||||
path: dist
|
path: dist.zip
|
||||||
build_macos:
|
build_macos:
|
||||||
name: MacOS Build
|
name: MacOS Build
|
||||||
if: github.repository == 'odin-lang/Odin'
|
if: github.repository == 'odin-lang/Odin'
|
||||||
@@ -76,15 +78,15 @@ jobs:
|
|||||||
- uses: actions/checkout@v1
|
- uses: actions/checkout@v1
|
||||||
- name: Download LLVM and setup PATH
|
- name: Download LLVM and setup PATH
|
||||||
run: |
|
run: |
|
||||||
brew install llvm@13
|
brew install llvm@13 dylibbundler
|
||||||
echo "/usr/local/opt/llvm@13/bin" >> $GITHUB_PATH
|
echo "/usr/local/opt/llvm@13/bin" >> $GITHUB_PATH
|
||||||
TMP_PATH=$(xcrun --show-sdk-path)/user/include
|
TMP_PATH=$(xcrun --show-sdk-path)/user/include
|
||||||
echo "CPATH=$TMP_PATH" >> $GITHUB_ENV
|
echo "CPATH=$TMP_PATH" >> $GITHUB_ENV
|
||||||
- name: build odin
|
- name: build odin
|
||||||
run: make nightly
|
# These -L makes the linker prioritize system libraries over LLVM libraries, this is mainly to
|
||||||
- name: Odin run
|
# not link with libunwind bundled with LLVM but link with libunwind on the system.
|
||||||
run: ./odin run examples/demo
|
run: CXXFLAGS="-L/usr/lib/system -L/usr/lib" make nightly
|
||||||
- name: Copy artifacts
|
- name: Bundle
|
||||||
run: |
|
run: |
|
||||||
mkdir dist
|
mkdir dist
|
||||||
cp odin dist
|
cp odin dist
|
||||||
@@ -94,28 +96,33 @@ jobs:
|
|||||||
cp -r core dist
|
cp -r core dist
|
||||||
cp -r vendor dist
|
cp -r vendor dist
|
||||||
cp -r examples dist
|
cp -r examples dist
|
||||||
|
dylibbundler -b -x dist/odin -d dist/libs -od -p @executable_path/libs
|
||||||
|
# Zipping so executable permissions are retained, see https://github.com/actions/upload-artifact/issues/38
|
||||||
|
zip -r dist.zip dist
|
||||||
|
- name: Odin run
|
||||||
|
run: ./dist/odin run examples/demo
|
||||||
- name: Upload artifact
|
- name: Upload artifact
|
||||||
uses: actions/upload-artifact@v1
|
uses: actions/upload-artifact@v1
|
||||||
with:
|
with:
|
||||||
name: macos_artifacts
|
name: macos_artifacts
|
||||||
path: dist
|
path: dist.zip
|
||||||
build_macos_arm:
|
build_macos_arm:
|
||||||
name: MacOS ARM Build
|
name: MacOS ARM Build
|
||||||
if: github.repository == 'odin-lang/Odin'
|
if: github.repository == 'odin-lang/Odin'
|
||||||
runs-on: macos-14
|
runs-on: macos-14 # ARM machine
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v1
|
- uses: actions/checkout@v1
|
||||||
- name: Download LLVM and setup PATH
|
- name: Download LLVM and setup PATH
|
||||||
run: |
|
run: |
|
||||||
brew install llvm@13
|
brew install llvm@13 dylibbundler
|
||||||
echo "/opt/homebrew/opt/llvm@13/bin" >> $GITHUB_PATH
|
echo "/opt/homebrew/opt/llvm@13/bin" >> $GITHUB_PATH
|
||||||
TMP_PATH=$(xcrun --show-sdk-path)/user/include
|
TMP_PATH=$(xcrun --show-sdk-path)/user/include
|
||||||
echo "CPATH=$TMP_PATH" >> $GITHUB_ENV
|
echo "CPATH=$TMP_PATH" >> $GITHUB_ENV
|
||||||
- name: build odin
|
- name: build odin
|
||||||
run: make nightly
|
# These -L makes the linker prioritize system libraries over LLVM libraries, this is mainly to
|
||||||
- name: Odin run
|
# not link with libunwind bundled with LLVM but link with libunwind on the system.
|
||||||
run: ./odin run examples/demo
|
run: CXXFLAGS="-L/usr/lib/system -L/usr/lib" make nightly
|
||||||
- name: Copy artifacts
|
- name: Bundle
|
||||||
run: |
|
run: |
|
||||||
mkdir dist
|
mkdir dist
|
||||||
cp odin dist
|
cp odin dist
|
||||||
@@ -125,11 +132,16 @@ jobs:
|
|||||||
cp -r core dist
|
cp -r core dist
|
||||||
cp -r vendor dist
|
cp -r vendor dist
|
||||||
cp -r examples dist
|
cp -r examples dist
|
||||||
|
dylibbundler -b -x dist/odin -d dist/libs -od -p @executable_path/libs
|
||||||
|
# Zipping so executable permissions are retained, see https://github.com/actions/upload-artifact/issues/38
|
||||||
|
zip -r dist.zip dist
|
||||||
|
- name: Odin run
|
||||||
|
run: ./dist/odin run examples/demo
|
||||||
- name: Upload artifact
|
- name: Upload artifact
|
||||||
uses: actions/upload-artifact@v1
|
uses: actions/upload-artifact@v1
|
||||||
with:
|
with:
|
||||||
name: macos_arm_artifacts
|
name: macos_arm_artifacts
|
||||||
path: dist
|
path: dist.zip
|
||||||
upload_b2:
|
upload_b2:
|
||||||
runs-on: [ubuntu-latest]
|
runs-on: [ubuntu-latest]
|
||||||
needs: [build_windows, build_macos, build_macos_arm, build_ubuntu]
|
needs: [build_windows, build_macos, build_macos_arm, build_ubuntu]
|
||||||
@@ -182,9 +194,9 @@ jobs:
|
|||||||
echo Uploading artifcates to B2
|
echo Uploading artifcates to B2
|
||||||
chmod +x ./ci/upload_create_nightly.sh
|
chmod +x ./ci/upload_create_nightly.sh
|
||||||
./ci/upload_create_nightly.sh "$BUCKET" windows-amd64 windows_artifacts/
|
./ci/upload_create_nightly.sh "$BUCKET" windows-amd64 windows_artifacts/
|
||||||
./ci/upload_create_nightly.sh "$BUCKET" ubuntu-amd64 ubuntu_artifacts/
|
./ci/upload_create_nightly.sh "$BUCKET" ubuntu-amd64 ubuntu_artifacts/dist.zip
|
||||||
./ci/upload_create_nightly.sh "$BUCKET" macos-amd64 macos_artifacts/
|
./ci/upload_create_nightly.sh "$BUCKET" macos-amd64 macos_artifacts/dist.zip
|
||||||
./ci/upload_create_nightly.sh "$BUCKET" macos-arm64 macos_arm_artifacts/
|
./ci/upload_create_nightly.sh "$BUCKET" macos-arm64 macos_arm_artifacts/dist.zip
|
||||||
|
|
||||||
echo Deleting old artifacts in B2
|
echo Deleting old artifacts in B2
|
||||||
python3 ci/delete_old_binaries.py "$BUCKET" "$DAYS_TO_KEEP"
|
python3 ci/delete_old_binaries.py "$BUCKET" "$DAYS_TO_KEEP"
|
||||||
|
|||||||
@@ -28,6 +28,7 @@ tests/internal/test_map
|
|||||||
tests/internal/test_pow
|
tests/internal/test_pow
|
||||||
tests/internal/test_rtti
|
tests/internal/test_rtti
|
||||||
tests/core/test_core_compress
|
tests/core/test_core_compress
|
||||||
|
tests/core/test_core_container
|
||||||
tests/core/test_core_filepath
|
tests/core/test_core_filepath
|
||||||
tests/core/test_core_fmt
|
tests/core/test_core_fmt
|
||||||
tests/core/test_core_i18n
|
tests/core/test_core_i18n
|
||||||
|
|||||||
@@ -280,7 +280,7 @@ Typeid_Kind :: enum u8 {
|
|||||||
|
|
||||||
// NOTE(bill): only the ones that are needed (not all types)
|
// NOTE(bill): only the ones that are needed (not all types)
|
||||||
// This will be set by the compiler
|
// This will be set by the compiler
|
||||||
type_table: []Type_Info
|
type_table: []^Type_Info
|
||||||
|
|
||||||
args__: []cstring
|
args__: []cstring
|
||||||
|
|
||||||
@@ -609,7 +609,7 @@ __type_info_of :: proc "contextless" (id: typeid) -> ^Type_Info #no_bounds_check
|
|||||||
if n < 0 || n >= len(type_table) {
|
if n < 0 || n >= len(type_table) {
|
||||||
n = 0
|
n = 0
|
||||||
}
|
}
|
||||||
return &type_table[n]
|
return type_table[n]
|
||||||
}
|
}
|
||||||
|
|
||||||
when !ODIN_NO_RTTI {
|
when !ODIN_NO_RTTI {
|
||||||
|
|||||||
+1
-2
@@ -63,8 +63,7 @@ Darwin)
|
|||||||
fi
|
fi
|
||||||
|
|
||||||
CXXFLAGS="$CXXFLAGS $($LLVM_CONFIG --cxxflags --ldflags)"
|
CXXFLAGS="$CXXFLAGS $($LLVM_CONFIG --cxxflags --ldflags)"
|
||||||
LDFLAGS="$LDFLAGS -liconv -ldl -framework System"
|
LDFLAGS="$LDFLAGS -liconv -ldl -framework System -lLLVM"
|
||||||
LDFLAGS="$LDFLAGS -lLLVM-C"
|
|
||||||
;;
|
;;
|
||||||
FreeBSD)
|
FreeBSD)
|
||||||
CXXFLAGS="$CXXFLAGS $($LLVM_CONFIG --cxxflags --ldflags)"
|
CXXFLAGS="$CXXFLAGS $($LLVM_CONFIG --cxxflags --ldflags)"
|
||||||
|
|||||||
Regular → Executable
+13
-1
@@ -1,5 +1,7 @@
|
|||||||
#!/bin/bash
|
#!/bin/bash
|
||||||
|
|
||||||
|
set -e
|
||||||
|
|
||||||
bucket=$1
|
bucket=$1
|
||||||
platform=$2
|
platform=$2
|
||||||
artifact=$3
|
artifact=$3
|
||||||
@@ -9,5 +11,15 @@ filename="odin-$platform-nightly+$now.zip"
|
|||||||
|
|
||||||
echo "Creating archive $filename from $artifact and uploading to $bucket"
|
echo "Creating archive $filename from $artifact and uploading to $bucket"
|
||||||
|
|
||||||
7z a -bd "output/$filename" -r "$artifact"
|
# If this is already zipped up (done before artifact upload to keep permissions in tact), just move it.
|
||||||
|
if [ "${artifact: -4}" == ".zip" ]
|
||||||
|
then
|
||||||
|
echo "Artifact already a zip"
|
||||||
|
mkdir -p "output"
|
||||||
|
mv "$artifact" "output/$filename"
|
||||||
|
else
|
||||||
|
echo "Artifact needs to be zipped"
|
||||||
|
7z a -bd "output/$filename" -r "$artifact"
|
||||||
|
fi
|
||||||
|
|
||||||
b2 upload-file --noProgress "$bucket" "output/$filename" "nightly/$filename"
|
b2 upload-file --noProgress "$bucket" "output/$filename" "nightly/$filename"
|
||||||
@@ -0,0 +1,678 @@
|
|||||||
|
/*
|
||||||
|
package avl implements an AVL tree.
|
||||||
|
|
||||||
|
The implementation is non-intrusive, and non-recursive.
|
||||||
|
*/
|
||||||
|
package container_avl
|
||||||
|
|
||||||
|
import "base:intrinsics"
|
||||||
|
import "base:runtime"
|
||||||
|
import "core:slice"
|
||||||
|
|
||||||
|
_ :: intrinsics
|
||||||
|
_ :: runtime
|
||||||
|
|
||||||
|
// Originally based on the CC0 implementation by Eric Biggers
|
||||||
|
// See: https://github.com/ebiggers/avl_tree/
|
||||||
|
|
||||||
|
// Direction specifies the traversal direction for a tree iterator.
|
||||||
|
Direction :: enum i8 {
|
||||||
|
// Backward is the in-order backwards direction.
|
||||||
|
Backward = -1,
|
||||||
|
// Forward is the in-order forwards direction.
|
||||||
|
Forward = 1,
|
||||||
|
}
|
||||||
|
|
||||||
|
// Ordering specifies order when inserting/finding values into the tree.
|
||||||
|
Ordering :: slice.Ordering
|
||||||
|
|
||||||
|
// Tree is an AVL tree.
|
||||||
|
Tree :: struct($Value: typeid) {
|
||||||
|
// user_data is a parameter that will be passed to the on_remove
|
||||||
|
// callback.
|
||||||
|
user_data: rawptr,
|
||||||
|
// on_remove is an optional callback that can be called immediately
|
||||||
|
// after a node is removed from the tree.
|
||||||
|
on_remove: proc(value: Value, user_data: rawptr),
|
||||||
|
|
||||||
|
_root: ^Node(Value),
|
||||||
|
_node_allocator: runtime.Allocator,
|
||||||
|
_cmp_fn: proc(a, b: Value) -> Ordering,
|
||||||
|
_size: int,
|
||||||
|
}
|
||||||
|
|
||||||
|
// Node is an AVL tree node.
|
||||||
|
//
|
||||||
|
// WARNING: It is unsafe to mutate value if the node is part of a tree
|
||||||
|
// if doing so will alter the Node's sort position relative to other
|
||||||
|
// elements in the tree.
|
||||||
|
Node :: struct($Value: typeid) {
|
||||||
|
value: Value,
|
||||||
|
|
||||||
|
_parent: ^Node(Value),
|
||||||
|
_left: ^Node(Value),
|
||||||
|
_right: ^Node(Value),
|
||||||
|
_balance: i8,
|
||||||
|
}
|
||||||
|
|
||||||
|
// Iterator is a tree iterator.
|
||||||
|
//
|
||||||
|
// WARNING: It is unsafe to modify the tree while iterating, except via
|
||||||
|
// the iterator_remove method.
|
||||||
|
Iterator :: struct($Value: typeid) {
|
||||||
|
_tree: ^Tree(Value),
|
||||||
|
_cur: ^Node(Value),
|
||||||
|
_next: ^Node(Value),
|
||||||
|
_direction: Direction,
|
||||||
|
_called_next: bool,
|
||||||
|
}
|
||||||
|
|
||||||
|
// init initializes a tree.
|
||||||
|
init :: proc {
|
||||||
|
init_ordered,
|
||||||
|
init_cmp,
|
||||||
|
}
|
||||||
|
|
||||||
|
// init_cmp initializes a tree.
|
||||||
|
init_cmp :: proc(
|
||||||
|
t: ^$T/Tree($Value),
|
||||||
|
cmp_fn: proc(a, b: Value) -> Ordering,
|
||||||
|
node_allocator := context.allocator,
|
||||||
|
) {
|
||||||
|
t._root = nil
|
||||||
|
t._node_allocator = node_allocator
|
||||||
|
t._cmp_fn = cmp_fn
|
||||||
|
t._size = 0
|
||||||
|
}
|
||||||
|
|
||||||
|
// init_ordered initializes a tree containing ordered items, with
|
||||||
|
// a comparison function that results in an ascending order sort.
|
||||||
|
init_ordered :: proc(
|
||||||
|
t: ^$T/Tree($Value),
|
||||||
|
node_allocator := context.allocator,
|
||||||
|
) where intrinsics.type_is_ordered_numeric(Value) {
|
||||||
|
init_cmp(t, slice.cmp_proc(Value), node_allocator)
|
||||||
|
}
|
||||||
|
|
||||||
|
// destroy de-initializes a tree.
|
||||||
|
destroy :: proc(t: ^$T/Tree($Value), call_on_remove: bool = true) {
|
||||||
|
iter := iterator(t, Direction.Forward)
|
||||||
|
for _ in iterator_next(&iter) {
|
||||||
|
iterator_remove(&iter, call_on_remove)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// len returns the number of elements in the tree.
|
||||||
|
len :: proc "contextless" (t: ^$T/Tree($Value)) -> int {
|
||||||
|
return t._size
|
||||||
|
}
|
||||||
|
|
||||||
|
// first returns the first node in the tree (in-order) or nil iff
|
||||||
|
// the tree is empty.
|
||||||
|
first :: proc "contextless" (t: ^$T/Tree($Value)) -> ^Node(Value) {
|
||||||
|
return tree_first_or_last_in_order(t, Direction.Backward)
|
||||||
|
}
|
||||||
|
|
||||||
|
// last returns the last element in the tree (in-order) or nil iff
|
||||||
|
// the tree is empty.
|
||||||
|
last :: proc "contextless" (t: ^$T/Tree($Value)) -> ^Node(Value) {
|
||||||
|
return tree_first_or_last_in_order(t, Direction.Forward)
|
||||||
|
}
|
||||||
|
|
||||||
|
// find finds the value in the tree, and returns the corresponding
|
||||||
|
// node or nil iff the value is not present.
|
||||||
|
find :: proc(t: ^$T/Tree($Value), value: Value) -> ^Node(Value) {
|
||||||
|
cur := t._root
|
||||||
|
descend_loop: for cur != nil {
|
||||||
|
switch t._cmp_fn(value, cur.value) {
|
||||||
|
case .Less:
|
||||||
|
cur = cur._left
|
||||||
|
case .Greater:
|
||||||
|
cur = cur._right
|
||||||
|
case .Equal:
|
||||||
|
break descend_loop
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return cur
|
||||||
|
}
|
||||||
|
|
||||||
|
// find_or_insert attempts to insert the value into the tree, and returns
|
||||||
|
// the node, a boolean indicating if the value was inserted, and the
|
||||||
|
// node allocator error if relevant. If the value is already
|
||||||
|
// present, the existing node is returned un-altered.
|
||||||
|
find_or_insert :: proc(
|
||||||
|
t: ^$T/Tree($Value),
|
||||||
|
value: Value,
|
||||||
|
) -> (
|
||||||
|
n: ^Node(Value),
|
||||||
|
inserted: bool,
|
||||||
|
err: runtime.Allocator_Error,
|
||||||
|
) {
|
||||||
|
n_ptr := &t._root
|
||||||
|
for n_ptr^ != nil {
|
||||||
|
n = n_ptr^
|
||||||
|
switch t._cmp_fn(value, n.value) {
|
||||||
|
case .Less:
|
||||||
|
n_ptr = &n._left
|
||||||
|
case .Greater:
|
||||||
|
n_ptr = &n._right
|
||||||
|
case .Equal:
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
parent := n
|
||||||
|
n = new(Node(Value), t._node_allocator) or_return
|
||||||
|
n.value = value
|
||||||
|
n._parent = parent
|
||||||
|
n_ptr^ = n
|
||||||
|
tree_rebalance_after_insert(t, n)
|
||||||
|
|
||||||
|
t._size += 1
|
||||||
|
inserted = true
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// remove removes a node or value from the tree, and returns true iff the
|
||||||
|
// removal was successful. While the node's value will be left intact,
|
||||||
|
// the node itself will be freed via the tree's node allocator.
|
||||||
|
remove :: proc {
|
||||||
|
remove_value,
|
||||||
|
remove_node,
|
||||||
|
}
|
||||||
|
|
||||||
|
// remove_value removes a value from the tree, and returns true iff the
|
||||||
|
// removal was successful. While the node's value will be left intact,
|
||||||
|
// the node itself will be freed via the tree's node allocator.
|
||||||
|
remove_value :: proc(t: ^$T/Tree($Value), value: Value, call_on_remove: bool = true) -> bool {
|
||||||
|
n := find(t, value)
|
||||||
|
if n == nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
return remove_node(t, n, call_on_remove)
|
||||||
|
}
|
||||||
|
|
||||||
|
// remove_node removes a node from the tree, and returns true iff the
|
||||||
|
// removal was successful. While the node's value will be left intact,
|
||||||
|
// the node itself will be freed via the tree's node allocator.
|
||||||
|
remove_node :: proc(t: ^$T/Tree($Value), node: ^Node(Value), call_on_remove: bool = true) -> bool {
|
||||||
|
if node._parent == node || (node._parent == nil && t._root != node) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
defer {
|
||||||
|
if call_on_remove && t.on_remove != nil {
|
||||||
|
t.on_remove(node.value, t.user_data)
|
||||||
|
}
|
||||||
|
free(node, t._node_allocator)
|
||||||
|
}
|
||||||
|
|
||||||
|
parent: ^Node(Value)
|
||||||
|
left_deleted: bool
|
||||||
|
|
||||||
|
t._size -= 1
|
||||||
|
if node._left != nil && node._right != nil {
|
||||||
|
parent, left_deleted = tree_swap_with_successor(t, node)
|
||||||
|
} else {
|
||||||
|
child := node._left
|
||||||
|
if child == nil {
|
||||||
|
child = node._right
|
||||||
|
}
|
||||||
|
parent = node._parent
|
||||||
|
if parent != nil {
|
||||||
|
if node == parent._left {
|
||||||
|
parent._left = child
|
||||||
|
left_deleted = true
|
||||||
|
} else {
|
||||||
|
parent._right = child
|
||||||
|
left_deleted = false
|
||||||
|
}
|
||||||
|
if child != nil {
|
||||||
|
child._parent = parent
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if child != nil {
|
||||||
|
child._parent = parent
|
||||||
|
}
|
||||||
|
t._root = child
|
||||||
|
node_reset(node)
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for {
|
||||||
|
if left_deleted {
|
||||||
|
parent = tree_handle_subtree_shrink(t, parent, +1, &left_deleted)
|
||||||
|
} else {
|
||||||
|
parent = tree_handle_subtree_shrink(t, parent, -1, &left_deleted)
|
||||||
|
}
|
||||||
|
if parent == nil {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
node_reset(node)
|
||||||
|
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
// iterator returns a tree iterator in the specified direction.
|
||||||
|
iterator :: proc "contextless" (t: ^$T/Tree($Value), direction: Direction) -> Iterator(Value) {
|
||||||
|
it: Iterator(Value)
|
||||||
|
it._tree = transmute(^Tree(Value))t
|
||||||
|
it._direction = direction
|
||||||
|
|
||||||
|
iterator_first(&it)
|
||||||
|
|
||||||
|
return it
|
||||||
|
}
|
||||||
|
|
||||||
|
// iterator_from_pos returns a tree iterator in the specified direction,
|
||||||
|
// spanning the range [pos, last] (inclusive).
|
||||||
|
iterator_from_pos :: proc "contextless" (
|
||||||
|
t: ^$T/Tree($Value),
|
||||||
|
pos: ^Node(Value),
|
||||||
|
direction: Direction,
|
||||||
|
) -> Iterator(Value) {
|
||||||
|
it: Iterator(Value)
|
||||||
|
it._tree = transmute(^Tree(Value))t
|
||||||
|
it._direction = direction
|
||||||
|
it._next = nil
|
||||||
|
it._called_next = false
|
||||||
|
|
||||||
|
if it._cur = pos; pos != nil {
|
||||||
|
it._next = node_next_or_prev_in_order(it._cur, it._direction)
|
||||||
|
}
|
||||||
|
|
||||||
|
return it
|
||||||
|
}
|
||||||
|
|
||||||
|
// iterator_get returns the node currently pointed to by the iterator,
|
||||||
|
// or nil iff the node has been removed, the tree is empty, or the end
|
||||||
|
// of the tree has been reached.
|
||||||
|
iterator_get :: proc "contextless" (it: ^$I/Iterator($Value)) -> ^Node(Value) {
|
||||||
|
return it._cur
|
||||||
|
}
|
||||||
|
|
||||||
|
// iterator_remove removes the node currently pointed to by the iterator,
|
||||||
|
// and returns true iff the removal was successful. Semantics are the
|
||||||
|
// same as the Tree remove.
|
||||||
|
iterator_remove :: proc(it: ^$I/Iterator($Value), call_on_remove: bool = true) -> bool {
|
||||||
|
if it._cur == nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
ok := remove_node(it._tree, it._cur, call_on_remove)
|
||||||
|
if ok {
|
||||||
|
it._cur = nil
|
||||||
|
}
|
||||||
|
|
||||||
|
return ok
|
||||||
|
}
|
||||||
|
|
||||||
|
// iterator_next advances the iterator and returns the (node, true) or
|
||||||
|
// or (nil, false) iff the end of the tree has been reached.
|
||||||
|
//
|
||||||
|
// Note: The first call to iterator_next will return the first node instead
|
||||||
|
// of advancing the iterator.
|
||||||
|
iterator_next :: proc "contextless" (it: ^$I/Iterator($Value)) -> (^Node(Value), bool) {
|
||||||
|
// This check is needed so that the first element gets returned from
|
||||||
|
// a brand-new iterator, and so that the somewhat contrived case where
|
||||||
|
// iterator_remove is called before the first call to iterator_next
|
||||||
|
// returns the correct value.
|
||||||
|
if !it._called_next {
|
||||||
|
it._called_next = true
|
||||||
|
|
||||||
|
// There can be the contrived case where iterator_remove is
|
||||||
|
// called before ever calling iterator_next, which needs to be
|
||||||
|
// handled as an actual call to next.
|
||||||
|
//
|
||||||
|
// If this happens it._cur will be nil, so only return the
|
||||||
|
// first value, if it._cur is valid.
|
||||||
|
if it._cur != nil {
|
||||||
|
return it._cur, true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if it._next == nil {
|
||||||
|
return nil, false
|
||||||
|
}
|
||||||
|
|
||||||
|
it._cur = it._next
|
||||||
|
it._next = node_next_or_prev_in_order(it._cur, it._direction)
|
||||||
|
|
||||||
|
return it._cur, true
|
||||||
|
}
|
||||||
|
|
||||||
|
@(private)
|
||||||
|
tree_first_or_last_in_order :: proc "contextless" (
|
||||||
|
t: ^$T/Tree($Value),
|
||||||
|
direction: Direction,
|
||||||
|
) -> ^Node(Value) {
|
||||||
|
first, sign := t._root, i8(direction)
|
||||||
|
if first != nil {
|
||||||
|
for {
|
||||||
|
tmp := node_get_child(first, +sign)
|
||||||
|
if tmp == nil {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
first = tmp
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return first
|
||||||
|
}
|
||||||
|
|
||||||
|
@(private)
|
||||||
|
tree_replace_child :: proc "contextless" (
|
||||||
|
t: ^$T/Tree($Value),
|
||||||
|
parent, old_child, new_child: ^Node(Value),
|
||||||
|
) {
|
||||||
|
if parent != nil {
|
||||||
|
if old_child == parent._left {
|
||||||
|
parent._left = new_child
|
||||||
|
} else {
|
||||||
|
parent._right = new_child
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
t._root = new_child
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@(private)
|
||||||
|
tree_rotate :: proc "contextless" (t: ^$T/Tree($Value), a: ^Node(Value), sign: i8) {
|
||||||
|
b := node_get_child(a, -sign)
|
||||||
|
e := node_get_child(b, +sign)
|
||||||
|
p := a._parent
|
||||||
|
|
||||||
|
node_set_child(a, -sign, e)
|
||||||
|
a._parent = b
|
||||||
|
|
||||||
|
node_set_child(b, +sign, a)
|
||||||
|
b._parent = p
|
||||||
|
|
||||||
|
if e != nil {
|
||||||
|
e._parent = a
|
||||||
|
}
|
||||||
|
|
||||||
|
tree_replace_child(t, p, a, b)
|
||||||
|
}
|
||||||
|
|
||||||
|
@(private)
|
||||||
|
tree_double_rotate :: proc "contextless" (
|
||||||
|
t: ^$T/Tree($Value),
|
||||||
|
b, a: ^Node(Value),
|
||||||
|
sign: i8,
|
||||||
|
) -> ^Node(Value) {
|
||||||
|
e := node_get_child(b, +sign)
|
||||||
|
f := node_get_child(e, -sign)
|
||||||
|
g := node_get_child(e, +sign)
|
||||||
|
p := a._parent
|
||||||
|
e_bal := e._balance
|
||||||
|
|
||||||
|
node_set_child(a, -sign, g)
|
||||||
|
a_bal := -e_bal
|
||||||
|
if sign * e_bal >= 0 {
|
||||||
|
a_bal = 0
|
||||||
|
}
|
||||||
|
node_set_parent_balance(a, e, a_bal)
|
||||||
|
|
||||||
|
node_set_child(b, +sign, f)
|
||||||
|
b_bal := -e_bal
|
||||||
|
if sign * e_bal <= 0 {
|
||||||
|
b_bal = 0
|
||||||
|
}
|
||||||
|
node_set_parent_balance(b, e, b_bal)
|
||||||
|
|
||||||
|
node_set_child(e, +sign, a)
|
||||||
|
node_set_child(e, -sign, b)
|
||||||
|
node_set_parent_balance(e, p, 0)
|
||||||
|
|
||||||
|
if g != nil {
|
||||||
|
g._parent = a
|
||||||
|
}
|
||||||
|
|
||||||
|
if f != nil {
|
||||||
|
f._parent = b
|
||||||
|
}
|
||||||
|
|
||||||
|
tree_replace_child(t, p, a, e)
|
||||||
|
|
||||||
|
return e
|
||||||
|
}
|
||||||
|
|
||||||
|
@(private)
|
||||||
|
tree_handle_subtree_growth :: proc "contextless" (
|
||||||
|
t: ^$T/Tree($Value),
|
||||||
|
node, parent: ^Node(Value),
|
||||||
|
sign: i8,
|
||||||
|
) -> bool {
|
||||||
|
old_balance_factor := parent._balance
|
||||||
|
if old_balance_factor == 0 {
|
||||||
|
node_adjust_balance_factor(parent, sign)
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
new_balance_factor := old_balance_factor + sign
|
||||||
|
if new_balance_factor == 0 {
|
||||||
|
node_adjust_balance_factor(parent, sign)
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
if sign * node._balance > 0 {
|
||||||
|
tree_rotate(t, parent, -sign)
|
||||||
|
node_adjust_balance_factor(parent, -sign)
|
||||||
|
node_adjust_balance_factor(node, -sign)
|
||||||
|
} else {
|
||||||
|
tree_double_rotate(t, node, parent, -sign)
|
||||||
|
}
|
||||||
|
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
@(private)
|
||||||
|
tree_rebalance_after_insert :: proc "contextless" (t: ^$T/Tree($Value), inserted: ^Node(Value)) {
|
||||||
|
node, parent := inserted, inserted._parent
|
||||||
|
switch {
|
||||||
|
case parent == nil:
|
||||||
|
return
|
||||||
|
case node == parent._left:
|
||||||
|
node_adjust_balance_factor(parent, -1)
|
||||||
|
case:
|
||||||
|
node_adjust_balance_factor(parent, +1)
|
||||||
|
}
|
||||||
|
|
||||||
|
if parent._balance == 0 {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
for done := false; !done; {
|
||||||
|
node = parent
|
||||||
|
if parent = node._parent; parent == nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if node == parent._left {
|
||||||
|
done = tree_handle_subtree_growth(t, node, parent, -1)
|
||||||
|
} else {
|
||||||
|
done = tree_handle_subtree_growth(t, node, parent, +1)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@(private)
|
||||||
|
tree_swap_with_successor :: proc "contextless" (
|
||||||
|
t: ^$T/Tree($Value),
|
||||||
|
x: ^Node(Value),
|
||||||
|
) -> (
|
||||||
|
^Node(Value),
|
||||||
|
bool,
|
||||||
|
) {
|
||||||
|
ret: ^Node(Value)
|
||||||
|
left_deleted: bool
|
||||||
|
|
||||||
|
y := x._right
|
||||||
|
if y._left == nil {
|
||||||
|
ret = y
|
||||||
|
} else {
|
||||||
|
q: ^Node(Value)
|
||||||
|
|
||||||
|
for {
|
||||||
|
q = y
|
||||||
|
if y = y._left; y._left == nil {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if q._left = y._right; q._left != nil {
|
||||||
|
q._left._parent = q
|
||||||
|
}
|
||||||
|
y._right = x._right
|
||||||
|
x._right._parent = y
|
||||||
|
ret = q
|
||||||
|
left_deleted = true
|
||||||
|
}
|
||||||
|
|
||||||
|
y._left = x._left
|
||||||
|
x._left._parent = y
|
||||||
|
|
||||||
|
y._parent = x._parent
|
||||||
|
y._balance = x._balance
|
||||||
|
|
||||||
|
tree_replace_child(t, x._parent, x, y)
|
||||||
|
|
||||||
|
return ret, left_deleted
|
||||||
|
}
|
||||||
|
|
||||||
|
@(private)
|
||||||
|
tree_handle_subtree_shrink :: proc "contextless" (
|
||||||
|
t: ^$T/Tree($Value),
|
||||||
|
parent: ^Node(Value),
|
||||||
|
sign: i8,
|
||||||
|
left_deleted: ^bool,
|
||||||
|
) -> ^Node(Value) {
|
||||||
|
old_balance_factor := parent._balance
|
||||||
|
if old_balance_factor == 0 {
|
||||||
|
node_adjust_balance_factor(parent, sign)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
node: ^Node(Value)
|
||||||
|
new_balance_factor := old_balance_factor + sign
|
||||||
|
if new_balance_factor == 0 {
|
||||||
|
node_adjust_balance_factor(parent, sign)
|
||||||
|
node = parent
|
||||||
|
} else {
|
||||||
|
node = node_get_child(parent, sign)
|
||||||
|
if sign * node._balance >= 0 {
|
||||||
|
tree_rotate(t, parent, -sign)
|
||||||
|
if node._balance == 0 {
|
||||||
|
node_adjust_balance_factor(node, -sign)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
node_adjust_balance_factor(parent, -sign)
|
||||||
|
node_adjust_balance_factor(node, -sign)
|
||||||
|
} else {
|
||||||
|
node = tree_double_rotate(t, node, parent, -sign)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
parent := parent
|
||||||
|
if parent = node._parent; parent != nil {
|
||||||
|
left_deleted^ = node == parent._left
|
||||||
|
}
|
||||||
|
return parent
|
||||||
|
}
|
||||||
|
|
||||||
|
@(private)
|
||||||
|
node_reset :: proc "contextless" (n: ^Node($Value)) {
|
||||||
|
// Mostly pointless as n will be deleted after this is called, but
|
||||||
|
// attempt to be able to catch cases of n not being in the tree.
|
||||||
|
n._parent = n
|
||||||
|
n._left = nil
|
||||||
|
n._right = nil
|
||||||
|
n._balance = 0
|
||||||
|
}
|
||||||
|
|
||||||
|
@(private)
|
||||||
|
node_set_parent_balance :: #force_inline proc "contextless" (
|
||||||
|
n, parent: ^Node($Value),
|
||||||
|
balance: i8,
|
||||||
|
) {
|
||||||
|
n._parent = parent
|
||||||
|
n._balance = balance
|
||||||
|
}
|
||||||
|
|
||||||
|
@(private)
|
||||||
|
node_get_child :: #force_inline proc "contextless" (n: ^Node($Value), sign: i8) -> ^Node(Value) {
|
||||||
|
if sign < 0 {
|
||||||
|
return n._left
|
||||||
|
}
|
||||||
|
return n._right
|
||||||
|
}
|
||||||
|
|
||||||
|
@(private)
|
||||||
|
node_next_or_prev_in_order :: proc "contextless" (
|
||||||
|
n: ^Node($Value),
|
||||||
|
direction: Direction,
|
||||||
|
) -> ^Node(Value) {
|
||||||
|
next, tmp: ^Node(Value)
|
||||||
|
sign := i8(direction)
|
||||||
|
|
||||||
|
if next = node_get_child(n, +sign); next != nil {
|
||||||
|
for {
|
||||||
|
tmp = node_get_child(next, -sign)
|
||||||
|
if tmp == nil {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
next = tmp
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
tmp, next = n, n._parent
|
||||||
|
for next != nil && tmp == node_get_child(next, +sign) {
|
||||||
|
tmp, next = next, next._parent
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return next
|
||||||
|
}
|
||||||
|
|
||||||
|
@(private)
|
||||||
|
node_set_child :: #force_inline proc "contextless" (
|
||||||
|
n: ^Node($Value),
|
||||||
|
sign: i8,
|
||||||
|
child: ^Node(Value),
|
||||||
|
) {
|
||||||
|
if sign < 0 {
|
||||||
|
n._left = child
|
||||||
|
} else {
|
||||||
|
n._right = child
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@(private)
|
||||||
|
node_adjust_balance_factor :: #force_inline proc "contextless" (n: ^Node($Value), amount: i8) {
|
||||||
|
n._balance += amount
|
||||||
|
}
|
||||||
|
|
||||||
|
@(private)
|
||||||
|
iterator_first :: proc "contextless" (it: ^Iterator($Value)) {
|
||||||
|
// This is private because behavior when the user manually calls
|
||||||
|
// iterator_first followed by iterator_next is unintuitive, since
|
||||||
|
// the first call to iterator_next MUST return the first node
|
||||||
|
// instead of advancing so that `for node in iterator_next(&next)`
|
||||||
|
// works as expected.
|
||||||
|
|
||||||
|
switch it._direction {
|
||||||
|
case .Forward:
|
||||||
|
it._cur = tree_first_or_last_in_order(it._tree, .Backward)
|
||||||
|
case .Backward:
|
||||||
|
it._cur = tree_first_or_last_in_order(it._tree, .Forward)
|
||||||
|
}
|
||||||
|
|
||||||
|
it._next = nil
|
||||||
|
it._called_next = false
|
||||||
|
|
||||||
|
if it._cur != nil {
|
||||||
|
it._next = node_next_or_prev_in_order(it._cur, it._direction)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,12 @@
|
|||||||
|
//+build freebsd, openbsd
|
||||||
|
package crypto
|
||||||
|
|
||||||
|
foreign import libc "system:c"
|
||||||
|
|
||||||
|
foreign libc {
|
||||||
|
arc4random_buf :: proc(buf: [^]byte, nbytes: uint) ---
|
||||||
|
}
|
||||||
|
|
||||||
|
_rand_bytes :: proc(dst: []byte) {
|
||||||
|
arc4random_buf(raw_data(dst), len(dst))
|
||||||
|
}
|
||||||
@@ -0,0 +1,12 @@
|
|||||||
|
package crypto
|
||||||
|
|
||||||
|
import "core:fmt"
|
||||||
|
import "core:sys/darwin"
|
||||||
|
|
||||||
|
_rand_bytes :: proc(dst: []byte) {
|
||||||
|
res := darwin.SecRandomCopyBytes(count=len(dst), bytes=raw_data(dst))
|
||||||
|
if res != .Success {
|
||||||
|
msg := darwin.CFStringCopyToOdinString(darwin.SecCopyErrorMessageString(res))
|
||||||
|
panic(fmt.tprintf("crypto/rand_bytes: SecRandomCopyBytes returned non-zero result: %v %s", res, msg))
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,7 +1,11 @@
|
|||||||
|
//+build !linux
|
||||||
|
//+build !windows
|
||||||
|
//+build !openbsd
|
||||||
|
//+build !freebsd
|
||||||
|
//+build !darwin
|
||||||
|
//+build !js
|
||||||
package crypto
|
package crypto
|
||||||
|
|
||||||
when ODIN_OS != .Linux && ODIN_OS != .OpenBSD && ODIN_OS != .Windows && ODIN_OS != .JS {
|
_rand_bytes :: proc(dst: []byte) {
|
||||||
_rand_bytes :: proc(dst: []byte) {
|
|
||||||
unimplemented("crypto: rand_bytes not supported on this OS")
|
unimplemented("crypto: rand_bytes not supported on this OS")
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,12 +0,0 @@
|
|||||||
package crypto
|
|
||||||
|
|
||||||
import "core:c"
|
|
||||||
|
|
||||||
foreign import libc "system:c"
|
|
||||||
foreign libc {
|
|
||||||
arc4random_buf :: proc "c" (buf: rawptr, nbytes: c.size_t) ---
|
|
||||||
}
|
|
||||||
|
|
||||||
_rand_bytes :: proc (dst: []byte) {
|
|
||||||
arc4random_buf(raw_data(dst), len(dst))
|
|
||||||
}
|
|
||||||
+115
-18
@@ -149,15 +149,29 @@ aprintln :: proc(args: ..any, sep := " ", allocator := context.allocator) -> str
|
|||||||
// Inputs:
|
// Inputs:
|
||||||
// - fmt: A format string with placeholders for the provided arguments.
|
// - fmt: A format string with placeholders for the provided arguments.
|
||||||
// - args: A variadic list of arguments to be formatted.
|
// - args: A variadic list of arguments to be formatted.
|
||||||
|
// - newline: Whether the string should end with a newline. (See `aprintfln`.)
|
||||||
//
|
//
|
||||||
// Returns: A formatted string. The returned string must be freed accordingly.
|
// Returns: A formatted string. The returned string must be freed accordingly.
|
||||||
//
|
//
|
||||||
aprintf :: proc(fmt: string, args: ..any, allocator := context.allocator) -> string {
|
aprintf :: proc(fmt: string, args: ..any, allocator := context.allocator, newline := false) -> string {
|
||||||
str: strings.Builder
|
str: strings.Builder
|
||||||
strings.builder_init(&str, allocator)
|
strings.builder_init(&str, allocator)
|
||||||
sbprintf(&str, fmt, ..args)
|
sbprintf(&str, fmt, ..args, newline=newline)
|
||||||
return strings.to_string(str)
|
return strings.to_string(str)
|
||||||
}
|
}
|
||||||
|
// Creates a formatted string using a format string and arguments, followed by a newline.
|
||||||
|
//
|
||||||
|
// *Allocates Using Context's Allocator*
|
||||||
|
//
|
||||||
|
// Inputs:
|
||||||
|
// - fmt: A format string with placeholders for the provided arguments.
|
||||||
|
// - args: A variadic list of arguments to be formatted.
|
||||||
|
//
|
||||||
|
// Returns: A formatted string. The returned string must be freed accordingly.
|
||||||
|
//
|
||||||
|
aprintfln :: proc(fmt: string, args: ..any, allocator := context.allocator) -> string {
|
||||||
|
return aprintf(fmt, ..args, allocator=allocator, newline=true)
|
||||||
|
}
|
||||||
// Creates a formatted string
|
// Creates a formatted string
|
||||||
//
|
//
|
||||||
// *Allocates Using Context's Temporary Allocator*
|
// *Allocates Using Context's Temporary Allocator*
|
||||||
@@ -197,15 +211,29 @@ tprintln :: proc(args: ..any, sep := " ") -> string {
|
|||||||
// Inputs:
|
// Inputs:
|
||||||
// - fmt: A format string with placeholders for the provided arguments.
|
// - fmt: A format string with placeholders for the provided arguments.
|
||||||
// - args: A variadic list of arguments to be formatted.
|
// - args: A variadic list of arguments to be formatted.
|
||||||
|
// - newline: Whether the string should end with a newline. (See `tprintfln`.)
|
||||||
//
|
//
|
||||||
// Returns: A formatted string.
|
// Returns: A formatted string.
|
||||||
//
|
//
|
||||||
tprintf :: proc(fmt: string, args: ..any) -> string {
|
tprintf :: proc(fmt: string, args: ..any, newline := false) -> string {
|
||||||
str: strings.Builder
|
str: strings.Builder
|
||||||
strings.builder_init(&str, context.temp_allocator)
|
strings.builder_init(&str, context.temp_allocator)
|
||||||
sbprintf(&str, fmt, ..args)
|
sbprintf(&str, fmt, ..args, newline=newline)
|
||||||
return strings.to_string(str)
|
return strings.to_string(str)
|
||||||
}
|
}
|
||||||
|
// Creates a formatted string using a format string and arguments, followed by a newline.
|
||||||
|
//
|
||||||
|
// *Allocates Using Context's Temporary Allocator*
|
||||||
|
//
|
||||||
|
// Inputs:
|
||||||
|
// - fmt: A format string with placeholders for the provided arguments.
|
||||||
|
// - args: A variadic list of arguments to be formatted.
|
||||||
|
//
|
||||||
|
// Returns: A formatted string.
|
||||||
|
//
|
||||||
|
tprintfln :: proc(fmt: string, args: ..any) -> string {
|
||||||
|
return tprintf(fmt, ..args, newline=true)
|
||||||
|
}
|
||||||
// Creates a formatted string using a supplied buffer as the backing array. Writes into the buffer.
|
// Creates a formatted string using a supplied buffer as the backing array. Writes into the buffer.
|
||||||
//
|
//
|
||||||
// Inputs:
|
// Inputs:
|
||||||
@@ -238,12 +266,25 @@ bprintln :: proc(buf: []byte, args: ..any, sep := " ") -> string {
|
|||||||
// - buf: The backing buffer
|
// - buf: The backing buffer
|
||||||
// - fmt: A format string with placeholders for the provided arguments
|
// - fmt: A format string with placeholders for the provided arguments
|
||||||
// - args: A variadic list of arguments to be formatted
|
// - args: A variadic list of arguments to be formatted
|
||||||
|
// - newline: Whether the string should end with a newline. (See `bprintfln`.)
|
||||||
//
|
//
|
||||||
// Returns: A formatted string
|
// Returns: A formatted string
|
||||||
//
|
//
|
||||||
bprintf :: proc(buf: []byte, fmt: string, args: ..any) -> string {
|
bprintf :: proc(buf: []byte, fmt: string, args: ..any, newline := false) -> string {
|
||||||
sb := strings.builder_from_bytes(buf)
|
sb := strings.builder_from_bytes(buf)
|
||||||
return sbprintf(&sb, fmt, ..args)
|
return sbprintf(&sb, fmt, ..args, newline=newline)
|
||||||
|
}
|
||||||
|
// Creates a formatted string using a supplied buffer as the backing array, followed by a newline. Writes into the buffer.
|
||||||
|
//
|
||||||
|
// Inputs:
|
||||||
|
// - buf: The backing buffer
|
||||||
|
// - fmt: A format string with placeholders for the provided arguments
|
||||||
|
// - args: A variadic list of arguments to be formatted
|
||||||
|
//
|
||||||
|
// Returns: A formatted string
|
||||||
|
//
|
||||||
|
bprintfln :: proc(buf: []byte, fmt: string, args: ..any) -> string {
|
||||||
|
return bprintf(buf, fmt, ..args, newline=true)
|
||||||
}
|
}
|
||||||
// Runtime assertion with a formatted message
|
// Runtime assertion with a formatted message
|
||||||
//
|
//
|
||||||
@@ -294,17 +335,31 @@ panicf :: proc(fmt: string, args: ..any, loc := #caller_location) -> ! {
|
|||||||
// Inputs:
|
// Inputs:
|
||||||
// - format: A format string with placeholders for the provided arguments
|
// - format: A format string with placeholders for the provided arguments
|
||||||
// - args: A variadic list of arguments to be formatted
|
// - args: A variadic list of arguments to be formatted
|
||||||
|
// - newline: Whether the string should end with a newline. (See `caprintfln`.)
|
||||||
//
|
//
|
||||||
// Returns: A formatted C string
|
// Returns: A formatted C string
|
||||||
//
|
//
|
||||||
caprintf :: proc(format: string, args: ..any) -> cstring {
|
caprintf :: proc(format: string, args: ..any, newline := false) -> cstring {
|
||||||
str: strings.Builder
|
str: strings.Builder
|
||||||
strings.builder_init(&str)
|
strings.builder_init(&str)
|
||||||
sbprintf(&str, format, ..args)
|
sbprintf(&str, format, ..args, newline=newline)
|
||||||
strings.write_byte(&str, 0)
|
strings.write_byte(&str, 0)
|
||||||
s := strings.to_string(str)
|
s := strings.to_string(str)
|
||||||
return cstring(raw_data(s))
|
return cstring(raw_data(s))
|
||||||
}
|
}
|
||||||
|
// Creates a formatted C string, followed by a newline.
|
||||||
|
//
|
||||||
|
// *Allocates Using Context's Allocator*
|
||||||
|
//
|
||||||
|
// Inputs:
|
||||||
|
// - format: A format string with placeholders for the provided arguments
|
||||||
|
// - args: A variadic list of arguments to be formatted
|
||||||
|
//
|
||||||
|
// Returns: A formatted C string
|
||||||
|
//
|
||||||
|
caprintfln :: proc(format: string, args: ..any) -> cstring {
|
||||||
|
return caprintf(format, ..args, newline=true)
|
||||||
|
}
|
||||||
// Creates a formatted C string
|
// Creates a formatted C string
|
||||||
//
|
//
|
||||||
// *Allocates Using Context's Temporary Allocator*
|
// *Allocates Using Context's Temporary Allocator*
|
||||||
@@ -312,16 +367,30 @@ caprintf :: proc(format: string, args: ..any) -> cstring {
|
|||||||
// Inputs:
|
// Inputs:
|
||||||
// - format: A format string with placeholders for the provided arguments
|
// - format: A format string with placeholders for the provided arguments
|
||||||
// - args: A variadic list of arguments to be formatted
|
// - args: A variadic list of arguments to be formatted
|
||||||
|
// - newline: Whether the string should end with a newline. (See `ctprintfln`.)
|
||||||
|
//
|
||||||
|
// Returns: A formatted C string
|
||||||
|
//
|
||||||
|
ctprintf :: proc(format: string, args: ..any, newline := false) -> cstring {
|
||||||
|
str: strings.Builder
|
||||||
|
strings.builder_init(&str, context.temp_allocator)
|
||||||
|
sbprintf(&str, format, ..args, newline=newline)
|
||||||
|
strings.write_byte(&str, 0)
|
||||||
|
s := strings.to_string(str)
|
||||||
|
return cstring(raw_data(s))
|
||||||
|
}
|
||||||
|
// Creates a formatted C string, followed by a newline.
|
||||||
|
//
|
||||||
|
// *Allocates Using Context's Temporary Allocator*
|
||||||
|
//
|
||||||
|
// Inputs:
|
||||||
|
// - format: A format string with placeholders for the provided arguments
|
||||||
|
// - args: A variadic list of arguments to be formatted
|
||||||
//
|
//
|
||||||
// Returns: A formatted C string
|
// Returns: A formatted C string
|
||||||
//
|
//
|
||||||
ctprintf :: proc(format: string, args: ..any) -> cstring {
|
ctprintfln :: proc(format: string, args: ..any) -> cstring {
|
||||||
str: strings.Builder
|
return ctprintf(format, ..args, newline=true)
|
||||||
strings.builder_init(&str, context.temp_allocator)
|
|
||||||
sbprintf(&str, format, ..args)
|
|
||||||
strings.write_byte(&str, 0)
|
|
||||||
s := strings.to_string(str)
|
|
||||||
return cstring(raw_data(s))
|
|
||||||
}
|
}
|
||||||
// Formats using the default print settings and writes to the given strings.Builder
|
// Formats using the default print settings and writes to the given strings.Builder
|
||||||
//
|
//
|
||||||
@@ -355,13 +424,25 @@ sbprintln :: proc(buf: ^strings.Builder, args: ..any, sep := " ") -> string {
|
|||||||
// - buf: A pointer to a strings.Builder buffer
|
// - buf: A pointer to a strings.Builder buffer
|
||||||
// - fmt: The format string
|
// - fmt: The format string
|
||||||
// - args: A variadic list of arguments to be formatted
|
// - args: A variadic list of arguments to be formatted
|
||||||
|
// - newline: Whether a trailing newline should be written. (See `sbprintfln`.)
|
||||||
//
|
//
|
||||||
// Returns: The resulting formatted string
|
// Returns: The resulting formatted string
|
||||||
//
|
//
|
||||||
sbprintf :: proc(buf: ^strings.Builder, fmt: string, args: ..any) -> string {
|
sbprintf :: proc(buf: ^strings.Builder, fmt: string, args: ..any, newline := false) -> string {
|
||||||
wprintf(strings.to_writer(buf), fmt, ..args, flush=true)
|
wprintf(strings.to_writer(buf), fmt, ..args, flush=true, newline=newline)
|
||||||
return strings.to_string(buf^)
|
return strings.to_string(buf^)
|
||||||
}
|
}
|
||||||
|
// Formats and writes to a strings.Builder buffer according to the specified format string, followed by a newline.
|
||||||
|
//
|
||||||
|
// Inputs:
|
||||||
|
// - buf: A pointer to a strings.Builder to store the formatted string
|
||||||
|
// - args: A variadic list of arguments to be formatted
|
||||||
|
//
|
||||||
|
// Returns: A formatted string
|
||||||
|
//
|
||||||
|
sbprintfln :: proc(buf: ^strings.Builder, format: string, args: ..any) -> string {
|
||||||
|
return sbprintf(buf, format, ..args, newline=true)
|
||||||
|
}
|
||||||
// Formats and writes to an io.Writer using the default print settings
|
// Formats and writes to an io.Writer using the default print settings
|
||||||
//
|
//
|
||||||
// Inputs:
|
// Inputs:
|
||||||
@@ -435,10 +516,11 @@ wprintln :: proc(w: io.Writer, args: ..any, sep := " ", flush := true) -> int {
|
|||||||
// - w: An io.Writer to write to
|
// - w: An io.Writer to write to
|
||||||
// - fmt: The format string
|
// - fmt: The format string
|
||||||
// - args: A variadic list of arguments to be formatted
|
// - args: A variadic list of arguments to be formatted
|
||||||
|
// - newline: Whether a trailing newline should be written. (See `wprintfln`.)
|
||||||
//
|
//
|
||||||
// Returns: The number of bytes written
|
// Returns: The number of bytes written
|
||||||
//
|
//
|
||||||
wprintf :: proc(w: io.Writer, fmt: string, args: ..any, flush := true) -> int {
|
wprintf :: proc(w: io.Writer, fmt: string, args: ..any, flush := true, newline := false) -> int {
|
||||||
fi: Info
|
fi: Info
|
||||||
arg_index: int = 0
|
arg_index: int = 0
|
||||||
end := len(fmt)
|
end := len(fmt)
|
||||||
@@ -708,12 +790,27 @@ wprintf :: proc(w: io.Writer, fmt: string, args: ..any, flush := true) -> int {
|
|||||||
}
|
}
|
||||||
io.write_string(fi.writer, ")", &fi.n)
|
io.write_string(fi.writer, ")", &fi.n)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if newline {
|
||||||
|
io.write_byte(w, '\n', &fi.n)
|
||||||
|
}
|
||||||
if flush {
|
if flush {
|
||||||
io.flush(w)
|
io.flush(w)
|
||||||
}
|
}
|
||||||
|
|
||||||
return fi.n
|
return fi.n
|
||||||
}
|
}
|
||||||
|
// Formats and writes to an io.Writer according to the specified format string, followed by a newline.
|
||||||
|
//
|
||||||
|
// Inputs:
|
||||||
|
// - w: The io.Writer to write to.
|
||||||
|
// - args: A variadic list of arguments to be formatted.
|
||||||
|
//
|
||||||
|
// Returns: The number of bytes written.
|
||||||
|
//
|
||||||
|
wprintfln :: proc(w: io.Writer, format: string, args: ..any, flush := true) -> int {
|
||||||
|
return wprintf(w, format, ..args, flush=flush, newline=true)
|
||||||
|
}
|
||||||
// Writes a ^runtime.Type_Info value to an io.Writer
|
// Writes a ^runtime.Type_Info value to an io.Writer
|
||||||
//
|
//
|
||||||
// Inputs:
|
// Inputs:
|
||||||
|
|||||||
+10
-2
@@ -30,7 +30,7 @@ fprintln :: proc(fd: os.Handle, args: ..any, sep := " ", flush := true) -> int {
|
|||||||
return wprintln(w, ..args, sep=sep, flush=flush)
|
return wprintln(w, ..args, sep=sep, flush=flush)
|
||||||
}
|
}
|
||||||
// fprintf formats according to the specified format string and writes to fd
|
// fprintf formats according to the specified format string and writes to fd
|
||||||
fprintf :: proc(fd: os.Handle, fmt: string, args: ..any, flush := true) -> int {
|
fprintf :: proc(fd: os.Handle, fmt: string, args: ..any, flush := true, newline := false) -> int {
|
||||||
buf: [1024]byte
|
buf: [1024]byte
|
||||||
b: bufio.Writer
|
b: bufio.Writer
|
||||||
defer bufio.writer_flush(&b)
|
defer bufio.writer_flush(&b)
|
||||||
@@ -38,7 +38,11 @@ fprintf :: proc(fd: os.Handle, fmt: string, args: ..any, flush := true) -> int {
|
|||||||
bufio.writer_init_with_buf(&b, os.stream_from_handle(fd), buf[:])
|
bufio.writer_init_with_buf(&b, os.stream_from_handle(fd), buf[:])
|
||||||
|
|
||||||
w := bufio.writer_to_writer(&b)
|
w := bufio.writer_to_writer(&b)
|
||||||
return wprintf(w, fmt, ..args, flush=flush)
|
return wprintf(w, fmt, ..args, flush=flush, newline=newline)
|
||||||
|
}
|
||||||
|
// fprintfln formats according to the specified format string and writes to fd, followed by a newline.
|
||||||
|
fprintfln :: proc(fd: os.Handle, fmt: string, args: ..any, flush := true) -> int {
|
||||||
|
return fprintf(fd, fmt, ..args, flush=flush, newline=true)
|
||||||
}
|
}
|
||||||
fprint_type :: proc(fd: os.Handle, info: ^runtime.Type_Info, flush := true) -> (n: int, err: io.Error) {
|
fprint_type :: proc(fd: os.Handle, info: ^runtime.Type_Info, flush := true) -> (n: int, err: io.Error) {
|
||||||
buf: [1024]byte
|
buf: [1024]byte
|
||||||
@@ -67,6 +71,8 @@ print :: proc(args: ..any, sep := " ", flush := true) -> int { return fprint(o
|
|||||||
println :: proc(args: ..any, sep := " ", flush := true) -> int { return fprintln(os.stdout, ..args, sep=sep, flush=flush) }
|
println :: proc(args: ..any, sep := " ", flush := true) -> int { return fprintln(os.stdout, ..args, sep=sep, flush=flush) }
|
||||||
// printf formats according to the specified format string and writes to os.stdout
|
// printf formats according to the specified format string and writes to os.stdout
|
||||||
printf :: proc(fmt: string, args: ..any, flush := true) -> int { return fprintf(os.stdout, fmt, ..args, flush=flush) }
|
printf :: proc(fmt: string, args: ..any, flush := true) -> int { return fprintf(os.stdout, fmt, ..args, flush=flush) }
|
||||||
|
// printfln formats according to the specified format string and writes to os.stdout, followed by a newline.
|
||||||
|
printfln :: proc(fmt: string, args: ..any, flush := true) -> int { return fprintf(os.stdout, fmt, ..args, flush=flush, newline=true) }
|
||||||
|
|
||||||
// eprint formats using the default print settings and writes to os.stderr
|
// eprint formats using the default print settings and writes to os.stderr
|
||||||
eprint :: proc(args: ..any, sep := " ", flush := true) -> int { return fprint(os.stderr, ..args, sep=sep, flush=flush) }
|
eprint :: proc(args: ..any, sep := " ", flush := true) -> int { return fprint(os.stderr, ..args, sep=sep, flush=flush) }
|
||||||
@@ -74,3 +80,5 @@ eprint :: proc(args: ..any, sep := " ", flush := true) -> int { return fprint(
|
|||||||
eprintln :: proc(args: ..any, sep := " ", flush := true) -> int { return fprintln(os.stderr, ..args, sep=sep, flush=flush) }
|
eprintln :: proc(args: ..any, sep := " ", flush := true) -> int { return fprintln(os.stderr, ..args, sep=sep, flush=flush) }
|
||||||
// eprintf formats according to the specified format string and writes to os.stderr
|
// eprintf formats according to the specified format string and writes to os.stderr
|
||||||
eprintf :: proc(fmt: string, args: ..any, flush := true) -> int { return fprintf(os.stderr, fmt, ..args, flush=flush) }
|
eprintf :: proc(fmt: string, args: ..any, flush := true) -> int { return fprintf(os.stderr, fmt, ..args, flush=flush) }
|
||||||
|
// eprintfln formats according to the specified format string and writes to os.stderr, followed by a newline.
|
||||||
|
eprintfln :: proc(fmt: string, args: ..any, flush := true) -> int { return fprintf(os.stderr, fmt, ..args, flush=flush, newline=true) }
|
||||||
|
|||||||
@@ -777,6 +777,11 @@ int_from_bytes_little_python :: proc(a: ^Int, buf: []u8, signed := false, alloca
|
|||||||
*/
|
*/
|
||||||
INT_ONE, INT_ZERO, INT_MINUS_ONE, INT_INF, INT_MINUS_INF, INT_NAN := &Int{}, &Int{}, &Int{}, &Int{}, &Int{}, &Int{}
|
INT_ONE, INT_ZERO, INT_MINUS_ONE, INT_INF, INT_MINUS_INF, INT_NAN := &Int{}, &Int{}, &Int{}, &Int{}, &Int{}, &Int{}
|
||||||
|
|
||||||
|
@(init, private)
|
||||||
|
_init_constants :: proc() {
|
||||||
|
initialize_constants()
|
||||||
|
}
|
||||||
|
|
||||||
initialize_constants :: proc() -> (res: int) {
|
initialize_constants :: proc() -> (res: int) {
|
||||||
internal_set( INT_ZERO, 0); INT_ZERO.flags = {.Immutable}
|
internal_set( INT_ZERO, 0); INT_ZERO.flags = {.Immutable}
|
||||||
internal_set( INT_ONE, 1); INT_ONE.flags = {.Immutable}
|
internal_set( INT_ONE, 1); INT_ONE.flags = {.Immutable}
|
||||||
@@ -788,7 +793,7 @@ initialize_constants :: proc() -> (res: int) {
|
|||||||
*/
|
*/
|
||||||
internal_set( INT_NAN, 1); INT_NAN.flags = {.Immutable, .NaN}
|
internal_set( INT_NAN, 1); INT_NAN.flags = {.Immutable, .NaN}
|
||||||
internal_set( INT_INF, 1); INT_INF.flags = {.Immutable, .Inf}
|
internal_set( INT_INF, 1); INT_INF.flags = {.Immutable, .Inf}
|
||||||
internal_set( INT_INF, -1); INT_MINUS_INF.flags = {.Immutable, .Inf}
|
internal_set(INT_MINUS_INF, -1); INT_MINUS_INF.flags = {.Immutable, .Inf}
|
||||||
|
|
||||||
return _DEFAULT_MUL_KARATSUBA_CUTOFF
|
return _DEFAULT_MUL_KARATSUBA_CUTOFF
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2046,9 +2046,9 @@ internal_int_inverse_modulo :: proc(dest, a, b: ^Int, allocator := context.alloc
|
|||||||
if internal_is_positive(a) && internal_eq(b, 1) { return internal_zero(dest) }
|
if internal_is_positive(a) && internal_eq(b, 1) { return internal_zero(dest) }
|
||||||
|
|
||||||
/*
|
/*
|
||||||
`b` cannot be negative and has to be > 1
|
`b` cannot be negative and b has to be > 1
|
||||||
*/
|
*/
|
||||||
if internal_is_negative(b) || internal_gt(b, 1) { return .Invalid_Argument }
|
if internal_is_negative(b) || !internal_gt(b, 1) { return .Invalid_Argument }
|
||||||
|
|
||||||
/*
|
/*
|
||||||
If the modulus is odd we can use a faster routine instead.
|
If the modulus is odd we can use a faster routine instead.
|
||||||
|
|||||||
+160
-27
@@ -645,41 +645,174 @@ trunc :: proc{
|
|||||||
|
|
||||||
@(require_results)
|
@(require_results)
|
||||||
round_f16 :: proc "contextless" (x: f16) -> f16 {
|
round_f16 :: proc "contextless" (x: f16) -> f16 {
|
||||||
return ceil(x - 0.5) if x < 0 else floor(x + 0.5)
|
// origin: Go /src/math/floor.go
|
||||||
}
|
//
|
||||||
@(require_results)
|
// Copyright (c) 2009 The Go Authors. All rights reserved.
|
||||||
round_f16le :: proc "contextless" (x: f16le) -> f16le {
|
//
|
||||||
return ceil(x - 0.5) if x < 0 else floor(x + 0.5)
|
// Redistribution and use in source and binary forms, with or without
|
||||||
}
|
// modification, are permitted provided that the following conditions are
|
||||||
@(require_results)
|
// met:
|
||||||
round_f16be :: proc "contextless" (x: f16be) -> f16be {
|
//
|
||||||
return ceil(x - 0.5) if x < 0 else floor(x + 0.5)
|
// * Redistributions of source code must retain the above copyright
|
||||||
|
// notice, this list of conditions and the following disclaimer.
|
||||||
|
// * Redistributions in binary form must reproduce the above
|
||||||
|
// copyright notice, this list of conditions and the following disclaimer
|
||||||
|
// in the documentation and/or other materials provided with the
|
||||||
|
// distribution.
|
||||||
|
// * Neither the name of Google Inc. nor the names of its
|
||||||
|
// contributors may be used to endorse or promote products derived from
|
||||||
|
// this software without specific prior written permission.
|
||||||
|
//
|
||||||
|
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||||
|
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||||
|
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
||||||
|
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
||||||
|
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||||
|
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||||
|
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||||
|
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||||
|
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||||
|
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||||
|
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||||
|
|
||||||
|
mask :: F16_MASK
|
||||||
|
shift :: F16_SHIFT
|
||||||
|
bias :: F16_BIAS
|
||||||
|
|
||||||
|
bits := transmute(u16)x
|
||||||
|
e := (bits >> shift) & mask
|
||||||
|
|
||||||
|
if e < bias {
|
||||||
|
bits &= 0x8000
|
||||||
|
if e == bias - 1 {
|
||||||
|
bits |= transmute(u16)f16(1)
|
||||||
|
}
|
||||||
|
} else if e < bias + shift {
|
||||||
|
half :: 1 << (shift - 1)
|
||||||
|
mantissa :: (1 << shift) - 1
|
||||||
|
e -= bias
|
||||||
|
bits += half >> e
|
||||||
|
bits &~= mantissa >> e
|
||||||
|
}
|
||||||
|
|
||||||
|
return transmute(f16)bits
|
||||||
}
|
}
|
||||||
|
@(require_results) round_f16le :: proc "contextless" (x: f16le) -> f16le { return #force_inline f16le(round_f16(f16(x))) }
|
||||||
|
@(require_results) round_f16be :: proc "contextless" (x: f16be) -> f16be { return #force_inline f16be(round_f16(f16(x))) }
|
||||||
|
|
||||||
@(require_results)
|
@(require_results)
|
||||||
round_f32 :: proc "contextless" (x: f32) -> f32 {
|
round_f32 :: proc "contextless" (x: f32) -> f32 {
|
||||||
return ceil(x - 0.5) if x < 0 else floor(x + 0.5)
|
// origin: Go /src/math/floor.go
|
||||||
}
|
//
|
||||||
@(require_results)
|
// Copyright (c) 2009 The Go Authors. All rights reserved.
|
||||||
round_f32le :: proc "contextless" (x: f32le) -> f32le {
|
//
|
||||||
return ceil(x - 0.5) if x < 0 else floor(x + 0.5)
|
// Redistribution and use in source and binary forms, with or without
|
||||||
}
|
// modification, are permitted provided that the following conditions are
|
||||||
@(require_results)
|
// met:
|
||||||
round_f32be :: proc "contextless" (x: f32be) -> f32be {
|
//
|
||||||
return ceil(x - 0.5) if x < 0 else floor(x + 0.5)
|
// * Redistributions of source code must retain the above copyright
|
||||||
|
// notice, this list of conditions and the following disclaimer.
|
||||||
|
// * Redistributions in binary form must reproduce the above
|
||||||
|
// copyright notice, this list of conditions and the following disclaimer
|
||||||
|
// in the documentation and/or other materials provided with the
|
||||||
|
// distribution.
|
||||||
|
// * Neither the name of Google Inc. nor the names of its
|
||||||
|
// contributors may be used to endorse or promote products derived from
|
||||||
|
// this software without specific prior written permission.
|
||||||
|
//
|
||||||
|
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||||
|
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||||
|
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
||||||
|
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
||||||
|
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||||
|
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||||
|
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||||
|
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||||
|
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||||
|
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||||
|
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||||
|
|
||||||
|
mask :: F32_MASK
|
||||||
|
shift :: F32_SHIFT
|
||||||
|
bias :: F32_BIAS
|
||||||
|
|
||||||
|
bits := transmute(u32)x
|
||||||
|
e := (bits >> shift) & mask
|
||||||
|
|
||||||
|
if e < bias {
|
||||||
|
bits &= 0x8000_0000
|
||||||
|
if e == bias - 1 {
|
||||||
|
bits |= transmute(u32)f32(1)
|
||||||
|
}
|
||||||
|
} else if e < bias + shift {
|
||||||
|
half :: 1 << (shift - 1)
|
||||||
|
mantissa :: (1 << shift) - 1
|
||||||
|
e -= bias
|
||||||
|
bits += half >> e
|
||||||
|
bits &~= mantissa >> e
|
||||||
|
}
|
||||||
|
|
||||||
|
return transmute(f32)bits
|
||||||
}
|
}
|
||||||
|
@(require_results) round_f32le :: proc "contextless" (x: f32le) -> f32le { return #force_inline f32le(round_f32(f32(x))) }
|
||||||
|
@(require_results) round_f32be :: proc "contextless" (x: f32be) -> f32be { return #force_inline f32be(round_f32(f32(x))) }
|
||||||
|
|
||||||
@(require_results)
|
@(require_results)
|
||||||
round_f64 :: proc "contextless" (x: f64) -> f64 {
|
round_f64 :: proc "contextless" (x: f64) -> f64 {
|
||||||
return ceil(x - 0.5) if x < 0 else floor(x + 0.5)
|
// origin: Go /src/math/floor.go
|
||||||
}
|
//
|
||||||
@(require_results)
|
// Copyright (c) 2009 The Go Authors. All rights reserved.
|
||||||
round_f64le :: proc "contextless" (x: f64le) -> f64le {
|
//
|
||||||
return ceil(x - 0.5) if x < 0 else floor(x + 0.5)
|
// Redistribution and use in source and binary forms, with or without
|
||||||
}
|
// modification, are permitted provided that the following conditions are
|
||||||
@(require_results)
|
// met:
|
||||||
round_f64be :: proc "contextless" (x: f64be) -> f64be {
|
//
|
||||||
return ceil(x - 0.5) if x < 0 else floor(x + 0.5)
|
// * Redistributions of source code must retain the above copyright
|
||||||
|
// notice, this list of conditions and the following disclaimer.
|
||||||
|
// * Redistributions in binary form must reproduce the above
|
||||||
|
// copyright notice, this list of conditions and the following disclaimer
|
||||||
|
// in the documentation and/or other materials provided with the
|
||||||
|
// distribution.
|
||||||
|
// * Neither the name of Google Inc. nor the names of its
|
||||||
|
// contributors may be used to endorse or promote products derived from
|
||||||
|
// this software without specific prior written permission.
|
||||||
|
//
|
||||||
|
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||||
|
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||||
|
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
||||||
|
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
||||||
|
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||||
|
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||||
|
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||||
|
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||||
|
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||||
|
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||||
|
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||||
|
|
||||||
|
mask :: F64_MASK
|
||||||
|
shift :: F64_SHIFT
|
||||||
|
bias :: F64_BIAS
|
||||||
|
|
||||||
|
bits := transmute(u64)x
|
||||||
|
e := (bits >> shift) & mask
|
||||||
|
|
||||||
|
if e < bias {
|
||||||
|
bits &= 0x8000_0000_0000_0000
|
||||||
|
if e == bias - 1 {
|
||||||
|
bits |= transmute(u64)f64(1)
|
||||||
|
}
|
||||||
|
} else if e < bias + shift {
|
||||||
|
half :: 1 << (shift - 1)
|
||||||
|
mantissa :: (1 << shift) - 1
|
||||||
|
e -= bias
|
||||||
|
bits += half >> e
|
||||||
|
bits &~= mantissa >> e
|
||||||
|
}
|
||||||
|
|
||||||
|
return transmute(f64)bits
|
||||||
}
|
}
|
||||||
|
@(require_results) round_f64le :: proc "contextless" (x: f64le) -> f64le { return #force_inline f64le(round_f64(f64(x))) }
|
||||||
|
@(require_results) round_f64be :: proc "contextless" (x: f64be) -> f64be { return #force_inline f64be(round_f64(f64(x))) }
|
||||||
round :: proc{
|
round :: proc{
|
||||||
round_f16, round_f16le, round_f16be,
|
round_f16, round_f16le, round_f16be,
|
||||||
round_f32, round_f32le, round_f32be,
|
round_f32, round_f32le, round_f32be,
|
||||||
|
|||||||
+2
-5
@@ -1,7 +1,6 @@
|
|||||||
package os
|
package os
|
||||||
|
|
||||||
import "base:runtime"
|
import "base:runtime"
|
||||||
import "core:mem"
|
|
||||||
import "core:strconv"
|
import "core:strconv"
|
||||||
import "core:unicode/utf8"
|
import "core:unicode/utf8"
|
||||||
|
|
||||||
@@ -160,13 +159,11 @@ write_entire_file :: proc(name: string, data: []byte, truncate := true) -> (succ
|
|||||||
}
|
}
|
||||||
|
|
||||||
write_ptr :: proc(fd: Handle, data: rawptr, len: int) -> (int, Errno) {
|
write_ptr :: proc(fd: Handle, data: rawptr, len: int) -> (int, Errno) {
|
||||||
s := transmute([]byte)mem.Raw_Slice{data, len}
|
return write(fd, ([^]byte)(data)[:len])
|
||||||
return write(fd, s)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
read_ptr :: proc(fd: Handle, data: rawptr, len: int) -> (int, Errno) {
|
read_ptr :: proc(fd: Handle, data: rawptr, len: int) -> (int, Errno) {
|
||||||
s := transmute([]byte)mem.Raw_Slice{data, len}
|
return read(fd, ([^]byte)(data)[:len])
|
||||||
return read(fd, s)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
heap_allocator_proc :: runtime.heap_allocator_proc
|
heap_allocator_proc :: runtime.heap_allocator_proc
|
||||||
|
|||||||
@@ -1,6 +1,5 @@
|
|||||||
package os2
|
package os2
|
||||||
|
|
||||||
import "core:mem"
|
|
||||||
import "base:runtime"
|
import "base:runtime"
|
||||||
import "core:strconv"
|
import "core:strconv"
|
||||||
import "core:unicode/utf8"
|
import "core:unicode/utf8"
|
||||||
@@ -64,13 +63,11 @@ write_encoded_rune :: proc(f: ^File, r: rune) -> (n: int, err: Error) {
|
|||||||
|
|
||||||
|
|
||||||
write_ptr :: proc(f: ^File, data: rawptr, len: int) -> (n: int, err: Error) {
|
write_ptr :: proc(f: ^File, data: rawptr, len: int) -> (n: int, err: Error) {
|
||||||
s := transmute([]byte)mem.Raw_Slice{data, len}
|
return write(f, ([^]byte)(data)[:len])
|
||||||
return write(f, s)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
read_ptr :: proc(f: ^File, data: rawptr, len: int) -> (n: int, err: Error) {
|
read_ptr :: proc(f: ^File, data: rawptr, len: int) -> (n: int, err: Error) {
|
||||||
s := transmute([]byte)mem.Raw_Slice{data, len}
|
return read(f, ([^]byte)(data)[:len])
|
||||||
return read(f, s)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -9,6 +9,7 @@ Example:
|
|||||||
guard :: proc{
|
guard :: proc{
|
||||||
mutex_guard,
|
mutex_guard,
|
||||||
rw_mutex_guard,
|
rw_mutex_guard,
|
||||||
|
recursive_mutex_guard,
|
||||||
ticket_mutex_guard,
|
ticket_mutex_guard,
|
||||||
benaphore_guard,
|
benaphore_guard,
|
||||||
recursive_benaphore_guard,
|
recursive_benaphore_guard,
|
||||||
@@ -31,6 +32,7 @@ shared_guard :: proc{
|
|||||||
lock :: proc{
|
lock :: proc{
|
||||||
mutex_lock,
|
mutex_lock,
|
||||||
rw_mutex_lock,
|
rw_mutex_lock,
|
||||||
|
recursive_mutex_lock,
|
||||||
ticket_mutex_lock,
|
ticket_mutex_lock,
|
||||||
benaphore_lock,
|
benaphore_lock,
|
||||||
recursive_benaphore_lock,
|
recursive_benaphore_lock,
|
||||||
@@ -43,6 +45,7 @@ lock :: proc{
|
|||||||
unlock :: proc{
|
unlock :: proc{
|
||||||
mutex_unlock,
|
mutex_unlock,
|
||||||
rw_mutex_unlock,
|
rw_mutex_unlock,
|
||||||
|
recursive_mutex_unlock,
|
||||||
ticket_mutex_unlock,
|
ticket_mutex_unlock,
|
||||||
benaphore_unlock,
|
benaphore_unlock,
|
||||||
recursive_benaphore_unlock,
|
recursive_benaphore_unlock,
|
||||||
@@ -55,6 +58,7 @@ unlock :: proc{
|
|||||||
try_lock :: proc{
|
try_lock :: proc{
|
||||||
mutex_try_lock,
|
mutex_try_lock,
|
||||||
rw_mutex_try_lock,
|
rw_mutex_try_lock,
|
||||||
|
recursive_mutex_try_lock,
|
||||||
benaphore_try_lock,
|
benaphore_try_lock,
|
||||||
recursive_benaphore_try_lock,
|
recursive_benaphore_try_lock,
|
||||||
atomic_mutex_try_lock,
|
atomic_mutex_try_lock,
|
||||||
|
|||||||
@@ -0,0 +1,98 @@
|
|||||||
|
//+build darwin
|
||||||
|
package darwin
|
||||||
|
|
||||||
|
import "core:runtime"
|
||||||
|
|
||||||
|
foreign import core_foundation "system:CoreFoundation.framework"
|
||||||
|
|
||||||
|
CFTypeRef :: distinct rawptr
|
||||||
|
|
||||||
|
CFStringRef :: distinct CFTypeRef
|
||||||
|
|
||||||
|
CFIndex :: int
|
||||||
|
|
||||||
|
CFRange :: struct {
|
||||||
|
location: CFIndex,
|
||||||
|
length: CFIndex,
|
||||||
|
}
|
||||||
|
|
||||||
|
CFStringEncoding :: enum u32 {
|
||||||
|
ASCII = 1,
|
||||||
|
NEXTSTEP = 2,
|
||||||
|
JapaneseEUC = 3,
|
||||||
|
UTF8 = 4,
|
||||||
|
ISOLatin1 = 5,
|
||||||
|
Symbol = 6,
|
||||||
|
NonLossyASCII = 7,
|
||||||
|
ShiftJIS = 8,
|
||||||
|
ISOLatin2 = 9,
|
||||||
|
Unicode = 10,
|
||||||
|
WindowsCP1251 = 11,
|
||||||
|
WindowsCP1252 = 12,
|
||||||
|
WindowsCP1253 = 13,
|
||||||
|
WindowsCP1254 = 14,
|
||||||
|
WindowsCP1250 = 15,
|
||||||
|
ISO2022JP = 21,
|
||||||
|
MacOSRoman = 30,
|
||||||
|
|
||||||
|
UTF16 = Unicode,
|
||||||
|
|
||||||
|
UTF16BigEndian = 0x90000100,
|
||||||
|
UTF16LittleEndian = 0x94000100,
|
||||||
|
|
||||||
|
UTF32 = 0x8c000100,
|
||||||
|
UTF32BigEndian = 0x98000100,
|
||||||
|
UTF32LittleEndian = 0x9c000100,
|
||||||
|
}
|
||||||
|
|
||||||
|
foreign core_foundation {
|
||||||
|
// Copies the character contents of a string to a local C string buffer after converting the characters to a given encoding.
|
||||||
|
CFStringGetCString :: proc(theString: CFStringRef, buffer: [^]byte, bufferSize: CFIndex, encoding: CFStringEncoding) -> Bool ---
|
||||||
|
|
||||||
|
// Returns the number (in terms of UTF-16 code pairs) of Unicode characters in a string.
|
||||||
|
CFStringGetLength :: proc(theString: CFStringRef) -> CFIndex ---
|
||||||
|
|
||||||
|
// Returns the maximum number of bytes a string of a specified length (in Unicode characters) will take up if encoded in a specified encoding.
|
||||||
|
CFStringGetMaximumSizeForEncoding :: proc(length: CFIndex, encoding: CFStringEncoding) -> CFIndex ---
|
||||||
|
|
||||||
|
// Fetches a range of the characters from a string into a byte buffer after converting the characters to a specified encoding.
|
||||||
|
CFStringGetBytes :: proc(
|
||||||
|
thestring: CFStringRef,
|
||||||
|
range: CFRange,
|
||||||
|
encoding: CFStringEncoding,
|
||||||
|
lossByte: u8,
|
||||||
|
isExternalRepresentation: Bool,
|
||||||
|
buffer: [^]byte,
|
||||||
|
maxBufLen: CFIndex,
|
||||||
|
usedBufLen: ^CFIndex,
|
||||||
|
) -> CFIndex ---
|
||||||
|
|
||||||
|
// Releases a Core Foundation object.
|
||||||
|
@(link_name="CFRelease")
|
||||||
|
_CFRelease :: proc(cf: CFTypeRef) ---
|
||||||
|
}
|
||||||
|
|
||||||
|
// Releases a Core Foundation object.
|
||||||
|
CFRelease :: proc {
|
||||||
|
CFReleaseString,
|
||||||
|
}
|
||||||
|
|
||||||
|
// Releases a Core Foundation string.
|
||||||
|
CFReleaseString :: #force_inline proc(theString: CFStringRef) {
|
||||||
|
_CFRelease(CFTypeRef(theString))
|
||||||
|
}
|
||||||
|
|
||||||
|
CFStringCopyToOdinString :: proc(theString: CFStringRef, allocator := context.allocator) -> (str: string, ok: bool) #optional_ok {
|
||||||
|
length := CFStringGetLength(theString)
|
||||||
|
max := CFStringGetMaximumSizeForEncoding(length, .UTF8)
|
||||||
|
|
||||||
|
buf, err := make([]byte, max, allocator)
|
||||||
|
if err != nil { return }
|
||||||
|
|
||||||
|
raw_str := runtime.Raw_String{
|
||||||
|
data = raw_data(buf),
|
||||||
|
}
|
||||||
|
CFStringGetBytes(theString, {0, length}, .UTF8, 0, false, raw_data(buf), max, &raw_str.len)
|
||||||
|
|
||||||
|
return transmute(string)raw_str, true
|
||||||
|
}
|
||||||
@@ -0,0 +1,4 @@
|
|||||||
|
//+build darwin
|
||||||
|
package darwin
|
||||||
|
|
||||||
|
Bool :: b8
|
||||||
@@ -0,0 +1,26 @@
|
|||||||
|
//+build darwin
|
||||||
|
package darwin
|
||||||
|
|
||||||
|
foreign import security "system:Security.framework"
|
||||||
|
|
||||||
|
// A reference to a random number generator.
|
||||||
|
SecRandomRef :: distinct rawptr
|
||||||
|
|
||||||
|
OSStatus :: distinct i32
|
||||||
|
|
||||||
|
errSec :: enum OSStatus {
|
||||||
|
Success = 0, // No error.
|
||||||
|
Unimplemented = -4, // Function or operation not implemented.
|
||||||
|
|
||||||
|
// Many more...
|
||||||
|
}
|
||||||
|
|
||||||
|
foreign security {
|
||||||
|
// Synonym for nil, uses a cryptographically secure random number generator.
|
||||||
|
kSecRandomDefault: SecRandomRef
|
||||||
|
|
||||||
|
// Generates an array of cryptographically secure random bytes.
|
||||||
|
SecRandomCopyBytes :: proc(rnd: SecRandomRef = kSecRandomDefault, count: uint, bytes: [^]byte) -> errSec ---
|
||||||
|
|
||||||
|
SecCopyErrorMessageString :: proc(status: errSec, reserved: rawptr = nil) -> CFStringRef ---
|
||||||
|
}
|
||||||
@@ -1,6 +1,7 @@
|
|||||||
// +build windows
|
// +build windows
|
||||||
package sys_windows
|
package sys_windows
|
||||||
|
|
||||||
|
import "base:intrinsics"
|
||||||
foreign import user32 "system:User32.lib"
|
foreign import user32 "system:User32.lib"
|
||||||
|
|
||||||
@(default_calling_convention="system")
|
@(default_calling_convention="system")
|
||||||
@@ -155,6 +156,9 @@ foreign user32 {
|
|||||||
GetCursorPos :: proc(lpPoint: LPPOINT) -> BOOL ---
|
GetCursorPos :: proc(lpPoint: LPPOINT) -> BOOL ---
|
||||||
SetCursorPos :: proc(X: c_int, Y: c_int) -> BOOL ---
|
SetCursorPos :: proc(X: c_int, Y: c_int) -> BOOL ---
|
||||||
SetCursor :: proc(hCursor: HCURSOR) -> HCURSOR ---
|
SetCursor :: proc(hCursor: HCURSOR) -> HCURSOR ---
|
||||||
|
when !intrinsics.is_package_imported("raylib") {
|
||||||
|
ShowCursor :: proc(bShow: BOOL) -> INT ---
|
||||||
|
}
|
||||||
|
|
||||||
EnumDisplaySettingsW :: proc(lpszDeviceName: LPCWSTR, iModeNum: DWORD, lpDevMode: ^DEVMODEW) -> BOOL ---
|
EnumDisplaySettingsW :: proc(lpszDeviceName: LPCWSTR, iModeNum: DWORD, lpDevMode: ^DEVMODEW) -> BOOL ---
|
||||||
|
|
||||||
|
|||||||
@@ -369,6 +369,10 @@ datetime_to_time :: proc "contextless" (year, month, day, hour, minute, second:
|
|||||||
mod = year % divisor
|
mod = year % divisor
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
_is_leap_year :: proc "contextless" (year: int) -> bool {
|
||||||
|
return year%4 == 0 && (year%100 != 0 || year%400 == 0)
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
ok = true
|
ok = true
|
||||||
|
|
||||||
@@ -395,6 +399,10 @@ datetime_to_time :: proc "contextless" (year, month, day, hour, minute, second:
|
|||||||
|
|
||||||
days += int(days_before[_m]) + _d
|
days += int(days_before[_m]) + _d
|
||||||
|
|
||||||
|
if _is_leap_year(year) && _m >= 2 {
|
||||||
|
days += 1
|
||||||
|
}
|
||||||
|
|
||||||
s += i64(days) * SECONDS_PER_DAY
|
s += i64(days) * SECONDS_PER_DAY
|
||||||
s += i64(hour) * SECONDS_PER_HOUR
|
s += i64(hour) * SECONDS_PER_HOUR
|
||||||
s += i64(minute) * SECONDS_PER_MINUTE
|
s += i64(minute) * SECONDS_PER_MINUTE
|
||||||
|
|||||||
@@ -14,6 +14,7 @@ import shoco "core:compress/shoco"
|
|||||||
import gzip "core:compress/gzip"
|
import gzip "core:compress/gzip"
|
||||||
import zlib "core:compress/zlib"
|
import zlib "core:compress/zlib"
|
||||||
|
|
||||||
|
import avl "core:container/avl"
|
||||||
import bit_array "core:container/bit_array"
|
import bit_array "core:container/bit_array"
|
||||||
import priority_queue "core:container/priority_queue"
|
import priority_queue "core:container/priority_queue"
|
||||||
import queue "core:container/queue"
|
import queue "core:container/queue"
|
||||||
@@ -131,6 +132,7 @@ _ :: compress
|
|||||||
_ :: shoco
|
_ :: shoco
|
||||||
_ :: gzip
|
_ :: gzip
|
||||||
_ :: zlib
|
_ :: zlib
|
||||||
|
_ :: avl
|
||||||
_ :: bit_array
|
_ :: bit_array
|
||||||
_ :: priority_queue
|
_ :: priority_queue
|
||||||
_ :: queue
|
_ :: queue
|
||||||
|
|||||||
@@ -5820,6 +5820,26 @@ gb_internal bool check_builtin_procedure(CheckerContext *c, Operand *operand, As
|
|||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
case BuiltinProc_type_bit_set_backing_type:
|
||||||
|
{
|
||||||
|
Operand op = {};
|
||||||
|
Type *type = check_type(c, ce->args[0]);
|
||||||
|
Type *bt = base_type(type);
|
||||||
|
if (bt == nullptr || bt == t_invalid) {
|
||||||
|
error(ce->args[0], "Expected a type for '%.*s'", LIT(builtin_name));
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if (bt->kind != Type_BitSet) {
|
||||||
|
gbString s = type_to_string(type);
|
||||||
|
error(ce->args[0], "Expected a bit_set type for '%.*s', got %s", LIT(builtin_name), s);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
operand->mode = Addressing_Type;
|
||||||
|
operand->type = bit_set_to_int(bt);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
case BuiltinProc_type_equal_proc:
|
case BuiltinProc_type_equal_proc:
|
||||||
{
|
{
|
||||||
Operand op = {};
|
Operand op = {};
|
||||||
|
|||||||
@@ -282,6 +282,8 @@ BuiltinProc__type_simple_boolean_end,
|
|||||||
|
|
||||||
BuiltinProc_type_field_index_of,
|
BuiltinProc_type_field_index_of,
|
||||||
|
|
||||||
|
BuiltinProc_type_bit_set_backing_type,
|
||||||
|
|
||||||
BuiltinProc_type_equal_proc,
|
BuiltinProc_type_equal_proc,
|
||||||
BuiltinProc_type_hasher_proc,
|
BuiltinProc_type_hasher_proc,
|
||||||
BuiltinProc_type_map_info,
|
BuiltinProc_type_map_info,
|
||||||
@@ -586,6 +588,8 @@ gb_global BuiltinProc builtin_procs[BuiltinProc_COUNT] = {
|
|||||||
|
|
||||||
{STR_LIT("type_field_index_of"), 2, false, Expr_Expr, BuiltinProcPkg_intrinsics},
|
{STR_LIT("type_field_index_of"), 2, false, Expr_Expr, BuiltinProcPkg_intrinsics},
|
||||||
|
|
||||||
|
{STR_LIT("type_bit_set_backing_type"), 1, false, Expr_Expr, BuiltinProcPkg_intrinsics},
|
||||||
|
|
||||||
{STR_LIT("type_equal_proc"), 1, false, Expr_Expr, BuiltinProcPkg_intrinsics},
|
{STR_LIT("type_equal_proc"), 1, false, Expr_Expr, BuiltinProcPkg_intrinsics},
|
||||||
{STR_LIT("type_hasher_proc"), 1, false, Expr_Expr, BuiltinProcPkg_intrinsics},
|
{STR_LIT("type_hasher_proc"), 1, false, Expr_Expr, BuiltinProcPkg_intrinsics},
|
||||||
{STR_LIT("type_map_info"), 1, false, Expr_Expr, BuiltinProcPkg_intrinsics},
|
{STR_LIT("type_map_info"), 1, false, Expr_Expr, BuiltinProcPkg_intrinsics},
|
||||||
|
|||||||
+9
-53
@@ -1053,41 +1053,6 @@ struct lbGlobalVariable {
|
|||||||
bool is_initialized;
|
bool is_initialized;
|
||||||
};
|
};
|
||||||
|
|
||||||
gb_internal lbProcedure *lb_create_startup_type_info(lbModule *m) {
|
|
||||||
if (build_context.no_rtti) {
|
|
||||||
return nullptr;
|
|
||||||
}
|
|
||||||
Type *proc_type = alloc_type_proc(nullptr, nullptr, 0, nullptr, 0, false, ProcCC_CDecl);
|
|
||||||
|
|
||||||
lbProcedure *p = lb_create_dummy_procedure(m, str_lit(LB_STARTUP_TYPE_INFO_PROC_NAME), proc_type);
|
|
||||||
p->is_startup = true;
|
|
||||||
LLVMSetLinkage(p->value, LLVMInternalLinkage);
|
|
||||||
|
|
||||||
lb_add_attribute_to_proc(m, p->value, "nounwind");
|
|
||||||
// lb_add_attribute_to_proc(p->module, p->value, "mustprogress");
|
|
||||||
// lb_add_attribute_to_proc(p->module, p->value, "nofree");
|
|
||||||
// lb_add_attribute_to_proc(p->module, p->value, "norecurse");
|
|
||||||
// lb_add_attribute_to_proc(p->module, p->value, "nosync");
|
|
||||||
// lb_add_attribute_to_proc(p->module, p->value, "willreturn");
|
|
||||||
if (!LB_USE_GIANT_PACKED_STRUCT) {
|
|
||||||
lb_add_attribute_to_proc(m, p->value, "optnone");
|
|
||||||
lb_add_attribute_to_proc(m, p->value, "noinline");
|
|
||||||
}
|
|
||||||
|
|
||||||
lb_begin_procedure_body(p);
|
|
||||||
|
|
||||||
lb_setup_type_info_data(p);
|
|
||||||
|
|
||||||
lb_end_procedure_body(p);
|
|
||||||
|
|
||||||
if (!m->debug_builder && LLVMVerifyFunction(p->value, LLVMReturnStatusAction)) {
|
|
||||||
gb_printf_err("LLVM CODE GEN FAILED FOR PROCEDURE: %s\n", "main");
|
|
||||||
LLVMDumpValue(p->value);
|
|
||||||
gb_printf_err("\n\n\n\n");
|
|
||||||
LLVMVerifyFunction(p->value, LLVMAbortProcessAction);
|
|
||||||
}
|
|
||||||
return p;
|
|
||||||
}
|
|
||||||
|
|
||||||
gb_internal lbProcedure *lb_create_objc_names(lbModule *main_module) {
|
gb_internal lbProcedure *lb_create_objc_names(lbModule *main_module) {
|
||||||
if (build_context.metrics.os != TargetOs_darwin) {
|
if (build_context.metrics.os != TargetOs_darwin) {
|
||||||
@@ -1129,7 +1094,7 @@ gb_internal void lb_finalize_objc_names(lbProcedure *p) {
|
|||||||
lb_end_procedure_body(p);
|
lb_end_procedure_body(p);
|
||||||
}
|
}
|
||||||
|
|
||||||
gb_internal lbProcedure *lb_create_startup_runtime(lbModule *main_module, lbProcedure *startup_type_info, lbProcedure *objc_names, Array<lbGlobalVariable> &global_variables) { // Startup Runtime
|
gb_internal lbProcedure *lb_create_startup_runtime(lbModule *main_module, lbProcedure *objc_names, Array<lbGlobalVariable> &global_variables) { // Startup Runtime
|
||||||
Type *proc_type = alloc_type_proc(nullptr, nullptr, 0, nullptr, 0, false, ProcCC_Odin);
|
Type *proc_type = alloc_type_proc(nullptr, nullptr, 0, nullptr, 0, false, ProcCC_Odin);
|
||||||
|
|
||||||
lbProcedure *p = lb_create_dummy_procedure(main_module, str_lit(LB_STARTUP_RUNTIME_PROC_NAME), proc_type);
|
lbProcedure *p = lb_create_dummy_procedure(main_module, str_lit(LB_STARTUP_RUNTIME_PROC_NAME), proc_type);
|
||||||
@@ -1139,9 +1104,7 @@ gb_internal lbProcedure *lb_create_startup_runtime(lbModule *main_module, lbProc
|
|||||||
|
|
||||||
lb_begin_procedure_body(p);
|
lb_begin_procedure_body(p);
|
||||||
|
|
||||||
if (startup_type_info) {
|
lb_setup_type_info_data(main_module);
|
||||||
LLVMBuildCall2(p->builder, lb_type_internal_for_procedures_raw(main_module, startup_type_info->type), startup_type_info->value, nullptr, 0, "");
|
|
||||||
}
|
|
||||||
|
|
||||||
if (objc_names) {
|
if (objc_names) {
|
||||||
LLVMBuildCall2(p->builder, lb_type_internal_for_procedures_raw(main_module, objc_names->type), objc_names->value, nullptr, 0, "");
|
LLVMBuildCall2(p->builder, lb_type_internal_for_procedures_raw(main_module, objc_names->type), objc_names->value, nullptr, 0, "");
|
||||||
@@ -1201,7 +1164,7 @@ gb_internal lbProcedure *lb_create_startup_runtime(lbModule *main_module, lbProc
|
|||||||
lbValue data = lb_emit_struct_ep(p, var.var, 0);
|
lbValue data = lb_emit_struct_ep(p, var.var, 0);
|
||||||
lbValue ti = lb_emit_struct_ep(p, var.var, 1);
|
lbValue ti = lb_emit_struct_ep(p, var.var, 1);
|
||||||
lb_emit_store(p, data, lb_emit_conv(p, gp, t_rawptr));
|
lb_emit_store(p, data, lb_emit_conv(p, gp, t_rawptr));
|
||||||
lb_emit_store(p, ti, lb_type_info(main_module, var_type));
|
lb_emit_store(p, ti, lb_type_info(p, var_type));
|
||||||
} else {
|
} else {
|
||||||
LLVMTypeRef vt = llvm_addr_type(p->module, var.var);
|
LLVMTypeRef vt = llvm_addr_type(p->module, var.var);
|
||||||
lbValue src0 = lb_emit_conv(p, var.init, t);
|
lbValue src0 = lb_emit_conv(p, var.init, t);
|
||||||
@@ -1426,7 +1389,6 @@ gb_internal WORKER_TASK_PROC(lb_llvm_function_pass_per_module) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (m == &m->gen->default_module) {
|
if (m == &m->gen->default_module) {
|
||||||
lb_llvm_function_pass_per_function_internal(m, m->gen->startup_type_info);
|
|
||||||
lb_llvm_function_pass_per_function_internal(m, m->gen->startup_runtime);
|
lb_llvm_function_pass_per_function_internal(m, m->gen->startup_runtime);
|
||||||
lb_llvm_function_pass_per_function_internal(m, m->gen->cleanup_runtime);
|
lb_llvm_function_pass_per_function_internal(m, m->gen->cleanup_runtime);
|
||||||
lb_llvm_function_pass_per_function_internal(m, m->gen->objc_names);
|
lb_llvm_function_pass_per_function_internal(m, m->gen->objc_names);
|
||||||
@@ -2691,17 +2653,19 @@ gb_internal bool lb_generate_code(lbGenerator *gen) {
|
|||||||
|
|
||||||
{ // Add type info data
|
{ // Add type info data
|
||||||
isize max_type_info_count = info->minimum_dependency_type_info_set.count+1;
|
isize max_type_info_count = info->minimum_dependency_type_info_set.count+1;
|
||||||
Type *t = alloc_type_array(t_type_info, max_type_info_count);
|
Type *t = alloc_type_array(t_type_info_ptr, max_type_info_count);
|
||||||
|
|
||||||
// IMPORTANT NOTE(bill): As LLVM does not have a union type, an array of unions cannot be initialized
|
// IMPORTANT NOTE(bill): As LLVM does not have a union type, an array of unions cannot be initialized
|
||||||
// at compile time without cheating in some way. This means to emulate an array of unions is to use
|
// at compile time without cheating in some way. This means to emulate an array of unions is to use
|
||||||
// a giant packed struct of "corrected" data types.
|
// a giant packed struct of "corrected" data types.
|
||||||
|
|
||||||
LLVMTypeRef internal_llvm_type = lb_setup_type_info_data_internal_type(m, max_type_info_count);
|
LLVMTypeRef internal_llvm_type = lb_type(m, t);
|
||||||
|
|
||||||
LLVMValueRef g = LLVMAddGlobal(m->mod, internal_llvm_type, LB_TYPE_INFO_DATA_NAME);
|
LLVMValueRef g = LLVMAddGlobal(m->mod, internal_llvm_type, LB_TYPE_INFO_DATA_NAME);
|
||||||
LLVMSetInitializer(g, LLVMConstNull(internal_llvm_type));
|
LLVMSetInitializer(g, LLVMConstNull(internal_llvm_type));
|
||||||
LLVMSetLinkage(g, USE_SEPARATE_MODULES ? LLVMExternalLinkage : LLVMInternalLinkage);
|
LLVMSetLinkage(g, USE_SEPARATE_MODULES ? LLVMExternalLinkage : LLVMInternalLinkage);
|
||||||
|
LLVMSetUnnamedAddress(g, LLVMGlobalUnnamedAddr);
|
||||||
|
LLVMSetGlobalConstant(g, /*true*/false);
|
||||||
|
|
||||||
lbValue value = {};
|
lbValue value = {};
|
||||||
value.value = g;
|
value.value = g;
|
||||||
@@ -2710,11 +2674,6 @@ gb_internal bool lb_generate_code(lbGenerator *gen) {
|
|||||||
lb_global_type_info_data_entity = alloc_entity_variable(nullptr, make_token_ident(LB_TYPE_INFO_DATA_NAME), t, EntityState_Resolved);
|
lb_global_type_info_data_entity = alloc_entity_variable(nullptr, make_token_ident(LB_TYPE_INFO_DATA_NAME), t, EntityState_Resolved);
|
||||||
lb_add_entity(m, lb_global_type_info_data_entity, value);
|
lb_add_entity(m, lb_global_type_info_data_entity, value);
|
||||||
|
|
||||||
if (LB_USE_GIANT_PACKED_STRUCT) {
|
|
||||||
LLVMSetLinkage(g, LLVMPrivateLinkage);
|
|
||||||
LLVMSetUnnamedAddress(g, LLVMGlobalUnnamedAddr);
|
|
||||||
LLVMSetGlobalConstant(g, /*true*/false);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
{ // Type info member buffer
|
{ // Type info member buffer
|
||||||
// NOTE(bill): Removes need for heap allocation by making it global memory
|
// NOTE(bill): Removes need for heap allocation by making it global memory
|
||||||
@@ -2750,9 +2709,7 @@ gb_internal bool lb_generate_code(lbGenerator *gen) {
|
|||||||
LLVMValueRef g = LLVMAddGlobal(m->mod, lb_type(m, t), name);
|
LLVMValueRef g = LLVMAddGlobal(m->mod, lb_type(m, t), name);
|
||||||
LLVMSetInitializer(g, LLVMConstNull(lb_type(m, t)));
|
LLVMSetInitializer(g, LLVMConstNull(lb_type(m, t)));
|
||||||
LLVMSetLinkage(g, LLVMInternalLinkage);
|
LLVMSetLinkage(g, LLVMInternalLinkage);
|
||||||
if (LB_USE_GIANT_PACKED_STRUCT) {
|
|
||||||
lb_make_global_private_const(g);
|
lb_make_global_private_const(g);
|
||||||
}
|
|
||||||
return lb_addr({g, alloc_type_pointer(t)});
|
return lb_addr({g, alloc_type_pointer(t)});
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -2921,12 +2878,11 @@ gb_internal bool lb_generate_code(lbGenerator *gen) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
TIME_SECTION("LLVM Runtime Type Information Creation");
|
TIME_SECTION("LLVM Runtime Objective-C Names Creation");
|
||||||
gen->startup_type_info = lb_create_startup_type_info(default_module);
|
|
||||||
gen->objc_names = lb_create_objc_names(default_module);
|
gen->objc_names = lb_create_objc_names(default_module);
|
||||||
|
|
||||||
TIME_SECTION("LLVM Runtime Startup Creation (Global Variables & @(init))");
|
TIME_SECTION("LLVM Runtime Startup Creation (Global Variables & @(init))");
|
||||||
gen->startup_runtime = lb_create_startup_runtime(default_module, gen->startup_type_info, gen->objc_names, global_variables);
|
gen->startup_runtime = lb_create_startup_runtime(default_module, gen->objc_names, global_variables);
|
||||||
|
|
||||||
TIME_SECTION("LLVM Runtime Cleanup Creation & @(fini)");
|
TIME_SECTION("LLVM Runtime Cleanup Creation & @(fini)");
|
||||||
gen->cleanup_runtime = lb_create_cleanup_runtime(default_module);
|
gen->cleanup_runtime = lb_create_cleanup_runtime(default_module);
|
||||||
|
|||||||
@@ -225,7 +225,6 @@ struct lbGenerator : LinkerData {
|
|||||||
std::atomic<u32> global_array_index;
|
std::atomic<u32> global_array_index;
|
||||||
std::atomic<u32> global_generated_index;
|
std::atomic<u32> global_generated_index;
|
||||||
|
|
||||||
lbProcedure *startup_type_info;
|
|
||||||
lbProcedure *startup_runtime;
|
lbProcedure *startup_runtime;
|
||||||
lbProcedure *cleanup_runtime;
|
lbProcedure *cleanup_runtime;
|
||||||
lbProcedure *objc_names;
|
lbProcedure *objc_names;
|
||||||
@@ -486,7 +485,7 @@ gb_internal lbValue lb_emit_mul_add(lbProcedure *p, lbValue a, lbValue b, lbValu
|
|||||||
|
|
||||||
gb_internal void lb_fill_slice(lbProcedure *p, lbAddr const &slice, lbValue base_elem, lbValue len);
|
gb_internal void lb_fill_slice(lbProcedure *p, lbAddr const &slice, lbValue base_elem, lbValue len);
|
||||||
|
|
||||||
gb_internal lbValue lb_type_info(lbModule *m, Type *type);
|
gb_internal lbValue lb_type_info(lbProcedure *p, Type *type);
|
||||||
|
|
||||||
gb_internal lbValue lb_find_or_add_entity_string(lbModule *m, String const &str);
|
gb_internal lbValue lb_find_or_add_entity_string(lbModule *m, String const &str);
|
||||||
gb_internal lbValue lb_generate_anonymous_proc_lit(lbModule *m, String const &prefix_name, Ast *expr, lbProcedure *parent = nullptr);
|
gb_internal lbValue lb_generate_anonymous_proc_lit(lbModule *m, String const &prefix_name, Ast *expr, lbProcedure *parent = nullptr);
|
||||||
@@ -587,7 +586,6 @@ gb_internal LLVMTypeRef llvm_array_type(LLVMTypeRef ElementType, uint64_t Elemen
|
|||||||
|
|
||||||
#define LB_STARTUP_RUNTIME_PROC_NAME "__$startup_runtime"
|
#define LB_STARTUP_RUNTIME_PROC_NAME "__$startup_runtime"
|
||||||
#define LB_CLEANUP_RUNTIME_PROC_NAME "__$cleanup_runtime"
|
#define LB_CLEANUP_RUNTIME_PROC_NAME "__$cleanup_runtime"
|
||||||
#define LB_STARTUP_TYPE_INFO_PROC_NAME "__$startup_type_info"
|
|
||||||
#define LB_TYPE_INFO_DATA_NAME "__$type_info_data"
|
#define LB_TYPE_INFO_DATA_NAME "__$type_info_data"
|
||||||
#define LB_TYPE_INFO_TYPES_NAME "__$type_info_types_data"
|
#define LB_TYPE_INFO_TYPES_NAME "__$type_info_types_data"
|
||||||
#define LB_TYPE_INFO_NAMES_NAME "__$type_info_names_data"
|
#define LB_TYPE_INFO_NAMES_NAME "__$type_info_names_data"
|
||||||
|
|||||||
@@ -746,8 +746,8 @@ gb_internal void lb_debug_complete_types(lbModule *m) {
|
|||||||
|
|
||||||
case Type_Map:
|
case Type_Map:
|
||||||
GB_ASSERT(t_raw_map != nullptr);
|
GB_ASSERT(t_raw_map != nullptr);
|
||||||
bt = base_type(bt->Map.debug_metadata_type);
|
// bt = base_type(bt->Map.debug_metadata_type);
|
||||||
// bt = base_type(t_raw_map);
|
bt = base_type(t_raw_map);
|
||||||
GB_ASSERT(bt->kind == Type_Struct);
|
GB_ASSERT(bt->kind == Type_Struct);
|
||||||
/*fallthrough*/
|
/*fallthrough*/
|
||||||
case Type_Struct:
|
case Type_Struct:
|
||||||
|
|||||||
@@ -1755,7 +1755,7 @@ gb_internal lbValue lb_build_builtin_proc(lbProcedure *p, Ast *expr, TypeAndValu
|
|||||||
TypeAndValue tav = type_and_value_of_expr(arg);
|
TypeAndValue tav = type_and_value_of_expr(arg);
|
||||||
if (tav.mode == Addressing_Type) {
|
if (tav.mode == Addressing_Type) {
|
||||||
Type *t = default_type(type_of_expr(arg));
|
Type *t = default_type(type_of_expr(arg));
|
||||||
return lb_type_info(p->module, t);
|
return lb_type_info(p, t);
|
||||||
}
|
}
|
||||||
GB_ASSERT(is_type_typeid(tav.type));
|
GB_ASSERT(is_type_typeid(tav.type));
|
||||||
|
|
||||||
@@ -3361,9 +3361,9 @@ gb_internal lbValue lb_build_call_expr_internal(lbProcedure *p, Ast *expr) {
|
|||||||
for (Ast *var_arg : variadic) {
|
for (Ast *var_arg : variadic) {
|
||||||
lbValue arg = lb_build_expr(p, var_arg);
|
lbValue arg = lb_build_expr(p, var_arg);
|
||||||
if (is_type_any(elem_type)) {
|
if (is_type_any(elem_type)) {
|
||||||
array_add(&args, lb_emit_conv(p, arg, default_type(arg.type)));
|
array_add(&args, lb_emit_conv(p, arg, c_vararg_promote_type(default_type(arg.type))));
|
||||||
} else {
|
} else {
|
||||||
array_add(&args, lb_emit_conv(p, arg, elem_type));
|
array_add(&args, lb_emit_conv(p, arg, c_vararg_promote_type(elem_type)));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
|||||||
@@ -748,7 +748,7 @@ gb_internal void lb_build_range_enum(lbProcedure *p, Type *enum_type, Type *val_
|
|||||||
i64 enum_count = t->Enum.fields.count;
|
i64 enum_count = t->Enum.fields.count;
|
||||||
lbValue max_count = lb_const_int(m, t_int, enum_count);
|
lbValue max_count = lb_const_int(m, t_int, enum_count);
|
||||||
|
|
||||||
lbValue ti = lb_type_info(m, t);
|
lbValue ti = lb_type_info(p, t);
|
||||||
lbValue variant = lb_emit_struct_ep(p, ti, 4);
|
lbValue variant = lb_emit_struct_ep(p, ti, 4);
|
||||||
lbValue eti_ptr = lb_emit_conv(p, variant, t_type_info_enum_ptr);
|
lbValue eti_ptr = lb_emit_conv(p, variant, t_type_info_enum_ptr);
|
||||||
lbValue values = lb_emit_load(p, lb_emit_struct_ep(p, eti_ptr, 2));
|
lbValue values = lb_emit_load(p, lb_emit_struct_ep(p, eti_ptr, 2));
|
||||||
|
|||||||
+149
-897
File diff suppressed because it is too large
Load Diff
@@ -365,6 +365,9 @@ enum Typeid_Kind : u8 {
|
|||||||
Typeid_Matrix,
|
Typeid_Matrix,
|
||||||
Typeid_SoaPointer,
|
Typeid_SoaPointer,
|
||||||
Typeid_Bit_Field,
|
Typeid_Bit_Field,
|
||||||
|
|
||||||
|
Typeid__COUNT
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
// IMPORTANT NOTE(bill): This must match the same as the in core.odin
|
// IMPORTANT NOTE(bill): This must match the same as the in core.odin
|
||||||
@@ -562,6 +565,14 @@ gb_global Type *t_f16 = &basic_types[Basic_f16];
|
|||||||
gb_global Type *t_f32 = &basic_types[Basic_f32];
|
gb_global Type *t_f32 = &basic_types[Basic_f32];
|
||||||
gb_global Type *t_f64 = &basic_types[Basic_f64];
|
gb_global Type *t_f64 = &basic_types[Basic_f64];
|
||||||
|
|
||||||
|
gb_global Type *t_f16be = &basic_types[Basic_f16be];
|
||||||
|
gb_global Type *t_f32be = &basic_types[Basic_f32be];
|
||||||
|
gb_global Type *t_f64be = &basic_types[Basic_f64be];
|
||||||
|
|
||||||
|
gb_global Type *t_f16le = &basic_types[Basic_f16le];
|
||||||
|
gb_global Type *t_f32le = &basic_types[Basic_f32le];
|
||||||
|
gb_global Type *t_f64le = &basic_types[Basic_f64le];
|
||||||
|
|
||||||
gb_global Type *t_complex32 = &basic_types[Basic_complex32];
|
gb_global Type *t_complex32 = &basic_types[Basic_complex32];
|
||||||
gb_global Type *t_complex64 = &basic_types[Basic_complex64];
|
gb_global Type *t_complex64 = &basic_types[Basic_complex64];
|
||||||
gb_global Type *t_complex128 = &basic_types[Basic_complex128];
|
gb_global Type *t_complex128 = &basic_types[Basic_complex128];
|
||||||
@@ -2389,6 +2400,9 @@ gb_internal bool is_type_comparable(Type *t) {
|
|||||||
|
|
||||||
case Type_SimdVector:
|
case Type_SimdVector:
|
||||||
return true;
|
return true;
|
||||||
|
|
||||||
|
case Type_BitField:
|
||||||
|
return is_type_comparable(t->BitField.backing_type);
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@@ -2796,6 +2810,29 @@ gb_internal bool are_types_identical_internal(Type *x, Type *y, bool check_tuple
|
|||||||
return are_types_identical(x->SimdVector.elem, y->SimdVector.elem);
|
return are_types_identical(x->SimdVector.elem, y->SimdVector.elem);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
case Type_BitField:
|
||||||
|
if (are_types_identical(x->BitField.backing_type, y->BitField.backing_type) &&
|
||||||
|
x->BitField.fields.count == y->BitField.fields.count) {
|
||||||
|
for_array(i, x->BitField.fields) {
|
||||||
|
Entity *a = x->BitField.fields[i];
|
||||||
|
Entity *b = y->BitField.fields[i];
|
||||||
|
if (!are_types_identical(a->type, b->type)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if (a->token.string != b->token.string) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if (x->BitField.bit_sizes[i] != y->BitField.bit_sizes[i]) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if (x->BitField.bit_offsets[i] != y->BitField.bit_offsets[i]) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
@@ -2819,6 +2856,49 @@ gb_internal Type *default_type(Type *type) {
|
|||||||
return type;
|
return type;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// See https://en.cppreference.com/w/c/language/conversion#Default_argument_promotions
|
||||||
|
gb_internal Type *c_vararg_promote_type(Type *type) {
|
||||||
|
GB_ASSERT(type != nullptr);
|
||||||
|
|
||||||
|
Type *core = core_type(type);
|
||||||
|
|
||||||
|
if (core->kind == Type_BitSet) {
|
||||||
|
core = core_type(bit_set_to_int(core));
|
||||||
|
}
|
||||||
|
|
||||||
|
if (core->kind == Type_Basic) {
|
||||||
|
switch (core->Basic.kind) {
|
||||||
|
case Basic_f32:
|
||||||
|
case Basic_UntypedFloat:
|
||||||
|
return t_f64;
|
||||||
|
case Basic_f32le:
|
||||||
|
return t_f64le;
|
||||||
|
case Basic_f32be:
|
||||||
|
return t_f64be;
|
||||||
|
|
||||||
|
case Basic_UntypedBool:
|
||||||
|
case Basic_bool:
|
||||||
|
case Basic_b8:
|
||||||
|
case Basic_b16:
|
||||||
|
case Basic_i8:
|
||||||
|
case Basic_i16:
|
||||||
|
case Basic_u8:
|
||||||
|
case Basic_u16:
|
||||||
|
return t_i32;
|
||||||
|
|
||||||
|
case Basic_i16le:
|
||||||
|
case Basic_u16le:
|
||||||
|
return t_i32le;
|
||||||
|
|
||||||
|
case Basic_i16be:
|
||||||
|
case Basic_u16be:
|
||||||
|
return t_i32be;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return type;
|
||||||
|
}
|
||||||
|
|
||||||
gb_internal bool union_variant_index_types_equal(Type *v, Type *vt) {
|
gb_internal bool union_variant_index_types_equal(Type *v, Type *vt) {
|
||||||
if (are_types_identical(v, vt)) {
|
if (are_types_identical(v, vt)) {
|
||||||
return true;
|
return true;
|
||||||
|
|||||||
+28
-22
@@ -1,8 +1,11 @@
|
|||||||
ODIN=../../odin
|
ODIN=../../odin
|
||||||
PYTHON=$(shell which python3)
|
PYTHON=$(shell which python3)
|
||||||
|
COMMON=-vet -strict-style
|
||||||
|
COLLECTION=-collection:tests=..
|
||||||
|
|
||||||
all: c_libc_test \
|
all: c_libc_test \
|
||||||
compress_test \
|
compress_test \
|
||||||
|
container_test \
|
||||||
crypto_test \
|
crypto_test \
|
||||||
download_test_assets \
|
download_test_assets \
|
||||||
encoding_test \
|
encoding_test \
|
||||||
@@ -27,64 +30,67 @@ download_test_assets:
|
|||||||
$(PYTHON) download_assets.py
|
$(PYTHON) download_assets.py
|
||||||
|
|
||||||
image_test:
|
image_test:
|
||||||
$(ODIN) run image/test_core_image.odin -file -out:test_core_image
|
$(ODIN) run image $(COMMON) -out:test_core_image
|
||||||
|
|
||||||
compress_test:
|
compress_test:
|
||||||
$(ODIN) run compress/test_core_compress.odin -file -out:test_core_compress
|
$(ODIN) run compress $(COMMON) -out:test_core_compress
|
||||||
|
|
||||||
|
container_test:
|
||||||
|
$(ODIN) run container $(COMMON) $(COLLECTION) -out:test_core_container
|
||||||
|
|
||||||
strings_test:
|
strings_test:
|
||||||
$(ODIN) run strings/test_core_strings.odin -file -out:test_core_strings
|
$(ODIN) run strings $(COMMON) -out:test_core_strings
|
||||||
|
|
||||||
hash_test:
|
hash_test:
|
||||||
$(ODIN) run hash -o:speed -no-bounds-check -out:test_hash
|
$(ODIN) run hash $(COMMON) -o:speed -no-bounds-check -out:test_hash
|
||||||
|
|
||||||
crypto_test:
|
crypto_test:
|
||||||
$(ODIN) run crypto -o:speed -no-bounds-check -out:test_crypto
|
$(ODIN) run crypto $(COMMON) -o:speed -no-bounds-check -out:test_crypto
|
||||||
|
|
||||||
noise_test:
|
noise_test:
|
||||||
$(ODIN) run math/noise -out:test_noise
|
$(ODIN) run math/noise $(COMMON) -out:test_noise
|
||||||
|
|
||||||
encoding_test:
|
encoding_test:
|
||||||
$(ODIN) run encoding/hxa -out:test_hxa -collection:tests=..
|
$(ODIN) run encoding/hxa $(COMMON) $(COLLECTION) -out:test_hxa
|
||||||
$(ODIN) run encoding/json -out:test_json
|
$(ODIN) run encoding/json $(COMMON) -out:test_json
|
||||||
$(ODIN) run encoding/varint -out:test_varint
|
$(ODIN) run encoding/varint $(COMMON) -out:test_varint
|
||||||
$(ODIN) run encoding/xml -out:test_xml
|
$(ODIN) run encoding/xml $(COMMON) -out:test_xml
|
||||||
|
|
||||||
math_test:
|
math_test:
|
||||||
$(ODIN) run math/test_core_math.odin -file -collection:tests=.. -out:test_core_math
|
$(ODIN) run math $(COMMON) $(COLLECTION) -out:test_core_math
|
||||||
|
|
||||||
linalg_glsl_math_test:
|
linalg_glsl_math_test:
|
||||||
$(ODIN) run math/linalg/glsl/test_linalg_glsl_math.odin -file -collection:tests=.. -out:test_linalg_glsl_math
|
$(ODIN) run math/linalg/glsl $(COMMON) $(COLLECTION) -out:test_linalg_glsl_math
|
||||||
|
|
||||||
filepath_test:
|
filepath_test:
|
||||||
$(ODIN) run path/filepath/test_core_filepath.odin -file -collection:tests=.. -out:test_core_filepath
|
$(ODIN) run path/filepath $(COMMON) $(COLLECTION) -out:test_core_filepath
|
||||||
|
|
||||||
reflect_test:
|
reflect_test:
|
||||||
$(ODIN) run reflect/test_core_reflect.odin -file -collection:tests=.. -out:test_core_reflect
|
$(ODIN) run reflect $(COMMON) $(COLLECTION) -out:test_core_reflect
|
||||||
|
|
||||||
slice_test:
|
slice_test:
|
||||||
$(ODIN) run slice/test_core_slice.odin -file -out:test_core_slice
|
$(ODIN) run slice $(COMMON) -out:test_core_slice
|
||||||
|
|
||||||
os_exit_test:
|
os_exit_test:
|
||||||
$(ODIN) run os/test_core_os_exit.odin -file -out:test_core_os_exit && exit 1 || exit 0
|
$(ODIN) run os/test_core_os_exit.odin -file -out:test_core_os_exit && exit 1 || exit 0
|
||||||
|
|
||||||
i18n_test:
|
i18n_test:
|
||||||
$(ODIN) run text/i18n -out:test_core_i18n
|
$(ODIN) run text/i18n $(COMMON) -out:test_core_i18n
|
||||||
|
|
||||||
match_test:
|
match_test:
|
||||||
$(ODIN) run text/match -out:test_core_match
|
$(ODIN) run text/match $(COMMON) -out:test_core_match
|
||||||
|
|
||||||
c_libc_test:
|
c_libc_test:
|
||||||
$(ODIN) run c/libc -out:test_core_libc
|
$(ODIN) run c/libc $(COMMON) -out:test_core_libc
|
||||||
|
|
||||||
net_test:
|
net_test:
|
||||||
$(ODIN) run net -out:test_core_net
|
$(ODIN) run net $(COMMON) -out:test_core_net
|
||||||
|
|
||||||
fmt_test:
|
fmt_test:
|
||||||
$(ODIN) run fmt -out:test_core_fmt
|
$(ODIN) run fmt $(COMMON) -out:test_core_fmt
|
||||||
|
|
||||||
thread_test:
|
thread_test:
|
||||||
$(ODIN) run thread -out:test_core_thread
|
$(ODIN) run thread $(COMMON) -out:test_core_thread
|
||||||
|
|
||||||
runtime_test:
|
runtime_test:
|
||||||
$(ODIN) run runtime -out:test_core_runtime
|
$(ODIN) run runtime $(COMMON) -out:test_core_runtime
|
||||||
|
|||||||
@@ -2,7 +2,6 @@ package test_core_libc
|
|||||||
|
|
||||||
import "core:fmt"
|
import "core:fmt"
|
||||||
import "core:os"
|
import "core:os"
|
||||||
import "core:strings"
|
|
||||||
import "core:testing"
|
import "core:testing"
|
||||||
|
|
||||||
TEST_count := 0
|
TEST_count := 0
|
||||||
|
|||||||
@@ -0,0 +1,161 @@
|
|||||||
|
package test_core_container
|
||||||
|
|
||||||
|
import "core:container/avl"
|
||||||
|
import "core:math/rand"
|
||||||
|
import "core:slice"
|
||||||
|
import "core:testing"
|
||||||
|
|
||||||
|
import tc "tests:common"
|
||||||
|
|
||||||
|
@(test)
|
||||||
|
test_avl :: proc(t: ^testing.T) {
|
||||||
|
tc.log(t, "Testing avl")
|
||||||
|
|
||||||
|
// Initialization.
|
||||||
|
tree: avl.Tree(int)
|
||||||
|
avl.init(&tree, slice.cmp_proc(int))
|
||||||
|
tc.expect(t, avl.len(&tree) == 0, "empty: len should be 0")
|
||||||
|
tc.expect(t, avl.first(&tree) == nil, "empty: first should be nil")
|
||||||
|
tc.expect(t, avl.last(&tree) == nil, "empty: last should be nil")
|
||||||
|
|
||||||
|
iter := avl.iterator(&tree, avl.Direction.Forward)
|
||||||
|
tc.expect(t, avl.iterator_get(&iter) == nil, "empty/iterator: first node should be nil")
|
||||||
|
|
||||||
|
// Test insertion.
|
||||||
|
NR_INSERTS :: 32 + 1 // Ensure at least 1 collision.
|
||||||
|
inserted_map := make(map[int]^avl.Node(int))
|
||||||
|
for i := 0; i < NR_INSERTS; i += 1 {
|
||||||
|
v := int(rand.uint32() & 0x1f)
|
||||||
|
existing_node, in_map := inserted_map[v]
|
||||||
|
|
||||||
|
n, ok, _ := avl.find_or_insert(&tree, v)
|
||||||
|
tc.expect(t, in_map != ok, "insert: ok should match inverse of map lookup")
|
||||||
|
if ok {
|
||||||
|
inserted_map[v] = n
|
||||||
|
} else {
|
||||||
|
tc.expect(t, existing_node == n, "insert: expecting existing node")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
nrEntries := len(inserted_map)
|
||||||
|
tc.expect(t, avl.len(&tree) == nrEntries, "insert: len after")
|
||||||
|
tree_validate(t, &tree)
|
||||||
|
|
||||||
|
// Ensure that all entries can be found.
|
||||||
|
for k, v in inserted_map {
|
||||||
|
tc.expect(t, v == avl.find(&tree, k), "Find(): Node")
|
||||||
|
tc.expect(t, k == v.value, "Find(): Node value")
|
||||||
|
}
|
||||||
|
|
||||||
|
// Test the forward/backward iterators.
|
||||||
|
inserted_values: [dynamic]int
|
||||||
|
for k in inserted_map {
|
||||||
|
append(&inserted_values, k)
|
||||||
|
}
|
||||||
|
slice.sort(inserted_values[:])
|
||||||
|
|
||||||
|
iter = avl.iterator(&tree, avl.Direction.Forward)
|
||||||
|
visited: int
|
||||||
|
for node in avl.iterator_next(&iter) {
|
||||||
|
v, idx := node.value, visited
|
||||||
|
tc.expect(t, inserted_values[idx] == v, "iterator/forward: value")
|
||||||
|
tc.expect(t, node == avl.iterator_get(&iter), "iterator/forward: get")
|
||||||
|
visited += 1
|
||||||
|
}
|
||||||
|
tc.expect(t, visited == nrEntries, "iterator/forward: visited")
|
||||||
|
|
||||||
|
slice.reverse(inserted_values[:])
|
||||||
|
iter = avl.iterator(&tree, avl.Direction.Backward)
|
||||||
|
visited = 0
|
||||||
|
for node in avl.iterator_next(&iter) {
|
||||||
|
v, idx := node.value, visited
|
||||||
|
tc.expect(t, inserted_values[idx] == v, "iterator/backward: value")
|
||||||
|
visited += 1
|
||||||
|
}
|
||||||
|
tc.expect(t, visited == nrEntries, "iterator/backward: visited")
|
||||||
|
|
||||||
|
// Test removal.
|
||||||
|
rand.shuffle(inserted_values[:])
|
||||||
|
for v, i in inserted_values {
|
||||||
|
node := avl.find(&tree, v)
|
||||||
|
tc.expect(t, node != nil, "remove: find (pre)")
|
||||||
|
|
||||||
|
ok := avl.remove(&tree, v)
|
||||||
|
tc.expect(t, ok, "remove: succeeds")
|
||||||
|
tc.expect(t, nrEntries - (i + 1) == avl.len(&tree), "remove: len (post)")
|
||||||
|
tree_validate(t, &tree)
|
||||||
|
|
||||||
|
tc.expect(t, nil == avl.find(&tree, v), "remove: find (post")
|
||||||
|
}
|
||||||
|
tc.expect(t, avl.len(&tree) == 0, "remove: len should be 0")
|
||||||
|
tc.expect(t, avl.first(&tree) == nil, "remove: first should be nil")
|
||||||
|
tc.expect(t, avl.last(&tree) == nil, "remove: last should be nil")
|
||||||
|
|
||||||
|
// Refill the tree.
|
||||||
|
for v in inserted_values {
|
||||||
|
avl.find_or_insert(&tree, v)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Test that removing the node doesn't break the iterator.
|
||||||
|
iter = avl.iterator(&tree, avl.Direction.Forward)
|
||||||
|
if node := avl.iterator_get(&iter); node != nil {
|
||||||
|
v := node.value
|
||||||
|
|
||||||
|
ok := avl.iterator_remove(&iter)
|
||||||
|
tc.expect(t, ok, "iterator/remove: success")
|
||||||
|
|
||||||
|
ok = avl.iterator_remove(&iter)
|
||||||
|
tc.expect(t, !ok, "iterator/remove: redundant removes should fail")
|
||||||
|
|
||||||
|
tc.expect(t, avl.find(&tree, v) == nil, "iterator/remove: node should be gone")
|
||||||
|
tc.expect(t, avl.iterator_get(&iter) == nil, "iterator/remove: get should return nil")
|
||||||
|
|
||||||
|
// Ensure that iterator_next still works.
|
||||||
|
node, ok = avl.iterator_next(&iter)
|
||||||
|
tc.expect(t, ok == (avl.len(&tree) > 0), "iterator/remove: next should return false")
|
||||||
|
tc.expect(t, node == avl.first(&tree), "iterator/remove: next should return first")
|
||||||
|
|
||||||
|
tree_validate(t, &tree)
|
||||||
|
}
|
||||||
|
tc.expect(t, avl.len(&tree) == nrEntries - 1, "iterator/remove: len should drop by 1")
|
||||||
|
|
||||||
|
avl.destroy(&tree)
|
||||||
|
tc.expect(t, avl.len(&tree) == 0, "destroy: len should be 0")
|
||||||
|
}
|
||||||
|
|
||||||
|
@(private)
|
||||||
|
tree_validate :: proc(t: ^testing.T, tree: ^avl.Tree($Value)) {
|
||||||
|
tree_check_invariants(t, tree, tree._root, nil)
|
||||||
|
}
|
||||||
|
|
||||||
|
@(private)
|
||||||
|
tree_check_invariants :: proc(
|
||||||
|
t: ^testing.T,
|
||||||
|
tree: ^avl.Tree($Value),
|
||||||
|
node, parent: ^avl.Node(Value),
|
||||||
|
) -> int {
|
||||||
|
if node == nil {
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
|
||||||
|
// Validate the parent pointer.
|
||||||
|
tc.expect(t, parent == node._parent, "invalid parent pointer")
|
||||||
|
|
||||||
|
// Validate that the balance factor is -1, 0, 1.
|
||||||
|
tc.expect(
|
||||||
|
t,
|
||||||
|
node._balance == -1 || node._balance == 0 || node._balance == 1,
|
||||||
|
"invalid balance factor",
|
||||||
|
)
|
||||||
|
|
||||||
|
// Recursively derive the height of the left and right sub-trees.
|
||||||
|
l_height := tree_check_invariants(t, tree, node._left, node)
|
||||||
|
r_height := tree_check_invariants(t, tree, node._right, node)
|
||||||
|
|
||||||
|
// Validate the AVL invariant and the balance factor.
|
||||||
|
tc.expect(t, int(node._balance) == r_height - l_height, "AVL balance factor invariant violated")
|
||||||
|
if l_height > r_height {
|
||||||
|
return l_height + 1
|
||||||
|
}
|
||||||
|
|
||||||
|
return r_height + 1
|
||||||
|
}
|
||||||
@@ -0,0 +1,26 @@
|
|||||||
|
package test_core_container
|
||||||
|
|
||||||
|
import "core:fmt"
|
||||||
|
import "core:testing"
|
||||||
|
|
||||||
|
import tc "tests:common"
|
||||||
|
|
||||||
|
expect_equal :: proc(t: ^testing.T, the_slice, expected: []int, loc := #caller_location) {
|
||||||
|
_eq :: proc(a, b: []int) -> bool {
|
||||||
|
if len(a) != len(b) do return false
|
||||||
|
for a, i in a {
|
||||||
|
if b[i] != a do return false
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
tc.expect(t, _eq(the_slice, expected), fmt.tprintf("Expected %v, got %v\n", the_slice, expected), loc)
|
||||||
|
}
|
||||||
|
|
||||||
|
main :: proc() {
|
||||||
|
t := testing.T{}
|
||||||
|
|
||||||
|
test_avl(&t)
|
||||||
|
test_small_array(&t)
|
||||||
|
|
||||||
|
tc.report(&t)
|
||||||
|
}
|
||||||
@@ -1,29 +1,19 @@
|
|||||||
package test_core_compress
|
package test_core_container
|
||||||
|
|
||||||
import "core:fmt"
|
|
||||||
import "core:testing"
|
import "core:testing"
|
||||||
import "core:container/small_array"
|
import "core:container/small_array"
|
||||||
|
|
||||||
import tc "tests:common"
|
import tc "tests:common"
|
||||||
|
|
||||||
main :: proc() {
|
@(test)
|
||||||
t := testing.T{}
|
test_small_array :: proc(t: ^testing.T) {
|
||||||
test_small_array_removes(&t)
|
tc.log(t, "Testing small_array")
|
||||||
test_small_array_inject_at(&t)
|
|
||||||
tc.report(&t)
|
test_small_array_removes(t)
|
||||||
|
test_small_array_inject_at(t)
|
||||||
}
|
}
|
||||||
|
|
||||||
expect_equal :: proc(t: ^testing.T, the_slice, expected: []int, loc := #caller_location) {
|
@(test)
|
||||||
_eq :: proc(a, b: []int) -> bool {
|
|
||||||
if len(a) != len(b) do return false
|
|
||||||
for a, i in a {
|
|
||||||
if b[i] != a do return false
|
|
||||||
}
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
tc.expect(t, _eq(the_slice, expected), fmt.tprintf("Expected %v, got %v\n", the_slice, expected), loc)
|
|
||||||
}
|
|
||||||
|
|
||||||
@test
|
|
||||||
test_small_array_removes :: proc(t: ^testing.T) {
|
test_small_array_removes :: proc(t: ^testing.T) {
|
||||||
array: small_array.Small_Array(10, int)
|
array: small_array.Small_Array(10, int)
|
||||||
small_array.append(&array, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9)
|
small_array.append(&array, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9)
|
||||||
@@ -42,7 +32,7 @@ test_small_array_removes :: proc(t: ^testing.T) {
|
|||||||
expect_equal(t, small_array.slice(&array), []int { 9, 2, 7, 4 })
|
expect_equal(t, small_array.slice(&array), []int { 9, 2, 7, 4 })
|
||||||
}
|
}
|
||||||
|
|
||||||
@test
|
@(test)
|
||||||
test_small_array_inject_at :: proc(t: ^testing.T) {
|
test_small_array_inject_at :: proc(t: ^testing.T) {
|
||||||
array: small_array.Small_Array(13, int)
|
array: small_array.Small_Array(13, int)
|
||||||
small_array.append(&array, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9)
|
small_array.append(&array, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9)
|
||||||
|
|||||||
@@ -19,6 +19,10 @@ main :: proc() {
|
|||||||
test_trunc_f32(&t)
|
test_trunc_f32(&t)
|
||||||
test_trunc_f64(&t)
|
test_trunc_f64(&t)
|
||||||
|
|
||||||
|
test_round_f16(&t)
|
||||||
|
test_round_f32(&t)
|
||||||
|
test_round_f64(&t)
|
||||||
|
|
||||||
test_nan(&t)
|
test_nan(&t)
|
||||||
test_acos(&t)
|
test_acos(&t)
|
||||||
test_acosh(&t)
|
test_acosh(&t)
|
||||||
@@ -307,6 +311,183 @@ test_trunc_f64 :: proc(t: ^testing.T) {
|
|||||||
tc.expect(t, math.is_nan_f64(r), fmt.tprintf("%s(%f) -> %f != NaN", #procedure, v, r))
|
tc.expect(t, math.is_nan_f64(r), fmt.tprintf("%s(%f) -> %f != NaN", #procedure, v, r))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@test
|
||||||
|
test_round_f16 :: proc(t: ^testing.T) {
|
||||||
|
r, v: f16
|
||||||
|
|
||||||
|
Datum :: struct {
|
||||||
|
i: int,
|
||||||
|
v: f16,
|
||||||
|
e: f16,
|
||||||
|
}
|
||||||
|
@static data := []Datum{
|
||||||
|
{ 0, 10.5, 11 },
|
||||||
|
{ 1, -10.5, -11 },
|
||||||
|
|
||||||
|
{ 2, math.F16_MAX, math.F16_MAX },
|
||||||
|
{ 3, -math.F16_MAX, -math.F16_MAX },
|
||||||
|
{ 4, math.F16_MIN, 0.0 },
|
||||||
|
{ 5, -math.F16_MIN, -0.0 },
|
||||||
|
{ 6, 0.0, 0.0 },
|
||||||
|
{ 7, -0.0, -0.0 },
|
||||||
|
{ 8, 1, 1 },
|
||||||
|
{ 9, -1, -1 },
|
||||||
|
{ 10, math.INF_F16, math.INF_F16 },
|
||||||
|
{ 11, math.NEG_INF_F16, math.NEG_INF_F16 },
|
||||||
|
|
||||||
|
/* From https://en.wikipedia.org/wiki/Half-precision_floating-point_format */
|
||||||
|
{ 12, 0h3C01, 1 }, // 0x1.004p+0 (smallest > 1)
|
||||||
|
{ 13, -0h3C01, -1 },
|
||||||
|
{ 14, 0h3BFF, 1 }, // 0x1.ffcp-1 (largest < 1)
|
||||||
|
{ 15, -0h3BFF, -1 },
|
||||||
|
{ 16, 0h0001, 0.0 }, // 0x0.004p-14 (smallest subnormal)
|
||||||
|
{ 17, -0h0001, -0.0 },
|
||||||
|
{ 18, 0h03FF, 0.0 }, // 0x0.ffcp-14 (largest subnormal)
|
||||||
|
{ 19, -0h03FF, -0.0 },
|
||||||
|
|
||||||
|
{ 20, 0hC809, -8 }, // -0x1.024p+3
|
||||||
|
{ 21, 0h4458, 4 }, // 0x1.16p+2
|
||||||
|
}
|
||||||
|
|
||||||
|
for d, i in data {
|
||||||
|
assert(i == d.i)
|
||||||
|
r = math.round_f16(d.v)
|
||||||
|
tc.expect(t, r == d.e, fmt.tprintf("i:%d %s(%h) -> %h != %h", i, #procedure, d.v, r, d.e))
|
||||||
|
}
|
||||||
|
|
||||||
|
v = math.SNAN_F16
|
||||||
|
r = math.round_f16(v)
|
||||||
|
tc.expect(t, math.is_nan_f16(r), fmt.tprintf("%s(%f) -> %f != NaN", #procedure, v, r))
|
||||||
|
|
||||||
|
v = math.QNAN_F16
|
||||||
|
r = math.round_f16(v)
|
||||||
|
tc.expect(t, math.is_nan_f16(r), fmt.tprintf("%s(%f) -> %f != NaN", #procedure, v, r))
|
||||||
|
}
|
||||||
|
|
||||||
|
@test
|
||||||
|
test_round_f32 :: proc(t: ^testing.T) {
|
||||||
|
r, v: f32
|
||||||
|
|
||||||
|
Datum :: struct {
|
||||||
|
i: int,
|
||||||
|
v: f32,
|
||||||
|
e: f32,
|
||||||
|
}
|
||||||
|
@static data := []Datum{
|
||||||
|
{ 0, 10.5, 11 },
|
||||||
|
{ 1, -10.5, -11 },
|
||||||
|
|
||||||
|
{ 2, math.F32_MAX, math.F32_MAX },
|
||||||
|
{ 3, -math.F32_MAX, -math.F32_MAX },
|
||||||
|
{ 4, math.F32_MIN, 0.0 },
|
||||||
|
{ 5, -math.F32_MIN, -0.0 },
|
||||||
|
{ 6, 0.0, 0.0 },
|
||||||
|
{ 7, -0.0, -0.0 },
|
||||||
|
{ 8, 1, 1 },
|
||||||
|
{ 9, -1, -1 },
|
||||||
|
{ 10, math.INF_F32, math.INF_F32 },
|
||||||
|
{ 11, math.NEG_INF_F32, math.NEG_INF_F32 },
|
||||||
|
|
||||||
|
/* From https://en.wikipedia.org/wiki/Single-precision_floating-point_format */
|
||||||
|
{ 12, 0h3F80_0001, 1 }, // 0x1.000002p+0 (smallest > 1)
|
||||||
|
{ 13, -0h3F80_0001, -1 },
|
||||||
|
{ 14, 0h3F7F_FFFF, 1 }, // 0x1.fffffep-1 (largest < 1)
|
||||||
|
{ 15, -0h3F7F_FFFF, -1 },
|
||||||
|
{ 16, 0h0000_0001, 0.0 }, // 0x0.000002p-126 (smallest subnormal)
|
||||||
|
{ 17, -0h0000_0001, -0.0 },
|
||||||
|
{ 18, 0h007F_FFFF, 0.0 }, // 0x0.fffffep-126 (largest subnormal)
|
||||||
|
{ 19, -0h007F_FFFF, -0.0 },
|
||||||
|
|
||||||
|
/* From libc-test src/math/sanity/roundf.h */
|
||||||
|
{ 20, 0hC101_11D0, -8 }, // -0x1.0223ap+3
|
||||||
|
{ 21, 0h408B_0C34, 4 }, // 0x1.161868p+2
|
||||||
|
{ 22, 0hC106_1A5A, -8 }, // -0x1.0c34b4p+3
|
||||||
|
{ 23, 0hC0D1_0378, -7 }, // -0x1.a206fp+2
|
||||||
|
{ 24, 0h4114_45DE, 9 }, // 0x1.288bbcp+3
|
||||||
|
{ 25, 0h3F29_77E8, 1.0 }, // 0x1.52efdp-1
|
||||||
|
{ 26, 0hBED0_2E64, -0.0 }, // -0x1.a05cc8p-2
|
||||||
|
{ 27, 0h3F0F_CF7D, 1.0 }, // 0x1.1f9efap-1
|
||||||
|
{ 28, 0h3F46_2ED8, 1.0 }, // 0x1.8c5dbp-1
|
||||||
|
{ 29, 0hBF2D_C375, -1.0 }, // -0x1.5b86eap-1
|
||||||
|
}
|
||||||
|
|
||||||
|
for d, i in data {
|
||||||
|
assert(i == d.i)
|
||||||
|
r = math.round_f32(d.v)
|
||||||
|
tc.expect(t, r == d.e, fmt.tprintf("i:%d %s(%h) -> %h != %h", i, #procedure, d.v, r, d.e))
|
||||||
|
}
|
||||||
|
|
||||||
|
v = math.SNAN_F32
|
||||||
|
r = math.round_f32(v)
|
||||||
|
tc.expect(t, math.is_nan_f32(r), fmt.tprintf("%s(%f) -> %f != NaN", #procedure, v, r))
|
||||||
|
|
||||||
|
v = math.QNAN_F32
|
||||||
|
r = math.round_f32(v)
|
||||||
|
tc.expect(t, math.is_nan_f32(r), fmt.tprintf("%s(%f) -> %f != NaN", #procedure, v, r))
|
||||||
|
}
|
||||||
|
|
||||||
|
@test
|
||||||
|
test_round_f64 :: proc(t: ^testing.T) {
|
||||||
|
r, v: f64
|
||||||
|
|
||||||
|
Datum :: struct {
|
||||||
|
i: int,
|
||||||
|
v: f64,
|
||||||
|
e: f64,
|
||||||
|
}
|
||||||
|
data := []Datum{
|
||||||
|
{ 0, 10.5, 11 }, // Issue #1574 fract in linalg/glm is broken
|
||||||
|
{ 1, -10.5, -11 },
|
||||||
|
|
||||||
|
{ 2, math.F64_MAX, math.F64_MAX },
|
||||||
|
{ 3, -math.F64_MAX, -math.F64_MAX },
|
||||||
|
{ 4, math.F64_MIN, 0.0 },
|
||||||
|
{ 5, -math.F64_MIN, -0.0 },
|
||||||
|
{ 6, 0.0, 0.0 },
|
||||||
|
{ 7, -0.0, -0.0 },
|
||||||
|
{ 8, 1, 1 },
|
||||||
|
{ 9, -1, -1 },
|
||||||
|
{ 10, math.INF_F64, math.INF_F64 },
|
||||||
|
{ 11, math.NEG_INF_F64, math.NEG_INF_F64 },
|
||||||
|
|
||||||
|
/* From https://en.wikipedia.org/wiki/Double-precision_floating-point_format */
|
||||||
|
{ 12, 0h3FF0_0000_0000_0001, 1 }, // 0x1.0000000000001p+0 (smallest > 1)
|
||||||
|
{ 13, -0h3FF0_0000_0000_0001, -1 },
|
||||||
|
{ 14, 0h3FEF_FFFF_FFFF_FFFF, 1 }, // 0x1.fffffffffffffp-1 (largest < 1)
|
||||||
|
{ 15, -0h3FEF_FFFF_FFFF_FFFF, -1 },
|
||||||
|
{ 16, 0h0000_0000_0000_0001, 0.0 }, // 0x0.0000000000001p-1022 (smallest subnormal)
|
||||||
|
{ 17, -0h0000_0000_0000_0001, -0.0 },
|
||||||
|
{ 18, 0h000F_FFFF_FFFF_FFFF, 0.0 }, // 0x0.fffffffffffffp-1022 (largest subnormal)
|
||||||
|
{ 19, -0h000F_FFFF_FFFF_FFFF, -0.0 },
|
||||||
|
|
||||||
|
/* From libc-test src/math/sanity/round.h */
|
||||||
|
{ 20, 0hC020_2239_F3C6_A8F1, -8 }, // -0x1.02239f3c6a8f1p+3
|
||||||
|
{ 21, 0h4011_6186_8E18_BC67, 4 }, // 0x1.161868e18bc67p+2
|
||||||
|
{ 22, 0hC020_C34B_3E01_E6E7, -8 }, // -0x1.0c34b3e01e6e7p+3
|
||||||
|
{ 23, 0hC01A_206F_0A19_DCC4, -7 }, // -0x1.a206f0a19dcc4p+2
|
||||||
|
{ 24, 0h4022_88BB_B0D6_A1E6, 9 }, // 0x1.288bbb0d6a1e6p+3
|
||||||
|
{ 25, 0h3FE5_2EFD_0CD8_0497, 1.0 }, // 0x1.52efd0cd80497p-1
|
||||||
|
{ 26, 0hBFDA_05CC_7544_81D1, -0.0 }, // -0x1.a05cc754481d1p-2
|
||||||
|
{ 27, 0h3FE1_F9EF_9347_45CB, 1.0 }, // 0x1.1f9ef934745cbp-1
|
||||||
|
{ 28, 0h3FE8_C5DB_097F_7442, 1.0 }, // 0x1.8c5db097f7442p-1
|
||||||
|
{ 29, 0hBFE5_B86E_A811_8A0E, -1.0 }, // -0x1.5b86ea8118a0ep-1
|
||||||
|
}
|
||||||
|
|
||||||
|
for d, i in data {
|
||||||
|
assert(i == d.i)
|
||||||
|
r = math.round_f64(d.v)
|
||||||
|
tc.expect(t, r == d.e, fmt.tprintf("i:%d %s(%h) -> %h != %h", i, #procedure, d.v, r, d.e))
|
||||||
|
}
|
||||||
|
|
||||||
|
v = math.SNAN_F64
|
||||||
|
r = math.round_f64(v)
|
||||||
|
tc.expect(t, math.is_nan_f64(r), fmt.tprintf("%s(%f) -> %f != NaN", #procedure, v, r))
|
||||||
|
|
||||||
|
v = math.QNAN_F64
|
||||||
|
r = math.round_f64(v)
|
||||||
|
tc.expect(t, math.is_nan_f64(r), fmt.tprintf("%s(%f) -> %f != NaN", #procedure, v, r))
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
vf := []f64{
|
vf := []f64{
|
||||||
4.9790119248836735e+00,
|
4.9790119248836735e+00,
|
||||||
|
|||||||
@@ -202,8 +202,11 @@ test_captures :: proc(t: ^testing.T) {
|
|||||||
// match all captures
|
// match all captures
|
||||||
compare_captures :: proc(t: ^testing.T, test: ^Temp, haystack: string, comp: []string, loc := #caller_location) {
|
compare_captures :: proc(t: ^testing.T, test: ^Temp, haystack: string, comp: []string, loc := #caller_location) {
|
||||||
length, err := match.find_aux(haystack, test.pattern, 0, false, &test.captures)
|
length, err := match.find_aux(haystack, test.pattern, 0, false, &test.captures)
|
||||||
if failed(t, len(comp) == length) {
|
result := len(comp) == length && err == .OK
|
||||||
logf(t, "Captures Compare Failed -> Lengths %d != %d\n", len(comp), length)
|
if failed(t, result == true) {
|
||||||
|
logf(t, "Captures Compare Failed!\n")
|
||||||
|
logf(t, "\tErr: %v\n", err)
|
||||||
|
logf(t, "\tLengths: %v != %v\n", len(comp), length)
|
||||||
}
|
}
|
||||||
|
|
||||||
for i in 0..<length {
|
for i in 0..<length {
|
||||||
|
|||||||
Vendored
+61
@@ -0,0 +1,61 @@
|
|||||||
|
//+build linux
|
||||||
|
package egl
|
||||||
|
|
||||||
|
NativeDisplayType :: distinct rawptr
|
||||||
|
NativeWindowType :: distinct rawptr
|
||||||
|
Display :: distinct rawptr
|
||||||
|
Surface :: distinct rawptr
|
||||||
|
Config :: distinct rawptr
|
||||||
|
Context :: distinct rawptr
|
||||||
|
|
||||||
|
NO_DISPLAY :: Display(uintptr(0))
|
||||||
|
NO_CONTEXT :: Context(uintptr(0))
|
||||||
|
NO_SURFACE :: Surface(uintptr(0))
|
||||||
|
|
||||||
|
CONTEXT_OPENGL_CORE_PROFILE_BIT :: 0x00000001
|
||||||
|
WINDOW_BIT :: 0x0004
|
||||||
|
OPENGL_BIT :: 0x0008
|
||||||
|
|
||||||
|
BLUE_SIZE :: 0x3022
|
||||||
|
GREEN_SIZE :: 0x3023
|
||||||
|
RED_SIZE :: 0x3024
|
||||||
|
DEPTH_SIZE :: 0x3025
|
||||||
|
STENCIL_SIZE :: 0x3026
|
||||||
|
|
||||||
|
SURFACE_TYPE :: 0x3033
|
||||||
|
NONE :: 0x3038
|
||||||
|
COLOR_BUFFER_TYPE :: 0x303F
|
||||||
|
RENDERABLE_TYPE :: 0x3040
|
||||||
|
CONFORMANT :: 0x3042
|
||||||
|
|
||||||
|
BACK_BUFFER :: 0x3084
|
||||||
|
RENDER_BUFFER :: 0x3086
|
||||||
|
GL_COLORSPACE_SRGB :: 0x3089
|
||||||
|
GL_COLORSPACE_LINEAR :: 0x308A
|
||||||
|
RGB_BUFFER :: 0x308E
|
||||||
|
GL_COLORSPACE :: 0x309D
|
||||||
|
|
||||||
|
CONTEXT_MAJOR_VERSION :: 0x3098
|
||||||
|
CONTEXT_MINOR_VERSION :: 0x30FB
|
||||||
|
CONTEXT_OPENGL_PROFILE_MASK :: 0x30FD
|
||||||
|
|
||||||
|
OPENGL_API :: 0x30A2
|
||||||
|
|
||||||
|
foreign import egl "system:EGL"
|
||||||
|
@(default_calling_convention="c", link_prefix="egl")
|
||||||
|
foreign egl {
|
||||||
|
GetDisplay :: proc(display: NativeDisplayType) -> Display ---
|
||||||
|
Initialize :: proc(display: Display, major: ^i32, minor: ^i32) -> i32 ---
|
||||||
|
BindAPI :: proc(api: u32) -> i32 ---
|
||||||
|
ChooseConfig :: proc(display: Display, attrib_list: ^i32, configs: ^Context, config_size: i32, num_config: ^i32) -> i32 ---
|
||||||
|
CreateWindowSurface :: proc(display: Display, config: Config, native_window: NativeWindowType, attrib_list: ^i32) -> Surface ---
|
||||||
|
CreateContext :: proc(display: Display, config: Config, share_context: Context, attrib_list: ^i32) -> Context ---
|
||||||
|
MakeCurrent :: proc(display: Display, draw: Surface, read: Surface, ctx: Context) -> i32 ---
|
||||||
|
SwapInterval :: proc(display: Display, interval: i32) -> i32 ---
|
||||||
|
SwapBuffers :: proc(display: Display, surface: Surface) -> i32 ---
|
||||||
|
GetProcAddress :: proc(name: cstring) -> rawptr ---
|
||||||
|
}
|
||||||
|
|
||||||
|
gl_set_proc_address :: proc(p: rawptr, name: cstring) {
|
||||||
|
(^rawptr)(p)^ = GetProcAddress(name)
|
||||||
|
}
|
||||||
Vendored
+1
@@ -149,6 +149,7 @@ WaitEvents :: glfw.WaitEvents
|
|||||||
WaitEventsTimeout :: glfw.WaitEventsTimeout
|
WaitEventsTimeout :: glfw.WaitEventsTimeout
|
||||||
PostEmptyEvent :: glfw.PostEmptyEvent
|
PostEmptyEvent :: glfw.PostEmptyEvent
|
||||||
|
|
||||||
|
RawMouseMotionSupported :: glfw.RawMouseMotionSupported
|
||||||
GetInputMode :: glfw.GetInputMode
|
GetInputMode :: glfw.GetInputMode
|
||||||
SetInputMode :: glfw.SetInputMode
|
SetInputMode :: glfw.SetInputMode
|
||||||
|
|
||||||
|
|||||||
Vendored
+6
-6
@@ -95,7 +95,7 @@ MAX_TEXT_BUFFER_LENGTH :: #config(RAYLIB_MAX_TEXT_BUFFER_LENGTH, 1024)
|
|||||||
|
|
||||||
#assert(size_of(rune) == size_of(c.int))
|
#assert(size_of(rune) == size_of(c.int))
|
||||||
|
|
||||||
RAYLIB_SHARED :: #config(RAYLIB_SHARED, true)
|
RAYLIB_SHARED :: #config(RAYLIB_SHARED, false)
|
||||||
|
|
||||||
when ODIN_OS == .Windows {
|
when ODIN_OS == .Windows {
|
||||||
when RAYLIB_SHARED {
|
when RAYLIB_SHARED {
|
||||||
@@ -318,8 +318,8 @@ GlyphInfo :: struct {
|
|||||||
// Font type, includes texture and charSet array data
|
// Font type, includes texture and charSet array data
|
||||||
Font :: struct {
|
Font :: struct {
|
||||||
baseSize: c.int, // Base size (default chars height)
|
baseSize: c.int, // Base size (default chars height)
|
||||||
charsCount: c.int, // Number of characters
|
glyphCount: c.int, // Number of characters
|
||||||
charsPadding: c.int, // Padding around the chars
|
glyphPadding: c.int, // Padding around the chars
|
||||||
texture: Texture2D, // Characters texture atlas
|
texture: Texture2D, // Characters texture atlas
|
||||||
recs: [^]Rectangle, // Characters rectangles in texture
|
recs: [^]Rectangle, // Characters rectangles in texture
|
||||||
glyphs: [^]GlyphInfo, // Characters info data
|
glyphs: [^]GlyphInfo, // Characters info data
|
||||||
@@ -404,7 +404,7 @@ BoneInfo :: struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Model type
|
// Model type
|
||||||
Model :: struct {
|
Model :: struct #align(align_of(uintptr)) {
|
||||||
transform: Matrix, // Local transform matrix
|
transform: Matrix, // Local transform matrix
|
||||||
|
|
||||||
meshCount: c.int, // Number of meshes
|
meshCount: c.int, // Number of meshes
|
||||||
@@ -425,6 +425,7 @@ ModelAnimation :: struct {
|
|||||||
frameCount: c.int, // Number of animation frames
|
frameCount: c.int, // Number of animation frames
|
||||||
bones: [^]BoneInfo, // Bones information (skeleton)
|
bones: [^]BoneInfo, // Bones information (skeleton)
|
||||||
framePoses: [^][^]Transform, // Poses array by frame
|
framePoses: [^][^]Transform, // Poses array by frame
|
||||||
|
name: [32]byte, // Animation name
|
||||||
}
|
}
|
||||||
|
|
||||||
// Ray type (useful for raycast)
|
// Ray type (useful for raycast)
|
||||||
@@ -490,7 +491,6 @@ VrDeviceInfo :: struct {
|
|||||||
vResolution: c.int, // Vertical resolution in pixels
|
vResolution: c.int, // Vertical resolution in pixels
|
||||||
hScreenSize: f32, // Horizontal size in meters
|
hScreenSize: f32, // Horizontal size in meters
|
||||||
vScreenSize: f32, // Vertical size in meters
|
vScreenSize: f32, // Vertical size in meters
|
||||||
vScreenCenter: f32, // Screen center in meters
|
|
||||||
eyeToScreenDistance: f32, // Distance between eye and display in meters
|
eyeToScreenDistance: f32, // Distance between eye and display in meters
|
||||||
lensSeparationDistance: f32, // Lens separation distance in meters
|
lensSeparationDistance: f32, // Lens separation distance in meters
|
||||||
interpupillaryDistance: f32, // IPD (distance between pupils) in meters
|
interpupillaryDistance: f32, // IPD (distance between pupils) in meters
|
||||||
@@ -499,7 +499,7 @@ VrDeviceInfo :: struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// VR Stereo rendering configuration for simulator
|
// VR Stereo rendering configuration for simulator
|
||||||
VrStereoConfig :: struct {
|
VrStereoConfig :: struct #align(4) {
|
||||||
projection: [2]Matrix, // VR projection matrices (per eye)
|
projection: [2]Matrix, // VR projection matrices (per eye)
|
||||||
viewOffset: [2]Matrix, // VR view offset matrices (per eye)
|
viewOffset: [2]Matrix, // VR view offset matrices (per eye)
|
||||||
leftLensCenter: [2]f32, // VR left lens center
|
leftLensCenter: [2]f32, // VR left lens center
|
||||||
|
|||||||
Vendored
+2
-2
@@ -88,8 +88,8 @@ foreign lib {
|
|||||||
RWwrite :: proc(ctx: ^RWops, size: c.size_t, num: c.size_t) -> c.size_t ---
|
RWwrite :: proc(ctx: ^RWops, size: c.size_t, num: c.size_t) -> c.size_t ---
|
||||||
RWclose :: proc(ctx: ^RWops) -> c.int ---
|
RWclose :: proc(ctx: ^RWops) -> c.int ---
|
||||||
|
|
||||||
LoadFile_RW :: proc(src: ^RWops, datasize: c.size_t, freesrc: bool) -> rawptr ---
|
LoadFile_RW :: proc(src: ^RWops, datasize: ^c.size_t, freesrc: bool) -> rawptr ---
|
||||||
LoadFile :: proc(file: rawptr, datasize: c.size_t) -> rawptr ---
|
LoadFile :: proc(file: rawptr, datasize: ^c.size_t) -> rawptr ---
|
||||||
|
|
||||||
ReadU8 :: proc(src: ^RWops) -> u8 ---
|
ReadU8 :: proc(src: ^RWops) -> u8 ---
|
||||||
ReadLE16 :: proc(src: ^RWops) -> u16 ---
|
ReadLE16 :: proc(src: ^RWops) -> u16 ---
|
||||||
|
|||||||
Reference in New Issue
Block a user