Subversion Repositories HelenOS-historic

Rev

Rev 276 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
1 jermar 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>
40 jermar 32
#include <arch/arg.h>
115 jermar 33
#include <arch/asm.h>
228 cejka 34
#include <arch/fmath.h>
35
 
195 vana 36
#include <arch.h>
1 jermar 37
 
61 decky 38
static char digits[] = "0123456789abcdef"; /**< Hexadecimal characters */
39
static spinlock_t printflock;              /**< printf spinlock */
1 jermar 40
 
228 cejka 41
#define DEFAULT_DOUBLE_PRECISION 16
42
#define DEFAULT_DOUBLE_BUFFER_SIZE 128
61 decky 43
 
264 cejka 44
void print_double(double num, __u8 modifier, __u16 precision)
228 cejka 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; 
264 cejka 51
 
266 cejka 52
 
228 cejka 53
    if (fmath_is_nan(num)) {
54
        print_str("NaN");
55
        return;
56
    }
57
 
266 cejka 58
    if (num<0.0) {
228 cejka 59
        putchar('-');
266 cejka 60
        num=num*-1.0;
323 jermar 61
    }
228 cejka 62
 
266 cejka 63
 
64
    if (fmath_is_infinity(num)) {
65
        print_str("Inf");
66
        return;
323 jermar 67
    }
266 cejka 68
 
264 cejka 69
    if ((modifier=='E')||(modifier=='e')) {
70
        intval2=fmath_fint(fmath_get_decimal_exponent(num),&intval);
71
        exponent=intval;
276 cejka 72
        if ((intval2<0.0)) exponent--;
264 cejka 73
        num = num / ((fmath_dpow(10.0,exponent)));
74
 
323 jermar 75
        print_double(num,modifier+1,precision); /* modifier+1 = E => F or e => f */
264 cejka 76
        putchar(modifier);
77
        if (exponent<0) {
78
            putchar('-');
79
            exponent*=-1;
323 jermar 80
        }
264 cejka 81
        print_number(exponent,10);
82
        return;
323 jermar 83
    }
264 cejka 84
 
323 jermar 85
    /* TODO: rounding constant - when we got fraction >= 0.5, we must increment last printed number */
228 cejka 86
 
323 jermar 87
    /*
88
     * Here is a problem with cumulative error while printing big double values -> we will divide
89
     * the number with a power of 10, print new number with better method for small numbers and
90
     * then print decimal point at correct position.
91
     */
228 cejka 92
 
93
    fmath_fint(fmath_get_decimal_exponent(num),&intval);
94
 
95
    exponent=(intval>0.0?intval:0);
96
 
97
    precision+=exponent;
98
 
99
    if (exponent>0) num = num / ((fmath_dpow(10.0,exponent)));
100
 
101
    num=fmath_fint(num,&intval);
102
 
103
    if (precision>0) {
104
        counter=precision-1;
105
        if (exponent>0) counter++;
106
 
107
        if (counter>=DEFAULT_DOUBLE_BUFFER_SIZE) {
108
            counter=DEFAULT_DOUBLE_BUFFER_SIZE;
109
        }
110
        exponenttmp=exponent;
111
        while(counter>=0) {
112
            num *= 10.0;
113
            num = fmath_fint(num,&intval2);
114
            buf[counter--]=((int)intval2)+'0';
115
            exponenttmp--;
116
            if ((exponenttmp==0)&&(counter>=0)) buf[counter--]='.';
117
        }
118
        counter=precision;
119
        if ((exponent==0)&&(counter<DEFAULT_DOUBLE_BUFFER_SIZE)) buf[counter]='.';
120
        counter++; 
121
    } else {
122
        counter=0; 
123
    }
124
 
276 cejka 125
    in1=intval;
126
    if (in1==0.0) {
228 cejka 127
        if (counter<DEFAULT_DOUBLE_BUFFER_SIZE) buf[counter++]='0';
128
    } else {
129
        while(( in1>0 )&&(counter<DEFAULT_DOUBLE_BUFFER_SIZE)) {
130
 
131
            in2=in1;
132
            in1/=10;
133
            buf[counter]=in2-in1*10 + '0';
134
            counter++;
135
        }
136
    }
137
 
138
    counter = (counter>=DEFAULT_DOUBLE_BUFFER_SIZE?DEFAULT_DOUBLE_BUFFER_SIZE:counter);
139
    while (counter>0) {
140
        putchar(buf[--counter]);
323 jermar 141
    }
228 cejka 142
    return;
143
}
144
 
61 decky 145
/** Print NULL terminated string
146
 *
147
 * Print characters from str using putchar() until
148
 * \x00 character is reached.
149
 *
150
 * @param str Characters to print.
151
 *
152
 */
63 decky 153
void print_str(const char *str)
1 jermar 154
{
68 decky 155
    int i = 0;
1 jermar 156
    char c;
68 decky 157
 
1 jermar 158
    while (c = str[i++])
68 decky 159
        putchar(c);
1 jermar 160
}
161
 
162
 
61 decky 163
/** Print hexadecimal digits
164
 *
165
 * Print fixed count of hexadecimal digits from
166
 * the number num. The digits are printed in
167
 * natural left-to-right order starting with
168
 * the width-th digit.
169
 *
170
 * @param num   Number containing digits.
171
 * @param width Count of digits to print.
172
 *
1 jermar 173
 */
181 cejka 174
void print_fixed_hex(const __u64 num, const int width)
1 jermar 175
{
176
    int i;
177
 
178
    for (i = width*8 - 4; i >= 0; i -= 4)
179
        putchar(digits[(num>>i) & 0xf]);
180
}
181
 
61 decky 182
 
183
/** Print number in given base
184
 *
185
 * Print significant digits of a number in given
186
 * base.
187
 *
188
 * @param num  Number to print.
189
 * @param base Base to print the number in (should
190
 *             be in range 2 .. 16).
191
 *
1 jermar 192
 */
68 decky 193
void print_number(const __native num, const unsigned int base)
63 decky 194
{
195
    int val = num;
42 jermar 196
    char d[sizeof(__native)*8+1];       /* this is good enough even for base == 2 */
68 decky 197
    int i = sizeof(__native)*8-1;
198
 
1 jermar 199
    do {
63 decky 200
        d[i--] = digits[val % base];
201
    } while (val /= base);
1 jermar 202
 
42 jermar 203
    d[sizeof(__native)*8] = 0; 
1 jermar 204
    print_str(&d[i + 1]);
205
}
206
 
61 decky 207
 
208
/** General formatted text print
209
 *
210
 * Print text formatted according the fmt parameter
211
 * and variant arguments. Each formatting directive
212
 * begins with % (percentage) character and one of the
213
 * following character:
214
 *
215
 * %    Prints the percentage character.
66 jermar 216
 * s    The next variant argument is treated as char*
61 decky 217
 *      and printed as a NULL terminated string.
66 jermar 218
 * c    The next variant argument is treated as a single char.
77 jermar 219
 * p    The next variant argument is treated as a maximum
220
 *      bit-width integer with respect to architecture
221
 *      and printed in full hexadecimal width.
222
 * P    As with 'p', but '0x' is prefixed.
223
 * q    The next variant argument is treated as a 64b integer
224
 *      and printed in full hexadecimal width.
225
 * Q    As with 'q', but '0x' is prefixed.
66 jermar 226
 * l    The next variant argument is treated as a 32b integer
61 decky 227
 *      and printed in full hexadecimal width.
228
 * L    As with 'l', but '0x' is prefixed.
66 jermar 229
 * w    The next variant argument is treated as a 16b integer
61 decky 230
 *      and printed in full hexadecimal width.
231
 * W    As with 'w', but '0x' is prefixed.
66 jermar 232
 * b    The next variant argument is treated as a 8b integer
61 decky 233
 *      and printed in full hexadecimal width.
234
 * N    As with 'b', but '0x' is prefixed.
66 jermar 235
 * d    The next variant argument is treated as integer
61 decky 236
 *      and printed in standard decimal format (only significant
237
 *      digits).
66 jermar 238
 * x    The next variant argument is treated as integer
61 decky 239
 *      and printed in standard hexadecimal format (only significant
240
 *      digits).
241
 * X    As with 'x', but '0x' is prefixed.
323 jermar 242
 * .    The decimal number following period will be treated as precision
243
 *      for printing floating point numbers. One of 'e', 'E', 'f' or 'F'
244
 *      must follow.
245
 * e    The next variant argument is treated as double precision float
246
 *      and printed in exponent notation with only one digit before decimal point
247
 *      in specified precision. The exponent sign is printed as 'e'.
248
 * E    As with 'e', but the exponent sign is printed as 'E'.
249
 * f    The next variant argument is treated as double precision float
250
 *      and printed in decimal notation in specified precision.
251
 * F    As with 'f'.
61 decky 252
 *
253
 * All other characters from fmt except the formatting directives
254
 * are printed in verbatim.
255
 *
256
 * @param fmt Formatting NULL terminated string.
257
 *
1 jermar 258
 */
63 decky 259
void printf(const char *fmt, ...)
1 jermar 260
{
40 jermar 261
    int irqpri, i = 0;
262
    va_list ap;
263
    char c;
264 cejka 264
 
265
    __u16 precision;
266
 
40 jermar 267
    va_start(ap, fmt);
268
 
1 jermar 269
    irqpri = cpu_priority_high();
270
    spinlock_lock(&printflock);
40 jermar 271
 
1 jermar 272
    while (c = fmt[i++]) {
273
        switch (c) {
274
 
275
            /* control character */
276
            case '%':
323 jermar 277
            precision = DEFAULT_DOUBLE_PRECISION;
278
            if (fmt[i]=='.') {
279
                precision=0;
280
                c=fmt[++i];
281
                while((c>='0')&&(c<='9')) {
282
                    precision = precision*10 + c - '0';
264 cejka 283
                    c=fmt[++i];
284
                }
323 jermar 285
            }
264 cejka 286
 
323 jermar 287
            switch (c = fmt[i++]) {
1 jermar 288
 
323 jermar 289
                /* percentile itself */
290
                case '%':
291
                break;
1 jermar 292
 
323 jermar 293
                /*
294
                 * String and character conversions.
295
                 */
296
                case 's':
297
                print_str(va_arg(ap, char_ptr));
298
                goto loop;
1 jermar 299
 
323 jermar 300
                case 'c':
301
                c = (char) va_arg(ap, int);
302
                break;
1 jermar 303
 
323 jermar 304
                /*
305
                     * Hexadecimal conversions with fixed width.
306
                     */
307
                case 'P':
308
                print_str("0x");
309
                case 'p':
310
                    print_fixed_hex(va_arg(ap, __native), sizeof(__native));
311
                goto loop;
77 jermar 312
 
323 jermar 313
                case 'Q':
314
                print_str("0x");
315
                case 'q':
316
                    print_fixed_hex(va_arg(ap, __u64), INT64);
317
                goto loop;
77 jermar 318
 
323 jermar 319
                case 'L':
320
                print_str("0x");
321
                case 'l':
322
                    print_fixed_hex(va_arg(ap, __native), INT32);
323
                goto loop;
1 jermar 324
 
323 jermar 325
                case 'W':
326
                print_str("0x");
327
                case 'w':
328
                    print_fixed_hex(va_arg(ap, __native), INT16);
329
                goto loop;
1 jermar 330
 
323 jermar 331
                case 'B':
332
                print_str("0x");
333
                case 'b':
334
                    print_fixed_hex(va_arg(ap, __native), INT8);
335
                goto loop;
1 jermar 336
 
323 jermar 337
                /*
338
                     * Floating point conversions.
339
                     */
340
                case 'F':
341
                    print_double(va_arg(ap, double),'F',precision);
342
                goto loop;
264 cejka 343
 
323 jermar 344
                case 'f':
345
                    print_double(va_arg(ap, double),'f',precision);
346
                goto loop;
264 cejka 347
 
323 jermar 348
                case 'E':
349
                    print_double(va_arg(ap, double),'E',precision);
350
                goto loop;
351
                case 'e':
352
                    print_double(va_arg(ap, double),'e',precision);
353
                goto loop;
264 cejka 354
 
323 jermar 355
                /*
356
                     * Decimal and hexadecimal conversions.
357
                     */
358
                case 'd':
359
                    print_number(va_arg(ap, __native), 10);
360
                goto loop;
1 jermar 361
 
323 jermar 362
                case 'X':
363
                print_str("0x");
364
                case 'x':
365
                    print_number(va_arg(ap, __native), 16);
366
                goto loop;
1 jermar 367
 
323 jermar 368
                /*
369
                 * Bad formatting.
370
                 */
371
                default:
372
                goto out;
373
            }
1 jermar 374
 
375
            default: putchar(c);
376
        }
377
 
378
loop:
379
        ;
380
    }
381
 
382
out:
383
    spinlock_unlock(&printflock);
384
    cpu_priority_restore(irqpri);
40 jermar 385
 
386
    va_end(ap);
1 jermar 387
}