Subversion Repositories HelenOS-historic

Rev

Rev 623 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 623 Rev 674
1
/*
1
/*
2
 * Copyright (C) 2001-2004 Jakub Jermar
2
 * Copyright (C) 2001-2004 Jakub Jermar
3
 * All rights reserved.
3
 * All rights reserved.
4
 *
4
 *
5
 * Redistribution and use in source and binary forms, with or without
5
 * Redistribution and use in source and binary forms, with or without
6
 * modification, are permitted provided that the following conditions
6
 * modification, are permitted provided that the following conditions
7
 * are met:
7
 * are met:
8
 *
8
 *
9
 * - Redistributions of source code must retain the above copyright
9
 * - Redistributions of source code must retain the above copyright
10
 *   notice, this list of conditions and the following disclaimer.
10
 *   notice, this list of conditions and the following disclaimer.
11
 * - Redistributions in binary form must reproduce the above copyright
11
 * - Redistributions in binary form must reproduce the above copyright
12
 *   notice, this list of conditions and the following disclaimer in the
12
 *   notice, this list of conditions and the following disclaimer in the
13
 *   documentation and/or other materials provided with the distribution.
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
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.
15
 *   derived from this software without specific prior written permission.
16
 *
16
 *
17
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
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
18
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
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
23
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
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
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.
26
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
 */
27
 */
28
 
28
 
29
#include <putchar.h>
29
#include <putchar.h>
30
#include <print.h>
30
#include <print.h>
31
#include <synch/spinlock.h>
31
#include <synch/spinlock.h>
32
#include <arch/arg.h>
32
#include <arch/arg.h>
33
#include <arch/asm.h>
33
#include <arch/asm.h>
34
#include <arch/fmath.h>
-
 
35
 
34
 
36
#include <arch.h>
35
#include <arch.h>
37
 
36
 
38
static char digits[] = "0123456789abcdef";  /**< Hexadecimal characters */
37
static char digits[] = "0123456789abcdef";  /**< Hexadecimal characters */
39
SPINLOCK_INITIALIZE(printflock);        /**< printf spinlock */
38
SPINLOCK_INITIALIZE(printflock);        /**< printf spinlock */
40
 
39
 
41
#define DEFAULT_DOUBLE_PRECISION 16
-
 
42
#define DEFAULT_DOUBLE_BUFFER_SIZE 128
-
 
43
 
-
 
44
 
40
 
45
/** Print NULL terminated string
41
/** Print NULL terminated string
46
 *
42
 *
47
 * Print characters from str using putchar() until
43
 * Print characters from str using putchar() until
48
 * \\0 character is reached.
44
 * \\0 character is reached.
49
 *
45
 *
50
 * @param str Characters to print.
46
 * @param str Characters to print.
51
 *
47
 *
52
 */
48
 */
53
static void print_str(const char *str)
49
static void print_str(const char *str)
54
{
50
{
55
    int i = 0;
51
    int i = 0;
56
    char c;
52
    char c;
57
   
53
   
58
    while ((c = str[i++]))
54
    while ((c = str[i++]))
59
        putchar(c);
55
        putchar(c);
60
}
56
}
61
 
57
 
62
 
58
 
63
/** Print hexadecimal digits
59
/** Print hexadecimal digits
64
 *
60
 *
65
 * Print fixed count of hexadecimal digits from
61
 * Print fixed count of hexadecimal digits from
66
 * the number num. The digits are printed in
62
 * the number num. The digits are printed in
67
 * natural left-to-right order starting with
63
 * natural left-to-right order starting with
68
 * the width-th digit.
64
 * the width-th digit.
69
 *
65
 *
70
 * @param num   Number containing digits.
66
 * @param num   Number containing digits.
71
 * @param width Count of digits to print.
67
 * @param width Count of digits to print.
72
 *
68
 *
73
 */
69
 */
74
static void print_fixed_hex(const __u64 num, const int width)
70
static void print_fixed_hex(const __u64 num, const int width)
75
{
71
{
76
    int i;
72
    int i;
77
   
73
   
78
    for (i = width*8 - 4; i >= 0; i -= 4)
74
    for (i = width*8 - 4; i >= 0; i -= 4)
79
        putchar(digits[(num>>i) & 0xf]);
75
        putchar(digits[(num>>i) & 0xf]);
80
}
76
}
81
 
77
 
82
 
78
 
83
/** Print number in given base
79
/** Print number in given base
84
 *
80
 *
85
 * Print significant digits of a number in given
81
 * Print significant digits of a number in given
86
 * base.
82
 * base.
87
 *
83
 *
88
 * @param num  Number to print.
84
 * @param num  Number to print.
89
 * @param base Base to print the number in (should
85
 * @param base Base to print the number in (should
90
 *             be in range 2 .. 16).
86
 *             be in range 2 .. 16).
91
 *
87
 *
92
 */
88
 */
93
static void print_number(const __native num, const unsigned int base)
89
static void print_number(const __native num, const unsigned int base)
94
{
90
{
95
    int val = num;
91
    int val = num;
96
    char d[sizeof(__native)*8+1];       /* this is good enough even for base == 2 */
92
    char d[sizeof(__native)*8+1];       /* this is good enough even for base == 2 */
97
    int i = sizeof(__native)*8-1;
93
    int i = sizeof(__native)*8-1;
98
   
94
   
99
    do {
95
    do {
100
        d[i--] = digits[val % base];
96
        d[i--] = digits[val % base];
101
    } while (val /= base);
97
    } while (val /= base);
102
   
98
   
103
    d[sizeof(__native)*8] = 0; 
99
    d[sizeof(__native)*8] = 0; 
104
    print_str(&d[i + 1]);
100
    print_str(&d[i + 1]);
105
}
101
}
106
 
102
 
107
 
103
 
108
static void print_double(double num, __u8 modifier, __u16 precision)
-
 
109
{
-
 
110
    double intval,intval2;
-
 
111
    int counter;
-
 
112
    int exponent,exponenttmp;
-
 
113
    unsigned char buf[DEFAULT_DOUBLE_BUFFER_SIZE];
-
 
114
    unsigned long in1,in2; 
-
 
115
   
-
 
116
 
-
 
117
    if (fmath_is_nan(num)) {
-
 
118
        print_str("NaN");
-
 
119
        return;
-
 
120
    }
-
 
121
   
-
 
122
    if (num<0.0) {
-
 
123
        putchar('-');
-
 
124
        num=num*-1.0;
-
 
125
    }
-
 
126
 
-
 
127
 
-
 
128
    if (fmath_is_infinity(num)) {
-
 
129
        print_str("Inf");
-
 
130
        return;
-
 
131
    }
-
 
132
 
-
 
133
    if ((modifier=='E')||(modifier=='e')) {
-
 
134
        intval2=fmath_fint(fmath_get_decimal_exponent(num),&intval);
-
 
135
        exponent=intval;
-
 
136
        if ((intval2<0.0)) exponent--;
-
 
137
        num = num / ((fmath_dpow(10.0,exponent)));
-
 
138
       
-
 
139
        print_double(num,modifier+1,precision); /* modifier+1 = E => F or e => f */
-
 
140
        putchar(modifier);
-
 
141
        if (exponent<0) {
-
 
142
            putchar('-');
-
 
143
            exponent*=-1;
-
 
144
        }
-
 
145
        print_number(exponent,10);
-
 
146
        return;
-
 
147
    }
-
 
148
       
-
 
149
    /* TODO: rounding constant - when we got fraction >= 0.5, we must increment last printed number */
-
 
150
 
-
 
151
    /*
-
 
152
     * Here is a problem with cumulative error while printing big double values -> we will divide
-
 
153
     * the number with a power of 10, print new number with better method for small numbers and
-
 
154
     * then print decimal point at correct position.
-
 
155
     */
-
 
156
   
-
 
157
    fmath_fint(fmath_get_decimal_exponent(num),&intval);
-
 
158
   
-
 
159
    exponent=(intval>0.0?intval:0);
-
 
160
   
-
 
161
    precision+=exponent;
-
 
162
   
-
 
163
    if (exponent>0) num = num / ((fmath_dpow(10.0,exponent)));
-
 
164
       
-
 
165
    num=fmath_fint(num,&intval);
-
 
166
   
-
 
167
    if (precision>0) {
-
 
168
        counter=precision-1;
-
 
169
        if (exponent>0) counter++;
-
 
170
       
-
 
171
        if (counter>=DEFAULT_DOUBLE_BUFFER_SIZE) {
-
 
172
            counter=DEFAULT_DOUBLE_BUFFER_SIZE;
-
 
173
        }
-
 
174
        exponenttmp=exponent;
-
 
175
        while(counter>=0) {
-
 
176
            num *= 10.0;
-
 
177
            num = fmath_fint(num,&intval2);
-
 
178
            buf[counter--]=((int)intval2)+'0';
-
 
179
            exponenttmp--;
-
 
180
            if ((exponenttmp==0)&&(counter>=0)) buf[counter--]='.';
-
 
181
        }
-
 
182
        counter=precision;
-
 
183
        if ((exponent==0)&&(counter<DEFAULT_DOUBLE_BUFFER_SIZE)) buf[counter]='.';
-
 
184
        counter++; 
-
 
185
    } else {
-
 
186
        counter=0; 
-
 
187
    }
-
 
188
   
-
 
189
    in1=intval;
-
 
190
    if (in1==0.0) {
-
 
191
        if (counter<DEFAULT_DOUBLE_BUFFER_SIZE) buf[counter++]='0';
-
 
192
    } else {
-
 
193
        while(( in1>0 )&&(counter<DEFAULT_DOUBLE_BUFFER_SIZE)) {
-
 
194
           
-
 
195
            in2=in1;
-
 
196
            in1/=10;
-
 
197
            buf[counter]=in2-in1*10 + '0';
-
 
198
            counter++;
-
 
199
        }
-
 
200
    }
-
 
201
   
-
 
202
    counter = (counter>=DEFAULT_DOUBLE_BUFFER_SIZE?DEFAULT_DOUBLE_BUFFER_SIZE:counter);
-
 
203
    while (counter>0) {
-
 
204
        putchar(buf[--counter]);
-
 
205
    }
-
 
206
    return;
-
 
207
}
-
 
208
 
-
 
209
 
104
 
210
/** General formatted text print
105
/** General formatted text print
211
 *
106
 *
212
 * Print text formatted according the fmt parameter
107
 * Print text formatted according the fmt parameter
213
 * and variant arguments. Each formatting directive
108
 * and variant arguments. Each formatting directive
214
 * begins with \% (percentage) character and one of the
109
 * begins with \% (percentage) character and one of the
215
 * following character:
110
 * following character:
216
 *
111
 *
217
 * \%    Prints the percentage character.
112
 * \%    Prints the percentage character.
218
 *
113
 *
219
 * s    The next variant argument is treated as char*
114
 * s    The next variant argument is treated as char*
220
 *      and printed as a NULL terminated string.
115
 *      and printed as a NULL terminated string.
221
 *
116
 *
222
 * c    The next variant argument is treated as a single char.
117
 * c    The next variant argument is treated as a single char.
223
 *
118
 *
224
 * p    The next variant argument is treated as a maximum
119
 * p    The next variant argument is treated as a maximum
225
 *      bit-width integer with respect to architecture
120
 *      bit-width integer with respect to architecture
226
 *      and printed in full hexadecimal width.
121
 *      and printed in full hexadecimal width.
227
 *
122
 *
228
 * P    As with 'p', but '0x' is prefixed.
123
 * P    As with 'p', but '0x' is prefixed.
229
 *
124
 *
230
 * q    The next variant argument is treated as a 64b integer
125
 * q    The next variant argument is treated as a 64b integer
231
 *      and printed in full hexadecimal width.
126
 *      and printed in full hexadecimal width.
232
 *
127
 *
233
 * Q    As with 'q', but '0x' is prefixed.
128
 * Q    As with 'q', but '0x' is prefixed.
234
 *
129
 *
235
 * l    The next variant argument is treated as a 32b integer
130
 * l    The next variant argument is treated as a 32b integer
236
 *      and printed in full hexadecimal width.
131
 *      and printed in full hexadecimal width.
237
 *
132
 *
238
 * L    As with 'l', but '0x' is prefixed.
133
 * L    As with 'l', but '0x' is prefixed.
239
 *
134
 *
240
 * w    The next variant argument is treated as a 16b integer
135
 * w    The next variant argument is treated as a 16b integer
241
 *      and printed in full hexadecimal width.
136
 *      and printed in full hexadecimal width.
242
 *
137
 *
243
 * W    As with 'w', but '0x' is prefixed.
138
 * W    As with 'w', but '0x' is prefixed.
244
 *
139
 *
245
 * b    The next variant argument is treated as a 8b integer
140
 * b    The next variant argument is treated as a 8b integer
246
 *      and printed in full hexadecimal width.
141
 *      and printed in full hexadecimal width.
247
 *
142
 *
248
 * B    As with 'b', but '0x' is prefixed.
143
 * B    As with 'b', but '0x' is prefixed.
249
 *
144
 *
250
 * d    The next variant argument is treated as integer
145
 * d    The next variant argument is treated as integer
251
 *      and printed in standard decimal format (only significant
146
 *      and printed in standard decimal format (only significant
252
 *      digits).
147
 *      digits).
253
 *
148
 *
254
 * x    The next variant argument is treated as integer
149
 * x    The next variant argument is treated as integer
255
 *      and printed in standard hexadecimal format (only significant
150
 *      and printed in standard hexadecimal format (only significant
256
 *      digits).
151
 *      digits).
257
 *
152
 *
258
 * X    As with 'x', but '0x' is prefixed.
153
 * X    As with 'x', but '0x' is prefixed.
259
 *
154
 *
260
 * .    The decimal number following period will be treated as precision
155
 * .    The decimal number following period will be treated as precision
261
 *      for printing floating point numbers. One of 'e', 'E', 'f' or 'F'
156
 *      for printing floating point numbers. One of 'e', 'E', 'f' or 'F'
262
 *      must follow.
157
 *      must follow.
263
 *
158
 *
264
 * e    The next variant argument is treated as double precision float
-
 
265
 *      and printed in exponent notation with only one digit before decimal point
-
 
266
 *      in specified precision. The exponent sign is printed as 'e'.
-
 
267
 *
-
 
268
 * E    As with 'e', but the exponent sign is printed as 'E'.
-
 
269
 *
-
 
270
 * f    The next variant argument is treated as double precision float
-
 
271
 *      and printed in decimal notation in specified precision.
-
 
272
 *
-
 
273
 * F    As with 'f'.
-
 
274
 *
-
 
275
 * All other characters from fmt except the formatting directives
159
 * All other characters from fmt except the formatting directives
276
 * are printed in verbatim.
160
 * are printed in verbatim.
277
 *
161
 *
278
 * @param fmt Formatting NULL terminated string.
162
 * @param fmt Formatting NULL terminated string.
279
 */
163
 */
280
void printf(const char *fmt, ...)
164
void printf(const char *fmt, ...)
281
{
165
{
282
    int irqpri, i = 0;
166
    int irqpri, i = 0;
283
    va_list ap;
167
    va_list ap;
284
    char c;
168
    char c;
285
   
169
   
286
    __u16 precision;
-
 
287
   
-
 
288
    va_start(ap, fmt);
170
    va_start(ap, fmt);
289
 
171
 
290
    irqpri = interrupts_disable();
172
    irqpri = interrupts_disable();
291
    spinlock_lock(&printflock);
173
    spinlock_lock(&printflock);
292
 
174
 
293
    while ((c = fmt[i++])) {
175
    while ((c = fmt[i++])) {
294
        switch (c) {
176
        switch (c) {
295
 
177
 
296
            /* control character */
178
            /* control character */
297
            case '%':
179
            case '%':
298
            precision = DEFAULT_DOUBLE_PRECISION;
-
 
299
            if (fmt[i]=='.') {
-
 
300
                precision=0;
-
 
301
                c=fmt[++i];
-
 
302
                while((c>='0')&&(c<='9')) {
-
 
303
                    precision = precision*10 + c - '0';
-
 
304
                    c=fmt[++i];
-
 
305
                }
-
 
306
            }
-
 
307
           
180
           
308
            switch (c = fmt[i++]) {
181
            switch (c = fmt[i++]) {
309
 
182
 
310
                /* percentile itself */
183
                /* percentile itself */
311
                case '%':
184
                case '%':
312
                break;
185
                break;
313
 
186
 
314
                /*
187
                /*
315
                 * String and character conversions.
188
                 * String and character conversions.
316
                 */
189
                 */
317
                case 's':
190
                case 's':
318
                print_str(va_arg(ap, char_ptr));
191
                print_str(va_arg(ap, char_ptr));
319
                goto loop;
192
                goto loop;
320
 
193
 
321
                case 'c':
194
                case 'c':
322
                c = (char) va_arg(ap, int);
195
                c = (char) va_arg(ap, int);
323
                break;
196
                break;
324
 
197
 
325
                /*
198
                /*
326
                     * Hexadecimal conversions with fixed width.
199
                     * Hexadecimal conversions with fixed width.
327
                     */
200
                     */
328
                case 'P':
201
                case 'P':
329
                print_str("0x");
202
                print_str("0x");
330
                case 'p':
203
                case 'p':
331
                    print_fixed_hex(va_arg(ap, __native), sizeof(__native));
204
                    print_fixed_hex(va_arg(ap, __native), sizeof(__native));
332
                goto loop;
205
                goto loop;
333
 
206
 
334
                case 'Q':
207
                case 'Q':
335
                print_str("0x");
208
                print_str("0x");
336
                case 'q':
209
                case 'q':
337
                    print_fixed_hex(va_arg(ap, __u64), INT64);
210
                    print_fixed_hex(va_arg(ap, __u64), INT64);
338
                goto loop;
211
                goto loop;
339
 
212
 
340
                case 'L':
213
                case 'L':
341
                print_str("0x");
214
                print_str("0x");
342
                case 'l':
215
                case 'l':
343
                    print_fixed_hex(va_arg(ap, __native), INT32);
216
                    print_fixed_hex(va_arg(ap, __native), INT32);
344
                goto loop;
217
                goto loop;
345
 
218
 
346
                case 'W':
219
                case 'W':
347
                print_str("0x");
220
                print_str("0x");
348
                case 'w':
221
                case 'w':
349
                    print_fixed_hex(va_arg(ap, __native), INT16);
222
                    print_fixed_hex(va_arg(ap, __native), INT16);
350
                goto loop;
223
                goto loop;
351
 
224
 
352
                case 'B':
225
                case 'B':
353
                print_str("0x");
226
                print_str("0x");
354
                case 'b':
227
                case 'b':
355
                    print_fixed_hex(va_arg(ap, __native), INT8);
228
                    print_fixed_hex(va_arg(ap, __native), INT8);
356
                goto loop;
229
                goto loop;
357
 
230
   
358
                /*
-
 
359
                     * Floating point conversions.
-
 
360
                     */
-
 
361
                case 'F':
-
 
362
                    print_double(va_arg(ap, double),'F',precision);
-
 
363
                goto loop;
-
 
364
                   
-
 
365
                case 'f':
-
 
366
                    print_double(va_arg(ap, double),'f',precision);
-
 
367
                goto loop;
-
 
368
               
-
 
369
                case 'E':
-
 
370
                    print_double(va_arg(ap, double),'E',precision);
-
 
371
                goto loop;
-
 
372
                case 'e':
-
 
373
                    print_double(va_arg(ap, double),'e',precision);
-
 
374
                goto loop;
-
 
375
               
-
 
376
                /*
231
                /*
377
                     * Decimal and hexadecimal conversions.
232
                     * Decimal and hexadecimal conversions.
378
                     */
233
                     */
379
                case 'd':
234
                case 'd':
380
                    print_number(va_arg(ap, __native), 10);
235
                    print_number(va_arg(ap, __native), 10);
381
                goto loop;
236
                goto loop;
382
 
237
 
383
                case 'X':
238
                case 'X':
384
                print_str("0x");
239
                print_str("0x");
385
                case 'x':
240
                case 'x':
386
                    print_number(va_arg(ap, __native), 16);
241
                    print_number(va_arg(ap, __native), 16);
387
                goto loop;
242
                goto loop;
388
       
243
       
389
                /*
244
                /*
390
                 * Bad formatting.
245
                 * Bad formatting.
391
                 */
246
                 */
392
                default:
247
                default:
393
                goto out;
248
                goto out;
394
            }
249
            }
395
 
250
 
396
            default: putchar(c);
251
            default: putchar(c);
397
        }
252
        }
398
   
253
   
399
loop:
254
loop:
400
        ;
255
        ;
401
    }
256
    }
402
 
257
 
403
out:
258
out:
404
    spinlock_unlock(&printflock);
259
    spinlock_unlock(&printflock);
405
    interrupts_restore(irqpri);
260
    interrupts_restore(irqpri);
406
   
261
   
407
    va_end(ap);
262
    va_end(ap);
408
}
263
}
409
 
264