Coverage Report

Created: 2025-07-07 10:01

/work/workdir/UnpackedTarball/harfbuzz/src/hb-cache.hh
Line
Count
Source
1
/*
2
 * Copyright © 2012  Google, Inc.
3
 *
4
 *  This is part of HarfBuzz, a text shaping library.
5
 *
6
 * Permission is hereby granted, without written agreement and without
7
 * license or royalty fees, to use, copy, modify, and distribute this
8
 * software and its documentation for any purpose, provided that the
9
 * above copyright notice and the following two paragraphs appear in
10
 * all copies of this software.
11
 *
12
 * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR
13
 * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
14
 * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN
15
 * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
16
 * DAMAGE.
17
 *
18
 * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
19
 * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
20
 * FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS
21
 * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO
22
 * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
23
 *
24
 * Google Author(s): Behdad Esfahbod
25
 */
26
27
#ifndef HB_CACHE_HH
28
#define HB_CACHE_HH
29
30
#include "hb.hh"
31
32
33
/* Implements a lockfree and thread-safe cache for int->int functions,
34
 * using (optionally) _relaxed_ atomic integer operations.
35
 *
36
 * The cache is a fixed-size array of 16-bit or 32-bit integers,
37
 * typically 256 elements.
38
 *
39
 * The key is split into two parts: the cache index (high bits)
40
 * and the rest (low bits).
41
 *
42
 * The cache index is used to index into the array.  The array
43
 * member is a 16-bit or 32-bit integer that is used *both*
44
 * to store the low bits of the key, and the value.
45
 *
46
 * The value is stored in the least significant bits of the integer.
47
 * The low bits of the key are stored in the most significant bits
48
 * of the integer.
49
 *
50
 * A cache hit is detected by comparing the low bits of the key
51
 * with the high bits of the integer at the array position indexed
52
 * by the high bits of the key. If they match, the value is extracted
53
 * from the least significant bits of the integer and returned.
54
 * Otherwise, a cache miss is reported.
55
 *
56
 * Cache operations (storage and retrieval) involve just a few
57
 * arithmetic operations and a single memory access.
58
 */
59
60
template <unsigned int key_bits=16,
61
   unsigned int value_bits=8 + 32 - key_bits,
62
   unsigned int cache_bits=8,
63
   bool thread_safe=true>
64
struct hb_cache_t
65
{
66
  using item_t = typename std::conditional<thread_safe,
67
             typename std::conditional<key_bits + value_bits - cache_bits <= 16,
68
                     hb_atomic_t<unsigned short>,
69
                     hb_atomic_t<unsigned int>>::type,
70
             typename std::conditional<key_bits + value_bits - cache_bits <= 16,
71
                     unsigned short,
72
                     unsigned int>::type
73
            >::type;
74
75
  static_assert ((key_bits >= cache_bits), "");
76
  static_assert ((key_bits + value_bits <= cache_bits + 8 * sizeof (item_t)), "");
77
78
180
  hb_cache_t () { clear (); }
hb_cache_t<21u, 3u, 8u, true>::hb_cache_t()
Line
Count
Source
78
60
  hb_cache_t () { clear (); }
hb_cache_t<15u, 8u, 7u, true>::hb_cache_t()
Line
Count
Source
78
60
  hb_cache_t () { clear (); }
hb_cache_t<21u, 19u, 8u, true>::hb_cache_t()
Line
Count
Source
78
60
  hb_cache_t () { clear (); }
Unexecuted instantiation: hb_cache_t<24u, 16u, 8u, true>::hb_cache_t()
79
80
  void clear ()
81
290
  {
82
290
    for (auto &v : values)
83
52.4k
      v = -1;
84
290
  }
hb_cache_t<21u, 3u, 8u, true>::clear()
Line
Count
Source
81
60
  {
82
60
    for (auto &v : values)
83
15.3k
      v = -1;
84
60
  }
hb_cache_t<15u, 8u, 7u, true>::clear()
Line
Count
Source
81
170
  {
82
170
    for (auto &v : values)
83
21.7k
      v = -1;
84
170
  }
hb_cache_t<21u, 19u, 8u, true>::clear()
Line
Count
Source
81
60
  {
82
60
    for (auto &v : values)
83
15.3k
      v = -1;
84
60
  }
Unexecuted instantiation: hb_cache_t<24u, 16u, 8u, true>::clear()
85
86
  bool get (unsigned int key, unsigned int *value) const
87
1.18G
  {
88
1.18G
    unsigned int k = key & ((1u<<cache_bits)-1);
89
1.18G
    unsigned int v = values[k];
90
1.18G
    if ((key_bits + value_bits - cache_bits == 8 * sizeof (item_t) && v == (unsigned int) -1) ||
91
1.18G
  (v >> value_bits) != (key >> cache_bits))
92
200M
      return false;
93
982M
    *value = v & ((1u<<value_bits)-1);
94
982M
    return true;
95
1.18G
  }
hb_cache_t<15u, 8u, 7u, true>::get(unsigned int, unsigned int*) const
Line
Count
Source
87
154M
  {
88
154M
    unsigned int k = key & ((1u<<cache_bits)-1);
89
154M
    unsigned int v = values[k];
90
154M
    if ((key_bits + value_bits - cache_bits == 8 * sizeof (item_t) && v == (unsigned int) -1) ||
91
154M
  (v >> value_bits) != (key >> cache_bits))
92
135M
      return false;
93
19.2M
    *value = v & ((1u<<value_bits)-1);
94
19.2M
    return true;
95
154M
  }
hb_cache_t<21u, 3u, 8u, true>::get(unsigned int, unsigned int*) const
Line
Count
Source
87
502M
  {
88
502M
    unsigned int k = key & ((1u<<cache_bits)-1);
89
502M
    unsigned int v = values[k];
90
502M
    if ((key_bits + value_bits - cache_bits == 8 * sizeof (item_t) && v == (unsigned int) -1) ||
91
502M
  (v >> value_bits) != (key >> cache_bits))
92
1.53M
      return false;
93
500M
    *value = v & ((1u<<value_bits)-1);
94
500M
    return true;
95
502M
  }
hb_cache_t<21u, 19u, 8u, true>::get(unsigned int, unsigned int*) const
Line
Count
Source
87
526M
  {
88
526M
    unsigned int k = key & ((1u<<cache_bits)-1);
89
526M
    unsigned int v = values[k];
90
526M
    if ((key_bits + value_bits - cache_bits == 8 * sizeof (item_t) && v == (unsigned int) -1) ||
91
526M
  (v >> value_bits) != (key >> cache_bits))
92
63.4M
      return false;
93
462M
    *value = v & ((1u<<value_bits)-1);
94
462M
    return true;
95
526M
  }
Unexecuted instantiation: hb_cache_t<24u, 16u, 8u, true>::get(unsigned int, unsigned int*) const
96
97
  bool set (unsigned int key, unsigned int value)
98
138M
  {
99
138M
    if (unlikely ((key >> key_bits) || (value >> value_bits)))
100
135M
      return false; /* Overflows */
101
2.76M
    unsigned int k = key & ((1u<<cache_bits)-1);
102
2.76M
    unsigned int v = ((key>>cache_bits)<<value_bits) | value;
103
2.76M
    values[k] = v;
104
2.76M
    return true;
105
138M
  }
hb_cache_t<15u, 8u, 7u, true>::set(unsigned int, unsigned int)
Line
Count
Source
98
135M
  {
99
135M
    if (unlikely ((key >> key_bits) || (value >> value_bits)))
100
135M
      return false; /* Overflows */
101
4.06k
    unsigned int k = key & ((1u<<cache_bits)-1);
102
4.06k
    unsigned int v = ((key>>cache_bits)<<value_bits) | value;
103
4.06k
    values[k] = v;
104
4.06k
    return true;
105
135M
  }
hb_cache_t<21u, 3u, 8u, true>::set(unsigned int, unsigned int)
Line
Count
Source
98
1.53M
  {
99
1.53M
    if (unlikely ((key >> key_bits) || (value >> value_bits)))
100
226k
      return false; /* Overflows */
101
1.31M
    unsigned int k = key & ((1u<<cache_bits)-1);
102
1.31M
    unsigned int v = ((key>>cache_bits)<<value_bits) | value;
103
1.31M
    values[k] = v;
104
1.31M
    return true;
105
1.53M
  }
hb_cache_t<21u, 19u, 8u, true>::set(unsigned int, unsigned int)
Line
Count
Source
98
1.44M
  {
99
1.44M
    if (unlikely ((key >> key_bits) || (value >> value_bits)))
100
0
      return false; /* Overflows */
101
1.44M
    unsigned int k = key & ((1u<<cache_bits)-1);
102
1.44M
    unsigned int v = ((key>>cache_bits)<<value_bits) | value;
103
1.44M
    values[k] = v;
104
1.44M
    return true;
105
1.44M
  }
Unexecuted instantiation: hb_cache_t<24u, 16u, 8u, true>::set(unsigned int, unsigned int)
106
107
  private:
108
  item_t values[1u<<cache_bits];
109
};
110
111
112
#endif /* HB_CACHE_HH */