summaryrefslogtreecommitdiffstats
path: root/libc/src/__support/number_pair.h
blob: ee6667b1299fe8a8d238607399dce8993cc79e6c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
//===-- Utilities for pairs of numbers. -------------------------*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#ifndef LLVM_LIBC_SRC___SUPPORT_NUMBER_PAIR_H
#define LLVM_LIBC_SRC___SUPPORT_NUMBER_PAIR_H

#include "CPP/type_traits.h"

#include <stddef.h>

namespace LIBC_NAMESPACE {

template <typename T> struct NumberPair {
  T lo = T(0);
  T hi = T(0);
};

template <typename T>
cpp::enable_if_t<cpp::is_integral_v<T> && cpp::is_unsigned_v<T>,
                 NumberPair<T>> constexpr split(T a) {
  constexpr size_t HALF_BIT_WIDTH = sizeof(T) * 4;
  constexpr T LOWER_HALF_MASK = (T(1) << HALF_BIT_WIDTH) - T(1);
  NumberPair<T> result;
  result.lo = a & LOWER_HALF_MASK;
  result.hi = a >> HALF_BIT_WIDTH;
  return result;
}

} // namespace LIBC_NAMESPACE

#endif // LLVM_LIBC_SRC___SUPPORT_NUMBER_PAIR_H