Skip to content
New issue

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

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

Already on GitHub? # to your account

Refactored UInt32 and UInt64 methods nextPowerOf2 #371

Merged
merged 4 commits into from
May 1, 2018

Conversation

adamnemecek
Copy link
Contributor

Refactored nextPowerOf2 methods.

Motivation:

Make them shorter and somewhat faster.

Modifications:

Changed the implementations of nextPowerOf2

Result:

Methods are shorter.

@swift-nio-bot
Copy link

Can one of the admins verify this patch?

1 similar comment
@swift-nio-bot
Copy link

Can one of the admins verify this patch?

@Lukasa
Copy link
Contributor

Lukasa commented Apr 28, 2018

@swift-nio-bot test this please

@adamnemecek adamnemecek changed the title * refactored UInt32 and UInt64 methods nextPowerOf2 Refactored UInt32 and UInt64 methods nextPowerOf2 Apr 28, 2018
n += 1

return n
return 1 << (64 - (self - 1).leadingZeroBitCount)
Copy link
Member

Choose a reason for hiding this comment

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

@adamnemecek ah nice, didn't know we had leadingZeroBitCount, hope that gets lowered to the LZCNT instruction.

You can actually get the number of bits a type has from Self.bitWidth. Then the code for all unsigned integer types will actually be the same. You can probably just put the extension on UnsignedInteger and you'll get it for all.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Copy link
Member

Choose a reason for hiding this comment

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

@adamnemecek awesome, thank you! TIL that godbolt supports Swift, how cool's that!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

You can guess who added the support. His name starts with A and ends with dam Nemecek.

Copy link
Member

@weissi weissi left a comment

Choose a reason for hiding this comment

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

that's awesome, TIL about leadingZeroBitCount. Just some minor nits

@@ -97,22 +97,10 @@ extension ByteBuffer {
extension UInt64 {
/// Returns the next power of two.
public func nextPowerOf2() -> UInt64 {
guard self > 0 else {
if self == 0 {
Copy link
Member

Choose a reason for hiding this comment

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

I don't think we should take this change. Happy to make it guard self != 0 else { ... } though. But the guard tells/should tell the compiler that this is the less likely case and it can optimise for that.

Copy link
Member

@normanmaurer normanmaurer left a comment

Choose a reason for hiding this comment

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

Looks great... thanks!

@tomerd
Copy link
Member

tomerd commented May 1, 2018

@swift-nio-bot test this please

Copy link
Member

@weissi weissi left a comment

Choose a reason for hiding this comment

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

awesome, thank you!

@weissi
Copy link
Member

weissi commented May 1, 2018

@swift-nio-bot test this please

@Lukasa Lukasa added the 🔨 semver/patch No public API change. label May 1, 2018
@Lukasa Lukasa added this to the 1.6.0 milestone May 1, 2018
@Lukasa
Copy link
Contributor

Lukasa commented May 1, 2018

@swift-nio-bot test this please

@Lukasa Lukasa merged commit a022954 into apple:master May 1, 2018
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
🔨 semver/patch No public API change.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants