Skip to main content
  • Home
  • Development
  • Documentation
  • Donate
  • Operational login
  • Browse the archive

swh logo
SoftwareHeritage
Software
Heritage
Archive
Features
  • Search

  • Downloads

  • Save code now

  • Add forge now

  • Help

  • 40d4000
  • /
  • benchmark
  • /
  • bench_cmp.cpp
Raw File Download

To reference or cite the objects present in the Software Heritage archive, permalinks based on SoftWare Hash IDentifiers (SWHIDs) must be used.
Select below a type of object currently browsed in order to display its associated SWHID and permalink.

  • content
  • directory
content badge
swh:1:cnt:48a34a9d10f8a40b7328311b74ac70d24cbe4d83
directory badge
swh:1:dir:aaece065fa536102698ec5d8c99644a5c47f5236

This interface enables to generate software citations, provided that the root directory of browsed objects contains a citation.cff or codemeta.json file.
Select below a type of object currently browsed in order to generate citations for them.

  • content
  • directory
Generate software citation in BibTex format (requires biblatex-software package)
Generating citation ...
Generate software citation in BibTex format (requires biblatex-software package)
Generating citation ...
bench_cmp.cpp
#include <cassert>
#include <cstdint>
#include <iostream>
#include <limits>
#include <random>
#include <string>
#include <utility>

#include <tdc/intrisics/pcmp.hpp>
#include <tdc/intrisics/tzcnt.hpp>
#include <tdc/stat/phase.hpp>

#include <tlx/cmdline_parser.hpp>

template<typename elem_t, typename matrix_t>
size_t rank_linear(const elem_t e, const matrix_t& m) {
    constexpr size_t num = sizeof(matrix_t) / sizeof(elem_t);
    const elem_t* array = (const elem_t*)&m;
    size_t i = 0;
    while(i < num && e >= array[i]) {
        ++i;
    }
    return i;
}

struct {
    size_t num = 1'000'000;
    size_t seed = 147;
    uint64_t matrix = 0xF3'E2'CC'91'7F'44'25'01ULL;
} options;

template<typename T>
struct repeat;

template<>
struct repeat<uint8_t> {
    inline uint64_t operator()(const uint8_t x) {
        return uint64_t(x) * 0x01'01'01'01'01'01'01'01ULL;
    }
};

template<typename elem_t, typename matrix_t>
size_t rank_pcmp(const elem_t e, const matrix_t& m) {
    const matrix_t e_repeat = repeat<elem_t>{}(e);
    const auto cmp = tdc::intrisics::pcmpgtu<matrix_t, elem_t>(m, e_repeat);
    const size_t rank = tdc::intrisics::tzcnt0(cmp) / std::numeric_limits<elem_t>::digits;
#ifndef NDEBUG
    const size_t correct = rank_linear(e, m);
    assert(rank == correct);
#endif
    return rank;
}

template<typename F>
void bench(const std::string& name, F rank_func) {
    std::default_random_engine gen(options.seed);
    std::uniform_int_distribution<uint8_t> dist(0, UINT8_MAX);
    
    tdc::stat::Phase phase("rank");
    size_t chk = 0;
    for(size_t i = 0; i < options.num; i++) {
        chk += rank_func(dist(gen), options.matrix);
    }
    
    auto guard = phase.suppress();
    phase.log("chk", chk);
    std::cout << "RESULT algo=" << name << " " << phase.to_keyval() << std::endl;
}

int main(int argc, char** argv) {
    tlx::CmdlineParser cp;
    cp.add_bytes('n', "num", options.num, "The length of the sequence (default: 1M).");
    cp.add_bytes('s', "seed", options.seed, "The random seed.");
    if(!cp.process(argc, argv)) {
        return -1;
    }

    bench("linear", [](uint8_t x, uint64_t m){ return rank_linear(x, m); });
    bench("pcmp", [](uint8_t x, uint64_t m){ return rank_pcmp(x, m); });
}

back to top

Software Heritage — Copyright (C) 2015–2025, The Software Heritage developers. License: GNU AGPLv3+.
The source code of Software Heritage itself is available on our development forge.
The source code files archived by Software Heritage are available under their own copyright and licenses.
Terms of use: Archive access, API— Content policy— Contact— JavaScript license information— Web API