Intel HEXL
Intel Homomorphic Encryption Acceleration Library, accelerating the modular arithmetic operations used in homomorphic encryption.
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
gcc.hpp
Go to the documentation of this file.
1 // Copyright (C) 2020-2021 Intel Corporation
2 // SPDX-License-Identifier: Apache-2.0
3 
4 #pragma once
5 #include <cmath>
6 #include <iostream>
7 
8 #include "hexl/util/check.hpp"
9 #include "hexl/util/types.hpp"
10 
11 namespace intel {
12 namespace hexl {
13 
14 #ifdef HEXL_USE_GNU
15 // Return x * y as 128-bit integer
16 // Correctness if x * y < 128 bits
17 inline uint128_t MultiplyUInt64(uint64_t x, uint64_t y) {
18  return uint128_t(x) * uint128_t(y);
19 }
20 
21 inline uint64_t BarrettReduce128(uint64_t input_hi, uint64_t input_lo,
22  uint64_t modulus) {
23  HEXL_CHECK(modulus != 0, "modulus == 0")
24  uint128_t n = (static_cast<uint128_t>(input_hi) << 64) |
25  (static_cast<uint128_t>(input_lo));
26 
27  return static_cast<uint64_t>(n % modulus);
28  // TODO(fboemer): actually use barrett reduction if performance-critical
29 }
30 
31 // Returns low 64bit of 128b/64b where x1=high 64b, x0=low 64b
32 inline uint64_t DivideUInt128UInt64Lo(uint64_t x1, uint64_t x0, uint64_t y) {
33  uint128_t n =
34  (static_cast<uint128_t>(x1) << 64) | (static_cast<uint128_t>(x0));
35  uint128_t q = n / y;
36 
37  return static_cast<uint64_t>(q);
38 }
39 
40 // Multiplies x * y as 128-bit integer.
41 // @param prod_hi Stores high 64 bits of product
42 // @param prod_lo Stores low 64 bits of product
43 inline void MultiplyUInt64(uint64_t x, uint64_t y, uint64_t* prod_hi,
44  uint64_t* prod_lo) {
45  uint128_t prod = MultiplyUInt64(x, y);
46  *prod_hi = static_cast<uint64_t>(prod >> 64);
47  *prod_lo = static_cast<uint64_t>(prod);
48 }
49 
50 // Return the high 128 minus BitShift bits of the 128-bit product x * y
51 template <int BitShift>
52 inline uint64_t MultiplyUInt64Hi(uint64_t x, uint64_t y) {
53  uint128_t product = MultiplyUInt64(x, y);
54  return static_cast<uint64_t>(product >> BitShift);
55 }
56 
57 // Returns most-significant bit of the input
58 inline uint64_t MSB(uint64_t input) {
59  return static_cast<uint64_t>(std::log2l(input));
60 }
61 
62 #define HEXL_LOOP_UNROLL_4 _Pragma("GCC unroll 4")
63 #define HEXL_LOOP_UNROLL_8 _Pragma("GCC unroll 8")
64 
65 #endif
66 
67 } // namespace hexl
68 } // namespace intel
#define HEXL_CHECK(cond, expr)
Definition: check.hpp:39
Definition: eltwise-add-mod.hpp:8