Subversion Repositories HelenOS-historic

Rev

Rev 195 | 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
 
228 cejka 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
 
61 decky 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
 */
63 decky 134
void print_str(const char *str)
1 jermar 135
{
68 decky 136
    int i = 0;
1 jermar 137
    char c;
68 decky 138
 
1 jermar 139
    while (c = str[i++])
68 decky 140
        putchar(c);
1 jermar 141
}
142
 
143
 
61 decky 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
 *
1 jermar 154
 */
181 cejka 155
void print_fixed_hex(const __u64 num, const int width)
1 jermar 156
{
157
    int i;
158
 
159
    for (i = width*8 - 4; i >= 0; i -= 4)
160
        putchar(digits[(num>>i) & 0xf]);
161
}
162
 
61 decky 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
 *
1 jermar 173
 */
68 decky 174
void print_number(const __native num, const unsigned int base)
63 decky 175
{
176
    int val = num;
42 jermar 177
    char d[sizeof(__native)*8+1];       /* this is good enough even for base == 2 */
68 decky 178
    int i = sizeof(__native)*8-1;
179
 
1 jermar 180
    do {
63 decky 181
        d[i--] = digits[val % base];
182
    } while (val /= base);
1 jermar 183
 
42 jermar 184
    d[sizeof(__native)*8] = 0; 
1 jermar 185
    print_str(&d[i + 1]);
186
}
187
 
61 decky 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.
66 jermar 197
 * s    The next variant argument is treated as char*
61 decky 198
 *      and printed as a NULL terminated string.
66 jermar 199
 * c    The next variant argument is treated as a single char.
77 jermar 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.
66 jermar 207
 * l    The next variant argument is treated as a 32b integer
61 decky 208
 *      and printed in full hexadecimal width.
209
 * L    As with 'l', but '0x' is prefixed.
66 jermar 210
 * w    The next variant argument is treated as a 16b integer
61 decky 211
 *      and printed in full hexadecimal width.
212
 * W    As with 'w', but '0x' is prefixed.
66 jermar 213
 * b    The next variant argument is treated as a 8b integer
61 decky 214
 *      and printed in full hexadecimal width.
215
 * N    As with 'b', but '0x' is prefixed.
66 jermar 216
 * d    The next variant argument is treated as integer
61 decky 217
 *      and printed in standard decimal format (only significant
218
 *      digits).
66 jermar 219
 * x    The next variant argument is treated as integer
61 decky 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
 *
1 jermar 229
 */
63 decky 230
void printf(const char *fmt, ...)
1 jermar 231
{
40 jermar 232
    int irqpri, i = 0;
233
    va_list ap;
234
    char c;
1 jermar 235
 
40 jermar 236
    va_start(ap, fmt);
237
 
1 jermar 238
    irqpri = cpu_priority_high();
239
    spinlock_lock(&printflock);
40 jermar 240
 
1 jermar 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':
40 jermar 256
                    print_str(va_arg(ap, char_ptr));
1 jermar 257
                    goto loop;
258
 
259
                case 'c':
42 jermar 260
                    c = (char) va_arg(ap, int);
1 jermar 261
                    break;
262
 
263
                /*
264
                         * Hexadecimal conversions with fixed width.
265
                         */
77 jermar 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':
181 cejka 275
                        print_fixed_hex(va_arg(ap, __u64), INT64);
77 jermar 276
                    goto loop;
277
 
1 jermar 278
                case 'L':
279
                    print_str("0x");
280
                case 'l':
40 jermar 281
                        print_fixed_hex(va_arg(ap, __native), INT32);
1 jermar 282
                    goto loop;
283
 
284
                case 'W':
285
                    print_str("0x");
286
                case 'w':
40 jermar 287
                        print_fixed_hex(va_arg(ap, __native), INT16);
1 jermar 288
                    goto loop;
289
 
290
                case 'B':
291
                    print_str("0x");
292
                case 'b':
40 jermar 293
                        print_fixed_hex(va_arg(ap, __native), INT8);
1 jermar 294
                    goto loop;
295
 
296
                /*
228 cejka 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
                /*
1 jermar 305
                         * Decimal and hexadecimal conversions.
306
                         */
307
                case 'd':
40 jermar 308
                        print_number(va_arg(ap, __native), 10);
1 jermar 309
                    goto loop;
310
 
311
                case 'X':
312
                            print_str("0x");
313
                case 'x':
40 jermar 314
                        print_number(va_arg(ap, __native), 16);
1 jermar 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);
40 jermar 334
 
335
    va_end(ap);
1 jermar 336
}