Skip to content

Commit

Permalink
house cleaning for 32-bit compilation (p4lang#1582)
Browse files Browse the repository at this point in the history
* house cleaning for 32-bit compilation

Avoid using size_t as an implied 64-bit value, as on 32-bit systems that is not correct.

Reorganized the headers so that we can reuse bitops without gmp.

Made the add_cxx_compiler_options portable.
  • Loading branch information
Calin Cascaval committed Oct 29, 2018
1 parent 9aaca6a commit 41f142d
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 35 deletions.
4 changes: 2 additions & 2 deletions cmake/P4CUtils.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
35 changes: 15 additions & 20 deletions lib/bitops.h
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -18,8 +18,7 @@ limitations under the License.
#define P4C_LIB_BITOPS_H_

#include <limits.h>
#include "gmputil.h"
#include "exceptions.h"
#include "bitvec.h"

static inline unsigned bitcount(unsigned v) {
#if defined(__GNUC__) || defined(__clang__)
Expand All @@ -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) {
Expand All @@ -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_ */
14 changes: 7 additions & 7 deletions lib/bitvec.cpp
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -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<uintptr_t>(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<uintptr_t>(1) << (sz-1)); }
return rv;
} else {
return bitvec((data >> idx) & ~(~(uintptr_t)1 << (sz-1))); }
return bitvec((data >> idx) & ~(~static_cast<uintptr_t>(1) << (sz-1))); }
}

int bitvec::ffs(unsigned start) const {
uintptr_t val = ~0ULL;
uintptr_t val = ~static_cast<uintptr_t>(0);
unsigned idx = start / bits_per_unit;
val <<= (start % bits_per_unit);
while (idx < size && !(val &= word(idx))) {
++idx;
val = ~0ULL; }
val = ~static_cast<uintptr_t>(0); }
if (idx >= size) return -1;
unsigned rv = idx * bits_per_unit;
#if defined(__GNUC__) || defined(__clang__)
Expand All @@ -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<uintptr_t>(0);
unsigned idx = start / bits_per_unit;
val = ~(~val << (start % bits_per_unit));
while (!~(val |= word(idx))) {
Expand Down
4 changes: 2 additions & 2 deletions lib/bitvec.h
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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_
22 changes: 22 additions & 0 deletions lib/gmputil.h
Original file line number Diff line number Diff line change
Expand Up @@ -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_ */
5 changes: 2 additions & 3 deletions lib/hash.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::size_t>(0xc6a4a793UL) << 32UL)
+ static_cast<std::size_t>(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<const char*>(data);

Expand Down
2 changes: 1 addition & 1 deletion lib/stringify.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ limitations under the License.
#define P4C_LIB_STRINGIFY_H_

#include <stdint.h>
#include "gmputil.h" // for mpz_class
#include <gmpxx.h> // for mpz_class
#include "cstring.h"
#include "stringref.h"

Expand Down

0 comments on commit 41f142d

Please sign in to comment.