Skip to content

Commit

Permalink
Merge pull request #868 from RicoAntonioFelix/dev
Browse files Browse the repository at this point in the history
Changed getbits from a macro to a function template
  • Loading branch information
fwsGonzo authored Oct 13, 2016
2 parents 55c958e + a76074b commit ec51938
Showing 1 changed file with 11 additions and 8 deletions.
19 changes: 11 additions & 8 deletions api/net/util.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
#ifndef NET_UTIL_HPP
#define NET_UTIL_HPP

#include <stdint.h>
#include <cstdint>

namespace net {

Expand All @@ -28,29 +28,32 @@ namespace net {
* e.g., getbits(x, 31, 8) -- highest byte
* getbits(x, 7, 8) -- lowest byte
*/
#define getbits(x, p, n) (((x) >> ((p) + 1 - (n))) & ~(~0 << (n)))
template<typename T>
constexpr inline auto getbits(T&& x, T&& p, T&& n) noexcept {
return (x >> ((p + 1) - n)) & ~(~0 << n);
}

inline uint16_t ntohs(uint16_t n) noexcept {
constexpr inline uint16_t ntohs(const uint16_t n) noexcept {
return __builtin_bswap16(n);
}

inline uint16_t htons(uint16_t n) noexcept {
constexpr inline uint16_t htons(const uint16_t n) noexcept {
return __builtin_bswap16(n);
}

inline uint32_t ntohl(uint32_t n) noexcept {
constexpr inline uint32_t ntohl(const uint32_t n) noexcept {
return __builtin_bswap32(n);
}

inline uint32_t htonl(uint32_t n) noexcept {
constexpr inline uint32_t htonl(const uint32_t n) noexcept {
return __builtin_bswap32(n);
}

inline uint64_t ntohll(uint64_t n) noexcept {
constexpr inline uint64_t ntohll(const uint64_t n) noexcept {
return __builtin_bswap64(n);
}

inline uint64_t htonll(uint64_t n) noexcept {
constexpr inline uint64_t htonll(const uint64_t n) noexcept {
return __builtin_bswap64(n);
}

Expand Down

0 comments on commit ec51938

Please sign in to comment.