Wireshark 2.9.0-477-g68ec514b The Wireshark network protocol analyzer

- Main Page
- Related Pages
- Modules
- Namespaces
- Classes
- Files

- File List
- File Members

sign_ext.h
1 /*
2 * sign_ext.h
3 *
4 * Wireshark - Network traffic analyzer
5 * By Gerald Combs <gerald@wireshark.org>
6 * Copyright 1998 Gerald Combs
7 *
8 * SPDX-License-Identifier: GPL-2.0-or-later
9 */
10
11 #ifndef __WSUTIL_SIGN_EXT_H__
12 #define __WSUTIL_SIGN_EXT_H__
13
14 #include <glib.h>
15
16 /* sign extension routines */
17
18 static inline guint32
19 ws_sign_ext32(guint32 val, int no_of_bits)
20 {
21 g_assert (no_of_bits >= 0 && no_of_bits <= 32);
22
23 if ((no_of_bits == 0) || (no_of_bits == 32))
24 return val;
25
26 /*
27 * Don't shift signed values left; that's not valid in C99, at
28 * least, if the value is negative or if the shift count is
29 * the number of bits in the value - 1, and we might get
30 * compile-time or run-time complaints about that.
31 */
32 if (val & (1U << (no_of_bits-1)))
33 val |= (0xFFFFFFFFU << no_of_bits);
34
35 return val;
36 }
37
38 static inline guint64
39 ws_sign_ext64(guint64 val, int no_of_bits)
40 {
41 g_assert (no_of_bits >= 0 && no_of_bits <= 64);
42
43 if ((no_of_bits == 0) || (no_of_bits == 64))
44 return val;
45
46 /*
47 * Don't shift signed values left; that's not valid in C99, at
48 * least, if the value is negative or if the shift count is
49 * the number of bits in the value - 1, and we might get
50 * compile-time or run-time complaints about that.
51 */
52 if (val & (G_GUINT64_CONSTANT(1) << (no_of_bits-1)))
53 val |= (G_GUINT64_CONSTANT(0xFFFFFFFFFFFFFFFF) << no_of_bits);
54
55 return val;
56 }
57
58 /*
59 static inline guint64
60 ws_sign_ext64(guint64 val, int no_of_bits)
61 {
62 gint64 sval = (val << (64 - no_of_bits));
63
64 return (guint64) (sval >> (64 - no_of_bits));
65 }
66 */
67
68 #endif /* __WSUTIL_SIGN_EXT_H__ */