Subversion Repositories HelenOS-historic

Rev

Rev 195 | Go to most recent revision | Blame | Last modification | View Log | Download | RSS feed

  1. /*
  2.  * Copyright (C) 2001-2004 Jakub Jermar
  3.  * All rights reserved.
  4.  *
  5.  * Redistribution and use in source and binary forms, with or without
  6.  * modification, are permitted provided that the following conditions
  7.  * are met:
  8.  *
  9.  * - Redistributions of source code must retain the above copyright
  10.  *   notice, this list of conditions and the following disclaimer.
  11.  * - Redistributions in binary form must reproduce the above copyright
  12.  *   notice, this list of conditions and the following disclaimer in the
  13.  *   documentation and/or other materials provided with the distribution.
  14.  * - The name of the author may not be used to endorse or promote products
  15.  *   derived from this software without specific prior written permission.
  16.  *
  17.  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  18.  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  19.  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  20.  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
  21.  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  22.  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  23.  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  24.  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  25.  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  26.  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  27.  */
  28.  
  29. #include <putchar.h>
  30. #include <print.h>
  31. #include <synch/spinlock.h>
  32. #include <arch/arg.h>
  33. #include <arch/asm.h>
  34. #include <arch/fmath.h>
  35.  
  36. #include <arch.h>
  37.  
  38. static char digits[] = "0123456789abcdef"; /**< Hexadecimal characters */
  39. static spinlock_t printflock;              /**< printf spinlock */
  40.  
  41. #define DEFAULT_DOUBLE_PRECISION 16
  42. #define DEFAULT_DOUBLE_BUFFER_SIZE 128
  43.  
  44. void 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. }
  125.  
  126. /** Print NULL terminated string
  127.  *
  128.  * Print characters from str using putchar() until
  129.  * \x00 character is reached.
  130.  *
  131.  * @param str Characters to print.
  132.  *
  133.  */
  134. void print_str(const char *str)
  135. {
  136.     int i = 0;
  137.     char c;
  138.    
  139.     while (c = str[i++])
  140.         putchar(c);
  141. }
  142.  
  143.  
  144. /** Print hexadecimal digits
  145.  *
  146.  * Print fixed count of hexadecimal digits from
  147.  * the number num. The digits are printed in
  148.  * natural left-to-right order starting with
  149.  * the width-th digit.
  150.  *
  151.  * @param num   Number containing digits.
  152.  * @param width Count of digits to print.
  153.  *
  154.  */
  155. void print_fixed_hex(const __u64 num, const int width)
  156. {
  157.     int i;
  158.    
  159.     for (i = width*8 - 4; i >= 0; i -= 4)
  160.         putchar(digits[(num>>i) & 0xf]);
  161. }
  162.  
  163.  
  164. /** Print number in given base
  165.  *
  166.  * Print significant digits of a number in given
  167.  * base.
  168.  *
  169.  * @param num  Number to print.
  170.  * @param base Base to print the number in (should
  171.  *             be in range 2 .. 16).
  172.  *
  173.  */
  174. void print_number(const __native num, const unsigned int base)
  175. {
  176.     int val = num;
  177.     char d[sizeof(__native)*8+1];       /* this is good enough even for base == 2 */
  178.     int i = sizeof(__native)*8-1;
  179.    
  180.     do {
  181.         d[i--] = digits[val % base];
  182.     } while (val /= base);
  183.    
  184.     d[sizeof(__native)*8] = 0; 
  185.     print_str(&d[i + 1]);
  186. }
  187.  
  188.  
  189. /** General formatted text print
  190.  *
  191.  * Print text formatted according the fmt parameter
  192.  * and variant arguments. Each formatting directive
  193.  * begins with % (percentage) character and one of the
  194.  * following character:
  195.  *
  196.  * %    Prints the percentage character.
  197.  * s    The next variant argument is treated as char*
  198.  *      and printed as a NULL terminated string.
  199.  * c    The next variant argument is treated as a single char.
  200.  * p    The next variant argument is treated as a maximum
  201.  *      bit-width integer with respect to architecture
  202.  *      and printed in full hexadecimal width.
  203.  * P    As with 'p', but '0x' is prefixed.
  204.  * q    The next variant argument is treated as a 64b integer
  205.  *      and printed in full hexadecimal width.
  206.  * Q    As with 'q', but '0x' is prefixed.
  207.  * l    The next variant argument is treated as a 32b integer
  208.  *      and printed in full hexadecimal width.
  209.  * L    As with 'l', but '0x' is prefixed.
  210.  * w    The next variant argument is treated as a 16b integer
  211.  *      and printed in full hexadecimal width.
  212.  * W    As with 'w', but '0x' is prefixed.
  213.  * b    The next variant argument is treated as a 8b integer
  214.  *      and printed in full hexadecimal width.
  215.  * N    As with 'b', but '0x' is prefixed.
  216.  * d    The next variant argument is treated as integer
  217.  *      and printed in standard decimal format (only significant
  218.  *      digits).
  219.  * x    The next variant argument is treated as integer
  220.  *      and printed in standard hexadecimal format (only significant
  221.  *      digits).
  222.  * X    As with 'x', but '0x' is prefixed.
  223.  *
  224.  * All other characters from fmt except the formatting directives
  225.  * are printed in verbatim.
  226.  *
  227.  * @param fmt Formatting NULL terminated string.
  228.  *
  229.  */
  230. void printf(const char *fmt, ...)
  231. {
  232.     int irqpri, i = 0;
  233.     va_list ap;
  234.     char c;
  235.  
  236.     va_start(ap, fmt);
  237.  
  238.     irqpri = cpu_priority_high();
  239.     spinlock_lock(&printflock);
  240.  
  241.     while (c = fmt[i++]) {
  242.         switch (c) {
  243.  
  244.             /* control character */
  245.             case '%':
  246.                 switch (c = fmt[i++]) {
  247.  
  248.                 /* percentile itself */
  249.                 case '%':
  250.                     break;
  251.  
  252.                 /*
  253.                  * String and character conversions.
  254.                  */
  255.                 case 's':
  256.                     print_str(va_arg(ap, char_ptr));
  257.                     goto loop;
  258.  
  259.                 case 'c':
  260.                     c = (char) va_arg(ap, int);
  261.                     break;
  262.  
  263.                 /*
  264.                          * Hexadecimal conversions with fixed width.
  265.                          */
  266.                 case 'P':
  267.                     print_str("0x");
  268.                 case 'p':
  269.                         print_fixed_hex(va_arg(ap, __native), sizeof(__native));
  270.                     goto loop;
  271.  
  272.                 case 'Q':
  273.                     print_str("0x");
  274.                 case 'q':
  275.                         print_fixed_hex(va_arg(ap, __u64), INT64);
  276.                     goto loop;
  277.  
  278.                 case 'L':
  279.                     print_str("0x");
  280.                 case 'l':
  281.                         print_fixed_hex(va_arg(ap, __native), INT32);
  282.                     goto loop;
  283.  
  284.                 case 'W':
  285.                     print_str("0x");
  286.                 case 'w':
  287.                         print_fixed_hex(va_arg(ap, __native), INT16);
  288.                     goto loop;
  289.  
  290.                 case 'B':
  291.                     print_str("0x");
  292.                 case 'b':
  293.                         print_fixed_hex(va_arg(ap, __native), INT8);
  294.                     goto loop;
  295.  
  296.                 /*
  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.                 /*
  305.                          * Decimal and hexadecimal conversions.
  306.                          */
  307.                 case 'd':
  308.                         print_number(va_arg(ap, __native), 10);
  309.                     goto loop;
  310.  
  311.                 case 'X':
  312.                             print_str("0x");
  313.                 case 'x':
  314.                         print_number(va_arg(ap, __native), 16);
  315.                     goto loop;
  316.        
  317.                 /*
  318.                  * Bad formatting.
  319.                  */
  320.                 default:
  321.                     goto out;
  322.                 }
  323.  
  324.             default: putchar(c);
  325.         }
  326.    
  327. loop:
  328.         ;
  329.     }
  330.  
  331. out:
  332.     spinlock_unlock(&printflock);
  333.     cpu_priority_restore(irqpri);
  334.    
  335.     va_end(ap);
  336. }
  337.