lib: sbi: Fix printf handling of long long
authordramforever <dramforever@live.com>
Wed, 27 Jul 2022 16:30:05 +0000 (00:30 +0800)
committerAnup Patel <anup@brainfault.org>
Sat, 30 Jul 2022 06:25:51 +0000 (11:55 +0530)
Read long long arguments directly using va_arg. Remove original hack for
RV32 that read a long long arg as two long args.

This un-breaks the case on RV64 where e.g. the long long is followed by
an odd number of ints:

    sbi_printf("%d %lld", (int) 1, (long long) 2LL);

Also remove the acnt variable, which is now unused.

Signed-off-by: dramforever <dramforever@live.com>
Reviewed-by: Andrew Jones <ajones@ventanamicro.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
lib/sbi/sbi_console.c

index 7b9be4a..cfef2f8 100644 (file)
@@ -183,7 +183,7 @@ static int printi(char **out, u32 *out_len, long long i, int b, int sg,
 
 static int print(char **out, u32 *out_len, const char *format, va_list args)
 {
-       int width, flags, acnt = 0;
+       int width, flags;
        int pc = 0;
        char scr[2];
        unsigned long long tmp;
@@ -216,7 +216,6 @@ static int print(char **out, u32 *out_len, const char *format, va_list args)
                        }
                        if (*format == 's') {
                                char *s = va_arg(args, char *);
-                               acnt += sizeof(char *);
                                pc += prints(out, out_len, s ? s : "(null)",
                                             width, flags);
                                continue;
@@ -224,61 +223,40 @@ static int print(char **out, u32 *out_len, const char *format, va_list args)
                        if ((*format == 'd') || (*format == 'i')) {
                                pc += printi(out, out_len, va_arg(args, int),
                                             10, 1, width, flags, '0');
-                               acnt += sizeof(int);
                                continue;
                        }
                        if (*format == 'x') {
                                pc += printi(out, out_len,
                                             va_arg(args, unsigned int), 16, 0,
                                             width, flags, 'a');
-                               acnt += sizeof(unsigned int);
                                continue;
                        }
                        if (*format == 'X') {
                                pc += printi(out, out_len,
                                             va_arg(args, unsigned int), 16, 0,
                                             width, flags, 'A');
-                               acnt += sizeof(unsigned int);
                                continue;
                        }
                        if (*format == 'u') {
                                pc += printi(out, out_len,
                                             va_arg(args, unsigned int), 10, 0,
                                             width, flags, 'a');
-                               acnt += sizeof(unsigned int);
                                continue;
                        }
                        if (*format == 'p') {
                                pc += printi(out, out_len,
                                             va_arg(args, unsigned long), 16, 0,
                                             width, flags, 'a');
-                               acnt += sizeof(unsigned long);
                                continue;
                        }
                        if (*format == 'P') {
                                pc += printi(out, out_len,
                                             va_arg(args, unsigned long), 16, 0,
                                             width, flags, 'A');
-                               acnt += sizeof(unsigned long);
                                continue;
                        }
                        if (*format == 'l' && *(format + 1) == 'l') {
-                               while (acnt &
-                                      (sizeof(unsigned long long) - 1)) {
-                                       va_arg(args, int);
-                                       acnt += sizeof(int);
-                               }
-                               if (sizeof(unsigned long long) ==
-                                   sizeof(unsigned long)) {
-                                       tmp = va_arg(args, unsigned long long);
-                                       acnt += sizeof(unsigned long long);
-                               } else {
-                                       ((unsigned long *)&tmp)[0] =
-                                               va_arg(args, unsigned long);
-                                       ((unsigned long *)&tmp)[1] =
-                                               va_arg(args, unsigned long);
-                                       acnt += 2 * sizeof(unsigned long);
-                               }
+                               tmp = va_arg(args, unsigned long long);
                                if (*(format + 2) == 'u') {
                                        format += 2;
                                        pc += printi(out, out_len, tmp, 10, 0,
@@ -310,19 +288,16 @@ static int print(char **out, u32 *out_len, const char *format, va_list args)
                                                out, out_len,
                                                va_arg(args, unsigned long), 16,
                                                0, width, flags, 'a');
-                                       acnt += sizeof(unsigned long);
                                } else if (*(format + 1) == 'X') {
                                        format += 1;
                                        pc += printi(
                                                out, out_len,
                                                va_arg(args, unsigned long), 16,
                                                0, width, flags, 'A');
-                                       acnt += sizeof(unsigned long);
                                } else {
                                        pc += printi(out, out_len,
                                                     va_arg(args, long), 10, 1,
                                                     width, flags, '0');
-                                       acnt += sizeof(long);
                                }
                        }
                        if (*format == 'c') {
@@ -330,7 +305,6 @@ static int print(char **out, u32 *out_len, const char *format, va_list args)
                                scr[0] = va_arg(args, int);
                                scr[1] = '\0';
                                pc += prints(out, out_len, scr, width, flags);
-                               acnt += sizeof(int);
                                continue;
                        }
                } else {