Skip to content

Unsigned and Signed#271

Open
ktbarrett wants to merge 8 commits into
devfrom
unsigned-signed
Open

Unsigned and Signed#271
ktbarrett wants to merge 8 commits into
devfrom
unsigned-signed

Conversation

@ktbarrett

@ktbarrett ktbarrett commented Jun 8, 2026

Copy link
Copy Markdown
Owner

Closes #191.

These are useful for implementing a bunch of features currently found in cocotb's LogicArray (e.g. from_unsigned, to_unsigned) that we for binding coconext's DynLogicArray to Python in a way it can sit in place of cocotb's using the patcher.

This just uses uint64_t and int64_t. We will adapt UInt, and SInt from #278 once it's done.

@codecov

codecov Bot commented Jun 8, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 92.30%. Comparing base (f015a94) to head (e05e267).

Additional details and impacted files
@@            Coverage Diff             @@
##              dev     #271      +/-   ##
==========================================
+ Coverage   92.23%   92.30%   +0.07%     
==========================================
  Files          24       24              
  Lines        1339     1339              
  Branches      331      331              
==========================================
+ Hits         1235     1236       +1     
  Misses         45       45              
+ Partials       59       58       -1     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@ZiaCheemaGit ZiaCheemaGit mentioned this pull request Jun 25, 2026
@ZiaCheemaGit ZiaCheemaGit force-pushed the unsigned-signed branch 3 times, most recently from 3bb9234 to a5b17ca Compare June 29, 2026 20:29

@ktbarrett ktbarrett left a comment

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The shape looks good.

Comment thread cpp/include/coconext/types.hpp
Comment on lines +126 to +129
for (auto const& bit : other) {
temp_bit = bit ? 1 : 0;
value_ = (value_ << 1) | temp_bit;
}

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Welp, I guess we should move BitArray over to Bits as well now.

@ZiaCheemaGit ZiaCheemaGit Jun 30, 2026

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wrap BitArray around Bits? A packed Bitarray rework was mentioned in the unsigned signed docs. That is a similar thing?

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah it's similar. Right now the implicit upcast is kind of an expensive operation. If everything was just Bits under the hood, we can just copy/move the Bits object around.

Comment thread cpp/include/coconext/types/unsigned.hpp Outdated
@ZiaCheemaGit ZiaCheemaGit marked this pull request as ready for review July 4, 2026 06:09
@ZiaCheemaGit

Copy link
Copy Markdown
Collaborator

Follow ups would be

  • APInt BigInts
  • BitArray packing
  • Also I have left out slicing etc things as it would require rework after BitArray is packed

Comment on lines +602 to +608
if (storage_ == other.storage_) {
return std::strong_ordering::equal;
}
if (storage_ < other.storage_) {
return std::strong_ordering::less;
}
return std::strong_ordering::greater;

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems like you could just implement operator<=> for BigInt and leave the else branch as the only implementation.

@ktbarrett

Copy link
Copy Markdown
Owner Author

Sorry started to review, but got side-tracked, will have time tomorrow as I'm almost done my work for the week.

Comment thread tests/cpp/test_signed.cpp
@@ -0,0 +1,422 @@
#include <gtest/gtest.h>

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
#include <gtest/gtest.h>
// LCOV_EXCL_BR_START -- gtest macros generate noisy uncovered branches
#include <gtest/gtest.h>

Comment on lines +58 to +59
struct UnsignedTag {};
struct SignedTag {};

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lets not do this this way. This basically works because of EBO, but if we don't get EBO or the optimizer gets confused we break autovectorization.

We can accomplish the same thing with a template variable specialization:

template <typename T>
inline constexpr bool is_coconext_unsigned_v = false;

template <Range R>
inline constexpr bool is_coconext_unsigned_v<detail::Unsigned<R>> = true;

@ktbarrett ktbarrett left a comment

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The big issue is the order of operations issue in in-place operations. Everything else is kinda nitpicking.

Comment on lines +77 to +81
template <size_t W>
constexpr Signed(Bits<W> const& val) {
static_assert(W == R.length(), "Construction from Bits requires identical width");
value_ = val;
}

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bits is in the detail namespace. I don't think we should be having public methods get or return Bits.

Comment on lines +194 to +203
for (auto& bit : temp) {
if constexpr (!Bits<R.length()>::is_not_native_int) {
bit = to_bit(((value_.srl(bit_idx)) & temp_bit).raw());
} else {
bit = to_bit(((value_.srl(bit_idx)) & temp_bit).raw().get_word(0));
}
if (bit_idx > 0) {
bit_idx--;
}
}

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is very inefficient, but that will change once the BitArray specialization is in. I wonder if we should just be doing that change first to prevent churn?

Comment on lines +501 to +503
constexpr Range R_res = detail::int_downto_range(R.length() + 1);
auto ext = resize<R_res.length()>(*this);
return Signed<R_res>(detail::Bits<R_res.length()>(0) - ext.value_);

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably more simply stated as return Signed<1>(0) - *this

Comment on lines +555 to +585
template <Range R2>
constexpr Signed& operator+=(Signed<R2> const& rhs) {
this->value_ = this->value_ + resize<R.length()>(rhs).value_;
return *this;
}
template <Range R2>
constexpr Signed& operator-=(Signed<R2> const& rhs) {
this->value_ = this->value_ - resize<R.length()>(rhs).value_;
return *this;
}
template <Range R2>
constexpr Signed& operator*=(Signed<R2> const& rhs) {
this->value_ = this->value_ * resize<R.length()>(rhs).value_;
return *this;
}
template <Range R2>
constexpr Signed& operator/=(Signed<R2> const& rhs) {
if (!static_cast<bool>(rhs)) {
throw std::domain_error("Division by zero");
}
this->value_ = this->value_.sdiv(resize<R.length()>(rhs).value_);
return *this;
}
template <Range R2>
constexpr Signed& operator%=(Signed<R2> const& rhs) {
if (!static_cast<bool>(rhs)) {
throw std::domain_error("Division by zero");
}
this->value_ = this->value_.smod(resize<R.length()>(rhs).value_);
return *this;
}

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The order of operations here is off. We want full-sized operation then resize back down:

*this = resize<R>(*this + rhs);


template <Integer T>
constexpr Signed& operator+=(T const& rhs) {
*this += Signed<R>(rhs);

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can't assume RHS will fit into the LHS's bounds. This is the same order of operations bug. We want

*this = resize<R>(*this + rhs)

constexpr const_iterator(Signed const* parent, size_t offset)
: parent_(parent), offset_(offset) {}

constexpr reference operator*() const {

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

WE are going to need mutable references here at some point, but we can get to that later. WE will need a proper BitProxy class like std::vector uses and that should just go on Bits and we can remove this iterator.

Comment on lines +849 to +857
template <typename Target, typename Source>
requires detail::is_coconext_signed<Target>::value
constexpr Target as(Source const& source) noexcept {
static_assert(
Target::static_range.length() == Source::static_range.length(),
"as() requires equal widths."
);
return Target(static_cast<detail::Array<Bit, Source::static_range>>(source));
}

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this could be expanded to accept any object with Bits as the implementation type. You could make a detail::uses_Bits trait with Signed, Unsigned, BitArray, and eventually the fixed point and float classes all participating, then you only have one implementation of this function.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add Signed/Unsigned datatypes

2 participants