8000 dslx/stdlib: add iterative_div_mod by proppy · Pull Request #932 · google/xls · GitHub
[go: up one dir, main page]

Skip to content

dslx/stdlib: add iterative_div_mod - #932

Merged
copybara-service[bot] merged 1 commit into
google:mainfrom
proppy:binary-div
Apr 3, 2024
Merged

copybara-service[bot] merged 1 commit into
google:mainfrom
proppy:binary-div

Conversation

@proppy
@proppy proppy commented Apr 14, 2023 •
Copy link
Copy Markdown
Member
  • add iterative_div_mod based on
    // Unsigned division of two Vectors.
    // Returns a quotient Vector of n.size(), and a remainder Vector of d.size().
    //
    // Implements long division.
    DivisionResult UDivMod(const Vector& n, const Vector& d) {
    Vector nonzero_divisor = OrReduce(d);
    Vector divisor = ZeroExtend(d, d.size() + 1);
    Vector neg_divisor = Neg(divisor);
    Vector q(n.size(), Zero());
    Vector r(d.size(), Zero());
    for (int64_t i = q.size() - 1; i >= 0; --i) {
    // Shift the next bit of n into r.
    r.insert(r.begin(), n[i]);
    // Now: r.size() == d.size() + 1 == divisor.size().
    // If r >= divisor, then subtract divisor from r and set q[i] := 1.
    // Otherwise, set q[i] := 0.
    q[i] = Not(ULessThan(r, divisor));
    r = Select({q[i]}, {r, Add(r, neg_divisor)});
    // Remove the MSB of r; guaranteed to be 0 because r < d.
    // Ensures r.size() == d.size().
    r.erase(r.end() - 1);
    }
    // If dividing by zero, return all 1s for q and all 0s for r.
    q = Select({nonzero_divisor}, {Vector(q.size(), One()), q});
    r = Select({nonzero_divisor}, {Vector(r.size(), Zero()), r});
    return {.quotient = q, .remainder = r};
    }
    Vector UDiv(const Vector& n, const Vector& d) {
    return UDivMod(n, d).quotient;
    }
    Vector UMod(const Vector& n, const Vector& d) {
    return UDivMod(n, d).remainder;
    }
    // Signed division of two Vectors.
    // Returns a quotient Vector of n.size(), and a remainder Vector of d.size().
    //
    // Convention: remainder has the same sign as n.
    DivisionResult SDivMod(const Vector& n, const Vector& d) {
    Element nonzero_divisor = OrReduce(d).back();
    Element n_negative = n.size() > 0 ? n.back() : Zero();
    Element d_negative = d.size() > 0 ? d.back() : Zero();
    DivisionResult result = UDivMod(Abs(n), Abs(d));
    result.remainder =
    Select({n_negative}, {result.remainder, Neg(result.remainder)});
    result.quotient = Select({Xor(n_negative, d_negative)},
    {result.quotient, Neg(result.quotient)});
    // If dividing by zero and n is negative, return largest negative value;
    // otherwise, return largest positive value.
    Vector largest_positive = Vector(result.quotient.size(), One());
    Vector largest_negative = Vector(result.quotient.size(), Zero());
    if (result.quotient.size() > 0) {
    largest_positive.back() = Zero();
    largest_negative.back() = One();
    }
    result.quotient =
    Select({n_negative, nonzero_divisor},
    {largest_positive, largest_negative}, result.quotient);
    return result;
    }
    Vector SDiv(const Vector& n, const Vector& d) {
    return SDivMod(n, d).quotient;
    }
    Vector SMod(const Vector& n, const Vector& d) {
    return SDivMod(n, d).remainder;
    }
  • unify implementation with existing iterative_div function

@proppy
proppy commented Apr 19, 2023
Copy link
Copy Markdown
Member Author

@meheff fyi

@meheff meheff left a comment
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.

Thanks!

Comment thread xls/dslx/stdlib/std.x Resolved
Comment thread xls/dslx/stdlib/std.x Resolved
Comment thread xls/dslx/stdlib/std.x Resolved
Comment thread xls/dslx/stdlib/std.x Resolved
Comment thread xls/dslx/stdlib/std.x Resolved
Comment thread xls/dslx/stdlib/std.x Resolved
@proppy
proppy commented Jun 26, 2023
Copy link
Copy Markdown
Member Author

PTAL, unified the DSLX stdlib division with the existing IR divmod implementation.

@proppy proppy changed the title dslx/stdlib: add iterative_binary_div dslx/stdlib: add iterative_div_mod Jun 26, 2023
@proppy
proppy commented Jun 26, 2023
Copy link
Copy Markdown
Member Author

Rebased.

Comment thread xls/dslx/stdlib/std.x Resolved
@proppy
proppy requested review from ericastor and grebe June 26, 2023 09:10
Comment thread xls/dslx/stdlib/std.x Resolved
@proppy
proppy commented Jun 27, 2023
Copy link
Copy Markdown
Member Author

PTAL

@cdleary
cdleary commented Mar 27, 2024
Copy link
Copy Markdown
Collaborator

I think this need a rebase to merge, thanks.

@cdleary cdleary added the app Application level functionality (examples, uses of XLS stack) label Mar 27, 2024
Comment thread xls/dslx/stdlib/std.x Resolved
Comment thread xls/dslx/stdlib/std.x Resolved
@ericastor
Copy link
Copy Markdown
Contributor

Nevermind previous comments, we can fix them up after landing this. (Just noticed how long this was sitting, and that I'd already approved it ages ago.)

@copybara-service
copybara-service Bot merged commit c73f13d into google:main Apr 3, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

app Application level functionality (examples, uses of XLS stack) Reviewing Internally

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants

0