1/*
2 * Copyright 2015-present Facebook, Inc.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include <folly/experimental/Select64.h>
18
19#include <cstdint>
20
21#include <folly/ConstexprMath.h>
22#include <folly/Portability.h>
23#include <folly/Utility.h>
24
25namespace folly {
26namespace detail {
27
28namespace {
29
30constexpr std::uint8_t selectInByte(std::size_t i, std::size_t j) {
31 auto r = std::uint8_t(0);
32 while (j--) {
33 auto const s = folly::constexpr_find_first_set(i);
34 r += s;
35 i >>= s;
36 }
37 return i == 0 ? 8 : r + folly::constexpr_find_first_set(i) - 1;
38}
39
40template <std::size_t... I, std::size_t J>
41constexpr auto makeSelectInByteNestedArray(
42 index_sequence<I...>,
43 index_constant<J>) {
44 return std::array<std::uint8_t, sizeof...(I)>{{selectInByte(I, J)...}};
45}
46
47template <typename Is, std::size_t... J>
48constexpr auto makeSelectInByteArray(Is is, index_sequence<J...>) {
49 using inner = std::array<std::uint8_t, Is::size()>;
50 using outer = std::array<inner, sizeof...(J)>;
51 return outer{{makeSelectInByteNestedArray(is, index_constant<J>{})...}};
52}
53
54} // namespace
55
56FOLLY_STORAGE_CONSTEXPR std::array<std::array<std::uint8_t, 256>, 8> const
57 kSelectInByte = makeSelectInByteArray(
58 make_index_sequence<256>{},
59 make_index_sequence<8>{});
60
61} // namespace detail
62} // namespace folly
63