|
| 1 | +#include "ratest.h" |
| 2 | + |
| 3 | +#ifdef ENABLE_DPRINTF |
| 4 | + |
| 5 | +#include <stdint.h> |
| 6 | +#include <stdarg.h> |
| 7 | +#include <string.h> |
| 8 | + |
| 9 | +// a union to handle the |
| 10 | +union _d_bits { |
| 11 | + double d; |
| 12 | + struct { |
| 13 | + unsigned sign :1; |
| 14 | + unsigned exp :11; |
| 15 | + unsigned frac0 :20; |
| 16 | + unsigned frac1 :32; |
| 17 | + } b; |
| 18 | + unsigned u; |
| 19 | +}; |
| 20 | + |
| 21 | +#if 0 |
| 22 | +uint32_t strlen(const char *string) |
| 23 | +{ |
| 24 | + const char *s = string; |
| 25 | + |
| 26 | + while (*s++) { |
| 27 | + } |
| 28 | + return ~(string - s); |
| 29 | +} |
| 30 | +#endif |
| 31 | + |
| 32 | +#define CHAR_BIT 8 |
| 33 | +#define MININTSIZE (sizeof(unsigned long long)*CHAR_BIT/3+1) |
| 34 | +#define MINPOINTSIZE (sizeof(void *)*CHAR_BIT/4+1) |
| 35 | +#define REQUIREDBUFFER (MININTSIZE>MINPOINTSIZE?MININTSIZE:MINPOINTSIZE) |
| 36 | + |
| 37 | +/** |
| 38 | + * '#' |
| 39 | + * Used with o, exponent or X specifiers the value is preceeded with 0, 0x or 0X |
| 40 | + * respectively for values different than zero. |
| 41 | + * Used with a, A, e, E, f, F, g or G it forces the written output |
| 42 | + * to contain a decimal point even if no more digits follow. |
| 43 | + * By default, if no digits follow, no decimal point is written. |
| 44 | + */ |
| 45 | +#define ALTERNATEFLAG 1 /* '#' is set */ |
| 46 | + |
| 47 | +/** |
| 48 | + * '0' |
| 49 | + * Left-pads the number with zeroes (0) instead of spaces when padding is specified |
| 50 | + * (see width sub-specifier). |
| 51 | + */ |
| 52 | +#define ZEROPADFLAG 2 /* '0' is set */ |
| 53 | + |
| 54 | +/** |
| 55 | + * '-' |
| 56 | + * Left-justify within the given field width; |
| 57 | + * Right justification is the default (see width sub-specifier). |
| 58 | + */ |
| 59 | +#define LALIGNFLAG 4 /* '-' is set */ |
| 60 | + |
| 61 | +/** |
| 62 | + * ' ' |
| 63 | + * If no sign is going to be written, a blank space is inserted before the value. |
| 64 | + */ |
| 65 | +#define BLANKFLAG 8 /* ' ' is set */ |
| 66 | + |
| 67 | +/** |
| 68 | + * '+' |
| 69 | + * Forces to preceed the result with a plus or minus sign (+ or -) even for positive numbers. |
| 70 | + * By default, only negative numbers are preceded with a - sign. |
| 71 | + */ |
| 72 | +#define SIGNFLAG 16 /* '+' is set */ |
| 73 | + |
| 74 | +static const char flagc[] = { '#', '0', '-', ' ', '+' }; |
| 75 | + |
| 76 | +static unsigned __ulldivus(unsigned long long * llp, unsigned short n) { |
| 77 | + struct LL { |
| 78 | + unsigned long hi; |
| 79 | + union { |
| 80 | + unsigned long lo; |
| 81 | + struct { |
| 82 | + unsigned short exponent; |
| 83 | + unsigned short y; |
| 84 | + } s; |
| 85 | + } u; |
| 86 | + }* hl = (struct LL *) llp; |
| 87 | + |
| 88 | + unsigned r; |
| 89 | + unsigned long h = hl->hi; |
| 90 | + if (h) { |
| 91 | + unsigned l = hl->u.s.exponent; |
| 92 | + unsigned k = hl->u.s.y; |
| 93 | + unsigned c = h % n; |
| 94 | + h = h / n; |
| 95 | + l = l + (c << 16); |
| 96 | + c = l % n; |
| 97 | + l = l / n; |
| 98 | + k = k + (c << 16); |
| 99 | + r = k % n; |
| 100 | + k = k / n; |
| 101 | + hl->u.lo = (l << 16) + k; |
| 102 | + hl->hi = h + (l >> 16); |
| 103 | + return r; |
| 104 | + } |
| 105 | + |
| 106 | + r = hl->u.lo % n; |
| 107 | + hl->u.lo /= n; |
| 108 | + return r; |
| 109 | +} |
| 110 | + |
| 111 | +const unsigned char __ctype[]= |
| 112 | +{ 0x00, |
| 113 | + 0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x28,0x28,0x28,0x28,0x28,0x20,0x20, |
| 114 | + 0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20, |
| 115 | + 0x88,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10, |
| 116 | + 0x04,0x04,0x04,0x04,0x04,0x04,0x04,0x04,0x04,0x04,0x10,0x10,0x10,0x10,0x10,0x10, |
| 117 | + 0x10,0x41,0x41,0x41,0x41,0x41,0x41,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01, |
| 118 | + 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x10,0x10,0x10,0x10,0x10, |
| 119 | + 0x10,0x42,0x42,0x42,0x42,0x42,0x42,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02, |
| 120 | + 0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x10,0x10,0x10,0x10,0x20, |
| 121 | + 0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20, |
| 122 | + 0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20, |
| 123 | + 0x08,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10, |
| 124 | + 0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10, |
| 125 | + 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01, |
| 126 | + 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x10,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x02, |
| 127 | + 0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02, |
| 128 | + 0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x10,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02, |
| 129 | + 0x00,0x00,0x00 |
| 130 | +}; |
| 131 | + |
| 132 | +const unsigned char * const _ctype_=__ctype; |
| 133 | + |
| 134 | +int isdigit(int c) |
| 135 | +{ |
| 136 | + return _ctype_[1+c]&4; |
| 137 | +} |
| 138 | + |
| 139 | +static void vdprintf(const char *format, va_list args) |
| 140 | +{ |
| 141 | + while (*format) |
| 142 | + { |
| 143 | + if (*format == '%') { |
| 144 | + static const char lowertabel[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' }; |
| 145 | + static const char uppertabel[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' }; |
| 146 | + short width = 0; |
| 147 | + unsigned short preci = 0x7fff; |
| 148 | + short flags = 0; /* Specifications */ |
| 149 | + char type, subtype = 'i'; |
| 150 | + char buffer1[2]; /* Signs and that like */ |
| 151 | + char buffer[REQUIREDBUFFER]; /* The body */ |
| 152 | + char *buffer2 = buffer; /* So we can set this to any other strings */ |
| 153 | + uint32_t size1 = 0, size2 = 0; /* How many chars in buffer? */ |
| 154 | + const char *ptr = format + 1; /* pointer to format string */ |
| 155 | + unsigned short i, pad; /* Some temporary variables */ |
| 156 | + |
| 157 | + do /* read flags */ |
| 158 | + for (i = 0; i < sizeof(flagc); i++) |
| 159 | + if (flagc[i] == *ptr) { |
| 160 | + flags |= 1 << i; |
| 161 | + ptr++; |
| 162 | + break; |
| 163 | + } while (i < sizeof(flagc)); |
| 164 | + |
| 165 | + if (*ptr == '*') /* read width from arguments */ |
| 166 | + { |
| 167 | + signed int a; |
| 168 | + ptr++; |
| 169 | + a = va_arg(args, signed int); |
| 170 | + if (a < 0) { |
| 171 | + flags |= LALIGNFLAG; |
| 172 | + width = -a; |
| 173 | + } else |
| 174 | + width = a; |
| 175 | + } else { |
| 176 | + while (isdigit(*ptr)) |
| 177 | + width = width * 10 + (*ptr++ - '0'); |
| 178 | + } |
| 179 | + |
| 180 | + if (*ptr == 'h' || *ptr == 'l' || *ptr == 'L' || *ptr == 'j' |
| 181 | + || *ptr == 'z' || *ptr == 't') { |
| 182 | + subtype = *ptr++; |
| 183 | + if (*ptr == 'h' || *ptr == 'l') |
| 184 | + ++ptr, ++subtype; |
| 185 | + } else |
| 186 | + subtype = 0; |
| 187 | + |
| 188 | + type = *ptr++; |
| 189 | + |
| 190 | + switch (type) { |
| 191 | + case 'd': |
| 192 | + case 'i': |
| 193 | + case 'o': |
| 194 | + case 'p': |
| 195 | + case 'u': |
| 196 | + case 'x': |
| 197 | + case 'X': { |
| 198 | + unsigned long long v; |
| 199 | + const char *tabel; |
| 200 | + int base; |
| 201 | + |
| 202 | + if (type == 'p') { |
| 203 | + subtype = 'l'; /* This is written as %#lx */ |
| 204 | + type = 'x'; |
| 205 | + flags |= ALTERNATEFLAG; |
| 206 | + } |
| 207 | + |
| 208 | + if (type == 'd' || type == 'i') /* These are signed */ |
| 209 | + { |
| 210 | + signed long long v2; |
| 211 | + if (subtype == 'l') |
| 212 | + v2 = va_arg(args, signed long); |
| 213 | + else if (subtype == 'm' || subtype == 'j') |
| 214 | + v2 = va_arg(args, signed long long); |
| 215 | + else |
| 216 | + v2 = va_arg(args, signed int); |
| 217 | + if (v2 < 0) { |
| 218 | + buffer1[size1++] = '-'; |
| 219 | + v = -v2; |
| 220 | + } else { |
| 221 | + if (flags & SIGNFLAG) |
| 222 | + buffer1[size1++] = '+'; |
| 223 | + else if (flags & BLANKFLAG) |
| 224 | + buffer1[size1++] = ' '; |
| 225 | + v = v2; |
| 226 | + } |
| 227 | + } else /* These are unsigned */ |
| 228 | + { |
| 229 | + if (subtype == 'l') |
| 230 | + v = va_arg(args, unsigned long); |
| 231 | + else if (subtype == 'm' || subtype == 'j') |
| 232 | + v = va_arg(args, unsigned long long); |
| 233 | + else |
| 234 | + v = va_arg(args, unsigned int); |
| 235 | + if (flags & ALTERNATEFLAG) { |
| 236 | + if (type == 'o') { |
| 237 | + if (!preci || v) |
| 238 | + buffer1[size1++] = '0'; |
| 239 | + } else if ((type == 'x' || type == 'X') && v) { |
| 240 | + buffer1[size1++] = '0'; |
| 241 | + buffer1[size1++] = type; |
| 242 | + } |
| 243 | + } |
| 244 | + } |
| 245 | + |
| 246 | + buffer2 = &buffer[sizeof(buffer)]; /* Calculate body string */ |
| 247 | + base = type == 'x' || type == 'X' ? 16 : (type == 'o' ? 8 : 10); |
| 248 | + tabel = type != 'X' ? lowertabel : uppertabel; |
| 249 | + do { |
| 250 | + *--buffer2 = tabel[__ulldivus(&v, base)]; |
| 251 | + size2++; |
| 252 | + } while (v); |
| 253 | + if (preci == 0x7fff) /* default */ |
| 254 | + preci = 0; |
| 255 | + else |
| 256 | + flags &= ~ZEROPADFLAG; |
| 257 | + break; |
| 258 | + } |
| 259 | + case 'c': |
| 260 | + if (subtype == 'l') |
| 261 | + *buffer2 = va_arg(args, long); |
| 262 | + else |
| 263 | + *buffer2 = va_arg(args, int); |
| 264 | + size2 = 1; |
| 265 | + preci = 0; |
| 266 | + break; |
| 267 | + case 's': |
| 268 | + buffer2 = va_arg(args, char *); |
| 269 | + size2 = strlen(buffer2); |
| 270 | + size2 = size2 <= preci ? size2 : preci; |
| 271 | + preci = 0; |
| 272 | + break; |
| 273 | + |
| 274 | + case '%': |
| 275 | + buffer2 = "%"; |
| 276 | + size2 = 1; |
| 277 | + preci = 0; |
| 278 | + break; |
| 279 | + default: |
| 280 | + if (!type) |
| 281 | + ptr--; /* We've gone too far - step one back */ |
| 282 | + buffer2 = (char *) format; |
| 283 | + size2 = ptr - format; |
| 284 | + width = preci = 0; |
| 285 | + break; |
| 286 | + } |
| 287 | + |
| 288 | + pad = size1 + (size2 >= preci ? size2 : preci); /* Calculate the number of characters */ |
| 289 | + pad = pad >= width ? 0 : width - pad; /* and the number of resulting pad bytes */ |
| 290 | + |
| 291 | + if (flags & ZEROPADFLAG) /* print sign and that like */ |
| 292 | + for (i = 0; i < size1; i++) |
| 293 | + _debug_putc(buffer1[i]); |
| 294 | + |
| 295 | + if (!(flags & LALIGNFLAG)) /* Pad left */ |
| 296 | + for (i = 0; i < pad; i++) |
| 297 | + _debug_putc(flags&ZEROPADFLAG?'0':' '); |
| 298 | + |
| 299 | + if (!(flags & ZEROPADFLAG)) /* print sign if not zero padded */ |
| 300 | + for (i = 0; i < size1; i++) |
| 301 | + _debug_putc(buffer1[i]); |
| 302 | + |
| 303 | + for (i = size2; i < preci; i++) /* extend to precision */ |
| 304 | + _debug_putc('0'); |
| 305 | + |
| 306 | + for (i = 0; i < size2; i++) /* print body */ |
| 307 | + _debug_putc(buffer2[i]); |
| 308 | + |
| 309 | + if (flags & LALIGNFLAG) /* Pad right */ |
| 310 | + for (i = 0; i < pad; i++) |
| 311 | + _debug_putc(' '); |
| 312 | + |
| 313 | + format = ptr; |
| 314 | + } else |
| 315 | + _debug_putc(*format++); |
| 316 | + } |
| 317 | +} |
| 318 | + |
| 319 | +void dprintf(const char *format, ...) |
| 320 | +{ |
| 321 | + va_list args; |
| 322 | + |
| 323 | + va_start(args, format); |
| 324 | + vdprintf(format, args); |
| 325 | + va_end(args); |
| 326 | +} |
| 327 | + |
| 328 | +#endif |
0 commit comments