Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[c_api] Add bits ops. #1881

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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))$'
1 change: 1 addition & 0 deletions xls/public/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
151 changes: 151 additions & 0 deletions xls/public/c_api.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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<const xls::Bits*>(bits);
return reinterpret_cast<xls_bits*>(
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<const xls::Bits*>(bits);
xls::Bits result = xls::bits_ops::ShiftLeftLogical(*cpp_bits, shift_amount);
return reinterpret_cast<xls_bits*>(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<const xls::Bits*>(bits);
xls::Bits result = xls::bits_ops::ShiftRightLogical(*cpp_bits, shift_amount);
return reinterpret_cast<xls_bits*>(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<const xls::Bits*>(bits);
xls::Bits result = xls::bits_ops::ShiftRightArith(*cpp_bits, shift_amount);
return reinterpret_cast<xls_bits*>(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<const xls::Bits*>(bits);
xls::Bits result = xls::bits_ops::Negate(*cpp_bits);
return reinterpret_cast<xls_bits*>(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<const xls::Bits*>(bits);
xls::Bits result = xls::bits_ops::Abs(*cpp_bits);
return reinterpret_cast<xls_bits*>(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<const xls::Bits*>(bits);
xls::Bits result = xls::bits_ops::Not(*cpp_bits);
return reinterpret_cast<xls_bits*>(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<const xls::Bits*>(lhs);
const auto* cpp_rhs = reinterpret_cast<const xls::Bits*>(rhs);
xls::Bits result = xls::bits_ops::Add(*cpp_lhs, *cpp_rhs);
return reinterpret_cast<xls_bits*>(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<const xls::Bits*>(lhs);
const auto* cpp_rhs = reinterpret_cast<const xls::Bits*>(rhs);
xls::Bits result = xls::bits_ops::Sub(*cpp_lhs, *cpp_rhs);
return reinterpret_cast<xls_bits*>(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<const xls::Bits*>(lhs);
const auto* cpp_rhs = reinterpret_cast<const xls::Bits*>(rhs);
xls::Bits result = xls::bits_ops::And(*cpp_lhs, *cpp_rhs);
return reinterpret_cast<xls_bits*>(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<const xls::Bits*>(lhs);
const auto* cpp_rhs = reinterpret_cast<const xls::Bits*>(rhs);
xls::Bits result = xls::bits_ops::Or(*cpp_lhs, *cpp_rhs);
return reinterpret_cast<xls_bits*>(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<const xls::Bits*>(lhs);
const auto* cpp_rhs = reinterpret_cast<const xls::Bits*>(rhs);
xls::Bits result = xls::bits_ops::Xor(*cpp_lhs, *cpp_rhs);
return reinterpret_cast<xls_bits*>(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<const xls::Bits*>(lhs);
const auto* cpp_rhs = reinterpret_cast<const xls::Bits*>(rhs);
xls::Bits result = xls::bits_ops::UMul(*cpp_lhs, *cpp_rhs);
return reinterpret_cast<xls_bits*>(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<const xls::Bits*>(lhs);
const auto* cpp_rhs = reinterpret_cast<const xls::Bits*>(rhs);
xls::Bits result = xls::bits_ops::SMul(*cpp_lhs, *cpp_rhs);
return reinterpret_cast<xls_bits*>(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<const xls::Bits*>(bits);
Expand Down Expand Up @@ -302,6 +426,12 @@ void xls_bits_free(xls_bits* b) { delete reinterpret_cast<xls::Bits*>(b); }

void xls_value_free(xls_value* v) { delete reinterpret_cast<xls::Value*>(v); }

struct xls_value* xls_value_from_bits(const struct xls_bits* bits) {
CHECK(bits != nullptr);
const auto* cpp_bits = reinterpret_cast<const xls::Bits*>(bits);
return reinterpret_cast<xls_value*>(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<xls::Package*>(p);
Expand Down Expand Up @@ -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<const xls::Bits*>(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);
Expand Down
46 changes: 46 additions & 0 deletions xls/public/c_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand All @@ -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
Expand Down
16 changes: 16 additions & 0 deletions xls/public/c_api_symbols.txt
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
Expand Down
Loading