This is an archive of the discontinued LLVM Phabricator instance.

[libc++] Implement std::gcd using the binary version
Needs ReviewPublic

Authored by serge-sans-paille on Mar 13 2023, 1:09 PM.

Details

Reviewers
cjdb
ldionne
philnik
Group Reviewers
Restricted Project
Summary

This implementation is ~two times faster than current implementation in my setup

Code inspired by https://en.algorithmica.org/hpc/algorithms/gcd/
which itself is inspired by https://lemire.me/blog/2013/12/26/fastest-way-to-compute-the-greatest-common-divisor/

Diff Detail

Event Timeline

Herald added a project: Restricted Project. · View Herald TranscriptMar 13 2023, 1:09 PM
serge-sans-paille requested review of this revision.Mar 13 2023, 1:09 PM

First time contribution to libc++, I apologize in advance if the style / conformance is not correct.

+ constexpr version
+ check for ub in bitwise shift.

Still get a

# command output:
*** /home/ssp/sources/llvm-project/libcxx/test/libcxx/transitive_includes/cxx2b.csv
--- /home/ssp/sources/llvm-project/_build/runtimes/runtimes-bins/test/libcxx/Output/transitive_includes.sh.cpp.dir/t.tmp/transitive_includes.csv
***************
*** 432,437 ****
--- 432,438 ----
  numbersversion
  numericcmath
// -*- C++ -*-
  numericcstddef
+ numericinitializer_list
  numericlimits
  numericversion
  optionalcompare

error: command failed with exit status: 1
********************
Failed Tests (1):
  llvm-libc++-shared.cfg.in :: libcxx/transitive_includes.sh.cpp
cjdb added a comment.Mar 13 2023, 3:33 PM

Thanks for working on this. I haven't contributed to libc++ in a very long time, so the project's policies may be different to what I'm asking for. Having said that, the following things would be good to do:

  • Please add a description to your commit message explaining why this change is valuable. If there's a GitHub issue, please also link to that (just add something like Closes #1234).
  • If this is a functional change, please add a test. If this is a non-functional change, please add [NFC] to the commit message.
  • It may be worth moving the #ifdef to the body of the function, rather than creating two identical overloads whose interfaces might accidentally diverge over time.
libcxx/include/__numeric/gcd_lcm.h
62

Please fully qualify all the function calls.

serge-sans-paille edited the summary of this revision. (Show Details)

Take review into account.

@cjdb : should be good now :-)

Fix variable naming.

rebased + fix intermediate type

philnik set the repository for this revision to rG LLVM Github Monorepo.May 22 2023, 3:31 PM
Herald added a project: Restricted Project. · View Herald TranscriptMay 22 2023, 3:31 PM
Herald added a reviewer: Restricted Project. · View Herald Transcript
philnik requested changes to this revision.May 22 2023, 3:40 PM
philnik added a subscriber: philnik.

Thanks for working on this!
Could you add the benchmark you used to libcxx/benchmarks and give us the exact results you get?

libcxx/include/__numeric/gcd_lcm.h
17

These should be included unconditionally.

63

Same throughout

64

Is there anything C++20-specific other than countr_zero? If no, then I think it would make sense to add a __countr_zero to older language modes and forward the countr_zero implementation.

66
This revision now requires changes to proceed.May 22 2023, 3:40 PM
serge-sans-paille marked 5 inline comments as done.May 25 2023, 1:38 AM

Neither quick bench nor I on my local system can reproduce a 4x speedup. In fact, the naive version is significantly faster than your version. Are you sure everything works as expected?

libcxx/include/__bit/countr.h
34–35

This doesn't work pre-C++20.

35–36
libcxx/include/__numeric/gcd_lcm.h
64

Same for other places.

libcxx/test/libcxx/transitive_includes/cxx26.csv
191–211

This shouldn't be in here anymore. Did you maybe use an older version to generate this?

Updated benchmark that doesn't focus on a single value: https://quick-bench.com/q/XEb2zlF7cpb_gSjUB2f71X4Hbw8

serge-sans-paille edited the summary of this revision. (Show Details)May 26 2023, 2:16 AM
philnik added inline comments.Aug 3 2023, 1:43 PM
libcxx/include/__bit/countr.h
28

Please add the benchmarks under libcxx/benchmarks/.

libcxx/include/__numeric/gcd_lcm.h
54

Could you add a short comment explaining how the algorithm works?