This is an archive of the discontinued LLVM Phabricator instance.

[libcxx] Use integer division
ClosedPublic

Authored by phosek on Mar 29 2021, 10:52 AM.

Details

Reviewers
ldionne
Quuxplusone
curdeius
Group Reviewers
Restricted Project
Commits
rGbc4d3ca7bd44: [libcxx] Use integer division
Summary

In Python 3, math.floor returns int when both arguments are ints.
In Python 2, math.floor returns float. This leads to a failure
because the result of math.floor is used as an array index. While
Python 2 is on its way out, it's still used in some places so use an
integer division instead.

Diff Detail

Event Timeline

phosek created this revision.Mar 29 2021, 10:52 AM
phosek requested review of this revision.Mar 29 2021, 10:52 AM
Herald added a project: Restricted Project. · View Herald TranscriptMar 29 2021, 10:52 AM
Herald added a reviewer: Restricted Project. · View Herald Transcript
Quuxplusone requested changes to this revision.Mar 29 2021, 11:03 AM
Quuxplusone added a subscriber: Quuxplusone.
Quuxplusone added inline comments.
libcxx/utils/gdb/libcxx/printers.py
442

Even better: word = int(bit / self.bits_per_word)
(Note that math.floor rounds upward when the argument is negative; but we don't ever expect the argument to be negative here, so "cast to int" is exactly the right semantics.)

Even more better: word = bit // self.bits_per_word
to just use integer division in the first place. This is the best option, because // (line 442) and % (line 443) go together nicely as a pair.

This revision now requires changes to proceed.Mar 29 2021, 11:03 AM
phosek updated this revision to Diff 333937.Mar 29 2021, 11:24 AM
phosek retitled this revision from [libcxx] Convert the result of math.floor to int to [libcxx] Use integer division.
phosek edited the summary of this revision. (Show Details)

(Wait for a second libc++ reviewer to give it the green light; then) Ship it!

curdeius accepted this revision.Mar 29 2021, 11:53 AM
curdeius added a subscriber: curdeius.

LGTM.

This revision is now accepted and ready to land.Mar 29 2021, 11:53 AM
This revision was landed with ongoing or failed builds.Mar 29 2021, 12:00 PM
This revision was automatically updated to reflect the committed changes.