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

[stdlib] implement bit_ceil and bit_floor #2736

Closed
wants to merge 6 commits into from

Conversation

LJ-9801
Copy link
Contributor

@LJ-9801 LJ-9801 commented May 18, 2024

fix #2682

@LJ-9801
Copy link
Contributor Author

LJ-9801 commented May 18, 2024

Implementation of bit_ceil is based on c++ bit_ceil

@soraros
Copy link
Contributor

soraros commented May 18, 2024

Should we include the reference in doc string or comment in the future?

stdlib/src/bit/bit.mojo Outdated Show resolved Hide resolved
@LJ-9801
Copy link
Contributor Author

LJ-9801 commented May 19, 2024

Should we include the reference in doc string or comment in the future?

@soraros ops yeah you are right. I'll include it in the docstring for now. @laszlokindrat which one do you think is more suitable?

@LJ-9801 LJ-9801 force-pushed the bit-ceil-floor branch 2 times, most recently from c4ace69 to fcd81d2 Compare May 20, 2024 06:41
@LJ-9801 LJ-9801 marked this pull request as ready for review May 20, 2024 06:45
@LJ-9801 LJ-9801 requested a review from a team as a code owner May 20, 2024 06:45
Copy link
Contributor

@laszlokindrat laszlokindrat left a comment

Choose a reason for hiding this comment

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

Nice, thanks! I have a few requests for change, nothing major. This will be an excellent improvement for the bit module!

stdlib/src/bit/bit.mojo Outdated Show resolved Hide resolved
stdlib/src/bit/bit.mojo Outdated Show resolved Hide resolved
stdlib/test/bit/test_bit.mojo Outdated Show resolved Hide resolved
stdlib/src/bit/bit.mojo Show resolved Hide resolved
stdlib/src/bit/bit.mojo Outdated Show resolved Hide resolved
@laszlokindrat
Copy link
Contributor

Should we include the reference in doc string or comment in the future?

@soraros ops yeah you are right. I'll include it in the docstring for now. @laszlokindrat which one do you think is more suitable?

I would prefer to not reference external documentation. Can you please just ensure that the docstrings are (more or less) self explanatory?

@soraros
Copy link
Contributor

soraros commented May 20, 2024

@laszlokindrat My idea was that performant numerical code can contain a large amount of black magic, thus not very self-explanatory. If we are indeed using an existing implementation (from a paper, libc++, etc) and include a reference to them, it would be less surprising to future maintainers when they see the said magic. If you think a link is not fitting for the doctoring, maybe we could simply leave a comment.

@LJ-9801 LJ-9801 force-pushed the bit-ceil-floor branch 2 times, most recently from f1d7fd4 to 6a38210 Compare May 21, 2024 07:15
@LJ-9801
Copy link
Contributor Author

LJ-9801 commented May 21, 2024

@laszlokindrat I have fixed all the requests. In terms of the reference, I do agree with @soraros in that numerical code does sometime contains "black magic". While these two functions seems pretty ok to understand, there might be functions that has wizardry implementation in the future and adding a reference can definitely help future maintainer. I have taken out the c++ reference. If there is a consent, I can put it back in.

Signed-off-by: Jiexiang Liu <jiexiangliu114@gmail.com>
Signed-off-by: Jiexiang Liu <jiexiangliu114@gmail.com>
Signed-off-by: Jiexiang Liu <jiexiangliu114@gmail.com>
Signed-off-by: Jiexiang Liu <jiexiangliu114@gmail.com>
Comment on lines 357 to 361
alias ones = SIMD[type, simd_width].splat(1)

var less_then_one = (val <= ones).select(ones, val)

return (val > ones).select(1 << bit_width(val - ones), less_then_one)
Copy link
Contributor

@soraros soraros May 22, 2024

Choose a reason for hiding this comment

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

Could we simply write this? And similarly in bit_floor.

Suggested change
alias ones = SIMD[type, simd_width].splat(1)
var less_then_one = (val <= ones).select(ones, val)
return (val > ones).select(1 << bit_width(val - ones), less_then_one)
return (val <= 1).select(1, 1 << bit_width(val - 1))

Copy link
Contributor Author

Choose a reason for hiding this comment

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

just fixed it, thank!

Copy link
Contributor

Choose a reason for hiding this comment

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

Why do you need two select at all?

Copy link
Contributor Author

@LJ-9801 LJ-9801 May 23, 2024

Choose a reason for hiding this comment

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

ohhh my bad i misread your suggestions. I thought you just want to change the ones to 1.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

the added line seems to cause a crash in the reverse test sometime, I still kept the ones and did

(val > 1).select(1 << bit_width(val - ones), ones)

to avoid the crashing.

@laszlokindrat
Copy link
Contributor

If you think a link is not fitting for the doctoring, maybe we could simply leave a comment.

Yeah, a link within a comment in the implementation is fine, since that's a message to developers working on the stdlib. I just didn't want the generated docs to reference external links (and also, docstrings should explain what the function does, not how). @LJ-9801 could you please do this?

Copy link
Contributor

@laszlokindrat laszlokindrat left a comment

Choose a reason for hiding this comment

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

LGTM, thanks!

@JoeLoser
Copy link
Collaborator

!sync

@modularbot modularbot added the imported-internally Signals that a given pull request has been imported internally. label May 22, 2024
stdlib/src/bit/bit.mojo Outdated Show resolved Hide resolved
Signed-off-by: Jiexiang Liu <jiexiangliu114@gmail.com>
@LJ-9801
Copy link
Contributor Author

LJ-9801 commented May 23, 2024

@laszlokindrat @JoeLoser mojo crash on test_reversed.mojo? test crashes sometime on my M1 mac as well. Most of the time they pass. see 332c152

Signed-off-by: Jiexiang Liu <jiexiangliu114@gmail.com>
@laszlokindrat
Copy link
Contributor

!sync

@modularbot
Copy link
Collaborator

✅🟣 This contribution has been merged 🟣✅

Your pull request has been merged to the internal upstream Mojo sources. It will be reflected here in the Mojo repository on the nightly branch during the next Mojo nightly release, typically within the next 24-48 hours.

We use Copybara to merge external contributions, click here to learn more.

@modularbot modularbot added merged-internally Indicates that this pull request has been merged internally merged-externally Merged externally in public mojo repo labels May 26, 2024
@modularbot
Copy link
Collaborator

Landed in abfdac1! Thank you for your contribution 🎉

modularbot pushed a commit that referenced this pull request May 27, 2024
[External] [stdlib] implement `bit_ceil` and `bit_floor`

fix #2682

Co-authored-by: Jiexiang Liu <80805665+LJ-9801@users.noreply.github.com>
Closes #2736
MODULAR_ORIG_COMMIT_REV_ID: 3599b7ce4aba5d369ffb7e0fc09d1571dfb53f1e
@modularbot modularbot closed this May 27, 2024
Av1nag pushed a commit to Av1nag/mojo that referenced this pull request May 27, 2024
[External] [stdlib] implement `bit_ceil` and `bit_floor`

fix modularml#2682

Co-authored-by: Jiexiang Liu <80805665+LJ-9801@users.noreply.github.com>
Closes modularml#2736
MODULAR_ORIG_COMMIT_REV_ID: 3599b7ce4aba5d369ffb7e0fc09d1571dfb53f1e
Av1nag pushed a commit to Av1nag/mojo that referenced this pull request May 27, 2024
[External] [stdlib] implement `bit_ceil` and `bit_floor`

fix modularml#2682

Co-authored-by: Jiexiang Liu <80805665+LJ-9801@users.noreply.github.com>
Closes modularml#2736
MODULAR_ORIG_COMMIT_REV_ID: 3599b7ce4aba5d369ffb7e0fc09d1571dfb53f1e

Signed-off-by: Avinag <udayagiriavinag@gmail.com>
modularbot pushed a commit that referenced this pull request Jun 7, 2024
[External] [stdlib] implement `bit_ceil` and `bit_floor`

fix #2682

Co-authored-by: Jiexiang Liu <80805665+LJ-9801@users.noreply.github.com>
Closes #2736
MODULAR_ORIG_COMMIT_REV_ID: 3599b7ce4aba5d369ffb7e0fc09d1571dfb53f1e
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
imported-internally Signals that a given pull request has been imported internally. merged-externally Merged externally in public mojo repo merged-internally Indicates that this pull request has been merged internally
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants