ascii-chat
Binary v0.16.114-28-gf5913657f
Library lib/v0.3.2-3302-gf5913657f
Video chat in your terminal
Loading...
Searching...
No Matches
common.c File Reference

Go to the source code of this file.

Functions

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)
utf8_palette_cache_t * get_utf8_palette_cache (const char *ascii_chars)
 Get UTF-8 palette cache for a character set.
void build_utf8_luminance_cache (const char *ascii_chars, utf8_char_t cache[256])
 Build UTF-8 luminance cache for all 256 levels.
void build_utf8_ramp64_cache (const char *ascii_chars, utf8_char_t cache64[64], uint8_t char_index_ramp[ASCII_LUMINANCE_LEVELS])
void simd_caches_destroy_all (void)
 Destroy all SIMD caches (palette and other caches).
void init_dec3 (void)
 Initialize decimal-to-ASCII cache (0-255 → "0" to "255").
void init_default_luminance_palette (void)
 Initialize default luminance palette.
void ascii_simd_init (void)
 Initialize SIMD subsystem.

Variables

global_dec3_cache_t g_dec3_cache = {0}
 Global decimal cache instance.
char g_default_luminance_palette [ASCII_LUMINANCE_LEVELS] = {0}
 Default luminance palette (256 characters).

Function Documentation

◆ build_ramp64()

void build_ramp64 ( uint8_t ramp64[RAMP64_SIZE],
const char * ascii_chars )

Definition at line 42 of file video/ascii/common.c.

42 {
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}
unsigned char uint8_t
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
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
#define RAMP64_SIZE
UTF-8 character palette cache.
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

References utf8_char_info_t::bytes, RAMP64_SIZE, utf8_palette_create(), utf8_palette_destroy(), utf8_palette_get_char(), and utf8_palette_get_char_count().

◆ build_utf8_ramp64_cache()

void build_utf8_ramp64_cache ( const char * ascii_chars,
utf8_char_t cache64[64],
uint8_t char_index_ramp[ASCII_LUMINANCE_LEVELS] )

Definition at line 447 of file video/ascii/common.c.

447 {
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}

References ASCII_LUMINANCE_LEVELS, utf8_char_t::byte_len, and utf8_char_t::utf8_bytes.

Referenced by get_utf8_palette_cache().

◆ calculate_cache_eviction_score()

double calculate_cache_eviction_score ( uint64_t last_access_time,
uint32_t access_count,
uint64_t creation_time,
uint64_t current_time )

Definition at line 92 of file video/ascii/common.c.

92 {
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}
unsigned long long uint64_t
#define NS_PER_SEC_INT
Definition time.h:173

References CACHE_FREQUENCY_DECAY_TIME, CACHE_MAX_LIFETIME, CACHE_RECENCY_SCALE, and NS_PER_SEC_INT.

Referenced by get_utf8_palette_cache().