Subversion Repositories HelenOS-historic

Rev

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

Rev 195 Rev 228
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.h>
34
#include <arch/fmath.h>
35
 
35
 
-
 
36
#include <arch.h>
36
 
37
 
37
static char digits[] = "0123456789abcdef"; /**< Hexadecimal characters */
38
static char digits[] = "0123456789abcdef"; /**< Hexadecimal characters */
38
static spinlock_t printflock;              /**< printf spinlock */
39
static spinlock_t printflock;              /**< printf spinlock */
39
 
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
}
40
 
125
 
41
/** Print NULL terminated string
126
/** Print NULL terminated string
42
 *
127
 *
43
 * Print characters from str using putchar() until
128
 * Print characters from str using putchar() until
44
 * \x00 character is reached.
129
 * \x00 character is reached.
45
 *
130
 *
46
 * @param str Characters to print.
131
 * @param str Characters to print.
47
 *
132
 *
48
 */
133
 */
49
void print_str(const char *str)
134
void print_str(const char *str)
50
{
135
{
51
    int i = 0;
136
    int i = 0;
52
    char c;
137
    char c;
53
   
138
   
54
    while (c = str[i++])
139
    while (c = str[i++])
55
        putchar(c);
140
        putchar(c);
56
}
141
}
57
 
142
 
58
 
143
 
59
/** Print hexadecimal digits
144
/** Print hexadecimal digits
60
 *
145
 *
61
 * Print fixed count of hexadecimal digits from
146
 * Print fixed count of hexadecimal digits from
62
 * the number num. The digits are printed in
147
 * the number num. The digits are printed in
63
 * natural left-to-right order starting with
148
 * natural left-to-right order starting with
64
 * the width-th digit.
149
 * the width-th digit.
65
 *
150
 *
66
 * @param num   Number containing digits.
151
 * @param num   Number containing digits.
67
 * @param width Count of digits to print.
152
 * @param width Count of digits to print.
68
 *
153
 *
69
 */
154
 */
70
void print_fixed_hex(const __u64 num, const int width)
155
void print_fixed_hex(const __u64 num, const int width)
71
{
156
{
72
    int i;
157
    int i;
73
   
158
   
74
    for (i = width*8 - 4; i >= 0; i -= 4)
159
    for (i = width*8 - 4; i >= 0; i -= 4)
75
        putchar(digits[(num>>i) & 0xf]);
160
        putchar(digits[(num>>i) & 0xf]);
76
}
161
}
77
 
162
 
78
 
163
 
79
/** Print number in given base
164
/** Print number in given base
80
 *
165
 *
81
 * Print significant digits of a number in given
166
 * Print significant digits of a number in given
82
 * base.
167
 * base.
83
 *
168
 *
84
 * @param num  Number to print.
169
 * @param num  Number to print.
85
 * @param base Base to print the number in (should
170
 * @param base Base to print the number in (should
86
 *             be in range 2 .. 16).
171
 *             be in range 2 .. 16).
87
 *
172
 *
88
 */
173
 */
89
void print_number(const __native num, const unsigned int base)
174
void print_number(const __native num, const unsigned int base)
90
{
175
{
91
    int val = num;
176
    int val = num;
92
    char d[sizeof(__native)*8+1];       /* this is good enough even for base == 2 */
177
    char d[sizeof(__native)*8+1];       /* this is good enough even for base == 2 */
93
    int i = sizeof(__native)*8-1;
178
    int i = sizeof(__native)*8-1;
94
   
179
   
95
    do {
180
    do {
96
        d[i--] = digits[val % base];
181
        d[i--] = digits[val % base];
97
    } while (val /= base);
182
    } while (val /= base);
98
   
183
   
99
    d[sizeof(__native)*8] = 0; 
184
    d[sizeof(__native)*8] = 0; 
100
    print_str(&d[i + 1]);
185
    print_str(&d[i + 1]);
101
}
186
}
102
 
187
 
103
 
188
 
104
/** General formatted text print
189
/** General formatted text print
105
 *
190
 *
106
 * Print text formatted according the fmt parameter
191
 * Print text formatted according the fmt parameter
107
 * and variant arguments. Each formatting directive
192
 * and variant arguments. Each formatting directive
108
 * begins with % (percentage) character and one of the
193
 * begins with % (percentage) character and one of the
109
 * following character:
194
 * following character:
110
 *
195
 *
111
 * %    Prints the percentage character.
196
 * %    Prints the percentage character.
112
 * s    The next variant argument is treated as char*
197
 * s    The next variant argument is treated as char*
113
 *      and printed as a NULL terminated string.
198
 *      and printed as a NULL terminated string.
114
 * c    The next variant argument is treated as a single char.
199
 * c    The next variant argument is treated as a single char.
115
 * p    The next variant argument is treated as a maximum
200
 * p    The next variant argument is treated as a maximum
116
 *      bit-width integer with respect to architecture
201
 *      bit-width integer with respect to architecture
117
 *      and printed in full hexadecimal width.
202
 *      and printed in full hexadecimal width.
118
 * P    As with 'p', but '0x' is prefixed.
203
 * P    As with 'p', but '0x' is prefixed.
119
 * q    The next variant argument is treated as a 64b integer
204
 * q    The next variant argument is treated as a 64b integer
120
 *      and printed in full hexadecimal width.
205
 *      and printed in full hexadecimal width.
121
 * Q    As with 'q', but '0x' is prefixed.
206
 * Q    As with 'q', but '0x' is prefixed.
122
 * l    The next variant argument is treated as a 32b integer
207
 * l    The next variant argument is treated as a 32b integer
123
 *      and printed in full hexadecimal width.
208
 *      and printed in full hexadecimal width.
124
 * L    As with 'l', but '0x' is prefixed.
209
 * L    As with 'l', but '0x' is prefixed.
125
 * w    The next variant argument is treated as a 16b integer
210
 * w    The next variant argument is treated as a 16b integer
126
 *      and printed in full hexadecimal width.
211
 *      and printed in full hexadecimal width.
127
 * W    As with 'w', but '0x' is prefixed.
212
 * W    As with 'w', but '0x' is prefixed.
128
 * b    The next variant argument is treated as a 8b integer
213
 * b    The next variant argument is treated as a 8b integer
129
 *      and printed in full hexadecimal width.
214
 *      and printed in full hexadecimal width.
130
 * N    As with 'b', but '0x' is prefixed.
215
 * N    As with 'b', but '0x' is prefixed.
131
 * d    The next variant argument is treated as integer
216
 * d    The next variant argument is treated as integer
132
 *      and printed in standard decimal format (only significant
217
 *      and printed in standard decimal format (only significant
133
 *      digits).
218
 *      digits).
134
 * x    The next variant argument is treated as integer
219
 * x    The next variant argument is treated as integer
135
 *      and printed in standard hexadecimal format (only significant
220
 *      and printed in standard hexadecimal format (only significant
136
 *      digits).
221
 *      digits).
137
 * X    As with 'x', but '0x' is prefixed.
222
 * X    As with 'x', but '0x' is prefixed.
138
 *
223
 *
139
 * All other characters from fmt except the formatting directives
224
 * All other characters from fmt except the formatting directives
140
 * are printed in verbatim.
225
 * are printed in verbatim.
141
 *
226
 *
142
 * @param fmt Formatting NULL terminated string.
227
 * @param fmt Formatting NULL terminated string.
143
 *
228
 *
144
 */
229
 */
145
void printf(const char *fmt, ...)
230
void printf(const char *fmt, ...)
146
{
231
{
147
    int irqpri, i = 0;
232
    int irqpri, i = 0;
148
    va_list ap;
233
    va_list ap;
149
    char c;
234
    char c;
150
 
235
 
151
    va_start(ap, fmt);
236
    va_start(ap, fmt);
152
 
237
 
153
    irqpri = cpu_priority_high();
238
    irqpri = cpu_priority_high();
154
    spinlock_lock(&printflock);
239
    spinlock_lock(&printflock);
155
 
240
 
156
    while (c = fmt[i++]) {
241
    while (c = fmt[i++]) {
157
        switch (c) {
242
        switch (c) {
158
 
243
 
159
            /* control character */
244
            /* control character */
160
            case '%':
245
            case '%':
161
                switch (c = fmt[i++]) {
246
                switch (c = fmt[i++]) {
162
 
247
 
163
                /* percentile itself */
248
                /* percentile itself */
164
                case '%':
249
                case '%':
165
                    break;
250
                    break;
166
 
251
 
167
                /*
252
                /*
168
                 * String and character conversions.
253
                 * String and character conversions.
169
                 */
254
                 */
170
                case 's':
255
                case 's':
171
                    print_str(va_arg(ap, char_ptr));
256
                    print_str(va_arg(ap, char_ptr));
172
                    goto loop;
257
                    goto loop;
173
 
258
 
174
                case 'c':
259
                case 'c':
175
                    c = (char) va_arg(ap, int);
260
                    c = (char) va_arg(ap, int);
176
                    break;
261
                    break;
177
 
262
 
178
                /*
263
                /*
179
                         * Hexadecimal conversions with fixed width.
264
                         * Hexadecimal conversions with fixed width.
180
                         */
265
                         */
181
                case 'P':
266
                case 'P':
182
                    print_str("0x");
267
                    print_str("0x");
183
                case 'p':
268
                case 'p':
184
                        print_fixed_hex(va_arg(ap, __native), sizeof(__native));
269
                        print_fixed_hex(va_arg(ap, __native), sizeof(__native));
185
                    goto loop;
270
                    goto loop;
186
 
271
 
187
                case 'Q':
272
                case 'Q':
188
                    print_str("0x");
273
                    print_str("0x");
189
                case 'q':
274
                case 'q':
190
                        print_fixed_hex(va_arg(ap, __u64), INT64);
275
                        print_fixed_hex(va_arg(ap, __u64), INT64);
191
                    goto loop;
276
                    goto loop;
192
 
277
 
193
                case 'L':
278
                case 'L':
194
                    print_str("0x");
279
                    print_str("0x");
195
                case 'l':
280
                case 'l':
196
                        print_fixed_hex(va_arg(ap, __native), INT32);
281
                        print_fixed_hex(va_arg(ap, __native), INT32);
197
                    goto loop;
282
                    goto loop;
198
 
283
 
199
                case 'W':
284
                case 'W':
200
                    print_str("0x");
285
                    print_str("0x");
201
                case 'w':
286
                case 'w':
202
                        print_fixed_hex(va_arg(ap, __native), INT16);
287
                        print_fixed_hex(va_arg(ap, __native), INT16);
203
                    goto loop;
288
                    goto loop;
204
 
289
 
205
                case 'B':
290
                case 'B':
206
                    print_str("0x");
291
                    print_str("0x");
207
                case 'b':
292
                case 'b':
208
                        print_fixed_hex(va_arg(ap, __native), INT8);
293
                        print_fixed_hex(va_arg(ap, __native), INT8);
209
                    goto loop;
294
                    goto loop;
210
 
295
 
211
                /*
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
                /*
212
                         * Decimal and hexadecimal conversions.
305
                         * Decimal and hexadecimal conversions.
213
                         */
306
                         */
214
                case 'd':
307
                case 'd':
215
                        print_number(va_arg(ap, __native), 10);
308
                        print_number(va_arg(ap, __native), 10);
216
                    goto loop;
309
                    goto loop;
217
 
310
 
218
                case 'X':
311
                case 'X':
219
                            print_str("0x");
312
                            print_str("0x");
220
                case 'x':
313
                case 'x':
221
                        print_number(va_arg(ap, __native), 16);
314
                        print_number(va_arg(ap, __native), 16);
222
                    goto loop;
315
                    goto loop;
223
       
316
       
224
                /*
317
                /*
225
                 * Bad formatting.
318
                 * Bad formatting.
226
                 */
319
                 */
227
                default:
320
                default:
228
                    goto out;
321
                    goto out;
229
                }
322
                }
230
 
323
 
231
            default: putchar(c);
324
            default: putchar(c);
232
        }
325
        }
233
   
326
   
234
loop:
327
loop:
235
        ;
328
        ;
236
    }
329
    }
237
 
330
 
238
out:
331
out:
239
    spinlock_unlock(&printflock);
332
    spinlock_unlock(&printflock);
240
    cpu_priority_restore(irqpri);
333
    cpu_priority_restore(irqpri);
241
   
334
   
242
    va_end(ap);
335
    va_end(ap);
243
}
336
}
244
 
337