Line | Count | Source |
1 | | /** |
2 | | * @file integer.h |
3 | | * |
4 | | * This module provides functions for working with arbitrary-sized integers. |
5 | | */ |
6 | | #ifndef PRISM_INTEGER_H |
7 | | #define PRISM_INTEGER_H |
8 | | |
9 | | #include "prism/compiler/inline.h" |
10 | | |
11 | | #include <stdbool.h> |
12 | | #include <stddef.h> |
13 | | #include <stdint.h> |
14 | | |
15 | | /** |
16 | | * A structure represents an arbitrary-sized integer. The magnitude is stored as |
17 | | * 32-bit words, least significant first. |
18 | | */ |
19 | | typedef struct { |
20 | | /** |
21 | | * The number of words in the magnitude, with no leading zero words. Zero |
22 | | * has no words, so a zeroed pm_integer_t is always positive zero. |
23 | | */ |
24 | | uint32_t length; |
25 | | |
26 | | /** |
27 | | * Whether or not the integer is negative. |
28 | | */ |
29 | | bool negative; |
30 | | |
31 | | /** |
32 | | * The words of the magnitude. Read them through pm_integer_words or |
33 | | * pm_integer_uint64_value rather than directly. |
34 | | */ |
35 | | union { |
36 | | /** |
37 | | * The words of an integer whose length is at most 2. Words past the |
38 | | * length are zero. |
39 | | */ |
40 | | uint32_t value[2]; |
41 | | |
42 | | /** |
43 | | * The words of an integer whose length is greater than 2. |
44 | | */ |
45 | | uint32_t *values; |
46 | | } words; |
47 | | } pm_integer_t; |
48 | | |
49 | | /** |
50 | | * Return the words of an integer, least significant first. |
51 | | * |
52 | | * @param integer The integer to read. |
53 | | * @param length Set to the number of words returned, which is 0 for zero. |
54 | | * @returns A pointer to the words, valid for as long as the integer is. |
55 | | */ |
56 | | static PRISM_INLINE const uint32_t * |
57 | 8.90k | pm_integer_words(const pm_integer_t *integer, uint32_t *length) { |
58 | 8.90k | *length = integer->length; |
59 | | |
60 | 8.90k | if (integer->length <= 2) { Branch (60:9): [True: 8.44k, False: 97]
Branch (60:9): [True: 366, False: 4]
|
61 | 8.80k | return integer->words.value; |
62 | 8.80k | } else { |
63 | 101 | return integer->words.values; |
64 | 101 | } |
65 | 8.90k | } Unexecuted instantiation: json.c:pm_integer_words Unexecuted instantiation: diagnostic.c:pm_integer_words Unexecuted instantiation: errors_format.c:pm_integer_words Unexecuted instantiation: prettyprint.c:pm_integer_words serialize.c:pm_integer_words Line | Count | Source | 57 | 8.53k | pm_integer_words(const pm_integer_t *integer, uint32_t *length) { | 58 | 8.53k | *length = integer->length; | 59 | | | 60 | 8.53k | if (integer->length <= 2) { Branch (60:9): [True: 8.44k, False: 97]
| 61 | 8.44k | return integer->words.value; | 62 | 8.44k | } else { | 63 | 97 | return integer->words.values; | 64 | 97 | } | 65 | 8.53k | } |
Unexecuted instantiation: static_literals.c:pm_integer_words Unexecuted instantiation: strpbrk.c:pm_integer_words Unexecuted instantiation: regexp.c:pm_integer_words integer.c:pm_integer_words Line | Count | Source | 57 | 370 | pm_integer_words(const pm_integer_t *integer, uint32_t *length) { | 58 | 370 | *length = integer->length; | 59 | | | 60 | 370 | if (integer->length <= 2) { Branch (60:9): [True: 366, False: 4]
| 61 | 366 | return integer->words.value; | 62 | 366 | } else { | 63 | 4 | return integer->words.values; | 64 | 4 | } | 65 | 370 | } |
Unexecuted instantiation: string_query.c:pm_integer_words Unexecuted instantiation: parser.c:pm_integer_words Unexecuted instantiation: prism.c:pm_integer_words Unexecuted instantiation: tokens.c:pm_integer_words Unexecuted instantiation: comments.c:pm_integer_words Unexecuted instantiation: node.c:pm_integer_words Unexecuted instantiation: parse.c:pm_integer_words |
66 | | |
67 | | /** |
68 | | * Return the magnitude of an integer whose length is at most 2, which always |
69 | | * fits in a uint64_t. |
70 | | * |
71 | | * @param integer The integer to read, whose length must be at most 2. |
72 | | * @returns The magnitude of the integer. |
73 | | */ |
74 | | static PRISM_INLINE uint64_t |
75 | 16.9k | pm_integer_uint64_value(const pm_integer_t *integer) { |
76 | 16.9k | return ((uint64_t) integer->words.value[1] << 32) | integer->words.value[0]; |
77 | 16.9k | } Unexecuted instantiation: json.c:pm_integer_uint64_value Unexecuted instantiation: diagnostic.c:pm_integer_uint64_value Unexecuted instantiation: errors_format.c:pm_integer_uint64_value Unexecuted instantiation: prettyprint.c:pm_integer_uint64_value Unexecuted instantiation: serialize.c:pm_integer_uint64_value static_literals.c:pm_integer_uint64_value Line | Count | Source | 75 | 15 | pm_integer_uint64_value(const pm_integer_t *integer) { | 76 | 15 | return ((uint64_t) integer->words.value[1] << 32) | integer->words.value[0]; | 77 | 15 | } |
Unexecuted instantiation: strpbrk.c:pm_integer_uint64_value Unexecuted instantiation: regexp.c:pm_integer_uint64_value integer.c:pm_integer_uint64_value Line | Count | Source | 75 | 16.9k | pm_integer_uint64_value(const pm_integer_t *integer) { | 76 | 16.9k | return ((uint64_t) integer->words.value[1] << 32) | integer->words.value[0]; | 77 | 16.9k | } |
Unexecuted instantiation: string_query.c:pm_integer_uint64_value Unexecuted instantiation: parser.c:pm_integer_uint64_value Unexecuted instantiation: prism.c:pm_integer_uint64_value Unexecuted instantiation: tokens.c:pm_integer_uint64_value Unexecuted instantiation: comments.c:pm_integer_uint64_value Unexecuted instantiation: node.c:pm_integer_uint64_value Unexecuted instantiation: parse.c:pm_integer_uint64_value |
78 | | |
79 | | #endif |