Skip to content

Commit 9695b9d

Browse files
committed
applied ez80 extensions to nanoprintf
1 parent 9a0700e commit 9695b9d

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
@@ -54,6 +54,8 @@ extern "C" {
5454
#endif
5555
#endif
5656

57+
#ifndef _EZ80
58+
5759
NPF_VISIBILITY int npf_snprintf_(char * NPF_RESTRICT buffer,
5860
size_t bufsz,
5961
const char * NPF_RESTRICT format, ...)
@@ -84,6 +86,8 @@ NPF_VISIBILITY int npf_vpprintf(npf_putc pc,
8486
char const * NPF_RESTRICT format,
8587
va_list vlist) NPF_PRINTF_ATTR(3, 0);
8688

89+
#endif /* _EZ80 */
90+
8791
#ifdef __cplusplus
8892
}
8993
#endif
@@ -488,7 +492,9 @@ static int npf_parse_format_spec(char const *format, npf_format_spec_t *out_spec
488492
#endif
489493
break;
490494
#if NANOPRINTF_USE_FLOAT_FORMAT_SPECIFIERS == 1
495+
#if !defined(_EZ80) || NANOPRINTF_USE_LONG_DOUBLE_PRECISION
491496
case 'L': out_spec->length_modifier = NPF_FMT_SPEC_LEN_MOD_LONG_DOUBLE; break;
497+
#endif /* _EZ80 */
492498
#endif
493499
#if NANOPRINTF_USE_LARGE_FORMAT_SPECIFIERS == 1
494500
case 'j': out_spec->length_modifier = NPF_FMT_SPEC_LEN_MOD_LARGE_INTMAX; break;
@@ -587,6 +593,10 @@ static NPF_NOINLINE int npf_utoa_rev(
587593
typedef float npf_real_t;
588594
#define NPF_REAL_MANT_DIG FLT_MANT_DIG
589595
#define NPF_REAL_MAX_EXP FLT_MAX_EXP
596+
#elif NANOPRINTF_USE_LONG_DOUBLE_PRECISION
597+
typedef long double npf_real_t;
598+
#define NPF_REAL_MANT_DIG LDBL_MANT_DIG
599+
#define NPF_REAL_MAX_EXP LDBL_MAX_EXP
590600
#else
591601
typedef double npf_real_t;
592602
#define NPF_REAL_MANT_DIG DBL_MANT_DIG
@@ -643,7 +653,11 @@ static NPF_FORCE_INLINE npf_real_bin_t npf_real_to_int_rep(npf_real_t f) {
643653
npf_real_bin_t bin = 0;
644654
char const *src = (char const *)&f;
645655
char *dst = (char *)&bin;
656+
#ifndef _EZ80
646657
for (uint_fast8_t i = 0; i < sizeof(f); ++i) { dst[i] = src[i]; }
658+
#else /* _EZ80 */
659+
__builtin_memcpy(dst, src, sizeof(f));
660+
#endif /* _EZ80 */
647661
return bin;
648662
}
649663

@@ -813,7 +827,11 @@ static NPF_FORCE_INLINE npf_double_bin_t npf_double_to_int_rep(double f) {
813827
npf_double_bin_t bin = 0;
814828
char const *src = (char const *)&f;
815829
char *dst = (char *)&bin;
830+
#ifndef _EZ80
816831
for (uint_fast8_t i = 0; i < sizeof(f); ++i) { dst[i] = src[i]; }
832+
#else /* _EZ80 */
833+
__builtin_memcpy(dst, src, sizeof(f));
834+
#endif /* _EZ80 */
817835
return bin;
818836
}
819837
#else
@@ -932,13 +950,22 @@ typedef struct npf_cnt_putc_ctx {
932950
int n;
933951
} npf_cnt_putc_ctx_t;
934952

953+
#ifdef NANOPRINTF_STATIC_GLOBALS
954+
static npf_cnt_putc_ctx_t pc_cnt;
955+
#endif
956+
935957
static void npf_putc_cnt(int c, void *ctx) {
936-
npf_cnt_putc_ctx_t *pc_cnt = (npf_cnt_putc_ctx_t *)ctx;
937-
++pc_cnt->n;
938-
pc_cnt->pc(c, pc_cnt->ctx); // sibling-call optimization
958+
npf_cnt_putc_ctx_t *pc_putc_cnt = (npf_cnt_putc_ctx_t *)ctx;
959+
++pc_putc_cnt->n;
960+
pc_putc_cnt->pc(c, pc_putc_cnt->ctx); // sibling-call optimization
939961
}
940962

963+
#ifdef NANOPRINTF_STATIC_GLOBALS
964+
static void NPF_PUTC_IMPL(char VAL) { npf_putc_cnt((int)(VAL), &pc_cnt); }
965+
#define NPF_PUTC(VAL) NPF_PUTC_IMPL(VAL)
966+
#else
941967
#define NPF_PUTC(VAL) do { npf_putc_cnt((int)(VAL), &pc_cnt); } while (0)
968+
#endif
942969

943970
#define NPF_EXTRACT(MOD, CAST_TO, EXTRACT_AS) \
944971
case NPF_FMT_SPEC_LEN_MOD_##MOD: val = (CAST_TO)va_arg(args, EXTRACT_AS); break
@@ -949,7 +976,9 @@ static void npf_putc_cnt(int c, void *ctx) {
949976
int npf_vpprintf(npf_putc pc, void *pc_ctx, char const *format, va_list args) {
950977
npf_format_spec_t fs;
951978
char const *cur = format;
979+
#ifndef NANOPRINTF_STATIC_GLOBALS
952980
npf_cnt_putc_ctx_t pc_cnt;
981+
#endif
953982
pc_cnt.pc = pc;
954983
pc_cnt.ctx = pc_ctx;
955984
pc_cnt.n = 0;
@@ -1319,6 +1348,8 @@ int npf_vpprintf(npf_putc pc, void *pc_ctx, char const *format, va_list args) {
13191348
#undef NPF_EXTRACT
13201349
#undef NPF_WRITEBACK
13211350

1351+
#ifndef _EZ80
1352+
13221353
int npf_vsnprintf(char * NPF_RESTRICT buffer,
13231354
size_t bufsz,
13241355
char const * NPF_RESTRICT format,
@@ -1364,6 +1395,8 @@ int npf_snprintf_(char * NPF_RESTRICT buffer,
13641395
return rv;
13651396
}
13661397

1398+
#endif /* _EZ80 */
1399+
13671400
#if NPF_HAVE_GCC_WARNING_PRAGMAS
13681401
#pragma GCC diagnostic pop
13691402
#endif

0 commit comments

Comments
 (0)