| Wireshark 4.7.0 The Wireshark network protocol analyzer

Loading...
Searching...
No Matches
sign_ext.h
Go to the documentation of this file.
1
10#ifndef __WSUTIL_SIGN_EXT_H__
11#define __WSUTIL_SIGN_EXT_H__
12
13#include <inttypes.h>
14
15#include <glib.h>
16
17#include <wsutil/ws_assert.h>
18
19/* sign extension routines */
20
32static inline uint32_t
33ws_sign_ext32(uint32_t val, int no_of_bits)
34{
35 ws_assert (no_of_bits >= 0 && no_of_bits <= 32);
36
37 if ((no_of_bits == 0) || (no_of_bits == 32))
38 return val;
39
40 /*
41 * Don't shift signed values left; that's not valid in C99, at
42 * least, if the value is negative or if the shift count is
43 * the number of bits in the value - 1, and we might get
44 * compile-time or run-time complaints about that.
45 */
46 if (val & (1U << (no_of_bits-1)))
47 val |= (0xFFFFFFFFU << no_of_bits);
48
49 return val;
50}
51
63static inline uint64_t
64ws_sign_ext64(uint64_t val, int no_of_bits)
65{
66 ws_assert (no_of_bits >= 0 && no_of_bits <= 64);
67
68 if ((no_of_bits == 0) || (no_of_bits == 64))
69 return val;
70
71 /*
72 * Don't shift signed values left; that's not valid in C99, at
73 * least, if the value is negative or if the shift count is
74 * the number of bits in the value - 1, and we might get
75 * compile-time or run-time complaints about that.
76 */
77 if (val & (UINT64_C(1) << (no_of_bits-1)))
78 val |= (UINT64_C(0xFFFFFFFFFFFFFFFF) << no_of_bits);
79
80 return val;
81}
82
83/*
84static inline uint64_t
85ws_sign_ext64(uint64_t val, int no_of_bits)
86{
87 int64_t sval = (val << (64 - no_of_bits));
88
89 return (uint64_t) (sval >> (64 - no_of_bits));
90}
91*/
92
93#endif /* __WSUTIL_SIGN_EXT_H__ */
ws_assert.h
ws_assert
#define ws_assert(expr)
Unconditionally assert an expression when assertions are enabled.
Definition ws_assert.h:102