ascii-chat 0.11.33
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[256])
 Build UTF-8 ramp64 cache and character index ramp.
 
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 [256] = {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 41 of file video/ascii/common.c.

41 {
42 if (!ascii_chars) {
43 // Fallback to space character
44 for (int i = 0; i < RAMP64_SIZE; i++) {
45 ramp64[i] = ' ';
46 }
47 return;
48 }
49
50 // Use UTF-8 palette functions for proper character handling
51 utf8_palette_t *utf8_pal = utf8_palette_create(ascii_chars);
52 if (!utf8_pal) {
53 // Fallback to space character
54 for (int i = 0; i < RAMP64_SIZE; i++) {
55 ramp64[i] = ' ';
56 }
57 return;
58 }
59
60 size_t char_count = utf8_palette_get_char_count(utf8_pal);
61 if (char_count == 0) {
62 // No valid characters found, use space
63 for (int i = 0; i < RAMP64_SIZE; i++) {
64 ramp64[i] = ' ';
65 }
66 utf8_palette_destroy(utf8_pal);
67 return;
68 }
69
70 // Build the ramp64 lookup using UTF-8 character indices
71 for (int i = 0; i < RAMP64_SIZE; i++) {
72 // Map 0-63 to 0-(char_count-1) using proper character indexing
73 size_t char_idx = (i * (char_count - 1) + (RAMP64_SIZE - 1) / 2) / (RAMP64_SIZE - 1);
74 if (char_idx >= char_count) {
75 char_idx = char_count - 1;
76 }
77
78 // Get the first byte of the character at this character index
79 const utf8_char_info_t *char_info = utf8_palette_get_char(utf8_pal, char_idx);
80 if (char_info) {
81 ramp64[i] = (uint8_t)char_info->bytes[0];
82 } else {
83 ramp64[i] = ' '; // Fallback
84 }
85 }
86
87 utf8_palette_destroy(utf8_pal);
88}
unsigned char uint8_t
Definition common.h:56
void utf8_palette_destroy(utf8_palette_t *palette)
Destroy a UTF-8 palette and free resources.
Definition palette.c:485
char bytes[4]
UTF-8 byte sequence (max 4 bytes per character)
Definition palette.h:215
size_t utf8_palette_get_char_count(const utf8_palette_t *palette)
Get number of characters in UTF-8 palette.
Definition palette.c:502
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:494
utf8_palette_t * utf8_palette_create(const char *palette_string)
Create a UTF-8 palette from string.
Definition palette.c:363
#define RAMP64_SIZE
UTF-8 character palette cache.
UTF-8 character information structure.
Definition palette.h:213
UTF-8 palette structure.
Definition palette.h:236

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

◆ 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 91 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
Definition common.h:59
#define NS_PER_SEC_INT
Definition time.h:157
#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().