diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index f0dd5e451d..da84ad3b02 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -3,4 +3,4 @@ repos: rev: v5.0.0 hooks: - id: end-of-file-fixer - exclude: '^(xls/contrib/.*|.*\.(sh|ir|txt|patch|csv))$' + exclude: '^(xls/contrib/.*|docs_src/bazel_rules_macros.md|.*\.(sh|ir|txt|patch|csv))$' diff --git a/xls/public/BUILD b/xls/public/BUILD index cd7e4ebbf7..e6b19dc675 100644 --- a/xls/public/BUILD +++ b/xls/public/BUILD @@ -231,6 +231,7 @@ cc_library( "//xls/ir", "//xls/ir:bit_push_buffer", "//xls/ir:bits", + "//xls/ir:bits_ops", "//xls/ir:events", "//xls/ir:format_preference", "//xls/ir:ir_parser", diff --git a/xls/public/c_api.cc b/xls/public/c_api.cc index 5e4dc8cc57..3bd8120e8c 100644 --- a/xls/public/c_api.cc +++ b/xls/public/c_api.cc @@ -37,6 +37,7 @@ #include "xls/interpreter/function_interpreter.h" #include "xls/ir/bit_push_buffer.h" #include "xls/ir/bits.h" +#include "xls/ir/bits_ops.h" #include "xls/ir/events.h" #include "xls/ir/format_preference.h" #include "xls/ir/function.h" @@ -254,6 +255,129 @@ bool xls_bits_get_bit(const struct xls_bits* bits, int64_t index) { return cpp_bits->Get(index); } +struct xls_bits* xls_bits_width_slice(const struct xls_bits* bits, + int64_t start, int64_t width) { + CHECK(bits != nullptr); + const auto* cpp_bits = reinterpret_cast(bits); + return reinterpret_cast( + new xls::Bits(cpp_bits->Slice(start, width))); +} + +struct xls_bits* xls_bits_shift_left_logical(const struct xls_bits* bits, + int64_t shift_amount) { + CHECK(bits != nullptr); + const auto* cpp_bits = reinterpret_cast(bits); + xls::Bits result = xls::bits_ops::ShiftLeftLogical(*cpp_bits, shift_amount); + return reinterpret_cast(new xls::Bits(std::move(result))); +} + +struct xls_bits* xls_bits_shift_right_logical(const struct xls_bits* bits, + int64_t shift_amount) { + CHECK(bits != nullptr); + const auto* cpp_bits = reinterpret_cast(bits); + xls::Bits result = xls::bits_ops::ShiftRightLogical(*cpp_bits, shift_amount); + return reinterpret_cast(new xls::Bits(std::move(result))); +} + +struct xls_bits* xls_bits_shift_right_arithmetic(const struct xls_bits* bits, + int64_t shift_amount) { + CHECK(bits != nullptr); + const auto* cpp_bits = reinterpret_cast(bits); + xls::Bits result = xls::bits_ops::ShiftRightArith(*cpp_bits, shift_amount); + return reinterpret_cast(new xls::Bits(std::move(result))); +} + +struct xls_bits* xls_bits_negate(const struct xls_bits* bits) { + CHECK(bits != nullptr); + const auto* cpp_bits = reinterpret_cast(bits); + xls::Bits result = xls::bits_ops::Negate(*cpp_bits); + return reinterpret_cast(new xls::Bits(std::move(result))); +} + +struct xls_bits* xls_bits_abs(const struct xls_bits* bits) { + CHECK(bits != nullptr); + const auto* cpp_bits = reinterpret_cast(bits); + xls::Bits result = xls::bits_ops::Abs(*cpp_bits); + return reinterpret_cast(new xls::Bits(std::move(result))); +} + +struct xls_bits* xls_bits_not(const struct xls_bits* bits) { + CHECK(bits != nullptr); + const auto* cpp_bits = reinterpret_cast(bits); + xls::Bits result = xls::bits_ops::Not(*cpp_bits); + return reinterpret_cast(new xls::Bits(std::move(result))); +} + +struct xls_bits* xls_bits_add(const struct xls_bits* lhs, + const struct xls_bits* rhs) { + CHECK(lhs != nullptr); + CHECK(rhs != nullptr); + const auto* cpp_lhs = reinterpret_cast(lhs); + const auto* cpp_rhs = reinterpret_cast(rhs); + xls::Bits result = xls::bits_ops::Add(*cpp_lhs, *cpp_rhs); + return reinterpret_cast(new xls::Bits(std::move(result))); +} + +struct xls_bits* xls_bits_sub(const struct xls_bits* lhs, + const struct xls_bits* rhs) { + CHECK(lhs != nullptr); + CHECK(rhs != nullptr); + const auto* cpp_lhs = reinterpret_cast(lhs); + const auto* cpp_rhs = reinterpret_cast(rhs); + xls::Bits result = xls::bits_ops::Sub(*cpp_lhs, *cpp_rhs); + return reinterpret_cast(new xls::Bits(std::move(result))); +} + +struct xls_bits* xls_bits_and(const struct xls_bits* lhs, + const struct xls_bits* rhs) { + CHECK(lhs != nullptr); + CHECK(rhs != nullptr); + const auto* cpp_lhs = reinterpret_cast(lhs); + const auto* cpp_rhs = reinterpret_cast(rhs); + xls::Bits result = xls::bits_ops::And(*cpp_lhs, *cpp_rhs); + return reinterpret_cast(new xls::Bits(std::move(result))); +} + +struct xls_bits* xls_bits_or(const struct xls_bits* lhs, + const struct xls_bits* rhs) { + CHECK(lhs != nullptr); + CHECK(rhs != nullptr); + const auto* cpp_lhs = reinterpret_cast(lhs); + const auto* cpp_rhs = reinterpret_cast(rhs); + xls::Bits result = xls::bits_ops::Or(*cpp_lhs, *cpp_rhs); + return reinterpret_cast(new xls::Bits(std::move(result))); +} + +struct xls_bits* xls_bits_xor(const struct xls_bits* lhs, + const struct xls_bits* rhs) { + CHECK(lhs != nullptr); + CHECK(rhs != nullptr); + const auto* cpp_lhs = reinterpret_cast(lhs); + const auto* cpp_rhs = reinterpret_cast(rhs); + xls::Bits result = xls::bits_ops::Xor(*cpp_lhs, *cpp_rhs); + return reinterpret_cast(new xls::Bits(std::move(result))); +} + +struct xls_bits* xls_bits_umul(const struct xls_bits* lhs, + const struct xls_bits* rhs) { + CHECK(lhs != nullptr); + CHECK(rhs != nullptr); + const auto* cpp_lhs = reinterpret_cast(lhs); + const auto* cpp_rhs = reinterpret_cast(rhs); + xls::Bits result = xls::bits_ops::UMul(*cpp_lhs, *cpp_rhs); + return reinterpret_cast(new xls::Bits(std::move(result))); +} + +struct xls_bits* xls_bits_smul(const struct xls_bits* lhs, + const struct xls_bits* rhs) { + CHECK(lhs != nullptr); + CHECK(rhs != nullptr); + const auto* cpp_lhs = reinterpret_cast(lhs); + const auto* cpp_rhs = reinterpret_cast(rhs); + xls::Bits result = xls::bits_ops::SMul(*cpp_lhs, *cpp_rhs); + return reinterpret_cast(new xls::Bits(std::move(result))); +} + char* xls_bits_to_debug_string(const struct xls_bits* bits) { CHECK(bits != nullptr); const auto* cpp_bits = reinterpret_cast(bits); @@ -302,6 +426,12 @@ void xls_bits_free(xls_bits* b) { delete reinterpret_cast(b); } void xls_value_free(xls_value* v) { delete reinterpret_cast(v); } +struct xls_value* xls_value_from_bits(const struct xls_bits* bits) { + CHECK(bits != nullptr); + const auto* cpp_bits = reinterpret_cast(bits); + return reinterpret_cast(new xls::Value(std::move(*cpp_bits))); +} + struct xls_function_base* xls_package_get_top(struct xls_package* p) { CHECK(p != nullptr); xls::Package* cpp_package = reinterpret_cast(p); @@ -358,6 +488,27 @@ bool xls_value_to_string_format_preference( return true; } +bool xls_bits_to_string(const struct xls_bits* bits, + xls_format_preference format_preference, + bool include_bit_count, char** error_out, + char** result_out) { + CHECK(bits != nullptr); + CHECK(error_out != nullptr); + CHECK(result_out != nullptr); + + xls::FormatPreference cpp_format_preference; + if (!FormatPreferenceFromC(format_preference, &cpp_format_preference, + error_out)) { + return false; + } + + const auto* cpp_bits = reinterpret_cast(bits); + std::string s = + xls::BitsToString(*cpp_bits, cpp_format_preference, include_bit_count); + *result_out = xls::ToOwnedCString(s); + return true; +} + bool xls_value_eq(const struct xls_value* v, const struct xls_value* w) { CHECK(v != nullptr); CHECK(w != nullptr); diff --git a/xls/public/c_api.h b/xls/public/c_api.h index 33f90e26df..24db706fdc 100644 --- a/xls/public/c_api.h +++ b/xls/public/c_api.h @@ -142,6 +142,45 @@ bool xls_bits_eq(const struct xls_bits* a, const struct xls_bits* b); // returns 1, `xls_bits_get_bit(bits, 1)` returns 0. bool xls_bits_get_bit(const struct xls_bits* bits, int64_t index); +struct xls_bits* xls_bits_width_slice(const struct xls_bits* bits, + int64_t start, int64_t width); + +struct xls_bits* xls_bits_shift_left_logical(const struct xls_bits* bits, + int64_t shift_amount); + +struct xls_bits* xls_bits_shift_right_logical(const struct xls_bits* bits, + int64_t shift_amount); + +struct xls_bits* xls_bits_shift_right_arithmetic(const struct xls_bits* bits, + int64_t shift_amount); + +struct xls_bits* xls_bits_negate(const struct xls_bits* bits); + +struct xls_bits* xls_bits_abs(const struct xls_bits* bits); + +struct xls_bits* xls_bits_not(const struct xls_bits* bits); + +struct xls_bits* xls_bits_add(const struct xls_bits* lhs, + const struct xls_bits* rhs); + +struct xls_bits* xls_bits_sub(const struct xls_bits* lhs, + const struct xls_bits* rhs); + +struct xls_bits* xls_bits_and(const struct xls_bits* lhs, + const struct xls_bits* rhs); + +struct xls_bits* xls_bits_or(const struct xls_bits* lhs, + const struct xls_bits* rhs); + +struct xls_bits* xls_bits_xor(const struct xls_bits* lhs, + const struct xls_bits* rhs); + +struct xls_bits* xls_bits_umul(const struct xls_bits* lhs, + const struct xls_bits* rhs); + +struct xls_bits* xls_bits_smul(const struct xls_bits* lhs, + const struct xls_bits* rhs); + // Returns a new `bits[1]:0` XLS value which the caller must free. struct xls_value* xls_value_make_false(); @@ -164,9 +203,16 @@ bool xls_value_to_string_format_preference( const struct xls_value* v, xls_format_preference format_preference, char** error_out, char** result_out); +bool xls_bits_to_string(const struct xls_bits* bits, + xls_format_preference format_preference, + bool include_bit_count, char** error_out, + char** result_out); + // Deallocates a value, e.g. one as created by `xls_parse_typed_value`. void xls_value_free(struct xls_value* v); +struct xls_value* xls_value_from_bits(const struct xls_bits* bits); + // Flattens the given value to a sequence of bits in a bits "buffer" value. // // Note that in a tuple or array the earlier fields/members are stored in the diff --git a/xls/public/c_api_symbols.txt b/xls/public/c_api_symbols.txt index a7fdf99ab9..415fe9586b 100644 --- a/xls/public/c_api_symbols.txt +++ b/xls/public/c_api_symbols.txt @@ -1,10 +1,25 @@ +xls_bits_abs +xls_bits_add +xls_bits_and xls_bits_eq xls_bits_free xls_bits_get_bit xls_bits_get_bit_count xls_bits_make_sbits xls_bits_make_ubits +xls_bits_negate +xls_bits_not +xls_bits_or +xls_bits_shift_left_logical +xls_bits_shift_right_arithmetic +xls_bits_shift_right_logical +xls_bits_smul +xls_bits_sub xls_bits_to_debug_string +xls_bits_to_string +xls_bits_umul +xls_bits_width_slice +xls_bits_xor xls_c_str_free xls_convert_dslx_path_to_ir xls_convert_dslx_to_ir @@ -99,6 +114,7 @@ xls_type_to_string xls_value_eq xls_value_flatten_to_bits xls_value_free +xls_value_from_bits xls_value_get_bits xls_value_make_false xls_value_make_token