@geastack/host 0.1.22 → 0.1.23
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/include/gea/embedded-host.h +1 -0
- package/include/host/memory.h +15 -0
- package/include/host/simd.h +191 -0
- package/package.json +1 -1
package/include/host/memory.h
CHANGED
|
@@ -3,6 +3,13 @@
|
|
|
3
3
|
|
|
4
4
|
#include "backends.h"
|
|
5
5
|
|
|
6
|
+
#include <cstddef>
|
|
7
|
+
|
|
8
|
+
// A platform with fast on-chip RAM next to a large slow one (ESP32-S3: SRAM
|
|
9
|
+
// and PSRAM) defines these; elsewhere they are absent and the calls do nothing.
|
|
10
|
+
extern "C" void gea_memory_begin_internal(std::size_t bytes) __attribute__((weak));
|
|
11
|
+
extern "C" void gea_memory_end_internal() __attribute__((weak));
|
|
12
|
+
|
|
6
13
|
namespace gea::host {
|
|
7
14
|
|
|
8
15
|
struct MemoryFacade {
|
|
@@ -37,6 +44,14 @@ struct MemoryFacade {
|
|
|
37
44
|
double allocationPsramBytes() const { return gea::framework::memory::MemoryBackend::allocationPsramBytes(); }
|
|
38
45
|
double allocationSramPeakBytes() const { return gea::framework::memory::MemoryBackend::allocationSramPeakBytes(); }
|
|
39
46
|
double allocationPsramPeakBytes() const { return gea::framework::memory::MemoryBackend::allocationPsramPeakBytes(); }
|
|
47
|
+
// Allocations this task makes until endInternal() prefer on-chip RAM; `bytes`
|
|
48
|
+
// says how much to make room for (the display may yield staging buffers).
|
|
49
|
+
void beginInternal(double bytes) const {
|
|
50
|
+
if (gea_memory_begin_internal) gea_memory_begin_internal(bytes > 0 ? static_cast<std::size_t>(bytes) : 0);
|
|
51
|
+
}
|
|
52
|
+
void endInternal() const {
|
|
53
|
+
if (gea_memory_end_internal) gea_memory_end_internal();
|
|
54
|
+
}
|
|
40
55
|
};
|
|
41
56
|
|
|
42
57
|
inline constexpr MemoryFacade Memory{};
|
|
@@ -0,0 +1,191 @@
|
|
|
1
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
2
|
+
#pragma once
|
|
3
|
+
|
|
4
|
+
#include <cstddef>
|
|
5
|
+
#include <cstdint>
|
|
6
|
+
|
|
7
|
+
// ESP32-S3 PIE (Processor Instruction Extensions) vector kernels for per-frame
|
|
8
|
+
// byte-grid effects. Every other target compiles the scalar loop, which is also
|
|
9
|
+
// the reference the vector path must match bit for bit.
|
|
10
|
+
#if defined(__XTENSA__) && __has_include("sdkconfig.h")
|
|
11
|
+
#include "sdkconfig.h"
|
|
12
|
+
#endif
|
|
13
|
+
#if defined(CONFIG_IDF_TARGET_ESP32S3)
|
|
14
|
+
#define GEA_HOST_SIMD_PIE 1
|
|
15
|
+
#include "freertos/FreeRTOS.h"
|
|
16
|
+
#else
|
|
17
|
+
#define GEA_HOST_SIMD_PIE 0
|
|
18
|
+
#endif
|
|
19
|
+
|
|
20
|
+
namespace gea::host::simd {
|
|
21
|
+
|
|
22
|
+
inline constexpr int kMaxWindows = 8;
|
|
23
|
+
|
|
24
|
+
inline void sumWindowsRowScalar(std::uint8_t *dst, const std::uint8_t *const *rows, int windows, int count, int multiplier,
|
|
25
|
+
int shift) {
|
|
26
|
+
for (int x = 0; x < count; ++x) {
|
|
27
|
+
int sum = 0;
|
|
28
|
+
for (int k = 0; k < windows; ++k) sum += rows[k][x];
|
|
29
|
+
const int value = (sum * multiplier) >> shift;
|
|
30
|
+
dst[x] = static_cast<std::uint8_t>(value < 255 ? value : 255);
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
#if GEA_HOST_SIMD_PIE
|
|
35
|
+
// 16 cells per group: each window's 16 source bytes come from two aligned
|
|
36
|
+
// loads joined by EE.SRC.Q (sources sit at any byte offset), are widened to
|
|
37
|
+
// u16 by zipping with zero and accumulated with saturating s16 adds. The sum
|
|
38
|
+
// is then scaled by `multiplier >> SAR`, clamped to 255 and narrowed back by
|
|
39
|
+
// keeping the low bytes. `dst` must be 16-byte aligned; each source read
|
|
40
|
+
// reaches up to 31 bytes past its group's start.
|
|
41
|
+
//
|
|
42
|
+
// q registers have been seen corrupted by a mid-kernel preemption (rgb_panel's
|
|
43
|
+
// blend kernel), so the block runs with this core's interrupts masked; a row is
|
|
44
|
+
// a few microseconds.
|
|
45
|
+
inline void sumWindowsGroupsPie(std::uint8_t *dst, const std::uint8_t **rows, int windows, int groups, int multiplier, int shift) {
|
|
46
|
+
alignas(16) std::uint16_t constants[2] = {static_cast<std::uint16_t>(multiplier), 255};
|
|
47
|
+
const std::uint16_t *limit = constants + 1;
|
|
48
|
+
int column = 0;
|
|
49
|
+
const std::uint8_t **cursor;
|
|
50
|
+
const std::uint8_t *source;
|
|
51
|
+
int remaining;
|
|
52
|
+
const UBaseType_t interrupts = portSET_INTERRUPT_MASK_FROM_ISR();
|
|
53
|
+
asm volatile(
|
|
54
|
+
"wsr.sar %[shift]\n"
|
|
55
|
+
"ee.vldbc.16 q6, %[constants]\n"
|
|
56
|
+
"ee.vldbc.16 q7, %[limit]\n"
|
|
57
|
+
"loopnez %[groups], 3f\n"
|
|
58
|
+
"ee.zero.q q4\n"
|
|
59
|
+
"ee.zero.q q5\n"
|
|
60
|
+
"mov %[cursor], %[rows]\n"
|
|
61
|
+
"mov %[remaining], %[windows]\n"
|
|
62
|
+
"2:\n"
|
|
63
|
+
"l32i %[source], %[cursor], 0\n"
|
|
64
|
+
"add %[source], %[source], %[column]\n"
|
|
65
|
+
"ee.ld.128.usar.ip q0, %[source], 16\n"
|
|
66
|
+
"ee.ld.128.usar.ip q1, %[source], 0\n"
|
|
67
|
+
"ee.src.q q2, q0, q1\n"
|
|
68
|
+
"ee.zero.q q3\n"
|
|
69
|
+
"ee.vzip.8 q2, q3\n"
|
|
70
|
+
"ee.vadds.s16 q4, q4, q2\n"
|
|
71
|
+
"ee.vadds.s16 q5, q5, q3\n"
|
|
72
|
+
"addi %[cursor], %[cursor], 4\n"
|
|
73
|
+
"addi %[remaining], %[remaining], -1\n"
|
|
74
|
+
"bnez %[remaining], 2b\n"
|
|
75
|
+
"ee.vmul.s16 q4, q4, q6\n"
|
|
76
|
+
"ee.vmul.s16 q5, q5, q6\n"
|
|
77
|
+
"ee.vmin.s16 q4, q4, q7\n"
|
|
78
|
+
"ee.vmin.s16 q5, q5, q7\n"
|
|
79
|
+
"ee.vunzip.8 q4, q5\n"
|
|
80
|
+
"ee.vst.128.ip q4, %[dst], 16\n"
|
|
81
|
+
"addi %[column], %[column], 16\n"
|
|
82
|
+
"3:\n"
|
|
83
|
+
: [dst] "+&r"(dst), [column] "+&r"(column), [cursor] "=&r"(cursor), [source] "=&r"(source), [remaining] "=&r"(remaining)
|
|
84
|
+
: [rows] "r"(rows), [windows] "r"(windows), [groups] "r"(groups), [shift] "r"(shift), [constants] "r"(constants),
|
|
85
|
+
[limit] "r"(limit)
|
|
86
|
+
: "memory");
|
|
87
|
+
portCLEAR_INTERRUPT_MASK_FROM_ISR(interrupts);
|
|
88
|
+
}
|
|
89
|
+
#endif
|
|
90
|
+
|
|
91
|
+
// Whether start + (rows - 1) * stride + width <= length, without overflowing.
|
|
92
|
+
inline bool spanFits(std::size_t start, std::size_t stride, int rows, int width, std::size_t length) {
|
|
93
|
+
if (start > length) return false;
|
|
94
|
+
std::size_t room = length - start;
|
|
95
|
+
if (static_cast<std::size_t>(width) > room) return false;
|
|
96
|
+
room -= static_cast<std::size_t>(width);
|
|
97
|
+
const std::size_t steps = static_cast<std::size_t>(rows - 1);
|
|
98
|
+
return steps == 0 || stride == 0 || steps <= room / stride;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
// dst[dstStart + r*dstStride + x] =
|
|
102
|
+
// min(255, ((Σ_k src[offsets[k] + r*srcStride + x]) * multiplier) >> shift)
|
|
103
|
+
// for r in [0, rows), x in [0, width). Any read or write outside the arrays
|
|
104
|
+
// makes the whole call a no-op rather than a partial write.
|
|
105
|
+
inline void sumWindowsU8(std::uint8_t *dst, std::size_t dstLength, int dstStart, int dstStride, const std::uint8_t *src,
|
|
106
|
+
std::size_t srcLength, const std::int32_t *offsets, std::size_t offsetsLength, int windows, int srcStride,
|
|
107
|
+
int width, int rows, int multiplier, int shift) {
|
|
108
|
+
if (!dst || !src || !offsets || windows <= 0 || windows > kMaxWindows || static_cast<std::size_t>(windows) > offsetsLength) return;
|
|
109
|
+
if (width <= 0 || rows <= 0 || multiplier < 0 || shift < 0 || shift > 30 || dstStart < 0 || dstStride < 0 || srcStride < 0) return;
|
|
110
|
+
if (!spanFits(static_cast<std::size_t>(dstStart), static_cast<std::size_t>(dstStride), rows, width, dstLength)) return;
|
|
111
|
+
for (int k = 0; k < windows; ++k) {
|
|
112
|
+
if (offsets[k] < 0 || !spanFits(static_cast<std::size_t>(offsets[k]), static_cast<std::size_t>(srcStride), rows, width, srcLength)) return;
|
|
113
|
+
}
|
|
114
|
+
#if GEA_HOST_SIMD_PIE
|
|
115
|
+
// Sums stay in s16 lanes and the scaled value must not overflow one before
|
|
116
|
+
// the clamp; outside that range the scalar loop is exact.
|
|
117
|
+
const bool vector = multiplier <= 32767 && ((windows * 255 * multiplier) >> shift) <= 32767;
|
|
118
|
+
#endif
|
|
119
|
+
const std::uint8_t *rowSources[kMaxWindows];
|
|
120
|
+
for (int r = 0; r < rows; ++r) {
|
|
121
|
+
std::uint8_t *out = dst + dstStart + static_cast<std::ptrdiff_t>(r) * dstStride;
|
|
122
|
+
for (int k = 0; k < windows; ++k) rowSources[k] = src + offsets[k] + static_cast<std::ptrdiff_t>(r) * srcStride;
|
|
123
|
+
int count = width;
|
|
124
|
+
#if GEA_HOST_SIMD_PIE
|
|
125
|
+
if (vector && count >= 32) {
|
|
126
|
+
const int head = static_cast<int>((16 - (reinterpret_cast<std::uintptr_t>(out) & 15u)) & 15u);
|
|
127
|
+
sumWindowsRowScalar(out, rowSources, windows, head, multiplier, shift);
|
|
128
|
+
out += head;
|
|
129
|
+
count -= head;
|
|
130
|
+
for (int k = 0; k < windows; ++k) rowSources[k] += head;
|
|
131
|
+
int groups = count >> 4;
|
|
132
|
+
// The last group's loads reach 31 bytes past its start; keep them inside
|
|
133
|
+
// every source array by leaving trailing groups to the scalar tail.
|
|
134
|
+
std::ptrdiff_t slack = static_cast<std::ptrdiff_t>(srcLength);
|
|
135
|
+
for (int k = 0; k < windows; ++k) {
|
|
136
|
+
const std::ptrdiff_t room = static_cast<std::ptrdiff_t>(srcLength) - (rowSources[k] - src);
|
|
137
|
+
if (room < slack) slack = room;
|
|
138
|
+
}
|
|
139
|
+
while (groups > 0 && static_cast<std::ptrdiff_t>(groups - 1) * 16 + 32 > slack) --groups;
|
|
140
|
+
if (groups > 0) {
|
|
141
|
+
sumWindowsGroupsPie(out, rowSources, windows, groups, multiplier, shift);
|
|
142
|
+
out += groups * 16;
|
|
143
|
+
count -= groups * 16;
|
|
144
|
+
for (int k = 0; k < windows; ++k) rowSources[k] += groups * 16;
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
#endif
|
|
148
|
+
sumWindowsRowScalar(out, rowSources, windows, count, multiplier, shift);
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
} // namespace gea::host::simd
|
|
153
|
+
|
|
154
|
+
namespace gea::host {
|
|
155
|
+
|
|
156
|
+
namespace simd {
|
|
157
|
+
// The program's typed array, whether the call site passes the array view
|
|
158
|
+
// itself or a handle to one; null for an absent handle.
|
|
159
|
+
template <typename Array>
|
|
160
|
+
auto *viewOf(const Array &array) {
|
|
161
|
+
if constexpr (requires { array.data(); array.size(); }) {
|
|
162
|
+
return &const_cast<Array &>(array);
|
|
163
|
+
} else {
|
|
164
|
+
return array ? &*array : nullptr;
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
} // namespace simd
|
|
168
|
+
|
|
169
|
+
struct SimdFacade {
|
|
170
|
+
// Typed-array arguments are bound by reference to the program's own views:
|
|
171
|
+
// no copy, and no dependency here on the runtime that defines them. A view
|
|
172
|
+
// is a window onto shared bytes, so writing through it is the JS semantics.
|
|
173
|
+
template <typename Dst, typename Src, typename Offsets>
|
|
174
|
+
void sumWindowsU8(const Dst &dstArray, double dstStart, double dstStride, const Src &srcArray, const Offsets &offsetsArray,
|
|
175
|
+
double windows, double srcStride, double width, double rows, double multiplier, double shift) const {
|
|
176
|
+
auto *dst = simd::viewOf(dstArray);
|
|
177
|
+
auto *src = simd::viewOf(srcArray);
|
|
178
|
+
auto *offsets = simd::viewOf(offsetsArray);
|
|
179
|
+
if (!dst || !src || !offsets) return;
|
|
180
|
+
static_assert(sizeof(*dst->data()) == 1 && sizeof(*src->data()) == 1 && sizeof(*offsets->data()) == 4);
|
|
181
|
+
simd::sumWindowsU8(reinterpret_cast<std::uint8_t *>(dst->data()), dst->size(), static_cast<int>(dstStart),
|
|
182
|
+
static_cast<int>(dstStride), reinterpret_cast<const std::uint8_t *>(src->data()), src->size(),
|
|
183
|
+
reinterpret_cast<const std::int32_t *>(offsets->data()), offsets->size(), static_cast<int>(windows),
|
|
184
|
+
static_cast<int>(srcStride), static_cast<int>(width), static_cast<int>(rows), static_cast<int>(multiplier),
|
|
185
|
+
static_cast<int>(shift));
|
|
186
|
+
}
|
|
187
|
+
};
|
|
188
|
+
|
|
189
|
+
inline constexpr SimdFacade Simd{};
|
|
190
|
+
|
|
191
|
+
} // namespace gea::host
|