HikoGUI
A low latency retained GUI
Loading...
Searching...
No Matches
utf_32.hpp
Go to the documentation of this file.
1// Copyright Take Vos 2022.
2// Distributed under the Boost Software License, Version 1.0.
3// (See accompanying file LICENSE_1_0.txt or copy at https://www.boost.org/LICENSE_1_0.txt)
4
9#pragma once
10
11#include "char_converter.hpp"
12
13hi_warning_push();
14// C26490: Don't use reinterpret_cast.
15// Needed for SIMD intrinsics.
16hi_warning_ignore_msvc(26490);
17
18namespace hi { inline namespace v1 {
19
24template<>
25struct char_map<"utf-32"> {
26 using char_type = char32_t;
27
28 [[nodiscard]] std::endian guess_endian(void const *ptr, size_t size, std::endian endian) const noexcept
29 {
31 auto *ptr_ = static_cast<uint8_t const *>(ptr);
33
34 if (size < 4) {
35 return std::endian::native;
36 } else {
37 // Check for BOM.
38 if (ptr_[0] == 0x00 and ptr_[1] == 0x00 and ptr_[2] == 0xfe and ptr_[3] == 0xff) {
39 return std::endian::big;
40 } else if (ptr_[0] == 0xff and ptr_[1] == 0xfe and ptr_[2] == 0x00 and ptr_[3] == 0x00) {
41 return std::endian::little;
42 }
43
44 // Check for sequences of zeros.
45 auto count = std::array<size_t, 4>{};
46 for (auto i = 0; i != size; ++i) {
47 count[i % 4] = ptr_[i] == 0 ? count[i % 4] + 1 : 0;
48
49 if (i % 4 == 0 and count[0] >= 8) {
50 return std::endian::big;
51 } else if (i % 4 == 3 and count[3] >= 8) {
52 return std::endian::little;
53 }
54 }
55
56 return endian;
57 }
58 }
59
60 template<typename It, typename EndIt>
61 [[nodiscard]] constexpr std::pair<char32_t, bool> read(It& it, EndIt last) const noexcept
62 {
63 hi_axiom(it != last);
64
65 if (auto cu = *it++; cu < 0xd800) {
66 return {cu, true};
67
68 } else if (cu < 0xe000) {
69 // Surrogates
70 return {0xfffd, false};
71
72 } else if (cu < 0x11'0000) {
73 return {cu, true};
74
75 } else {
76 // Out-of-range
77 return {0xfffd, false};
78 }
79 }
80
81 [[nodiscard]] constexpr std::pair<uint8_t, bool> size(char32_t code_point) const noexcept
82 {
83 hi_axiom(code_point < 0x11'0000);
84 hi_axiom(not(code_point >= 0xd800 and code_point < 0xe000));
85 return {uint8_t{1}, true};
86 }
87
88 template<typename It>
89 constexpr void write(char32_t code_point, It& dst) const noexcept
90 {
91 hi_axiom(code_point < 0x11'0000);
92 hi_axiom(not(code_point >= 0xd800 and code_point < 0xe000));
93
94 *dst++ = code_point;
95 }
96
97#if defined(HI_HAS_SSE2)
98 template<typename It>
99 hi_force_inline __m128i read_ascii_chunk16(It it) const noexcept
100 {
101 // Load the UTF-16 data.
102 hilet c0 = _mm_loadu_si128(reinterpret_cast<__m128i const *>(std::addressof(*it)));
103 it += 4;
104 hilet c1 = _mm_loadu_si128(reinterpret_cast<__m128i const *>(std::addressof(*it)));
105 it += 4;
106 hilet c2 = _mm_loadu_si128(reinterpret_cast<__m128i const *>(std::addressof(*it)));
107 it += 4;
108 hilet c3 = _mm_loadu_si128(reinterpret_cast<__m128i const *>(std::addressof(*it)));
109
110 hilet lo = _mm_packs_epi32(c0, c1);
111 hilet hi = _mm_packs_epi32(c2, c3);
112
113 // To get _mm_packus_epi16() to work we need to prepare the data as follows:
114 // - bit 15 must be '0'.
115 // - if bit 15 was originally set than we need to set any of bits [14:8].
116
117 // Positive numbers -> 0b0000'0000
118 // Negative numbers -> 0b1000'0000
119 hilet sign_lo = _mm_srai_epi16(lo, 15);
120 hilet sign_hi = _mm_srai_epi16(hi, 15);
121 hilet sign = _mm_packs_epi16(sign_lo, sign_hi);
122
123 // ASCII -> 0b0ccc'cccc
124 // positive numbers -> 0b1???'????
125 // negative numbers -> 0b0000'0000
126 hilet chunk = _mm_packus_epi16(lo, hi);
127
128 // ASCII -> 0b0ccc'cccc
129 // positive numbers -> 0b1???'????
130 // negative numbers -> 0b1000'0000
131 return _mm_or_si128(chunk, sign);
132 }
133
134 template<typename It>
135 hi_force_inline void write_ascii_chunk16(__m128i chunk, It dst) const noexcept
136 {
137 hilet zero = _mm_setzero_si128();
138 hilet lo = _mm_unpacklo_epi8(chunk, zero);
139 hilet hi = _mm_unpackhi_epi8(chunk, zero);
140
141 hilet c0 = _mm_unpacklo_epi8(lo, zero);
142 hilet c1 = _mm_unpackhi_epi8(lo, zero);
143 hilet c2 = _mm_unpacklo_epi8(hi, zero);
144 hilet c3 = _mm_unpackhi_epi8(hi, zero);
145
146 _mm_storeu_si128(reinterpret_cast<__m128i *>(std::addressof(*dst)), c0);
147 dst += 4;
148 _mm_storeu_si128(reinterpret_cast<__m128i *>(std::addressof(*dst)), c1);
149 dst += 4;
150 _mm_storeu_si128(reinterpret_cast<__m128i *>(std::addressof(*dst)), c2);
151 dst += 4;
152 _mm_storeu_si128(reinterpret_cast<__m128i *>(std::addressof(*dst)), c3);
153 }
154#endif
155};
156
157}} // namespace hi::v1
158
159hi_warning_pop();
Definition of the char_converter<From,To> functor.
#define hi_axiom(expression,...)
Specify an axiom; an expression that is true.
Definition assert.hpp:238
#define hi_assert_not_null(x,...)
Assert if an expression is not nullptr.
Definition assert.hpp:223
#define hi_axiom_not_null(expression,...)
Assert if an expression is not nullptr.
Definition assert.hpp:257
#define hilet
Invariant should be the default for variables.
Definition utility.hpp:23
@ read
Allow read access to a file.
@ write
Allow write access to a file.
DOXYGEN BUG.
Definition algorithm.hpp:13
geometry/margins.hpp
Definition cache.hpp:11
Character encoder/decoder template.
Definition char_converter.hpp:83
T addressof(T... args)