/src/wireshark/wsutil/sign_ext.h
Line | Count | Source (jump to first uncovered line) |
1 | | /** @file |
2 | | * |
3 | | * Wireshark - Network traffic analyzer |
4 | | * By Gerald Combs <gerald@wireshark.org> |
5 | | * Copyright 1998 Gerald Combs |
6 | | * |
7 | | * SPDX-License-Identifier: GPL-2.0-or-later |
8 | | */ |
9 | | |
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 | | |
21 | | static inline uint32_t |
22 | | ws_sign_ext32(uint32_t val, int no_of_bits) |
23 | 15.9k | { |
24 | 15.9k | ws_assert (no_of_bits >= 0 && no_of_bits <= 32); |
25 | | |
26 | 15.9k | if ((no_of_bits == 0) || (no_of_bits == 32)) |
27 | 9.22k | return val; |
28 | | |
29 | | /* |
30 | | * Don't shift signed values left; that's not valid in C99, at |
31 | | * least, if the value is negative or if the shift count is |
32 | | * the number of bits in the value - 1, and we might get |
33 | | * compile-time or run-time complaints about that. |
34 | | */ |
35 | 6.71k | if (val & (1U << (no_of_bits-1))) |
36 | 2.07k | val |= (0xFFFFFFFFU << no_of_bits); |
37 | | |
38 | 6.71k | return val; |
39 | 15.9k | } Line | Count | Source | 23 | 11.7k | { | 24 | 11.7k | ws_assert (no_of_bits >= 0 && no_of_bits <= 32); | 25 | | | 26 | 11.7k | if ((no_of_bits == 0) || (no_of_bits == 32)) | 27 | 9.22k | return val; | 28 | | | 29 | | /* | 30 | | * Don't shift signed values left; that's not valid in C99, at | 31 | | * least, if the value is negative or if the shift count is | 32 | | * the number of bits in the value - 1, and we might get | 33 | | * compile-time or run-time complaints about that. | 34 | | */ | 35 | 2.56k | if (val & (1U << (no_of_bits-1))) | 36 | 875 | val |= (0xFFFFFFFFU << no_of_bits); | 37 | | | 38 | 2.56k | return val; | 39 | 11.7k | } |
Line | Count | Source | 23 | 4.14k | { | 24 | 4.14k | ws_assert (no_of_bits >= 0 && no_of_bits <= 32); | 25 | | | 26 | 4.14k | if ((no_of_bits == 0) || (no_of_bits == 32)) | 27 | 0 | return val; | 28 | | | 29 | | /* | 30 | | * Don't shift signed values left; that's not valid in C99, at | 31 | | * least, if the value is negative or if the shift count is | 32 | | * the number of bits in the value - 1, and we might get | 33 | | * compile-time or run-time complaints about that. | 34 | | */ | 35 | 4.14k | if (val & (1U << (no_of_bits-1))) | 36 | 1.20k | val |= (0xFFFFFFFFU << no_of_bits); | 37 | | | 38 | 4.14k | return val; | 39 | 4.14k | } |
Unexecuted instantiation: packet-signal-pdu.c:ws_sign_ext32 Unexecuted instantiation: packet-usb-hid.c:ws_sign_ext32 |
40 | | |
41 | | static inline uint64_t |
42 | | ws_sign_ext64(uint64_t val, int no_of_bits) |
43 | 7.98k | { |
44 | 7.98k | ws_assert (no_of_bits >= 0 && no_of_bits <= 64); |
45 | | |
46 | 7.98k | if ((no_of_bits == 0) || (no_of_bits == 64)) |
47 | 0 | return val; |
48 | | |
49 | | /* |
50 | | * Don't shift signed values left; that's not valid in C99, at |
51 | | * least, if the value is negative or if the shift count is |
52 | | * the number of bits in the value - 1, and we might get |
53 | | * compile-time or run-time complaints about that. |
54 | | */ |
55 | 7.98k | if (val & (UINT64_C(1) << (no_of_bits-1))) |
56 | 2.26k | val |= (UINT64_C(0xFFFFFFFFFFFFFFFF) << no_of_bits); |
57 | | |
58 | 7.98k | return val; |
59 | 7.98k | } Line | Count | Source | 43 | 5.16k | { | 44 | 5.16k | ws_assert (no_of_bits >= 0 && no_of_bits <= 64); | 45 | | | 46 | 5.16k | if ((no_of_bits == 0) || (no_of_bits == 64)) | 47 | 0 | return val; | 48 | | | 49 | | /* | 50 | | * Don't shift signed values left; that's not valid in C99, at | 51 | | * least, if the value is negative or if the shift count is | 52 | | * the number of bits in the value - 1, and we might get | 53 | | * compile-time or run-time complaints about that. | 54 | | */ | 55 | 5.16k | if (val & (UINT64_C(1) << (no_of_bits-1))) | 56 | 1.41k | val |= (UINT64_C(0xFFFFFFFFFFFFFFFF) << no_of_bits); | 57 | | | 58 | 5.16k | return val; | 59 | 5.16k | } |
Line | Count | Source | 43 | 2.82k | { | 44 | 2.82k | ws_assert (no_of_bits >= 0 && no_of_bits <= 64); | 45 | | | 46 | 2.82k | if ((no_of_bits == 0) || (no_of_bits == 64)) | 47 | 0 | return val; | 48 | | | 49 | | /* | 50 | | * Don't shift signed values left; that's not valid in C99, at | 51 | | * least, if the value is negative or if the shift count is | 52 | | * the number of bits in the value - 1, and we might get | 53 | | * compile-time or run-time complaints about that. | 54 | | */ | 55 | 2.82k | if (val & (UINT64_C(1) << (no_of_bits-1))) | 56 | 850 | val |= (UINT64_C(0xFFFFFFFFFFFFFFFF) << no_of_bits); | 57 | | | 58 | 2.82k | return val; | 59 | 2.82k | } |
Unexecuted instantiation: packet-signal-pdu.c:ws_sign_ext64 Unexecuted instantiation: packet-usb-hid.c:ws_sign_ext64 |
60 | | |
61 | | /* |
62 | | static inline uint64_t |
63 | | ws_sign_ext64(uint64_t val, int no_of_bits) |
64 | | { |
65 | | int64_t sval = (val << (64 - no_of_bits)); |
66 | | |
67 | | return (uint64_t) (sval >> (64 - no_of_bits)); |
68 | | } |
69 | | */ |
70 | | |
71 | | #endif /* __WSUTIL_SIGN_EXT_H__ */ |