Shamrock 2025.10.0
Astrophysical Code
Loading...
Searching...
No Matches
upper_bound.hpp
Go to the documentation of this file.
1// -------------------------------------------------------//
2//
3// SHAMROCK code for hydrodynamics
4// Copyright (c) 2021-2026 Timothée David--Cléris <tim.shamrock@proton.me>
5// SPDX-License-Identifier: CeCILL Free Software License Agreement v2.1
6// Shamrock is licensed under the CeCILL 2.1 License, see LICENSE for more information
7//
8// -------------------------------------------------------//
9
10#pragma once
11
19#include "shambase/assert.hpp"
20
21namespace shamalgs::primitives {
22
24 template<class Tkey>
26 const Tkey *__restrict__ key, u32 first, u32 last, const Tkey &value) {
27
28 // modified from https://mhdm.dev/posts/sb_lower_bound/
29
30 auto length = last - first;
31 while (length > 0) {
32 auto rem = length % 2;
33 length /= 2;
34 if (key[first + length] <= value) {
35 first += length + rem;
36 }
37 }
38
39 return first;
40 }
41} // namespace shamalgs::primitives
std::uint32_t u32
32 bit unsigned integer
Shamrock assertion utility.
namespace for primitive algorithm (e.g. sort, scan, reductions, ...)
constexpr u32 binary_search_upper_bound(const Tkey *__restrict__ key, u32 first, u32 last, const Tkey &value)
GPU compatible implementation of std::upper_bound.