Coverage Report

Created: 2023-06-07 06:21

/src/h2o/deps/golombset/golombset.h
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright (c) 2015 Kazuho Oku
3
 *
4
 * Permission is hereby granted, free of charge, to any person obtaining a copy
5
 * of this software and associated documentation files (the "Software"), to
6
 * deal in the Software without restriction, including without limitation the
7
 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
8
 * sell copies of the Software, and to permit persons to whom the Software is
9
 * furnished to do so, subject to the following conditions:
10
 *
11
 * The above copyright notice and this permission notice shall be included in
12
 * all copies or substantial portions of the Software.
13
 *
14
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
20
 * IN THE SOFTWARE.
21
 */
22
#ifndef GOLOMBSET_H
23
#define GOLOMBSET_H
24
25
#include <inttypes.h>
26
27
struct st_golombset_encode_t {
28
    unsigned char *dst;
29
    unsigned char *dst_max;
30
    unsigned dst_shift;
31
};
32
33
struct st_golombset_decode_t {
34
    const unsigned char *src;
35
    const unsigned char *src_max;
36
    unsigned src_shift;
37
};
38
39
static int golombset_encode_bit(struct st_golombset_encode_t *ctx, int bit)
40
0
{
41
0
    if (ctx->dst_shift == 0) {
42
0
        if (++ctx->dst == ctx->dst_max)
43
0
            return -1;
44
0
        *ctx->dst = 0;
45
0
        ctx->dst_shift = 8;
46
0
    }
47
0
    --ctx->dst_shift;
48
0
    if (bit)
49
0
        *ctx->dst |= 1 << ctx->dst_shift;
50
0
    return 0;
51
0
}
Unexecuted instantiation: cache_digests.c:golombset_encode_bit
Unexecuted instantiation: casper.c:golombset_encode_bit
52
53
static int golombset_encode_bits(struct st_golombset_encode_t *ctx, unsigned bits, uint64_t value)
54
0
{
55
0
    if (bits != 0) {
56
0
        do {
57
0
            --bits;
58
0
            if (golombset_encode_bit(ctx, (value >> bits) & 1) != 0)
59
0
                return -1;
60
0
        } while (bits != 0);
61
0
    }
62
0
    return 0;
63
0
}
Unexecuted instantiation: cache_digests.c:golombset_encode_bits
Unexecuted instantiation: casper.c:golombset_encode_bits
64
65
static int golombset_decode_bit(struct st_golombset_decode_t *ctx)
66
15.7M
{
67
15.7M
    if (ctx->src_shift == 0) {
68
2.05M
        if (++ctx->src == ctx->src_max)
69
97.5k
            return -1;
70
1.95M
        ctx->src_shift = 8;
71
1.95M
    }
72
15.6M
    return (*ctx->src >> --ctx->src_shift) & 1;
73
15.7M
}
cache_digests.c:golombset_decode_bit
Line
Count
Source
66
15.7M
{
67
15.7M
    if (ctx->src_shift == 0) {
68
2.05M
        if (++ctx->src == ctx->src_max)
69
97.5k
            return -1;
70
1.95M
        ctx->src_shift = 8;
71
1.95M
    }
72
15.6M
    return (*ctx->src >> --ctx->src_shift) & 1;
73
15.7M
}
Unexecuted instantiation: casper.c:golombset_decode_bit
74
75
static int golombset_decode_bits(struct st_golombset_decode_t *ctx, unsigned bits, uint64_t *value)
76
8.07M
{
77
8.07M
    int bit;
78
79
8.07M
    *value = 0;
80
9.86M
    for (; bits != 0; --bits) {
81
1.80M
        if ((bit = golombset_decode_bit(ctx)) == -1)
82
13.4k
            return -1;
83
1.79M
        *value = (*value * 2) + bit;
84
1.79M
    }
85
86
8.06M
    return 0;
87
8.07M
}
cache_digests.c:golombset_decode_bits
Line
Count
Source
76
8.07M
{
77
8.07M
    int bit;
78
79
8.07M
    *value = 0;
80
9.86M
    for (; bits != 0; --bits) {
81
1.80M
        if ((bit = golombset_decode_bit(ctx)) == -1)
82
13.4k
            return -1;
83
1.79M
        *value = (*value * 2) + bit;
84
1.79M
    }
85
86
8.06M
    return 0;
87
8.07M
}
Unexecuted instantiation: casper.c:golombset_decode_bits
88
89
static int golombset_encode_value(struct st_golombset_encode_t *ctx, unsigned fixed_bits, uint64_t value)
90
0
{
91
    /* emit quontient */
92
0
    uint64_t unary_bits = value >> fixed_bits;
93
0
    for (; unary_bits != 0; --unary_bits)
94
0
        if (golombset_encode_bit(ctx, 0) != 0)
95
0
            return -1;
96
0
    if (golombset_encode_bit(ctx, 1) != 0)
97
0
        return -1;
98
    /* emit remainder */
99
0
    return golombset_encode_bits(ctx, fixed_bits, value);
100
0
}
Unexecuted instantiation: cache_digests.c:golombset_encode_value
Unexecuted instantiation: casper.c:golombset_encode_value
101
102
static int golombset_decode_value(struct st_golombset_decode_t *ctx, unsigned fixed_bits, uint64_t *value)
103
7.96M
{
104
7.96M
    uint64_t q;
105
7.96M
    int bit;
106
107
    /* decode quontient */
108
13.9M
    for (q = 0; ; ++q) {
109
13.9M
        if ((bit = golombset_decode_bit(ctx)) == -1)
110
84.0k
            return -1;
111
13.8M
        if (bit)
112
7.88M
            break;
113
13.8M
    }
114
    /* decode remainder */
115
7.88M
    if (golombset_decode_bits(ctx, fixed_bits, value) == -1)
116
12.0k
        return -1;
117
    /* merge q and r */
118
7.86M
    *value += q << fixed_bits;
119
120
7.86M
    return 0;
121
7.88M
}
cache_digests.c:golombset_decode_value
Line
Count
Source
103
7.96M
{
104
7.96M
    uint64_t q;
105
7.96M
    int bit;
106
107
    /* decode quontient */
108
13.9M
    for (q = 0; ; ++q) {
109
13.9M
        if ((bit = golombset_decode_bit(ctx)) == -1)
110
84.0k
            return -1;
111
13.8M
        if (bit)
112
7.88M
            break;
113
13.8M
    }
114
    /* decode remainder */
115
7.88M
    if (golombset_decode_bits(ctx, fixed_bits, value) == -1)
116
12.0k
        return -1;
117
    /* merge q and r */
118
7.86M
    *value += q << fixed_bits;
119
120
7.86M
    return 0;
121
7.88M
}
Unexecuted instantiation: casper.c:golombset_decode_value
122
123
static int golombset_encode(unsigned fixed_bits, uint64_t *keys, size_t num_keys, void *buf, size_t *bufsize)
124
0
{
125
0
    struct st_golombset_encode_t ctx = {(unsigned char *)buf - 1, (unsigned char *)buf + *bufsize};
126
0
    size_t i;
127
0
    uint64_t next_min = 0;
128
129
0
    for (i = 0; i != num_keys; ++i) {
130
0
        if (golombset_encode_value(&ctx, fixed_bits, keys[i] - next_min) != 0)
131
0
            return -1;
132
0
        next_min = keys[i] + 1;
133
0
    }
134
135
0
    *bufsize = ctx.dst + 1 - (unsigned char *)buf;
136
137
0
    return 0;
138
0
}
Unexecuted instantiation: cache_digests.c:golombset_encode
Unexecuted instantiation: casper.c:golombset_encode
139
140
static int golombset_decode(unsigned fixed_bits, const void *buf, size_t bufsize, uint64_t *keys, size_t *num_keys)
141
0
{
142
0
    struct st_golombset_decode_t ctx = {buf, (unsigned char *)buf + bufsize, 8};
143
0
    size_t index = 0;
144
0
    uint64_t next_min = 0;
145
146
0
    while (1) {
147
0
        uint64_t value;
148
0
        if (golombset_decode_value(&ctx, fixed_bits, &value) != 0)
149
0
            break;
150
0
        if (index == *num_keys) {
151
            /* not enough space */
152
0
            return -1;
153
0
        }
154
0
        value += next_min;
155
0
        keys[index++] = value;
156
0
        next_min = value + 1;
157
0
    }
158
0
    *num_keys = index;
159
0
    return 0;
160
0
}
Unexecuted instantiation: cache_digests.c:golombset_decode
Unexecuted instantiation: casper.c:golombset_decode
161
162
#endif