# C++ 动态新闻推送 第15期

reddit/hackernews/lobsters/meetingcpp摘抄一些c++动态。

discord讨论群组 ｜飞书讨论群组｜知乎专栏

## 文章

Daniel Lemire 整的新活，如何更快的计算一个数有几位，正常的算法就是除10

log2(X)简单

`int int_log2(uint32_t x) { return 31 - __builtin_clz(x|1); }`

```    static uint32_t table[] = {9, 99, 999, 9999, 99999,
999999, 9999999, 99999999, 999999999};
int y = (9 * int_log2(x)) >> 5;
y += x > table[y];
return y + 1;```

luajit用到了类似的技巧 这里比乘9除32更精密一些

```/* min(2^32-1, 10^e-1) for e in range 0 through 10 */
static uint32_t ndigits_dec_threshold[] = {
0, 9U, 99U, 999U, 9999U, 99999U, 999999U,
9999999U, 99999999U, 999999999U, 0xffffffffU
};

/* Compute the number of digits in the decimal representation of x. */
static MSize ndigits_dec(uint32_t x)
{
MSize t = ((lj_fls(x | 1) * 77) >> 8) + 1; /* 2^8/77 is roughly log2(10) */
return t + (x > ndigits_dec_threshold[t]);
}```

```int fast_digit_count(uint32_t x) {
static uint64_t table[] = {
4294967296,  8589934582,  8589934582,  8589934582,  12884901788,
12884901788, 12884901788, 17179868184, 17179868184, 17179868184,
21474826480, 21474826480, 21474826480, 21474826480, 25769703776,
25769703776, 25769703776, 30063771072, 30063771072, 30063771072,
34349738368, 34349738368, 34349738368, 34349738368, 38554705664,
38554705664, 38554705664, 41949672960, 41949672960, 41949672960,
42949672960, 42949672960};
return (x + table[int_log2(x)]) >> 32;
}```

table的数用脚本找的

```#include <iostream>

class MyType {
public:
using type = char;
};

class MyOtherType {
public:
using other_type = int;
};

template<typename T>
void foo(T bar, typename T::type baz)
{
std::cout << "void foo(T bar, typename T::type baz) is called\n";
}

template<typename T>
void foo(T bar, typename T::other_type baz)
{
std::cout << "void foo(T bar, typename T::other_type baz) is called\n";
}

int main()
{
MyType m;
MyOtherType mo;
foo(m, 'a');
foo(mo, 42);
// error: no matching function for call to 'foo(MyOtherType&, const char [3])'
// foo(mo, "42");
}
/*
void foo(T bar, typename T::type baz) is called
void foo(T bar, typename T::other_type baz) is called
*/```

decltype std::declval

```#include <iostream>

class MyType {
public:
using type = char;
};

class MyOtherType {
public:
using other_type = int;
};

template<typename T>
decltype(typename T::type(), void()) foo(T bar)
{
std::cout << "decltype(typename T::type(), void()) foo(T bar) is called\n";
}

template<typename T>
decltype(typename T::other_type(), void()) foo(T bar)
{
std::cout << "decltype(typename T::other_type(), void()) is called\n";
}

int main()
{
MyType m;
MyOtherType mo;
foo(m);
foo(mo);
// error: no matching function for call to 'foo(MyOtherType&, const char [3])'
// foo(mo, "42");
}```

```template<typename T>
std::enable_if_t<std::is_integral<T>::value, T> f(T t){
//integral version
}
template<typename T>
std::enable_if_t<std::is_floating_point<T>::value, T> f(T t){
//floating point version
}```

concept

```#include <concepts>

template<typename T>
class MyClass {
public:
void f(T x) {
std::cout << "generic\n";
}

void f(T x) requires std::floating_point<T> {
std::cout << "with enable_if\n";
}
};```

```class foo {
private:
int data;
};

template<int foo::*Ptr>
int& get_data(foo& f) {
return f.*Ptr;
}

template<int foo::*Ptr>
struct foo_access {
friend int& get_data(foo& f) {
return f.*Ptr;
}
};

template struct foo_access<&foo::data>;
int& get_data(foo&);

int main() {
foo f{};
get_data(f) = 42; // access private data member
}```

```// \file compile-time-cpp/is-prime-17-constexpr-func.cc
#include <iostream>

constexpr bool is_prime(int v) {
for (int i = 2; i < v; i++) {
if (v % i == 0) {
return false;
}
}
return true;
}

template<int v>
struct IsPrime {
static constexpr bool value = is_prime(v);
};

int main() {
std::cout << 7 << " : " << IsPrime<7>::value << std::endl;
std::cout << 2000 << " : " << IsPrime<2000>::value << std::endl;
std::cout << 2003 << " : " << IsPrime<2003>::value << std::endl;

return 0;
}```

```#include <iostream>
#include <array>

template<int v>
consteval std::array<int, v + 1> sieve() {
std::array<int, v + 1> arr = {};
for(long long i = 2; i <= v; i++) {
if(arr[i]) {
continue;
}
for(long long j = i * i; j <= v; j+= i) {
arr[j] = 1;
}
}
return arr;
}

int main() {
auto sieve_array = sieve<12345>();
std::cout << 7 << " : " << sieve_array[7] << std::endl;
std::cout << 2000 << " : " << sieve_array[2000]<< std::endl;
std::cout << 2003 << " : " << sieve_array[2003]<< std::endl;

size_t i = 0;
std::cin >> i;
std::cout << sieve_array[i] << std::endl;

return 0;
}```

```#ifdef __has_builtin
#if __has_builtin(__type_pack_element)
#define MZ_HAS_TYPE_PACK_ELEMENT
#endif
#endif

#ifdef MZ_HAS_TYPE_PACK_ELEMENT

template <typename... T, size_t N>
struct type_list_selector<type_list<T...>, N>
{
using type = __type_pack_element<N, T...>;
};

#else

// ... all the previous type_list_selectors ...

#endif```

```__attribute__((always_inline)) inline void do_thing(int input)
{
// this code is always inlined at the call site
}

void hot_code()
{
// the program spends >80% of its runtime in this function
while (condition) {
...
do_thing(y);
...
}
}
void cool_code()
{
// the program spends <5% of its runtime in this function
...
do_thing(a);
do_thing(b);
do_thing(c);
}```

```void do_thing(int input)
{
// this code is not always inlined at the call site
}

__attribute__((flatten)) void hot_code()
{
// the program spends >80% of its runtime in this function
while (condition) {
call_something();   // inlined!
do_thing(y);        // inlined!
other_thing();      // also inlined!
}
}

void cool_code()
{
// the program spends <5% of its runtime in this function
...
do_thing(a);            // not inlined!
do_thing(b);            // not inlined!
do_thing(c);            // guess!
}```

TODO: 看不懂

## 项目

• https://github.com/Tencent/flare 腾讯出品的一个业务库，嵌入了各种常用客户端/rpc等等
• https://github.com/joaquintides/transrangers 更快的range
• oceanbase/oceanbase oceanbase又开源了
• https://github.com/jk-jeon/dragonbox/tree/1.0.0 高效的float-to-string算法，且合入了fmt库https://github.com/fmtlib/fmt/pull/1882

0 条评论

• ### C++ 动态新闻推送 第35期

怎么保证一个函数只被调用一次呢，这里有个点子，Destructive separation: move away and call Matt Godbolt a...

• ### C++ 动态新闻推送 第34期

从reddit/hackernews/lobsters/meetingcpp摘抄一些c++动态

• ### C++ 动态新闻推送 第33期

很多代码场景下c++的灵活性要高于强制安全检查，且一些场景下rust生成的汇编不如c++少

• ### C++ 动态新闻推送 第32期

从reddit/hackernews/lobsters/meetingcpp摘抄一些c++动态

• ### C++ 动态新闻推送 第31期

从reddit/hackernews/lobsters/meetingcpp摘抄一些c++动态

• ### C++ 动态新闻推送 第30期

从reddit/hackernews/lobsters/meetingcpp摘抄一些c++动态。

• ### C++ 动态新闻推送 第20期

从reddit/hackernews/lobsters/meetingcpp摘抄一些c++动态。

• ### C++ 动态新闻推送 第14期

从reddit/hackernews/lobsters/meetingcpp摘抄一些c++动态。

• ### C++ 动态新闻推送 第5期

从reddit/hackernews/lobsters/meetingcpp摘抄一些c++动态。

• ### C++ 动态新闻推送 第4期

从reddit/hackernews/lobsters/meetingcpp摘抄一些c++动态。

• ### C++ 动态新闻推送 第3期

从reddit/hackernews/lobsters/meetingcpp摘抄一些c++动态。

• ### C++ 动态新闻推送 第2期

从reddit/hackernews/lobsters/meetingcpp摘抄一些c++动态。

• ### C++ 动态新闻推送 第19期

从reddit/hackernews/lobsters/meetingcpp摘抄一些c++动态。

• ### C++ 动态新闻推送 第18期

从reddit/hackernews/lobsters/meetingcpp摘抄一些c++动态。

• ### C++ 动态新闻推送 第17期

从reddit/hackernews/lobsters/meetingcpp摘抄一些c++动态。

• ### C++ 动态新闻推送 第13期

从reddit/hackernews/lobsters/meetingcpp摘抄一些c++动态。

• ### C++ 动态新闻推送 第11期

从reddit/hackernews/lobsters/meetingcpp摘抄一些c++动态。

• ### C++ 动态新闻推送 第10期

从reddit/hackernews/lobsters/meetingcpp摘抄一些c++动态。

• ### C++ 动态新闻推送 第9期

从reddit/hackernews/lobsters/meetingcpp摘抄一些c++动态。