diff --git a/cmake/P4CUtils.cmake b/cmake/P4CUtils.cmake index ba7b3434ce..e525086cec 100644 --- a/cmake/P4CUtils.cmake +++ b/cmake/P4CUtils.cmake @@ -7,11 +7,11 @@ MACRO (add_cxx_compiler_option option) string (REPLACE "-" "" escaped_option ${escaped_option1}) set (_success "_HAVE_OPTION_${escaped_option}_") set (__required_flags_backup "${CMAKE_REQUIRED_FLAGS}") - set (CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} ${P4C_CXX_FLAGS}") + set (CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} ${${PROJECT_NAME}_CXX_FLAGS}") check_cxx_compiler_flag (${option} ${_success}) set (CMAKE_REQUIRED_FLAGS ${__required_flags_backup}) if (${_success}) - set (P4C_CXX_FLAGS "${P4C_CXX_FLAGS} ${option}") + set (${PROJECT_NAME}_CXX_FLAGS "${${PROJECT_NAME}_CXX_FLAGS} ${option}") endif (${_success}) endmacro (add_cxx_compiler_option) diff --git a/lib/bitops.h b/lib/bitops.h index d7687ff36c..06de93c476 100644 --- a/lib/bitops.h +++ b/lib/bitops.h @@ -1,5 +1,5 @@ /* -Copyright 2013-present Barefoot Networks, Inc. +Copyright 2013-present Barefoot Networks, Inc. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. @@ -18,8 +18,7 @@ limitations under the License. #define P4C_LIB_BITOPS_H_ #include -#include "gmputil.h" -#include "exceptions.h" +#include "bitvec.h" static inline unsigned bitcount(unsigned v) { #if defined(__GNUC__) || defined(__clang__) @@ -28,18 +27,7 @@ static inline unsigned bitcount(unsigned v) { unsigned rv = 0; while (v) { v &= v-1; ++rv; } #endif - return rv; } -static inline unsigned bitcount(mpz_class value) { - mpz_class v = value; - if (sgn(v) < 0) - BUG("bitcount of negative number %1%", value); - unsigned rv = 0; - while (v != 0) { v &= v-1; ++rv; } - return rv; } - -static inline int ffs(mpz_class v) { - if (v == 0) return -1; - return mpz_scan1(v.get_mpz_t(), 0); + return rv; } static inline int floor_log2(unsigned v) { @@ -49,14 +37,21 @@ static inline int floor_log2(unsigned v) { #else while (v) { rv++; v >>= 1; } #endif - return rv; } -static inline int floor_log2(mpz_class v) { - int rv = -1; - while (v > 0) { rv++; v /= 2; } - return rv; } + return rv; +} static inline int ceil_log2(unsigned v) { return v ? floor_log2(v-1) + 1 : -1; } +static inline unsigned bitmask2bytemask(const bitvec &a) { + int max = a.max().index(); + if (max < 0) return 0; + unsigned rv = 0; + for (unsigned i = 0; i <= max/8U; i++) + if (a.getrange(i*8, 8)) + rv |= 1 << i; + return rv; +} + #endif /* P4C_LIB_BITOPS_H_ */ diff --git a/lib/bitvec.cpp b/lib/bitvec.cpp index 79e6ce28ba..c489cc6665 100644 --- a/lib/bitvec.cpp +++ b/lib/bitvec.cpp @@ -1,5 +1,5 @@ /* -Copyright 2013-present Barefoot Networks, Inc. +Copyright 2013-present Barefoot Networks, Inc. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. @@ -93,24 +93,24 @@ bitvec bitvec::getslice(size_t idx, size_t sz) const { rv.ptr[i-1] |= ptr[idx + i] << (bits_per_unit - shift); rv.ptr[i] = ptr[idx + i] >> shift; } if ((sz %= bits_per_unit)) - rv.ptr[rv.size-1] &= ~(~(uintptr_t)1 << (sz-1)); + rv.ptr[rv.size-1] &= ~(~static_cast(1) << (sz-1)); } else { rv.data = ptr[idx] >> shift; if (shift != 0 && idx + 1 < size) rv.data |= ptr[idx + 1] << (bits_per_unit - shift); - rv.data &= ~(~(uintptr_t)1 << (sz-1)); } + rv.data &= ~(~static_cast(1) << (sz-1)); } return rv; } else { - return bitvec((data >> idx) & ~(~(uintptr_t)1 << (sz-1))); } + return bitvec((data >> idx) & ~(~static_cast(1) << (sz-1))); } } int bitvec::ffs(unsigned start) const { - uintptr_t val = ~0ULL; + uintptr_t val = ~static_cast(0); unsigned idx = start / bits_per_unit; val <<= (start % bits_per_unit); while (idx < size && !(val &= word(idx))) { ++idx; - val = ~0ULL; } + val = ~static_cast(0); } if (idx >= size) return -1; unsigned rv = idx * bits_per_unit; #if defined(__GNUC__) || defined(__clang__) @@ -123,7 +123,7 @@ int bitvec::ffs(unsigned start) const { } unsigned bitvec::ffz(unsigned start) const { - uintptr_t val = 0; + uintptr_t val = static_cast(0); unsigned idx = start / bits_per_unit; val = ~(~val << (start % bits_per_unit)); while (!~(val |= word(idx))) { diff --git a/lib/bitvec.h b/lib/bitvec.h index 61b02e1a4e..17c644b5ce 100644 --- a/lib/bitvec.h +++ b/lib/bitvec.h @@ -1,5 +1,5 @@ /* -Copyright 2013-present Barefoot Networks, Inc. +Copyright 2013-present Barefoot Networks, Inc. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. @@ -55,6 +55,7 @@ static inline int builtin_popcount(unsigned long x) { return __builtin_popcountl static inline int builtin_popcount(unsigned long long x) { return __builtin_popcountll(x); } #endif + class bitvec { size_t size; union { @@ -479,5 +480,4 @@ inline bitvec operator^(bitvec &&a, const bitvec &b) { inline bitvec operator-(bitvec &&a, const bitvec &b) { bitvec rv(std::move(a)); rv -= b; return rv; } - #endif // P4C_LIB_BITVEC_H_ diff --git a/lib/gmputil.h b/lib/gmputil.h index fb0c4a7cf1..83279f383d 100644 --- a/lib/gmputil.h +++ b/lib/gmputil.h @@ -44,4 +44,26 @@ mpz_class maskFromSlice(unsigned m, unsigned l); mpz_class mask(unsigned bits); } // namespace Util + +#include "exceptions.h" +static inline unsigned bitcount(mpz_class value) { + mpz_class v = value; + if (sgn(v) < 0) + BUG("bitcount of negative number %1%", value); + unsigned rv = 0; + while (v != 0) { v &= v-1; ++rv; } + return rv; +} + +static inline int ffs(mpz_class v) { + if (v == 0) return -1; + return mpz_scan1(v.get_mpz_t(), 0); +} + +static inline int floor_log2(mpz_class v) { + int rv = -1; + while (v > 0) { rv++; v /= 2; } + return rv; +} + #endif /* _LIB_GMPUTIL_H_ */ diff --git a/lib/hash.cpp b/lib/hash.cpp index 53ded1f97e..ba65bbe024 100644 --- a/lib/hash.cpp +++ b/lib/hash.cpp @@ -76,10 +76,9 @@ std::uint32_t murmur32(const void* data, std::uint32_t size) { } std::uint64_t murmur64(const void *data, std::uint64_t size) { - const std::size_t mul = (static_cast(0xc6a4a793UL) << 32UL) - + static_cast(0x5bd1e995UL); + const std::uint64_t mul = (UINT64_C(0xc6a4a793) << 32) + UINT64_C(0x5bd1e995); - const std::uint64_t seed = 0xc70f6907UL; + const std::uint64_t seed = UINT64_C(0xc70f6907); const char* const buf = static_cast(data); diff --git a/lib/stringify.h b/lib/stringify.h index 25c88abbf4..4a306effd4 100644 --- a/lib/stringify.h +++ b/lib/stringify.h @@ -20,7 +20,7 @@ limitations under the License. #define P4C_LIB_STRINGIFY_H_ #include -#include "gmputil.h" // for mpz_class +#include // for mpz_class #include "cstring.h" #include "stringref.h"