ascii-chat
Binary v0.15.83-53-gb711d8720
Library lib/v0.3.2-701-gb711d8720
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
Definition common.h:60
void utf8_palette_destroy(utf8_palette_t *palette)
Destroy a UTF-8 palette and free resources.
Definition palette.c:495
size_t utf8_palette_get_char_count(const utf8_palette_t *palette)
Get number of characters in UTF-8 palette.
Definition palette.c:512
char bytes[UTF8_MAX_BYTES]
UTF-8 byte sequence (max 4 bytes per character).
Definition palette.h:225
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:504
utf8_palette_t * utf8_palette_create(const char *palette_string)
Create a UTF-8 palette from string.
Definition palette.c:371
#define RAMP64_SIZE
UTF-8 character palette cache.
UTF-8 character information structure.
Definition palette.h:223
When code needs to work with utf8 char info, it keeps byte_len, display_width here instead of scatter...
Definition palette.h:249

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 449 of file video/ascii/common.c.

450 {
451 if (!ascii_chars || !cache64 || !char_index_ramp)
452 return;
453
454 // Reuse the luminance cache building logic but for 64 entries
455 // (Same UTF-8 parsing as above, but map to 64 entries instead of 256)
456
457 // Parse characters (same as above)
458 typedef struct {
459 const char *start;
460 int byte_len;
461 } char_info_t;
462
463 char_info_t char_infos[256];
464 int char_count = 0;
465 const char *p = ascii_chars;
466
467 while (*p && char_count < 255) {
468 char_infos[char_count].start = p;
469
470 if ((*p & 0xE0) == 0xC0) {
471 char_infos[char_count].byte_len = 2;
472 p += 2;
473 } else if ((*p & 0xF0) == 0xE0) {
474 char_infos[char_count].byte_len = 3;
475 p += 3;
476 } else if ((*p & 0xF8) == 0xF0) {
477 char_infos[char_count].byte_len = 4;
478 p += 4;
479 } else {
480 // ASCII characters (0x00-0x7F) and invalid sequences: treat as single byte
481 char_infos[char_count].byte_len = 1;
482 p++;
483 }
484 char_count++;
485 }
486
487 // Handle empty string case
488 if (char_count == 0)
489 return;
490
491 // Build 64-entry cache and index ramp
492 for (int i = 0; i < 64; i++) {
493 int char_idx = char_count > 1 ? (i * (char_count - 1) + 31) / 63 : 0;
494 if (char_idx >= char_count)
495 char_idx = char_count - 1;
496
497 // Store character index for SIMD lookup
498 char_index_ramp[i] = (uint8_t)char_idx;
499
500 // Cache UTF-8 character
501 cache64[i].byte_len = char_infos[char_idx].byte_len;
502 memcpy(cache64[i].utf8_bytes, char_infos[char_idx].start, char_infos[char_idx].byte_len);
503 if (cache64[i].byte_len < 4) {
504 cache64[i].utf8_bytes[cache64[i].byte_len] = '\0';
505 }
506 }
507}
uint8_t utf8_bytes[4]

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.

93 {
94 // Protect against unsigned underflow if times are inconsistent (clock adjustments, etc.)
95 uint64_t age_ns = (current_time >= last_access_time) ? (current_time - last_access_time) : 0;
96 uint64_t total_age_ns = (current_time >= creation_time) ? (current_time - creation_time) : 0;
97
98 uint64_t age_seconds = age_ns / NS_PER_SEC_INT;
99 uint64_t total_age_seconds = total_age_ns / NS_PER_SEC_INT;
100
101 // Frequency factor: high-use palettes get protection (logarithmic scaling)
102 double frequency_factor = 1.0 + log10(1.0 + access_count);
103
104 // Aging factor: frequency bonus decays over time (5-minute half-life)
105 double aging_factor = exp(-(double)age_seconds / CACHE_FREQUENCY_DECAY_TIME);
106
107 // Recent access bonus: strong protection for recently used (1-minute protection)
108 double recency_bonus = exp(-(double)age_seconds / CACHE_RECENCY_SCALE);
109
110 // Cache lifetime penalty: prevent immortal caches (1-hour max lifetime)
111 double lifetime_penalty = total_age_seconds > CACHE_MAX_LIFETIME ? 0.5 : 1.0;
112
113 // Final score: higher = keep longer
114 return (frequency_factor * aging_factor + recency_bonus) * lifetime_penalty;
115}
unsigned long long uint64_t
Definition common.h:63
#define NS_PER_SEC_INT
Definition time.h:169
#define CACHE_RECENCY_SCALE
#define CACHE_FREQUENCY_DECAY_TIME
#define CACHE_MAX_LIFETIME

References CACHE_FREQUENCY_DECAY_TIME, CACHE_MAX_LIFETIME, CACHE_RECENCY_SCALE, and NS_PER_SEC_INT.

Referenced by get_utf8_palette_cache().