Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .bazelrc
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
# Makes bazel test always print errors.
test --test_output=errors
test --test_output=errors
build --copt=-ftemplate-backtrace-limit=0
2 changes: 2 additions & 0 deletions .clangd
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
CompileFlags:
Add: ['-march=native']
5 changes: 5 additions & 0 deletions best/base/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ cc_library(
deps = [":macro"],
)

cc_library(
name = "arch",
hdrs = ["arch.h"],
)

cc_library(
name = "macro",
hdrs = [
Expand Down
109 changes: 109 additions & 0 deletions best/base/arch.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
/* //-*- C++ -*-///////////////////////////////////////////////////////////// *\

Copyright 2024
Miguel Young de la Sota and the Best Contributors 🧶🐈‍⬛

Licensed under the Apache License, Version 2.0 (the "License"); you may not
use this file except in compliance with the License. You may obtain a copy
of the License at

https://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
License for the specific language governing permissions and limitations
under the License.

\* ////////////////////////////////////////////////////////////////////////// */

#ifndef BEST_BASE_INTRINSICS_H_
#define BEST_BASE_INTRINSICS_H_

//! Helpers for working with vendor intrinsics. This file will include all
//! the relevant intrinsics headers *and* will define macros for detecting what
//! features are statically available.
//!
//! Every macro is always defined; they should be used with `#if`, not `#ifdef`.

/// # `BEST_X86`
///
/// True if we're targeting `x86_64`.
#ifdef __x86_64__
#define BEST_X86 1
#else
#define BEST_X86 0
#endif

/// # `BEST_AARCH64`
///
/// True if we're targeting `aarch64`.
#ifdef __aarch64__
#define BEST_AARCH64 1
#else
#define BEST_AARCH64 0
#endif

#if !(BEST_X86 || BEST_AARCH64)
#error "unsupported architecture :("
#endif

/// # `BEST_SSE`, `BEST_SSE2`, `BEST_SSE3`, `BEST_SSSE3`
/// # `BEST_SSE41`, `BEST_SSE42`, `BEST_SSE4A`
///
/// True if the named x86 extension is statically available.
#ifdef __SSE__
#define BEST_SSE 1
#else
#define BEST_SSE 0
#endif
#ifdef __SSE2__
#define BEST_SSE2 1
#else
#define BEST_SSE2 0
#endif
#ifdef __SSE3__
#define BEST_SSE3 1
#else
#define BEST_SSE3 0
#endif
#ifdef __SSSE3__
#define BEST_SSSE3 1
#else
#define BEST_SSSE3 0
#endif
#ifdef __SSE4_1__
#define BEST_SSE41 1
#else
#define BEST_SSE41 0
#endif
#ifdef __SSE4_2__
#define BEST_SSE42 1
#else
#define BEST_SSE42 0
#endif
#ifdef __SSE4A__
#define BEST_SSE4A 1
#else
#define BEST_SSE4A 0
#endif

/// # `BEST_AVX`, `BEST_AVX2`
///
/// True if the named x86 extension is statically available.
#ifdef __AVX__
#define BEST_AVX 1
#else
#define BEST_AVX 0
#endif
#ifdef __AVX2__
#define BEST_AVX2 1
#else
#define BEST_AVX2 0
#endif

#if BEST_X86
#include <x86intrin.h>
#endif // BEST_X86

#endif // BEST_BASE_INTRINSICS_H_
4 changes: 4 additions & 0 deletions best/base/fwd.h
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,10 @@ struct access;
namespace dyn_internal {
struct access;
}
namespace format_internal {
template <typename, typename...>
class templ;
}
} // namespace best

#endif // BEST_BASE_FWD_H_
27 changes: 27 additions & 0 deletions best/base/hint.h
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,33 @@ BEST_INLINE_ALWAYS constexpr void assume(bool truth) {
if (!std::is_constant_evaluated()) { asm volatile("" ::"m,r"(value)); }
return decltype(value)(value);
}

/// # `best::prefetch_locality`
///
/// A locality value for a prefetch operation. Specifies which cache the
/// prefetched memory should stick around in.
enum class prefetch_locality {
Nontemporal = 0,
L3 = 1,
L2 = 2,
L1 = 3,
};

/// # `best::prefetch_for_read()`, `best::prefetch_for_write()`
///
/// Prefetches the given memory.
template <best::prefetch_locality locality = best::prefetch_locality::L1>
BEST_INLINE_SYNTHETIC constexpr void prefetch_for_read(const void* addr) {
if (!std::is_constant_evaluated()) {
__builtin_prefetch(addr, 0, int(locality));
}
}
template <best::prefetch_locality locality = best::prefetch_locality::L1>
BEST_INLINE_SYNTHETIC constexpr void prefetch_for_write(const void* addr) {
if (!std::is_constant_evaluated()) {
__builtin_prefetch(addr, 1, int(locality));
}
}
} // namespace best

#endif // BEST_BASE_HINT_H_
36 changes: 36 additions & 0 deletions best/container/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ cc_library(
":pun",
"//best/base:port",
"//best/base:tags",
"//best/hash",
"//best/log/internal:crash",
],
)
Expand All @@ -31,6 +32,7 @@ cc_library(
deps = [
"//best/base:ord",
"//best/memory:ptr",
"//best/hash",
]
)

Expand All @@ -49,6 +51,7 @@ cc_library(
hdrs = ["option.h"],
deps = [
":choice",
"//best/hash",
"//best/log/internal:crash",
],
)
Expand Down Expand Up @@ -97,6 +100,7 @@ cc_library(
deps = [
":choice",
":row",
"//best/hash",
"//best/log/internal:crash",
],
)
Expand All @@ -122,6 +126,7 @@ cc_library(
],
deps = [
":object",
"//best/hash",
"//best/base:port",
"//best/base:tags",
],
Expand All @@ -143,6 +148,7 @@ cc_library(
hdrs = ["box.h"],
deps = [
"//best/func:dyn",
"//best/hash",
"//best/memory:allocator",
"//best/memory:ptr",
]
Expand Down Expand Up @@ -187,6 +193,36 @@ cc_test(
"//best/test:fodder",
],
)
hash
cc_library(
name = "table",
hdrs = [
"table.h",
"internal/table.h",
],
deps = [
":option",
"//best/base:arch",
"//best/hash",
"//best/hash:fxhash",
"//best/hash:impls",
"//best/memory:allocator",
"//best/memory:span",
"//best/memory:layout",
"//best/meta/traits:pairs",
],
)

cc_test(
name = "table_test",
srcs = ["table_test.cc"],
linkopts = ["-rdynamic"],
deps = [
":table",
"//best/test",
"//best/test:fodder",
],
)

cc_library(
name = "simple_option",
Expand Down
8 changes: 8 additions & 0 deletions best/container/box.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include "best/container/object.h"
#include "best/container/option.h"
#include "best/func/dyn.h"
#include "best/hash/hash.h"
#include "best/memory/allocator.h"
#include "best/memory/layout.h"
#include "best/memory/ptr.h"
Expand Down Expand Up @@ -260,6 +261,13 @@ class BEST_RELOCATABLE box final {
};
}

template <best::hash_state State>
constexpr friend void BestHash(best::hasher<State>& h, const box& value)
requires requires { h.write(*value); }
{
h.write(*value);
}

template <typename U>
constexpr bool operator==(const best::box<U>& that) const
requires best::equatable<best::view<T>, best::view<U>>
Expand Down
10 changes: 10 additions & 0 deletions best/container/choice.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,11 @@
#include "best/base/ord.h"
#include "best/base/tags.h"
#include "best/container/internal/choice.h"
#include "best/hash/hash.h"
#include "best/log/internal/crash.h"
#include "best/log/location.h"
#include "best/meta/init.h"
#include "best/meta/traits/empty.h"

//! A sum type, like `std::variant`.
//!
Expand Down Expand Up @@ -329,6 +331,14 @@ class choice final {
};
}

template <best::hash_state State>
constexpr friend void BestHash(best::hasher<State>& h, const choice& value)
requires (best::hashable<Alts> && ...)
{
value.index_match(
[&](auto idx, const auto& value) { h.write(idx.value, value); });
}

// Comparisons.
template <typename... Us>
BEST_INLINE_ALWAYS constexpr bool operator==(const choice<Us...>& that) const
Expand Down
Loading
Loading