From 9f46a3aeffd6bbaff03610129924f85e44e82280 Mon Sep 17 00:00:00 2001 From: John Hodge Date: Fri, 15 Feb 2013 10:43:05 +0800 Subject: [PATCH] Usermode/libc - ftoa() fixes, current test set passes --- Usermode/Libraries/Makefile.tpl | 2 +- Usermode/Libraries/libc.so_src/Makefile | 2 +- Usermode/Libraries/libc.so_src/TEST_printf.c | 5 ++-- Usermode/Libraries/libc.so_src/printf.c | 31 ++++++++++++++++---- 4 files changed, 30 insertions(+), 10 deletions(-) diff --git a/Usermode/Libraries/Makefile.tpl b/Usermode/Libraries/Makefile.tpl index 21f805cb..f0084e21 100644 --- a/Usermode/Libraries/Makefile.tpl +++ b/Usermode/Libraries/Makefile.tpl @@ -78,7 +78,7 @@ $(OUTPUTDIR)Libs/%: obj-native/%.no: %.c @mkdir -p $(dir $@) - $(NCC) -c $< -o $@ -MD -MP -MF $@.dep + $(NCC) -c $< -o $@ -Wall -std=gnu99 -MD -MP -MF $@.dep TEST_%: obj-native/TEST_%.no obj-native/%.no $(NCC) -o $@ $^ diff --git a/Usermode/Libraries/libc.so_src/Makefile b/Usermode/Libraries/libc.so_src/Makefile index 2e4e2b2c..0fd1e5be 100644 --- a/Usermode/Libraries/libc.so_src/Makefile +++ b/Usermode/Libraries/libc.so_src/Makefile @@ -25,4 +25,4 @@ EXP_%.txt: TEST_%.native rm TEST_printf.native %.native: %.c - $(NCC) $< -o $@ -Wall -Werror + $(NCC) $< -o $@ -Wall -std=gnu99 diff --git a/Usermode/Libraries/libc.so_src/TEST_printf.c b/Usermode/Libraries/libc.so_src/TEST_printf.c index ed78fd68..fe251d9d 100644 --- a/Usermode/Libraries/libc.so_src/TEST_printf.c +++ b/Usermode/Libraries/libc.so_src/TEST_printf.c @@ -23,9 +23,10 @@ int main(int argc, char *argv[]) TST("Integer", "%u", 1234); TST("Float", "%f", 3.1414926535); + TST("Float", "%f", 10.0); TST("Float", "%.10f", 3.1414926535); - TST("Float", "%e", 3.1414926535); - TST("Float", "%g", 3.1414926535); + TST("Float", "%e", 3.1415926535); + TST("Float", "%g", 3.1415926535); TST("Float", "%E", 1000000000.00); TST("Float", "%a", 16.0); TST("Float", "%a", 1024.0); diff --git a/Usermode/Libraries/libc.so_src/printf.c b/Usermode/Libraries/libc.so_src/printf.c index f4b13a4a..bd67e00f 100644 --- a/Usermode/Libraries/libc.so_src/printf.c +++ b/Usermode/Libraries/libc.so_src/printf.c @@ -240,6 +240,13 @@ EXPORT int _vcprintf_int(printf_putch_t putch_cb, void *putch_h, const char *for pos += _printf_ftoa(putch_cb, putch_h, arg_f, 10, FPN_SCI, precision, 0, bJustifyLeft, c == 'E'); break; + // Scientific Float + case 'g': + case 'G': + arg_f = bLong ? va_arg(args, long double) : va_arg(args, double); + pos += _printf_ftoa(putch_cb, putch_h, arg_f, 10, FPN_SHORTEST, + precision, 0, bJustifyLeft, c == 'G'); + break; // Hexadecimal Scientific case 'a': case 'A': @@ -632,12 +639,24 @@ size_t _printf_ftoa(printf_putch_t putch_cb, void *putch_h, long double num, siz { //TODO: //int first_set_sig = BSL(significand); - // TODO: if( num > pos(Base, 2+Precision+2+log_base(exponent) ) - Notation = FPN_SCI; + // bSign+log10(num)+1+precision vs. bSign+1+1+precision+1+1+log10(exponent) + int log10_num = exponent * 301 / 1000; // log_10(2) = 0.30102999566... + int log10_exp10 = 2; + int sci_len = (signisneg || bForceSign) + 2 + (Precision-1) + 2 + log10_exp10; + int std_whole_len = (log10_num > 0 ? log10_num : 1); + int std_len = (signisneg || bForceSign) + std_whole_len + 1 + (Precision-std_whole_len); + if( sci_len > std_len ) { + Precision -= std_whole_len; + Notation = FPN_STD; + } + else { + Precision -= 1; + Notation = FPN_SCI; + } } double precision_max = 1; - while(Precision--) + for(int i = Precision; i--; ) precision_max /= Base; // Determine scientific's exponent and starting denominator @@ -669,7 +688,7 @@ size_t _printf_ftoa(printf_putch_t putch_cb, void *putch_h, long double num, siz } else { - while( den < num ) + while( den <= num ) den *= Base; den /= Base; } @@ -694,10 +713,10 @@ size_t _printf_ftoa(printf_putch_t putch_cb, void *putch_h, long double num, siz } while( den >= 1 ); // Decimal point (if needed/forced) - if( den >= precision_max || bForcePoint ) + if( Precision > 0 || bForcePoint ) _putch('.'); // Decimal section - while( den >= precision_max ) + for(int i = Precision; i--; ) { num = _longdiv(num, den, &value); _putch(cDIGITS[value]); -- 2.20.1