bmv2
Designing your own switch target with bmv2
Loading...
Searching...
No Matches
bignum.h
1/* Copyright 2013-present Barefoot Networks, Inc.
2 *
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16/*
17 * Antonin Bas (antonin@barefootnetworks.com)
18 *
19 */
20
21#ifndef BM_BM_SIM_BIGNUM_H_
22#define BM_BM_SIM_BIGNUM_H_
23
24#include <gmp.h>
25
26// will need to implement this ourselves if we do not use Boost
27#include <boost/multiprecision/gmp.hpp>
28
29namespace bm {
30
31namespace bignum {
32
33 using boost::multiprecision::gmp_int;
34 using boost::multiprecision::number;
35
36 using Bignum = number<gmp_int>;
37
38 inline size_t export_bytes(char *dst, size_t size, const Bignum &src) {
39 size_t count;
40 mpz_export(dst, &count, 1, size, 1, 0, src.backend().data());
41 return count;
42 }
43
44 inline size_t export_size_in_bytes(const Bignum &src) {
45 return (mpz_sizeinbase(src.backend().data(), 2) + 7) / 8;
46 }
47
48 inline void import_bytes(Bignum *dst, const char *src, size_t size) {
49 mpz_import(dst->backend().data(), 1, 1, size, 1, 0, src);
50 }
51
52 inline int test_bit(const Bignum &v, size_t index) {
53 return mpz_tstbit(v.backend().data(), index);
54 }
55
56 inline void clear_bit(Bignum *v, size_t index) {
57 return mpz_clrbit(v->backend().data(), index);
58 }
59
60} // namespace bignum
61
62} // namespace bm
63
64#endif // BM_BM_SIM_BIGNUM_H_