Skip to content

feat: add bitmap utility functions #11

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

winding-lines
Copy link
Collaborator

Add some bitmap utility functions and related tests.

@winding-lines winding-lines requested a review from Copilot May 12, 2025 02:10
Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR adds new bitmap utility functions to the buffers module and corresponding tests, alongside introducing helper functions in the fixtures for boolean arrays.

  • Added new bitmap functions (e.g., bit_count, count_leading_bits, partial_byte_set, unsafe_range_set) in the buffers module.
  • Updated tests in test_buffers.mojo to verify bitmap behavior.
  • Created a new arrays.mojo file with helper functions (including assert_bitmap_set) to support tests.

Reviewed Changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.

File Description
firebolt/tests/test_buffers.mojo Added tests to validate new bitmap functions.
firebolt/test_fixtures/arrays.mojo New helper functions for boolean arrays and bitmap assertions.
firebolt/buffers.mojo Introduced bitmap utilities and related bit-counting functions.

@@ -82,6 +88,23 @@ struct Buffer(Movable):
else:
return self.size // sizeof[T]()

fn bit_count(self) -> Int:
Copy link
Owner

Choose a reason for hiding this comment

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

Shouldn't this be defined on the Bitmap struct?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Changed.

def test_count_leading_bits():
var b = Bitmap.alloc(10)
var expected_bits = b.length()
assert_equal(b.count_leading_bits(), expected_bits)
Copy link
Owner

Choose a reason for hiding this comment

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

count_leading_bits() is a little bit confusing, I would expect to count the set bits by default. Maybe we should rename it to count_leading_zeros()/count_leading_ones()?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Agree, added nicer APIs on top of the existing implementation.

@kszucs kszucs changed the title Feat: add bitmap utility functions feat: add bitmap utility functions Jun 11, 2025
)


@always_inline
Copy link
Owner

Choose a reason for hiding this comment

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

I assume it doesn't need to be inlines since it is a testing utility.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This is needed to get a nice error message for failed tests. Removing the @always_inline decoration leads to

open-source/max/mojo/stdlib/stdlib/builtin/_location.mojo:111:6: error: call location was not inlined the specified number of times: requires 1 more time(s)
Included from /var/folders/yg/_vw4ktqx5dn6t75j_hng0tvc0000gn/T/test-ca6050.mojo:8:
Included from /Users/mseritan/dev/typed-data/firebolt/bitmap-utility-functions/firebolt/tests/test_buffers.mojo:3:
/Users/mseritan/dev/typed-data/firebolt/bitmap-utility-functions/./firebolt/test_fixtures/arrays.mojo:66:37: note: called from
            location=__call_location(),

assert_equal(b.count_leading_bits(1), expected_bits - 1)
b.unsafe_set(0, False)

var to_test = List(0, 1, 7, 8, 10, 16, 31)
Copy link
Owner

Choose a reason for hiding this comment

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

Seems like this list is not being used just its length.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Good catch, that was a bug in the test.

bitmap.unsafe_range_set(0, 0, True)
assert_bitmap_set(bitmap, List[Int](), "range 0")

var to_test = List(0, 1, 7, 8, 15)
Copy link
Owner

@kszucs kszucs Jun 11, 2025

Choose a reason for hiding this comment

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

Out of curiosity, is it possible to use list literals in mojo nowadays?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Just tried it and it seems that Chris Lattner fixed that.

@kszucs
Copy link
Owner

kszucs commented Jun 11, 2025

Also there are bit utilities in the stdlib which we could use here https://docs.modular.com/mojo/stdlib/bit/bit/

@winding-lines winding-lines force-pushed the feat/bitmap-utility-functions branch from f2ea541 to 984c764 Compare June 15, 2025 16:53
@winding-lines
Copy link
Collaborator Author

Also there are bit utilities in the stdlib which we could use here https://docs.modular.com/mojo/stdlib/bit/bit/

They don't take a start position, I based this code on that implementation.

@winding-lines
Copy link
Collaborator Author

winding-lines commented Jun 15, 2025

@kszucs ready for re-review 🤗

@kszucs kszucs self-requested a review June 18, 2025 14:49
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.

2 participants