Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:27:16

0001 // Copyright 2024 The Abseil Authors
0002 //
0003 // Licensed under the Apache License, Version 2.0 (the "License");
0004 // you may not use this file except in compliance with the License.
0005 // You may obtain a copy of the License at
0006 //
0007 //     https://www.apache.org/licenses/LICENSE-2.0
0008 //
0009 // Unless required by applicable law or agreed to in writing, software
0010 // distributed under the License is distributed on an "AS IS" BASIS,
0011 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
0012 // See the License for the specific language governing permissions and
0013 // limitations under the License.
0014 
0015 #ifndef ABSL_DEBUGGING_INTERNAL_BOUNDED_UTF8_LENGTH_SEQUENCE_H_
0016 #define ABSL_DEBUGGING_INTERNAL_BOUNDED_UTF8_LENGTH_SEQUENCE_H_
0017 
0018 #include <cstdint>
0019 
0020 #include "absl/base/config.h"
0021 #include "absl/numeric/bits.h"
0022 
0023 namespace absl {
0024 ABSL_NAMESPACE_BEGIN
0025 namespace debugging_internal {
0026 
0027 // A sequence of up to max_elements integers between 1 and 4 inclusive, whose
0028 // insertion operation computes the sum of all the elements before the insertion
0029 // point.  This is useful in decoding Punycode, where one needs to know where in
0030 // a UTF-8 byte stream the n-th code point begins.
0031 //
0032 // BoundedUtf8LengthSequence is async-signal-safe and suitable for use in
0033 // symbolizing stack traces in a signal handler, provided max_elements is not
0034 // improvidently large.  For inputs of lengths accepted by the Rust demangler,
0035 // up to a couple hundred code points, InsertAndReturnSumOfPredecessors should
0036 // run in a few dozen clock cycles, on par with the other arithmetic required
0037 // for Punycode decoding.
0038 template <uint32_t max_elements>
0039 class BoundedUtf8LengthSequence {
0040  public:
0041   // Constructs an empty sequence.
0042   BoundedUtf8LengthSequence() = default;
0043 
0044   // Inserts `utf_length` at position `index`, shifting any existing elements at
0045   // or beyond `index` one position to the right.  If the sequence is already
0046   // full, the rightmost element is discarded.
0047   //
0048   // Returns the sum of the elements at positions 0 to `index - 1` inclusive.
0049   // If `index` is greater than the number of elements already inserted, the
0050   // excess positions in the range count 1 apiece.
0051   //
0052   // REQUIRES: index < max_elements and 1 <= utf8_length <= 4.
0053   uint32_t InsertAndReturnSumOfPredecessors(
0054       uint32_t index, uint32_t utf8_length) {
0055     // The caller shouldn't pass out-of-bounds inputs, but if it does happen,
0056     // clamp the values and try to continue.  If we're being called from a
0057     // signal handler, the last thing we want to do is crash.  Emitting
0058     // malformed UTF-8 is a lesser evil.
0059     if (index >= max_elements) index = max_elements - 1;
0060     if (utf8_length == 0 || utf8_length > 4) utf8_length = 1;
0061 
0062     const uint32_t word_index = index/32;
0063     const uint32_t bit_index = 2 * (index % 32);
0064     const uint64_t ones_bit = uint64_t{1} << bit_index;
0065 
0066     // Compute the sum of predecessors.
0067     //   - Each value from 1 to 4 is represented by a bit field with value from
0068     //     0 to 3, so the desired sum is index plus the sum of the
0069     //     representations actually stored.
0070     //   - For each bit field, a set low bit should contribute 1 to the sum, and
0071     //     a set high bit should contribute 2.
0072     //   - Another way to say the same thing is that each set bit contributes 1,
0073     //     and each set high bit contributes an additional 1.
0074     //   - So the sum we want is index + popcount(everything) + popcount(bits in
0075     //     odd positions).
0076     const uint64_t odd_bits_mask = 0xaaaaaaaaaaaaaaaa;
0077     const uint64_t lower_seminibbles_mask = ones_bit - 1;
0078     const uint64_t higher_seminibbles_mask = ~lower_seminibbles_mask;
0079     const uint64_t same_word_bits_below_insertion =
0080         rep_[word_index] & lower_seminibbles_mask;
0081     int full_popcount = absl::popcount(same_word_bits_below_insertion);
0082     int odd_popcount =
0083         absl::popcount(same_word_bits_below_insertion & odd_bits_mask);
0084     for (uint32_t j = word_index; j > 0; --j) {
0085       const uint64_t word_below_insertion = rep_[j - 1];
0086       full_popcount += absl::popcount(word_below_insertion);
0087       odd_popcount += absl::popcount(word_below_insertion & odd_bits_mask);
0088     }
0089     const uint32_t sum_of_predecessors =
0090         index + static_cast<uint32_t>(full_popcount + odd_popcount);
0091 
0092     // Now insert utf8_length's representation, shifting successors up one
0093     // place.
0094     for (uint32_t j = max_elements/32 - 1; j > word_index; --j) {
0095       rep_[j] = (rep_[j] << 2) | (rep_[j - 1] >> 62);
0096     }
0097     rep_[word_index] =
0098         (rep_[word_index] & lower_seminibbles_mask) |
0099         (uint64_t{utf8_length - 1} << bit_index) |
0100         ((rep_[word_index] & higher_seminibbles_mask) << 2);
0101 
0102     return sum_of_predecessors;
0103   }
0104 
0105  private:
0106   // If the (32 * i + j)-th element of the represented sequence has the value k
0107   // (0 <= j < 32, 1 <= k <= 4), then bits 2 * j and 2 * j + 1 of rep_[i]
0108   // contain the seminibble (k - 1).
0109   //
0110   // In particular, the zero-initialization of rep_ makes positions not holding
0111   // any inserted element count as 1 in InsertAndReturnSumOfPredecessors.
0112   //
0113   // Example: rep_ = {0xb1, ... the rest zeroes ...} represents the sequence
0114   // (2, 1, 4, 3, ... the rest 1's ...).  Constructing the sequence of Unicode
0115   // code points "Àa🂻中" = {U+00C0, U+0061, U+1F0BB, U+4E2D} (among many
0116   // other examples) would yield this value of rep_.
0117   static_assert(max_elements > 0 && max_elements % 32 == 0,
0118                 "max_elements must be a positive multiple of 32");
0119   uint64_t rep_[max_elements/32] = {};
0120 };
0121 
0122 }  // namespace debugging_internal
0123 ABSL_NAMESPACE_END
0124 }  // namespace absl
0125 
0126 #endif  // ABSL_DEBUGGING_INTERNAL_BOUNDED_UTF8_LENGTH_SEQUENCE_H_