OpenCores
URL https://opencores.org/ocsvn/an-fpga-implementation-of-low-latency-noc-based-mpsoc/an-fpga-implementation-of-low-latency-noc-based-mpsoc/trunk

Subversion Repositories an-fpga-implementation-of-low-latency-noc-based-mpsoc

[/] [an-fpga-implementation-of-low-latency-noc-based-mpsoc/] [trunk/] [mpsoc/] [src_processor/] [src_lib/] [simple-printf/] [prinf_long.c] - Blame information for rev 48

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 48 alirezamon
/*
2
 * Copyright (c) 2016, Matt Redfearn
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 are met:
7
 *
8
 * 1. Redistributions of source code must retain the above copyright notice,
9
 * this list of conditions and the following disclaimer.
10
 *
11
 * 2. Redistributions in binary form must reproduce the above copyright notice,
12
 * this list of conditions and the following disclaimer in the documentation
13
 * and/or other materials provided with the distribution.
14
 *
15
 * 3. Neither the name of the copyright holder nor the names of its
16
 * contributors may be used to endorse or promote products derived from this
17
 * software without specific prior written permission.
18
 *
19
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
23
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29
 * POSSIBILITY OF SUCH DAMAGE.
30
 */
31
 
32
#include <stddef.h>
33
#include <stdarg.h>
34
 
35
#ifdef TEST
36
#include <stdio.h>
37
#endif /* TEST */
38
 
39
static void simple_outputchar(char **str, char c)
40
{
41
        if (str) {
42
                **str = c;
43
                ++(*str);
44
        } else {
45
                outbyte(c);
46
        }
47
}
48
 
49
enum flags {
50
        PAD_ZERO        = 1,
51
        PAD_RIGHT       = 2,
52
};
53
 
54
static int prints(char **out, const char *string, int width, int flags)
55
{
56
        int pc = 0, padchar = ' ';
57
 
58
        if (width > 0) {
59
                int len = 0;
60
                const char *ptr;
61
                for (ptr = string; *ptr; ++ptr) ++len;
62
                if (len >= width) width = 0;
63
                else width -= len;
64
                if (flags & PAD_ZERO)
65
                        padchar = '0';
66
        }
67
        if (!(flags & PAD_RIGHT)) {
68
                for ( ; width > 0; --width) {
69
                        simple_outputchar(out, padchar);
70
                        ++pc;
71
                }
72
        }
73
        for ( ; *string ; ++string) {
74
                simple_outputchar(out, *string);
75
                ++pc;
76
        }
77
        for ( ; width > 0; --width) {
78
                simple_outputchar(out, padchar);
79
                ++pc;
80
        }
81
 
82
        return pc;
83
}
84
 
85
#define PRINT_BUF_LEN 64
86
 
87
static int simple_outputi(char **out, long long i, int base, int sign, int width, int flags, int letbase)
88
{
89
        char print_buf[PRINT_BUF_LEN];
90
        char *s;
91
        int t, neg = 0, pc = 0;
92
        unsigned long long u = i;
93
 
94
        if (i == 0) {
95
                print_buf[0] = '0';
96
                print_buf[1] = '\0';
97
                return prints(out, print_buf, width, flags);
98
        }
99
 
100
        if (sign && base == 10 && i < 0) {
101
                neg = 1;
102
                u = -i;
103
        }
104
 
105
        s = print_buf + PRINT_BUF_LEN-1;
106
        *s = '\0';
107
 
108
        while (u) {
109
                t = u % base;
110
                if( t >= 10 )
111
                        t += letbase - '0' - 10;
112
                *--s = t + '0';
113
                u /= base;
114
        }
115
 
116
        if (neg) {
117
                if( width && (flags & PAD_ZERO) ) {
118
                        simple_outputchar (out, '-');
119
                        ++pc;
120
                        --width;
121
                }
122
                else {
123
                        *--s = '-';
124
                }
125
        }
126
 
127
        return pc + prints (out, s, width, flags);
128
}
129
 
130
 
131
static int simple_vsprintf(char **out, char *format, va_list ap)
132
{
133
        int width, flags;
134
        int pc = 0;
135
        char scr[2];
136
        union {
137
                char c;
138
                char *s;
139
                int i;
140
                unsigned int u;
141
                long li;
142
                unsigned long lu;
143
                long long lli;
144
                unsigned long long llu;
145
                short hi;
146
                unsigned short hu;
147
                signed char hhi;
148
                unsigned char hhu;
149
                void *p;
150
        } u;
151
 
152
        for (; *format != 0; ++format) {
153
                if (*format == '%') {
154
                        ++format;
155
                        width = flags = 0;
156
                        if (*format == '\0')
157
                                break;
158
                        if (*format == '%')
159
                                goto out;
160
                        if (*format == '-') {
161
                                ++format;
162
                                flags = PAD_RIGHT;
163
                        }
164
                        while (*format == '0') {
165
                                ++format;
166
                                flags |= PAD_ZERO;
167
                        }
168
                        if (*format == '*') {
169
                                width = va_arg(ap, int);
170
                                format++;
171
                        } else {
172
                                for ( ; *format >= '0' && *format <= '9'; ++format) {
173
                                        width *= 10;
174
                                        width += *format - '0';
175
                                }
176
                        }
177
                        switch (*format) {
178
                case('i'):
179
                                case('d'):
180
                                        u.i = va_arg(ap, int);
181
                                        pc += simple_outputi(out, u.i, 10, 1, width, flags, 'a');
182
                                        break;
183
 
184
                                case('u'):
185
                                        u.u = va_arg(ap, unsigned int);
186
                                        pc += simple_outputi(out, u.u, 10, 0, width, flags, 'a');
187
                                        break;
188
 
189
                                case('x'):
190
                                        u.u = va_arg(ap, unsigned int);
191
                                        pc += simple_outputi(out, u.u, 16, 0, width, flags, 'a');
192
                                        break;
193
 
194
                                case('X'):
195
                                        u.u = va_arg(ap, unsigned int);
196
                                        pc += simple_outputi(out, u.u, 16, 0, width, flags, 'A');
197
                                        break;
198
 
199
                                case('c'):
200
                                        u.c = va_arg(ap, int);
201
                                        scr[0] = u.c;
202
                                        scr[1] = '\0';
203
                                        pc += prints(out, scr, width, flags);
204
                                        break;
205
 
206
                                case('s'):
207
                                        u.s = va_arg(ap, char *);
208
                                        pc += prints(out, u.s ? u.s : "(null)", width, flags);
209
                                        break;
210
                                case('l'):
211
                                        ++format;
212
                                        switch (*format) {
213
                case('i'):
214
                                                case('d'):
215
                                                        u.li = va_arg(ap, long);
216
                                                        pc += simple_outputi(out, u.li, 10, 1, width, flags, 'a');
217
                                                        break;
218
 
219
                                                case('u'):
220
                                                        u.lu = va_arg(ap, unsigned long);
221
                                                        pc += simple_outputi(out, u.lu, 10, 0, width, flags, 'a');
222
                                                        break;
223
 
224
                                                case('x'):
225
                                                        u.lu = va_arg(ap, unsigned long);
226
                                                        pc += simple_outputi(out, u.lu, 16, 0, width, flags, 'a');
227
                                                        break;
228
 
229
                                                case('X'):
230
                                                        u.lu = va_arg(ap, unsigned long);
231
                                                        pc += simple_outputi(out, u.lu, 16, 0, width, flags, 'A');
232
                                                        break;
233
 
234
                                                case('l'):
235
                                                        ++format;
236
                                                        switch (*format) {
237
                        case('i'):
238
                                                                case('d'):
239
                                                                        u.lli = va_arg(ap, long long);
240
                                                                        pc += simple_outputi(out, u.lli, 10, 1, width, flags, 'a');
241
                                                                        break;
242
 
243
                                                                case('u'):
244
                                                                        u.llu = va_arg(ap, unsigned long long);
245
                                                                        pc += simple_outputi(out, u.llu, 10, 0, width, flags, 'a');
246
                                                                        break;
247
 
248
                                                                case('x'):
249
                                                                        u.llu = va_arg(ap, unsigned long long);
250
                                                                        pc += simple_outputi(out, u.llu, 16, 0, width, flags, 'a');
251
                                                                        break;
252
 
253
                                                                case('X'):
254
                                                                        u.llu = va_arg(ap, unsigned long long);
255
                                                                        pc += simple_outputi(out, u.llu, 16, 0, width, flags, 'A');
256
                                                                        break;
257
 
258
                                                                default:
259
                                                                        break;
260
                                                        }
261
                                                        break;
262
                                                default:
263
                                                        break;
264
                                        }
265
                                        break;
266
                                case('h'):
267
                                        ++format;
268
                                        switch (*format) {
269
                case('i'):
270
                                                case('d'):
271
                                                        u.hi = va_arg(ap, int);
272
                                                        pc += simple_outputi(out, u.hi, 10, 1, width, flags, 'a');
273
                                                        break;
274
 
275
                                                case('u'):
276
                                                        u.hu = va_arg(ap, unsigned int);
277
                                                        pc += simple_outputi(out, u.lli, 10, 0, width, flags, 'a');
278
                                                        break;
279
 
280
                                                case('x'):
281
                                                        u.hu = va_arg(ap, unsigned int);
282
                                                        pc += simple_outputi(out, u.lli, 16, 0, width, flags, 'a');
283
                                                        break;
284
 
285
                                                case('X'):
286
                                                        u.hu = va_arg(ap, unsigned int);
287
                                                        pc += simple_outputi(out, u.lli, 16, 0, width, flags, 'A');
288
                                                        break;
289
 
290
                                                case('h'):
291
                                                        ++format;
292
                                                        switch (*format) {
293
                        case('i'):
294
                                                                case('d'):
295
                                                                        u.hhi = va_arg(ap, int);
296
                                                                        pc += simple_outputi(out, u.hhi, 10, 1, width, flags, 'a');
297
                                                                        break;
298
 
299
                                                                case('u'):
300
                                                                        u.hhu = va_arg(ap, unsigned int);
301
                                                                        pc += simple_outputi(out, u.lli, 10, 0, width, flags, 'a');
302
                                                                        break;
303
 
304
                                                                case('x'):
305
                                                                        u.hhu = va_arg(ap, unsigned int);
306
                                                                        pc += simple_outputi(out, u.lli, 16, 0, width, flags, 'a');
307
                                                                        break;
308
 
309
                                                                case('X'):
310
                                                                        u.hhu = va_arg(ap, unsigned int);
311
                                                                        pc += simple_outputi(out, u.lli, 16, 0, width, flags, 'A');
312
                                                                        break;
313
 
314
                                                                default:
315
                                                                        break;
316
                                                        }
317
                                                        break;
318
                                                default:
319
                                                        break;
320
                                        }
321
                                        break;
322
                                default:
323
                                        break;
324
                        }
325
                }
326
                else {
327
out:
328
                        simple_outputchar (out, *format);
329
                        ++pc;
330
                }
331
        }
332
        if (out) **out = '\0';
333
        return pc;
334
}
335
 
336
int simple_printf(char *fmt, ...)
337
{
338
        va_list ap;
339
        int r;
340
 
341
        va_start(ap, fmt);
342
        r = simple_vsprintf(NULL, fmt, ap);
343
        va_end(ap);
344
 
345
        return r;
346
}
347
 
348
int simple_sprintf(char *buf, char *fmt, ...)
349
{
350
        va_list ap;
351
        int r;
352
 
353
        va_start(ap, fmt);
354
        r = simple_vsprintf(&buf, fmt, ap);
355
        va_end(ap);
356
 
357
        return r;
358
}
359
 
360
 
361
#ifdef TEST
362
 
363
#define printf simple_printf
364
#define sprintf simple_sprintf
365
 
366
int main(int argc, char *argv[])
367
{
368
        static char shortstr[] = "Test";
369
        char buf[256];
370
 
371
        printf("percent:                \"%%\"\n");
372
        printf("bad format:             \"%z\"\n");
373
        printf("\n");
374
        printf("decimal:                \"%d\"\n", 12345);
375
        printf("decimal negative:       \"%d\"\n", -2345);
376
        printf("\n");
377
        printf("unsigned:               \"%u\"\n", 12345);
378
        printf("unsigned negative:      \"%u\"\n", -2345);
379
        printf("\n");
380
        printf("hex:                    \"%x\"\n", 0x12345);
381
        printf("hex negative:           \"%x\"\n", -0x12345);
382
        printf("\n");
383
        printf("long decimal:           \"%ld\"\n", 123456L);
384
        printf("long decimal negative:  \"%ld\"\n", -23456L);
385
        printf("\n");
386
        printf("long unsigned:          \"%lu\"\n", 123456L);
387
        printf("long unsigned negative: \"%lu\"\n", -123456L);
388
        printf("\n");
389
        printf("long hex:               \"%lx\"\n", 0x12345L);
390
        printf("long hex negative:      \"%lx\"\n", -0x12345L);
391
        printf("\n");
392
        printf("long long decimal:           \"%lld\"\n", 123456LL);
393
        printf("long long decimal negative:  \"%lld\"\n", -23456LL);
394
        printf("\n");
395
        printf("long long unsigned:          \"%llu\"\n", 123456LL);
396
        printf("long long unsigned negative: \"%llu\"\n", -123456LL);
397
        printf("\n");
398
        printf("long long hex:               \"%llx\"\n", 0x12345LL);
399
        printf("long long hex negative:      \"%llx\"\n", -0x12345LL);
400
        printf("\n");
401
        printf("zero-padded LD:         \"%010ld\"\n", 123456);
402
        printf("zero-padded LDN:        \"%010ld\"\n", -123456);
403
        printf("left-adjusted ZLDN:     \"%-010ld\"\n", -123456);
404
        printf("space-padded LDN:       \"%10ld\"\n", -123456);
405
        printf("left-adjusted SLDN:     \"%-10ld\"\n", -123456);
406
        printf("\n");
407
        printf("variable pad width:     \"%0*d\"\n", 15, -2345);
408
        printf("\n");
409
        printf("char:                   \"%c%c%c%c\"\n", 'T', 'e', 's', 't');
410
        printf("\n");
411
        printf("zero-padded string:     \"%010s\"\n", shortstr);
412
        printf("left-adjusted Z string: \"%-010s\"\n", shortstr);
413
        printf("space-padded string:    \"%10s\"\n", shortstr);
414
        printf("left-adjusted S string: \"%-10s\"\n", shortstr);
415
        printf("null string:            \"%s\"\n", (char *)NULL);
416
 
417
        sprintf(buf, "decimal:\t\"%d\"\n", -2345);
418
        printf("sprintf: %s", buf);
419
}
420
#endif /* TEST */
421
 
422
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.