ascii-chat
Binary v0.16.114-28-gf5913657f
Library lib/v0.3.2-3302-gf5913657f
Video chat in your terminal
Loading...
Searching...
No Matches
video/ascii/common.c
Go to the documentation of this file.
1
6
7#include <ascii-chat/common.h>
9#include <ascii-chat/uthash.h>
13#include <libplatform/lifecycle.h>
15#include <libplatform/cpu.h>
16#include <time.h>
17#include <math.h>
18#include <libplatform/atomic.h>
19
20// Include SIMD architecture headers for cleanup functions
21// Note: Only ONE SIMD implementation is compiled based on highest available instruction set
22#if SIMD_SUPPORT_NEON
24#elif SIMD_SUPPORT_AVX2
26#elif SIMD_SUPPORT_SSSE3
28#elif SIMD_SUPPORT_SSE2
30#elif SIMD_SUPPORT_SVE
32#endif
33
34// Default luminance palette initialization lifecycle
35static lifecycle_t g_default_palette_lc = LIFECYCLE_INIT;
36
37// Global variable definitions for extern declarations in common.h
40
41// Build 64-entry glyph LUT for vqtbl4q_u8 and other architecture's instrinsics (UTF-8 aware)
42void build_ramp64(uint8_t ramp64[RAMP64_SIZE], const char *ascii_chars) {
43 if (!ascii_chars) {
44 // Fallback to space character
45 for (int i = 0; i < RAMP64_SIZE; i++) {
46 ramp64[i] = ' ';
47 }
48 return;
49 }
50
51 // Use UTF-8 palette functions for proper character handling
52 utf8_palette_t *utf8_pal = utf8_palette_create(ascii_chars);
53 if (!utf8_pal) {
54 // Fallback to space character
55 for (int i = 0; i < RAMP64_SIZE; i++) {
56 ramp64[i] = ' ';
57 }
58 return;
59 }
60
61 size_t char_count = utf8_palette_get_char_count(utf8_pal);
62 if (char_count == 0) {
63 // No valid characters found, use space
64 for (int i = 0; i < RAMP64_SIZE; i++) {
65 ramp64[i] = ' ';
66 }
67 utf8_palette_destroy(utf8_pal);
68 return;
69 }
70
71 // Build the ramp64 lookup using UTF-8 character indices
72 for (int i = 0; i < RAMP64_SIZE; i++) {
73 // Map 0-63 to 0-(char_count-1) using proper character indexing
74 size_t char_idx = (i * (char_count - 1) + (RAMP64_SIZE - 1) / 2) / (RAMP64_SIZE - 1);
75 if (char_idx >= char_count) {
76 char_idx = char_count - 1;
77 }
78
79 // Get the first byte of the character at this character index
80 const utf8_char_info_t *char_info = utf8_palette_get_char(utf8_pal, char_idx);
81 if (char_info) {
82 ramp64[i] = (uint8_t)char_info->bytes[0];
83 } else {
84 ramp64[i] = ' '; // Fallback
85 }
86 }
87
88 utf8_palette_destroy(utf8_pal);
89}
90
91// Cache eviction helper functions
92double calculate_cache_eviction_score(uint64_t last_access_time, uint32_t access_count, uint64_t creation_time, uint64_t current_time) {
93 // Protect against unsigned underflow if times are inconsistent (clock adjustments, etc.)
94 uint64_t age_ns = (current_time >= last_access_time) ? (current_time - last_access_time) : 0;
95 uint64_t total_age_ns = (current_time >= creation_time) ? (current_time - creation_time) : 0;
96
97 uint64_t age_seconds = age_ns / NS_PER_SEC_INT;
98 uint64_t total_age_seconds = total_age_ns / NS_PER_SEC_INT;
99
100 // Frequency factor: high-use palettes get protection (logarithmic scaling)
101 double frequency_factor = 1.0 + log10(1.0 + access_count);
102
103 // Aging factor: frequency bonus decays over time (5-minute half-life)
104 double aging_factor = exp(-(double)age_seconds / CACHE_FREQUENCY_DECAY_TIME);
105
106 // Recent access bonus: strong protection for recently used (1-minute protection)
107 double recency_bonus = exp(-(double)age_seconds / CACHE_RECENCY_SCALE);
108
109 // Cache lifetime penalty: prevent immortal caches (1-hour max lifetime)
110 double lifetime_penalty = total_age_seconds > CACHE_MAX_LIFETIME ? 0.5 : 1.0;
111
112 // Final score: higher = keep longer
113 return (frequency_factor * aging_factor + recency_bonus) * lifetime_penalty;
114}
115
116// Fast palette string hashing using shared FNV-1a hash function
117static inline uint32_t hash_palette_string(const char *palette) {
118 return fnv1a_hash_string(palette);
119}
120
121// Forward declarations for eviction functions (defined after globals)
122static bool try_insert_with_eviction_utf8(uint32_t hash, utf8_palette_cache_t *new_cache);
123
124// UTF-8 palette cache system with min-heap eviction
125static utf8_palette_cache_t *g_utf8_cache_table = NULL; // uthash uses structure pointer as head
126static rwlock_t g_utf8_cache_rwlock = {0};
127static lifecycle_t g_utf8_cache_lc = LIFECYCLE_INIT;
128
129// Min-heap for O(log n) intelligent eviction
130static utf8_palette_cache_t **g_utf8_heap = NULL; // Min-heap array
131static size_t g_utf8_heap_size = 0; // Current entries in heap
132static const size_t g_utf8_heap_capacity = 2048; // Max heap size (matching uthash capacity)
133
134// char_index_ramp_cache removed - data is already stored in utf8_palette_cache_t.char_index_ramp[64]
135
136// Initialize UTF-8 cache system with min-heap (thread-safe)
137static bool init_utf8_cache_system(void) {
138 if (!lifecycle_init_once(&g_utf8_cache_lc)) {
139 // A competing renderer must wait for both the lock and heap to exist.
140 while (atomic_load_explicit(&g_utf8_cache_lc.state, memory_order_acquire) == LIFECYCLE_INITIALIZING)
141 platform_sleep_ms(1);
142 return lifecycle_is_initialized(&g_utf8_cache_lc);
143 }
144 if (rwlock_init(&g_utf8_cache_rwlock, "utf8_cache") != 0) {
145 lifecycle_init_abort(&g_utf8_cache_lc);
146 return false;
147 }
148 g_utf8_cache_lc.sync_type = LIFECYCLE_SYNC_RWLOCK;
149 g_utf8_cache_lc.sync.rwlock = &g_utf8_cache_rwlock;
150
151 // Initialize uthash head to NULL (required)
152 g_utf8_cache_table = NULL;
153 g_utf8_heap = SAFE_MALLOC(g_utf8_heap_capacity * sizeof(utf8_palette_cache_t *), utf8_palette_cache_t **);
154 g_utf8_heap_size = 0;
155 if (!g_utf8_heap) {
156 rwlock_destroy(&g_utf8_cache_rwlock);
157 g_utf8_cache_lc.sync_type = LIFECYCLE_SYNC_NONE;
158 lifecycle_init_abort(&g_utf8_cache_lc);
159 return false;
160 }
161 lifecycle_init_commit(&g_utf8_cache_lc);
162 return true;
163}
164
165// Min-heap management functions for UTF-8 cache
166static void utf8_heap_swap(size_t i, size_t j) {
167 utf8_palette_cache_t *temp = g_utf8_heap[i];
168 g_utf8_heap[i] = g_utf8_heap[j];
169 g_utf8_heap[j] = temp;
170
171 // Update heap indices in cache objects
172 g_utf8_heap[i]->heap_index = i;
173 g_utf8_heap[j]->heap_index = j;
174}
175
176static void utf8_heap_bubble_up(size_t index) {
177 while (index > 0) {
178 size_t parent = (index - 1) / 2;
179 if (g_utf8_heap[index]->cached_score >= g_utf8_heap[parent]->cached_score)
180 break;
181 utf8_heap_swap(index, parent);
182 index = parent;
183 }
184}
185
186static void utf8_heap_bubble_down(size_t index) {
187 while (true) {
188 size_t left = 2 * index + 1;
189 size_t right = 2 * index + 2;
190 size_t smallest = index;
191
192 if (left < g_utf8_heap_size && g_utf8_heap[left]->cached_score < g_utf8_heap[smallest]->cached_score) {
193 smallest = left;
194 }
195 if (right < g_utf8_heap_size && g_utf8_heap[right]->cached_score < g_utf8_heap[smallest]->cached_score) {
196 smallest = right;
197 }
198
199 if (smallest == index)
200 break;
201 utf8_heap_swap(index, smallest);
202 index = smallest;
203 }
204}
205
206static void utf8_heap_insert(utf8_palette_cache_t *cache, double score) {
207 if (g_utf8_heap_size >= g_utf8_heap_capacity) {
208 log_error("UTF8_HEAP: Heap capacity exceeded");
209 return;
210 }
211
212 cache->cached_score = score;
213 cache->heap_index = g_utf8_heap_size;
214 g_utf8_heap[g_utf8_heap_size] = cache;
215 g_utf8_heap_size++;
216
217 utf8_heap_bubble_up(cache->heap_index);
218}
219
220static utf8_palette_cache_t *utf8_heap_extract_min(void) {
221 if (g_utf8_heap_size == 0) {
222 return NULL;
223 }
224
225 utf8_palette_cache_t *min_cache = g_utf8_heap[0];
226
227 // Move last element to root and bubble down
228 g_utf8_heap_size--;
229 if (g_utf8_heap_size > 0) {
230 g_utf8_heap[0] = g_utf8_heap[g_utf8_heap_size];
231 g_utf8_heap[0]->heap_index = 0;
232 utf8_heap_bubble_down(0);
233 }
234
235 return min_cache;
236}
237
238static void utf8_heap_update_score(utf8_palette_cache_t *cache, double new_score) {
239 double old_score = cache->cached_score;
240 cache->cached_score = new_score;
241
242 if (new_score < old_score) {
243 utf8_heap_bubble_up(cache->heap_index);
244 } else {
245 utf8_heap_bubble_down(cache->heap_index);
246 }
247}
248
249// Min-heap functions now replace LRU list management
250
251// Thread-safe cache eviction implementations
252static bool try_insert_with_eviction_utf8(uint32_t hash, utf8_palette_cache_t *new_cache) {
253 // Already holding write lock
254 // Note: key should already be set by caller, but ensure it's set
255 new_cache->key = hash;
256
257 // Check if hash table is near capacity and evict proactively (80% threshold)
258 size_t entry_count = HASH_COUNT(g_utf8_cache_table);
259 if (entry_count >= g_utf8_heap_capacity) {
260 // Proactive eviction: free space before attempting insertion
261 utf8_palette_cache_t *victim_cache = utf8_heap_extract_min();
262 if (victim_cache) {
263 // WASM: Skip debug log to avoid blocking render loop
264
265 HASH_DEL(g_utf8_cache_table, victim_cache);
266 SAFE_FREE(victim_cache);
267 }
268 }
269
270 // Now attempt insertion (should succeed with freed space)
271 // Note: uthash doesn't have a failure path for insertion, so we handle eviction proactively
272 HASH_ADD_INT(g_utf8_cache_table, key, new_cache);
273
274 // Success: add to min-heap
275 uint64_t current_time = time_get_ns();
276 double initial_score = calculate_cache_eviction_score(current_time, 1, current_time, current_time);
277 utf8_heap_insert(new_cache, initial_score);
278 return true;
279}
280
281// char_ramp_cache functions removed - data already available in utf8_palette_cache_t
282
283// Get or create UTF-8 palette cache for a given palette
285 if (!ascii_chars)
286 return NULL;
287
288 // Check for empty string
289 if (ascii_chars[0] == '\0')
290 return NULL;
291
292 // Create hash of palette for cache key
293 uint32_t palette_hash = hash_palette_string(ascii_chars);
294
295 // Ensure the cache system is initialized
296 if (!init_utf8_cache_system())
297 return NULL;
298
299 // First try: read lock for cache lookup (allows multiple concurrent readers)
300 rwlock_rdlock(&g_utf8_cache_rwlock);
301
302 // Check if cache exists
303 utf8_palette_cache_t *cache = NULL;
304 HASH_FIND_INT(g_utf8_cache_table, &palette_hash, cache);
305 if (cache) {
306 // Cache hit: Update access tracking (atomics are thread-safe under rdlock)
307 uint64_t current_time = time_get_ns();
308 atomic_store_u64(&cache->last_access_time, current_time);
309 uint32_t new_access_count = (uint32_t)(atomic_fetch_add_u64(&cache->access_count, 1) + 1);
310
311 // Every 10th access: Update heap position (requires write lock)
312 if (new_access_count % 10 == 0) {
313 // Save the key before releasing read lock
314 uint32_t saved_key = cache->key;
315
316 // Release read lock and upgrade to write lock
317 rwlock_rdunlock(&g_utf8_cache_rwlock);
318 rwlock_wrlock(&g_utf8_cache_rwlock);
319
320 // Re-lookup cache entry after lock upgrade
321 // Another thread could have evicted this entry while we were upgrading locks
322 utf8_palette_cache_t *cache_relookup = NULL;
323 HASH_FIND_INT(g_utf8_cache_table, &saved_key, cache_relookup);
324 if (cache_relookup) {
325 // Cache entry still exists - safe to update heap position
326 uint64_t last_access = atomic_load_u64(&cache_relookup->last_access_time);
327 uint32_t access_count = (uint32_t)atomic_load_u64(&cache_relookup->access_count);
328 double new_score = calculate_cache_eviction_score(last_access, access_count, cache_relookup->creation_time, current_time);
329 utf8_heap_update_score(cache_relookup, new_score);
330 }
331 // If cache_relookup is NULL, entry was evicted - skip update (not an error)
332
333 rwlock_wrunlock(&g_utf8_cache_rwlock);
334 } else {
335 rwlock_rdunlock(&g_utf8_cache_rwlock);
336 }
337
338 return cache;
339 }
340
341 // Cache miss: Need to create new entry
342 // Release read lock and acquire write lock
343 rwlock_rdunlock(&g_utf8_cache_rwlock);
344 rwlock_wrlock(&g_utf8_cache_rwlock);
345
346 // Double-check: another thread might have created it while we upgraded locks
347 cache = NULL;
348 HASH_FIND_INT(g_utf8_cache_table, &palette_hash, cache);
349 if (cache) {
350 // Found it! Just update access tracking and return
351 uint64_t current_time = time_get_ns();
352 atomic_store_u64(&cache->last_access_time, current_time);
354 rwlock_wrunlock(&g_utf8_cache_rwlock);
355 return cache;
356 }
357
358 // Create new cache entry (holding write lock)
360 if (!cache) {
361 rwlock_wrunlock(&g_utf8_cache_rwlock);
362 return NULL;
363 }
364 memset(cache, 0, sizeof(utf8_palette_cache_t));
365
366 // Set the key for uthash
367 cache->key = palette_hash;
368
369 // Build both cache types
370 build_utf8_luminance_cache(ascii_chars, cache->cache);
371 build_utf8_ramp64_cache(ascii_chars, cache->cache64, cache->char_index_ramp);
372
373 // Store palette hash for validation
374 SAFE_STRNCPY(cache->palette_hash, ascii_chars, sizeof(cache->palette_hash));
375 cache->is_valid = true;
376
377 // Initialize eviction tracking
378 uint64_t current_time = time_get_ns();
379 atomic_store_u64(&cache->last_access_time, current_time);
380 atomic_store_u64(&cache->access_count, 1); // First access
381 cache->creation_time = current_time;
382
383 // Store in hash table with guaranteed eviction support
384 try_insert_with_eviction_utf8(palette_hash, cache);
385
386 // WASM: Skip debug log to avoid blocking render loop on main thread
387 // log_debug("UTF8_CACHE: Created new cache for palette='%s' (hash=0x%x)", ascii_chars, palette_hash);
388
389 rwlock_wrunlock(&g_utf8_cache_rwlock);
390 return cache;
391}
392
393// Build 256-entry UTF-8 cache for direct luminance lookup (monochrome renderers)
394void build_utf8_luminance_cache(const char *ascii_chars, utf8_char_t cache[256]) {
395 if (!ascii_chars || !cache)
396 return;
397
398 // Parse characters
399 typedef struct {
400 const char *start;
401 int byte_len;
402 } char_info_t;
403
404 char_info_t char_infos[256];
405 int char_count = 0;
406 const char *p = ascii_chars;
407
408 while (*p && char_count < 255) {
409 char_infos[char_count].start = p;
410
411 if ((*p & 0xE0) == 0xC0) {
412 char_infos[char_count].byte_len = 2;
413 p += 2;
414 } else if ((*p & 0xF0) == 0xE0) {
415 char_infos[char_count].byte_len = 3;
416 p += 3;
417 } else if ((*p & 0xF8) == 0xF0) {
418 char_infos[char_count].byte_len = 4;
419 p += 4;
420 } else {
421 // ASCII characters (0x00-0x7F) and invalid sequences: treat as single byte
422 char_infos[char_count].byte_len = 1;
423 p++;
424 }
425 char_count++;
426 }
427
428 // Handle empty string case
429 if (char_count == 0)
430 return;
431
432 // Build 256-entry cache
433 for (int i = 0; i < 256; i++) {
434 int char_idx = char_count > 1 ? (i * (char_count - 1) + 127) / 255 : 0;
435 if (char_idx >= char_count)
436 char_idx = char_count - 1;
437
438 cache[i].byte_len = (uint8_t)char_infos[char_idx].byte_len;
439 memcpy(cache[i].utf8_bytes, char_infos[char_idx].start, char_infos[char_idx].byte_len);
440 if (cache[i].byte_len < 4) {
441 cache[i].utf8_bytes[cache[i].byte_len] = '\0';
442 }
443 }
444}
445
446// Build 64-entry UTF-8 cache for SIMD color lookup
447void build_utf8_ramp64_cache(const char *ascii_chars, utf8_char_t cache64[64], uint8_t char_index_ramp[ASCII_LUMINANCE_LEVELS]) {
448 if (!ascii_chars || !cache64 || !char_index_ramp)
449 return;
450
451 // Reuse the luminance cache building logic but for 64 entries
452 // (Same UTF-8 parsing as above, but map to 64 entries instead of 256)
453
454 // Parse characters (same as above)
455 typedef struct {
456 const char *start;
457 int byte_len;
458 } char_info_t;
459
460 char_info_t char_infos[256];
461 int char_count = 0;
462 const char *p = ascii_chars;
463
464 while (*p && char_count < 255) {
465 char_infos[char_count].start = p;
466
467 if ((*p & 0xE0) == 0xC0) {
468 char_infos[char_count].byte_len = 2;
469 p += 2;
470 } else if ((*p & 0xF0) == 0xE0) {
471 char_infos[char_count].byte_len = 3;
472 p += 3;
473 } else if ((*p & 0xF8) == 0xF0) {
474 char_infos[char_count].byte_len = 4;
475 p += 4;
476 } else {
477 // ASCII characters (0x00-0x7F) and invalid sequences: treat as single byte
478 char_infos[char_count].byte_len = 1;
479 p++;
480 }
481 char_count++;
482 }
483
484 // Handle empty string case
485 if (char_count == 0)
486 return;
487
488 // Build 64-entry cache and index ramp
489 for (int i = 0; i < 64; i++) {
490 int char_idx = char_count > 1 ? (i * (char_count - 1) + 31) / 63 : 0;
491 if (char_idx >= char_count)
492 char_idx = char_count - 1;
493
494 // Store character index for SIMD lookup
495 char_index_ramp[i] = (uint8_t)char_idx;
496
497 // Cache UTF-8 character
498 cache64[i].byte_len = (uint8_t)char_infos[char_idx].byte_len;
499 memcpy(cache64[i].utf8_bytes, char_infos[char_idx].start, char_infos[char_idx].byte_len);
500 if (cache64[i].byte_len < 4) {
501 cache64[i].utf8_bytes[cache64[i].byte_len] = '\0';
502 }
503 }
504}
505
506// char_index_ramp_cache functions removed - data already available in utf8_palette_cache_t.char_index_ramp[64]
507
508// No callback needed - uthash iteration handles cleanup directly
509
510// Central cleanup function for all SIMD caches
512 log_dev("SIMD_CACHE: Starting cleanup of all SIMD caches");
513
514 // Only destroy caches if they were ever initialized
515 if (lifecycle_is_initialized(&g_utf8_cache_lc)) {
516 // Destroy shared UTF-8 palette cache (write lock for cleanup)
517 rwlock_wrlock(&g_utf8_cache_rwlock);
518 if (g_utf8_cache_table) {
519 // Free all UTF-8 cache entries using HASH_ITER
520 utf8_palette_cache_t *cache, *tmp;
521 HASH_ITER(hh, g_utf8_cache_table, cache, tmp) {
522 HASH_DEL(g_utf8_cache_table, cache);
523 SAFE_FREE(cache);
524 }
525 g_utf8_cache_table = NULL;
526 // WASM: Skip debug log to avoid blocking
527 // log_debug("UTF8_CACHE: Destroyed shared UTF-8 palette cache");
528 }
529 // Clean up heap arrays
530 if (g_utf8_heap) {
531 SAFE_FREE(g_utf8_heap);
532 g_utf8_heap = NULL;
533 g_utf8_heap_size = 0;
534 }
535 rwlock_wrunlock(&g_utf8_cache_rwlock);
536 // Shutdown lifecycle and destroy rwlock together
537 lifecycle_shutdown(&g_utf8_cache_lc);
538 }
539
540 // Cleanup executes only on CPUs that can enter the corresponding object file.
541#if SIMD_SUPPORT_SSSE3
542 if (platform_cpu_features() & CPU_FEATURE_SSSE3)
543 ssse3_caches_destroy();
544#endif
545#if SIMD_SUPPORT_SSE2
546 if (platform_cpu_features() & CPU_FEATURE_SSE2)
547 sse2_caches_destroy();
548#endif
549#if SIMD_SUPPORT_SVE
550 if (platform_cpu_features() & CPU_FEATURE_SVE)
551 sve_caches_destroy();
552#endif
553
554 log_dev("SIMD_CACHE: All SIMD caches destroyed");
555}
556
563void init_dec3(void) {
564 if (g_dec3_cache.dec3_initialized)
565 return;
566 for (int v = 0; v < 256; ++v) {
567 int d2 = v / 100; // 0..2
568 int r = v - d2 * 100; // 0..99
569 int d1 = r / 10; // 0..9
570 int d0 = r - d1 * 10; // 0..9
571
572 if (d2) {
573 g_dec3_cache.dec3_table[v].len = 3;
574 g_dec3_cache.dec3_table[v].s[0] = (char)('0' + d2);
575 g_dec3_cache.dec3_table[v].s[1] = (char)('0' + d1);
576 g_dec3_cache.dec3_table[v].s[2] = (char)('0' + d0);
577 } else if (d1) {
578 g_dec3_cache.dec3_table[v].len = 2;
579 g_dec3_cache.dec3_table[v].s[0] = (char)('0' + d1);
580 g_dec3_cache.dec3_table[v].s[1] = (char)('0' + d0);
581 } else {
582 g_dec3_cache.dec3_table[v].len = 1;
583 g_dec3_cache.dec3_table[v].s[0] = (char)('0' + d0);
584 }
585 }
586 g_dec3_cache.dec3_initialized = true;
587}
588
595static void do_init_default_luminance_palette(void) {
596 const size_t len = DEFAULT_ASCII_PALETTE_LEN;
597 for (int i = 0; i < 256; i++) {
598 size_t palette_index = (i * (len - 1) + 127) / 255;
599 if (palette_index >= len) {
600 palette_index = len - 1;
601 }
603 }
604}
605
607 if (!lifecycle_init(&g_default_palette_lc, "default_palette")) {
608 return; // Already initialized
609 }
610 do_init_default_luminance_palette();
611}
612
622
623// Output buffer functions moved to lib/video/output_buffer.c
uint64_t atomic_fetch_add_u64(atomic_t *a, uint64_t delta)
Atomically add to a uint64_t and return the previous value.
void atomic_store_u64(atomic_t *a, uint64_t value)
Atomically store a uint64_t value.
uint64_t atomic_load_u64(const atomic_t *a)
Atomically load a uint64_t value.
AVX2-optimized ASCII rendering functions.
Platform initialization and static synchronization helpers.
️⃣ FNV-1a Hash Function Implementation
unsigned int uint32_t
#define SAFE_STRNCPY(dst, src, size)
#define SAFE_FREE(ptr)
#define SAFE_MALLOC(size, cast)
unsigned long long uint64_t
unsigned char uint8_t
#define log_dev(...)
Log a DEV message (most verbose, development only).
Definition log/log.h:551
#define log_error(...)
Log an ERROR message.
Definition log/log.h:604
#define NS_PER_SEC_INT
Definition time.h:173
void utf8_palette_destroy(utf8_palette_t *palette)
Destroy a UTF-8 palette and free resources.
Definition palette.c:478
size_t utf8_palette_get_char_count(const utf8_palette_t *palette)
Get number of characters in UTF-8 palette.
Definition palette.c:495
const size_t DEFAULT_ASCII_PALETTE_LEN
Length of default ASCII palette.
Definition palette.c:32
const char DEFAULT_ASCII_PALETTE[]
Default ASCII palette for legacy functions.
Definition palette.c:31
char bytes[UTF8_MAX_BYTES]
UTF-8 byte sequence (max 4 bytes per character).
Definition palette.h:233
const utf8_char_info_t * utf8_palette_get_char(const utf8_palette_t *palette, size_t index)
Get UTF-8 character information at index.
Definition palette.c:487
utf8_palette_t * utf8_palette_create(const char *palette_string)
Create a UTF-8 palette from string.
Definition palette.c:354
global_dec3_cache_t g_dec3_cache
Global decimal cache instance.
void build_utf8_luminance_cache(const char *ascii_chars, utf8_char_t cache[256])
Build UTF-8 luminance cache for all 256 levels.
void simd_caches_destroy_all(void)
Destroy all SIMD caches (palette and other caches).
#define RAMP64_SIZE
UTF-8 character palette cache.
utf8_palette_cache_t * get_utf8_palette_cache(const char *ascii_chars)
Get UTF-8 palette cache for a character set.
void init_dec3(void)
Initialize decimal-to-ASCII cache (0-255 → "0" to "255").
#define ASCII_LUMINANCE_LEVELS
Number of luminance levels supported (256).
Definition ascii.h:438
char g_default_luminance_palette[ASCII_LUMINANCE_LEVELS]
Default luminance palette (256 characters).
struct utf8_palette_cache utf8_palette_cache_t
void init_default_luminance_palette(void)
Initialize default luminance palette.
void ascii_simd_init(void)
Initialize SIMD subsystem.
⚙️ Common definitions, error codes, macros, and types shared throughout the application
SIMD-optimized ASCII conversion interface.
🔢 Mathematical Utility Functions
NEON-optimized ASCII rendering functions.
ASCII Palette Management for Video-to-ASCII Conversion.
static bool size_t index
Definition queries.h:10
SSE2-optimized ASCII rendering functions.
SSSE3-optimized ASCII rendering functions.
Global decimal cache for digit conversion.
UTF-8 character information structure.
Definition palette.h:231
When code needs to work with utf8 char info, it keeps byte_len, display_width here instead of scatter...
Definition palette.h:259
SVE-optimized ASCII rendering functions.
⏱️ High-precision timing utilities using sokol_time.h and uthash
void build_ramp64(uint8_t ramp64[RAMP64_SIZE], const char *ascii_chars)
double calculate_cache_eviction_score(uint64_t last_access_time, uint32_t access_count, uint64_t creation_time, uint64_t current_time)
void build_utf8_ramp64_cache(const char *ascii_chars, utf8_char_t cache64[64], uint8_t char_index_ramp[ASCII_LUMINANCE_LEVELS])