Skip to content

Commit 46303e2

Browse files
committed
applied ez80 extensions to nanoprintf
1 parent 84e0596 commit 46303e2

2 files changed

Lines changed: 50 additions & 9 deletions

File tree

src/libc/printf/nanoprintf.c

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* @remarks don't set this above 40, or there is a chance that
1515
* frameset will exceed 127 (generating slower code).
1616
*/
17-
#define NANOPRINTF_CONVERSION_BUFFER_SIZE 36
17+
#define NANOPRINTF_CONVERSION_BUFFER_SIZE 24
1818

1919
static void npf_putc_std(int c, void *ctx) {
2020
(void)ctx;
@@ -26,19 +26,26 @@ static void npf_fputc_std(int c, void *ctx) {
2626
}
2727

2828
// This is a custom nanoprintf flag
29-
#define NANOPRINTF_PROMOTE_TO_LONG_DOUBLE 1
29+
#define NANOPRINTF_USE_LONG_DOUBLE_PRECISION 1
3030

31-
#if NANOPRINTF_PROMOTE_TO_LONG_DOUBLE == 1
31+
#if NANOPRINTF_USE_LONG_DOUBLE_PRECISION == 1
3232
#define NANOPRINTF_CONVERSION_FLOAT_TYPE unsigned long long
33+
#else
34+
#define NANOPRINTF_CONVERSION_FLOAT_TYPE unsigned long
3335
#endif
3436

3537
#define NANOPRINTF_USE_FIELD_WIDTH_FORMAT_SPECIFIERS 1
3638
#define NANOPRINTF_USE_PRECISION_FORMAT_SPECIFIERS 1
3739
#define NANOPRINTF_USE_FLOAT_FORMAT_SPECIFIERS 1
3840
#define NANOPRINTF_USE_LARGE_FORMAT_SPECIFIERS 1
41+
#define NANOPRINTF_USE_SMALL_FORMAT_SPECIFIERS 1
3942
#define NANOPRINTF_USE_BINARY_FORMAT_SPECIFIERS 1
4043
#define NANOPRINTF_USE_WRITEBACK_FORMAT_SPECIFIERS 1
4144
#define NANOPRINTF_USE_ALT_FORM_FLAG 1
45+
#define NANOPRINTF_USE_FLOAT_HEX_FORMAT_SPECIFIER 0
46+
47+
// Not applicable since sizeof(float) == sizeof(double) on the ez80
48+
#define NANOPRINTF_USE_FLOAT_SINGLE_PRECISION 0
4249

4350
#include "nanoprintf.h"
4451

@@ -50,13 +57,14 @@ int vsnprintf(char *__restrict buffer, size_t bufsz, char const *__restrict form
5057

5158
npf_putc const pc = buffer ? npf_bufputc : npf_bufputc_nop;
5259
int const n = npf_vpprintf(pc, &bufputc_ctx, format, vlist);
53-
pc('\0', &bufputc_ctx);
5460

61+
if (buffer && bufsz) {
5562
#ifdef NANOPRINTF_SNPRINTF_SAFE_EMPTY_STRING_ON_OVERFLOW
56-
if (bufsz && (n >= (int)bufsz)) { buffer[0] = '\0'; }
63+
buffer[(n < 0 || (unsigned)n >= bufsz) ? 0 : n] = '\0';
5764
#else
58-
if (bufsz && (n >= (int)bufsz)) { buffer[bufsz - 1] = '\0'; }
65+
buffer[n < 0 ? 0 : NPF_MIN((unsigned)n, bufsz - 1)] = '\0';
5966
#endif
67+
}
6068

6169
return n;
6270
}

src/libc/printf/nanoprintf.h

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ extern "C" {
5353
#endif
5454
#endif
5555

56+
#ifndef _EZ80
57+
5658
NPF_VISIBILITY int npf_snprintf_(char * NPF_RESTRICT buffer,
5759
size_t bufsz,
5860
const char * NPF_RESTRICT format, ...)
@@ -83,6 +85,8 @@ NPF_VISIBILITY int npf_vpprintf(npf_putc pc,
8385
char const * NPF_RESTRICT format,
8486
va_list vlist) NPF_PRINTF_ATTR(3, 0);
8587

88+
#endif /* _EZ80 */
89+
8690
#ifdef __cplusplus
8791
}
8892
#endif
@@ -487,7 +491,9 @@ static int npf_parse_format_spec(char const *format, npf_format_spec_t *out_spec
487491
#endif
488492
break;
489493
#if NANOPRINTF_USE_FLOAT_FORMAT_SPECIFIERS == 1
494+
#if !defined(_EZ80) || NANOPRINTF_USE_LONG_DOUBLE_PRECISION
490495
case 'L': out_spec->length_modifier = NPF_FMT_SPEC_LEN_MOD_LONG_DOUBLE; break;
496+
#endif /* _EZ80 */
491497
#endif
492498
#if NANOPRINTF_USE_LARGE_FORMAT_SPECIFIERS == 1
493499
case 'j': out_spec->length_modifier = NPF_FMT_SPEC_LEN_MOD_LARGE_INTMAX; break;
@@ -586,6 +592,10 @@ static NPF_NOINLINE int npf_utoa_rev(
586592
typedef float npf_real_t;
587593
#define NPF_REAL_MANT_DIG FLT_MANT_DIG
588594
#define NPF_REAL_MAX_EXP FLT_MAX_EXP
595+
#elif NANOPRINTF_USE_LONG_DOUBLE_PRECISION
596+
typedef long double npf_real_t;
597+
#define NPF_REAL_MANT_DIG LDBL_MANT_DIG
598+
#define NPF_REAL_MAX_EXP LDBL_MAX_EXP
589599
#else
590600
typedef double npf_real_t;
591601
#define NPF_REAL_MANT_DIG DBL_MANT_DIG
@@ -642,7 +652,11 @@ static NPF_FORCE_INLINE npf_real_bin_t npf_real_to_int_rep(npf_real_t f) {
642652
npf_real_bin_t bin = 0;
643653
char const *src = (char const *)&f;
644654
char *dst = (char *)&bin;
655+
#ifndef _EZ80
645656
for (uint_fast8_t i = 0; i < sizeof(f); ++i) { dst[i] = src[i]; }
657+
#else /* _EZ80 */
658+
__builtin_memcpy(dst, src, sizeof(f));
659+
#endif /* _EZ80 */
646660
return bin;
647661
}
648662

@@ -812,7 +826,11 @@ static NPF_FORCE_INLINE npf_double_bin_t npf_double_to_int_rep(double f) {
812826
npf_double_bin_t bin = 0;
813827
char const *src = (char const *)&f;
814828
char *dst = (char *)&bin;
829+
#ifndef _EZ80
815830
for (uint_fast8_t i = 0; i < sizeof(f); ++i) { dst[i] = src[i]; }
831+
#else /* _EZ80 */
832+
__builtin_memcpy(dst, src, sizeof(f));
833+
#endif /* _EZ80 */
816834
return bin;
817835
}
818836
#else
@@ -931,13 +949,22 @@ typedef struct npf_cnt_putc_ctx {
931949
int n;
932950
} npf_cnt_putc_ctx_t;
933951

952+
#ifdef NANOPRINTF_STATIC_GLOBALS
953+
static npf_cnt_putc_ctx_t pc_cnt;
954+
#endif
955+
934956
static void npf_putc_cnt(int c, void *ctx) {
935-
npf_cnt_putc_ctx_t *pc_cnt = (npf_cnt_putc_ctx_t *)ctx;
936-
++pc_cnt->n;
937-
pc_cnt->pc(c, pc_cnt->ctx); // sibling-call optimization
957+
npf_cnt_putc_ctx_t *pc_putc_cnt = (npf_cnt_putc_ctx_t *)ctx;
958+
++pc_putc_cnt->n;
959+
pc_putc_cnt->pc(c, pc_putc_cnt->ctx); // sibling-call optimization
938960
}
939961

962+
#ifdef NANOPRINTF_STATIC_GLOBALS
963+
static void NPF_PUTC_IMPL(char VAL) { npf_putc_cnt((int)(VAL), &pc_cnt); }
964+
#define NPF_PUTC(VAL) NPF_PUTC_IMPL(VAL)
965+
#else
940966
#define NPF_PUTC(VAL) do { npf_putc_cnt((int)(VAL), &pc_cnt); } while (0)
967+
#endif
941968

942969
#define NPF_EXTRACT(MOD, CAST_TO, EXTRACT_AS) \
943970
case NPF_FMT_SPEC_LEN_MOD_##MOD: val = (CAST_TO)va_arg(args, EXTRACT_AS); break
@@ -948,7 +975,9 @@ static void npf_putc_cnt(int c, void *ctx) {
948975
int npf_vpprintf(npf_putc pc, void *pc_ctx, char const *format, va_list args) {
949976
npf_format_spec_t fs;
950977
char const *cur = format;
978+
#ifndef NANOPRINTF_STATIC_GLOBALS
951979
npf_cnt_putc_ctx_t pc_cnt;
980+
#endif
952981
pc_cnt.pc = pc;
953982
pc_cnt.ctx = pc_ctx;
954983
pc_cnt.n = 0;
@@ -1318,6 +1347,8 @@ int npf_vpprintf(npf_putc pc, void *pc_ctx, char const *format, va_list args) {
13181347
#undef NPF_EXTRACT
13191348
#undef NPF_WRITEBACK
13201349

1350+
#ifndef _EZ80
1351+
13211352
int npf_vsnprintf(char * NPF_RESTRICT buffer,
13221353
size_t bufsz,
13231354
char const * NPF_RESTRICT format,
@@ -1363,6 +1394,8 @@ int npf_snprintf_(char * NPF_RESTRICT buffer,
13631394
return rv;
13641395
}
13651396

1397+
#endif /* _EZ80 */
1398+
13661399
#if NPF_HAVE_GCC_WARNING_PRAGMAS
13671400
#pragma GCC diagnostic pop
13681401
#endif

0 commit comments

Comments
 (0)