aboutsummaryrefslogtreecommitdiff
path: root/tests/tcg/mips/mips64-dsp/printf.c
blob: cf8676d3909b32bbfce3092eaf3ae2f4b3a7a79d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266

typedef unsigned long va_list;

#define ACC    4
#define __read(source)                    \
({ va_list __res;                    \
    __asm__ __volatile__(                \
        "move\t%0, " #source "\n\t"        \
        : "=r" (__res));            \
    __res;                        \
})

enum format_type {
    FORMAT_TYPE_NONE,
    FORMAT_TYPE_HEX,
    FORMAT_TYPE_ULONG,
    FORMAT_TYPE_FLOAT
};

struct printf_spec {
    char    type;
};

static int format_decode(char *fmt, struct printf_spec *spec)
{
    char *start = fmt;

    for (; *fmt ; ++fmt) {
        if (*fmt == '%') {
            break;
        }
    }

    switch (*++fmt) {
    case 'x':
        spec->type = FORMAT_TYPE_HEX;
        break;

    case 'd':
        spec->type = FORMAT_TYPE_ULONG;
        break;

    case 'f':
        spec->type = FORMAT_TYPE_FLOAT;
        break;

    default:
        spec->type = FORMAT_TYPE_NONE;
    }

    return ++fmt - start;
}

void *memcpy(void *dest, void *src, int n)
{
    int i;
    char *s = src;
    char *d = dest;

    for (i = 0; i < n; i++) {
        d[i] = s[i];
    }
    return dest;
}

char *number(char *buf, va_list num)
{
    int i;
    char *str = buf;
    static char digits[16] = "0123456789abcdef";
    str = str + sizeof(num) * 2;

    for (i = 0; i < sizeof(num) * 2; i++) {
        *--str = digits[num & 15];
        num >>= 4;
    }

    return buf + sizeof(num) * 2;
}

char *__number(char *buf, va_list num)
{
    int i;
    va_list mm = num;
    char *str = buf;

    if (!num) {
        *str++ = '0';
        return str;
    }

    for (i = 0; mm; mm = mm/10, i++) {
        /* Do nothing. */
    }

    str = str + i;

    while (num) {
        *--str = num % 10 + 48;
        num = num / 10;
    }

    return str + i;
}

va_list modf(va_list args, va_list *integer, va_list *num)
{
    int i;
    double dot_v = 0;
    va_list E, DOT, DOT_V;

    if (!args) {
        return 0;
    }

    for (i = 0, args = args << 1 >> 1; i < 52; i++) {
        if ((args >> i) & 0x1) {
            break;
        }
    }

    *integer = 0;

    if ((args >> 56 != 0x3f) || (args >> 52 == 0x3ff)) {
        E = (args >> 52) - 1023;
        DOT = 52 - E - i;
        DOT_V = args << (12 + E) >> (12 + E) >> i;
        *integer = ((args << 12 >> 12) >> (i + DOT)) | (1 << E);
    } else {
        E = ~((args >> 52) - 1023) + 1;
        DOT_V = args << 12 >> 12;

        dot_v += 1.0 / (1 << E);

        for (i = 1; i <= 16; i++) {
            if ((DOT_V >> (52 - i)) & 0x1) {
                dot_v += 1.0 / (1 << E + i);
            }
        }

        for (i = 1, E = 0; i <= ACC; i++) {
            dot_v *= 10;
            if (!(va_list)dot_v) {
                E++;
            }
    }

    *num = E;

    return dot_v;
    }

    if (args & 0xf) {
        for (i = 1; i <= 16; i++) {
            if ((DOT_V >> (DOT - i)) & 0x1) {
                dot_v += 1.0 / (1 << i);
            }
        }

        for (i = 1, E = 0; i <= ACC; i++) {
            dot_v *= 10;
            if (!(va_list)dot_v) {
                E++;
            }
        }

        *num = E;

        return dot_v;
    } else if (DOT) {
        for (i = 1; i <= DOT; i++) {
            if ((DOT_V >> (DOT - i)) & 0x1) {
                dot_v += 1.0 / (1 << i);
            }
        }

        for (i = 1; i <= ACC; i++) {
            dot_v = dot_v * 10;
        }

    return dot_v;
    }

    return 0;
}

int vsnprintf(char *buf, int size, char *fmt, va_list args)
{
    char *str, *mm;
    struct printf_spec spec = {0};

    str = mm = buf;

    while (*fmt) {
        char *old_fmt = fmt;
        int read = format_decode(fmt, &spec);

        fmt += read;

        switch (spec.type) {
        case FORMAT_TYPE_NONE: {
            memcpy(str, old_fmt, read);
            str += read;
            break;
        }
        case FORMAT_TYPE_HEX: {
            memcpy(str, old_fmt, read);
            str = number(str + read, args);
            for (; *mm ; ++mm) {
                if (*mm == '%') {
                    *mm = '0';
                break;
                }
            }
        break;
        }
        case FORMAT_TYPE_ULONG: {
            memcpy(str, old_fmt, read - 2);
            str = __number(str + read - 2, args);
            break;
        }
        case FORMAT_TYPE_FLOAT: {
            va_list integer, dot_v, num;
            dot_v = modf(args, &integer, &num);
            memcpy(str, old_fmt, read - 2);
            str += read - 2;
            if ((args >> 63 & 0x1)) {
                *str++ = '-';
            }
            str = __number(str, integer);
            if (dot_v) {
                *str++ = '.';
                while (num--) {
                    *str++ = '0';
                }
                str = __number(str, dot_v);
            }
            break;
        }
        }
    }
    *str = '\0';

    return str - buf;
}

static void serial_out(char *str)
{
    while (*str) {
        *(char *)0xffffffffb80003f8 = *str++;
    }
}

int vprintf(char *fmt, va_list args)
{
    int printed_len = 0;
    static char printf_buf[512];
    printed_len = vsnprintf(printf_buf, sizeof(printf_buf), fmt, args);
    serial_out(printf_buf);
    return printed_len;
}

int printf(char *fmt, ...)
{
    return vprintf(fmt, __read($5));
}