|
| 1 | +#include "bitfields.h" |
| 2 | +#include <assert.h> |
| 3 | +#include <limits.h> |
| 4 | +#include <stddef.h> |
| 5 | +#include <stdio.h> |
| 6 | +#include <stdlib.h> |
| 7 | + |
| 8 | +static int ALIGN = -30; |
| 9 | + |
| 10 | +#define print_sizeof(S) printf("size of %*s : %zu bytes\n", ALIGN, #S, sizeof((S){})) |
| 11 | + |
| 12 | +void print_sizeof_structs() { |
| 13 | + print_sizeof(SimpleSignedStr); |
| 14 | + print_sizeof(SimpleUnsignedStr); |
| 15 | + print_sizeof(ImplementationDefinedStr); |
| 16 | + print_sizeof(PossiblySmallStr); |
| 17 | + print_sizeof(SimpleUnsignedUnion); |
| 18 | + print_sizeof(ComplexStr); |
| 19 | + print_sizeof(StrWithBool); |
| 20 | + print_sizeof(StrWithUnnamedBitfields); |
| 21 | + print_sizeof(StrWithUnnamedZeroBitfield); |
| 22 | + print_sizeof(StrWithBreak); |
| 23 | +} |
| 24 | + |
| 25 | +int check_simple_signed_str(SimpleSignedStr s) { |
| 26 | + if (s.a == 1024 && s.b == -1 && s.d == -16) { |
| 27 | + return 1; |
| 28 | + } else if (s.b == 0) { |
| 29 | + return -1; |
| 30 | + } |
| 31 | + return 0; |
| 32 | +} |
| 33 | + |
| 34 | +int is_sum_greater_10(SimpleUnsignedStr s) { |
| 35 | + return (s.a + s.b + s.c + s.d > 10 && s.d > 5); |
| 36 | +} |
| 37 | + |
| 38 | +int is_all_greater_2(PossiblySmallStr s) { |
| 39 | + if (s.a > 2 && s.b > 2) { |
| 40 | + return 1; |
| 41 | + } |
| 42 | + return 0; |
| 43 | +} |
| 44 | + |
| 45 | +int union_greater_2(SimpleUnsignedUnion u) { |
| 46 | + if (u.c > 2) { |
| 47 | + return 1; |
| 48 | + } |
| 49 | + return 0; |
| 50 | +} |
| 51 | + |
| 52 | +int union_greater_2_short(SimpleUnsignedUnion u) { |
| 53 | + return u.c > 2; |
| 54 | +} |
| 55 | + |
| 56 | +int sum_of_fields(ComplexStr s) { |
| 57 | + if (!s.a) { |
| 58 | + return -1; |
| 59 | + } |
| 60 | + return s.a + s.b + s.c + s.d + s.e; |
| 61 | +} |
| 62 | + |
| 63 | +int decode_from_bool(StrWithBool s) { |
| 64 | + if (!s.a && !s.b && !s.c) { |
| 65 | + return 0; |
| 66 | + } else if (s.a && !s.b && !s.c) { |
| 67 | + return 1; |
| 68 | + } else if (!s.a && s.b && !s.c) { |
| 69 | + return 2; |
| 70 | + } else if (s.a && s.b && !s.c) { |
| 71 | + return 3; |
| 72 | + } else if (!s.a && !s.b && s.c) { |
| 73 | + return 4; |
| 74 | + } else if (s.a && !s.b && s.c) { |
| 75 | + return 5; |
| 76 | + } else if (!s.a && s.b && s.c) { |
| 77 | + return 6; |
| 78 | + } |
| 79 | + return 7; |
| 80 | +} |
| 81 | + |
| 82 | +int decode_from_bool_simple(StrWithBool s) { |
| 83 | + if (!s.a && !s.b && !s.c) { |
| 84 | + return 0; |
| 85 | + } else if (s.a && !s.b && s.c) { |
| 86 | + return 5; |
| 87 | + } |
| 88 | + return 7; |
| 89 | +} |
| 90 | + |
| 91 | + StrWithUnnamedBitfields mult_by_two(StrWithUnnamedBitfields s) { |
| 92 | + s.b1 *= 2; |
| 93 | + s.b2 *= 2; |
| 94 | + s.b3 *= 2; |
| 95 | + return s; |
| 96 | + } |
| 97 | + |
| 98 | + int is_nice(StrWithUnnamedZeroBitfield s) { |
| 99 | + if (s.b1 == 69 && s.b2 == 42 && s.b3 == 1488) { |
| 100 | + return 13; |
| 101 | + } |
| 102 | + return 0; |
| 103 | + } |
| 104 | + |
| 105 | +int check_fields_bounds(StrWithBreak s) { |
| 106 | + assert(s.b1 >= 0 && s.b1 <= 127); |
| 107 | + assert(s.breaking >= LLONG_MIN && s.breaking <= LLONG_MAX); |
| 108 | + assert(s.b2 >= -65536 && s.b2 <= 65535); |
| 109 | + assert(s.b3 == true || s.b3 == false); |
| 110 | + assert(s.b4 >= -2097152 && s.b4 <= 2097151); |
| 111 | + if (s.b1 >= 123 && s.b3) { |
| 112 | + return 1; |
| 113 | + } else if (s.b1 >= 123 && s.b4 < 0) { |
| 114 | + return 2; |
| 115 | + } else if (s.breaking > 42) { |
| 116 | + return 3; |
| 117 | + } |
| 118 | + return 4; |
| 119 | +} |
| 120 | + |
| 121 | +void simple_modify(SimpleSignedStr* s) { |
| 122 | + s->a++; |
| 123 | + s->b = ~s->b; |
| 124 | + if (s->c >= 0) { |
| 125 | + s->c *= 2; |
| 126 | + } |
| 127 | + s->d /= 2; |
| 128 | +} |
| 129 | + |
| 130 | +SimpleSignedStr* create_on_heap(int a, int b, int c, int d) { |
| 131 | + SimpleSignedStr* s = malloc(sizeof(SimpleSignedStr)); |
| 132 | + if (s) { |
| 133 | + s->a = s->b = s->c = s->d = -1; |
| 134 | + if (a >= -8388608 && a <= 8388607) { |
| 135 | + s->a = a; |
| 136 | + } |
| 137 | + if (b >= -1 && b <= 0) { |
| 138 | + s->b = b; |
| 139 | + } |
| 140 | + if (c >= -2 && c <= 1) { |
| 141 | + s->c = c; |
| 142 | + } |
| 143 | + if (d >= -16 && d <= 15) { |
| 144 | + s->d = d; |
| 145 | + } |
| 146 | + } |
| 147 | + return s; |
| 148 | +} |
0 commit comments