Skip to content

Commit

Permalink
[misc] Update "get_largest_pot" in scalar.h + Bug Fix (#3405)
Browse files Browse the repository at this point in the history
* Update scalar.h

Find the largest potence faster.

* Update scalar.h

* Update scalar.h

Fixed a bug

* Update scalar.h

Manually edited formatting errors.

* Update scalar.h

retry workflow

* Create scalar.h

retry workflow
  • Loading branch information
NiclasSchwalbe authored Nov 7, 2021
1 parent 8ad7468 commit 0e4ca37
Showing 1 changed file with 6 additions and 5 deletions.
11 changes: 6 additions & 5 deletions taichi/math/scalar.h
Original file line number Diff line number Diff line change
Expand Up @@ -161,12 +161,13 @@ TI_FORCE_INLINE bool abnormal(T m) noexcept {
inline int64 get_largest_pot(int64 a) noexcept {
TI_ASSERT_INFO(a > 0,
"a should be positive, instead of " + std::to_string(a));
// TODO: optimize
int64 i = 1;
while (i <= a / 2) {
i *= 2;

/* This code was copied from https://stackoverflow.com/a/20207950 and edited
It uses loop unrolling, which all (modern) compilers will do. */
for (int64 i = 1; i < 64; i *= 2) {
a |= (a >> i);
}
return i;
return a - (a >> 1);
}

TI_NAMESPACE_END

0 comments on commit 0e4ca37

Please sign in to comment.