Changeset e83a9f7 in mainline


Ignore:
Timestamp:
2005-09-01T15:02:43Z (19 years ago)
Author:
Josef Cejka <malyzelenyhnus@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
de25b6f
Parents:
623ba26c
Message:

Created fmath as basic tool for IEEE 754 floating point numbers.
Added option $F for printf for printing "double" type.

Files:
4 edited

Legend:

Unmodified
Added
Removed
  • arch/ia32/Makefile.inc

    r623ba26c re83a9f7  
    4848        arch/boot/boot.S \
    4949        arch/boot/memmap.S\
    50         arch/fpu_context.c
     50        arch/fpu_context.c\
     51        arch/fmath.c
  • include/print.h

    r623ba26c re83a9f7  
    3737#define INT64   8
    3838
     39static void print_double(double num,__u16 precision) ;
    3940static void print_str(const char *str);
    4041static void print_fixed_hex(const __u64 num, const int width);
  • src/debug/print.c

    r623ba26c re83a9f7  
    3232#include <arch/arg.h>
    3333#include <arch/asm.h>
     34#include <arch/fmath.h>
     35
    3436#include <arch.h>
    35 
    3637
    3738static char digits[] = "0123456789abcdef"; /**< Hexadecimal characters */
    3839static spinlock_t printflock;              /**< printf spinlock */
    3940
     41#define DEFAULT_DOUBLE_PRECISION 16
     42#define DEFAULT_DOUBLE_BUFFER_SIZE 128
     43
     44void print_double(double num, __u16 precision)
     45{
     46        double intval,intval2;
     47        int counter;
     48        int exponent,exponenttmp;
     49        unsigned char buf[DEFAULT_DOUBLE_BUFFER_SIZE];
     50        unsigned long in1,in2; 
     51        /*
     52        if (fmath_is_nan(num)) {
     53                print_str("NaN");
     54                return;
     55        }
     56        */
     57       
     58        if (fmath_is_negative(num)) {
     59                putchar('-');
     60                }
     61       
     62        num=fmath_abs(num);
     63
     64        /*
     65        if (fmath_is_infinity(num)) {
     66                print_str("Inf");
     67                }
     68        */
     69        //TODO: rounding constant - when we got fraction >= 0.5, we must increment last printed number
     70
     71        /* Here is problem with cumulative error while printing big double values -> we will divide
     72        the number with a power of 10, print new number with better method for small numbers and then print decimal point at correct position */
     73       
     74        fmath_fint(fmath_get_decimal_exponent(num),&intval);
     75       
     76        exponent=(intval>0.0?intval:0);
     77       
     78        precision+=exponent;
     79       
     80        if (exponent>0) num = num / ((fmath_dpow(10.0,exponent)));
     81               
     82        num=fmath_fint(num,&intval);
     83       
     84        if (precision>0) {
     85                counter=precision-1;
     86                if (exponent>0) counter++;
     87               
     88                if (counter>=DEFAULT_DOUBLE_BUFFER_SIZE) {
     89                        counter=DEFAULT_DOUBLE_BUFFER_SIZE;
     90                }
     91                exponenttmp=exponent;
     92                while(counter>=0) {
     93                        num *= 10.0;
     94                        num = fmath_fint(num,&intval2);
     95                        buf[counter--]=((int)intval2)+'0';
     96                        exponenttmp--;
     97                        if ((exponenttmp==0)&&(counter>=0)) buf[counter--]='.';
     98                }
     99                counter=precision;
     100                if ((exponent==0)&&(counter<DEFAULT_DOUBLE_BUFFER_SIZE)) buf[counter]='.';
     101                counter++;     
     102        } else {
     103                counter=0;     
     104        }
     105       
     106        if (intval==0.0) {
     107                if (counter<DEFAULT_DOUBLE_BUFFER_SIZE) buf[counter++]='0';
     108        } else {
     109                in1=intval;
     110                while(( in1>0 )&&(counter<DEFAULT_DOUBLE_BUFFER_SIZE)) {
     111                       
     112                        in2=in1;
     113                        in1/=10;
     114                        buf[counter]=in2-in1*10 + '0';
     115                        counter++;
     116                }
     117        }
     118       
     119        counter = (counter>=DEFAULT_DOUBLE_BUFFER_SIZE?DEFAULT_DOUBLE_BUFFER_SIZE:counter);
     120        while (counter>0) {
     121                putchar(buf[--counter]);
     122        };
     123        return;
     124}
    40125
    41126/** Print NULL terminated string
     
    210295
    211296                                /*
     297                                 * Floating point conversions.
     298                                 */
     299                               
     300                                case 'F':
     301                                case 'f':
     302                                        print_double(va_arg(ap, double),DEFAULT_DOUBLE_PRECISION);
     303                                        goto loop;
     304                                /*
    212305                                 * Decimal and hexadecimal conversions.
    213306                                 */
  • test/print/print1/test.c

    r623ba26c re83a9f7  
    3737        printf(" W  %W  %w \n",0x0123 ,0x0123);   
    3838        printf(" B  %B  %b \n",0x01 ,0x01);
     39        printf(" F  %F  %f (123456789.987654321)\n",123456789.987654321,123456789.987654321);
     40        printf(" F  %F  %f (-123456789.987654321e-10)\n",-123456789.987654321e-10,-123456789.987654321e-10);
    3941        return;
    4042}
Note: See TracChangeset for help on using the changeset viewer.