/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 | 10.5k | { |
24 | 10.5k | ws_assert (no_of_bits >= 0 && no_of_bits <= 32); |
25 | | |
26 | 10.5k | if ((no_of_bits == 0) || (no_of_bits == 32)) |
27 | 5.92k | 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.59k | if (val & (1U << (no_of_bits-1))) |
36 | 1.46k | val |= (0xFFFFFFFFU << no_of_bits); |
37 | | |
38 | 4.59k | return val; |
39 | 10.5k | } Line | Count | Source | 23 | 7.76k | { | 24 | 7.76k | ws_assert (no_of_bits >= 0 && no_of_bits <= 32); | 25 | | | 26 | 7.76k | if ((no_of_bits == 0) || (no_of_bits == 32)) | 27 | 5.92k | 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 | 1.83k | if (val & (1U << (no_of_bits-1))) | 36 | 725 | val |= (0xFFFFFFFFU << no_of_bits); | 37 | | | 38 | 1.83k | return val; | 39 | 7.76k | } |
Line | Count | Source | 23 | 2.76k | { | 24 | 2.76k | ws_assert (no_of_bits >= 0 && no_of_bits <= 32); | 25 | | | 26 | 2.76k | 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 | 2.76k | if (val & (1U << (no_of_bits-1))) | 36 | 736 | val |= (0xFFFFFFFFU << no_of_bits); | 37 | | | 38 | 2.76k | return val; | 39 | 2.76k | } |
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.01k | { |
44 | 7.01k | ws_assert (no_of_bits >= 0 && no_of_bits <= 64); |
45 | | |
46 | 7.01k | 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.01k | if (val & (UINT64_C(1) << (no_of_bits-1))) |
56 | 2.23k | val |= (UINT64_C(0xFFFFFFFFFFFFFFFF) << no_of_bits); |
57 | | |
58 | 7.01k | return val; |
59 | 7.01k | } Line | Count | Source | 43 | 4.12k | { | 44 | 4.12k | ws_assert (no_of_bits >= 0 && no_of_bits <= 64); | 45 | | | 46 | 4.12k | 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 | 4.12k | if (val & (UINT64_C(1) << (no_of_bits-1))) | 56 | 1.33k | val |= (UINT64_C(0xFFFFFFFFFFFFFFFF) << no_of_bits); | 57 | | | 58 | 4.12k | return val; | 59 | 4.12k | } |
Line | Count | Source | 43 | 2.89k | { | 44 | 2.89k | ws_assert (no_of_bits >= 0 && no_of_bits <= 64); | 45 | | | 46 | 2.89k | 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.89k | if (val & (UINT64_C(1) << (no_of_bits-1))) | 56 | 902 | val |= (UINT64_C(0xFFFFFFFFFFFFFFFF) << no_of_bits); | 57 | | | 58 | 2.89k | return val; | 59 | 2.89k | } |
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__ */ |