1 /* A reduced version of the printf function.
3 Copyright (C) 2011 Richard Henderson
5 This file is part of QEMU PALcode.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the text
15 of the GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; see the file COPYING. If not see
19 <http://www.gnu.org/licenses/>. */
26 static int print_buf_pad(char *buf
, int buflen
, char *p
, int width
, int pad
)
28 int len
= buf
+ buflen
- p
;
36 while (width
> buflen
)
50 static int print_decimal(unsigned long val
, int width
, int pad
)
53 char *p
= buf
+ sizeof(buf
);
63 /* Compiling with -Os results in a call to the division routine.
64 Do what the compiler ought to have done. */
65 d
= __builtin_alpha_umulh(val
, 0xcccccccccccccccd);
75 return print_buf_pad(buf
, sizeof(buf
), p
, width
, pad
);
78 static int print_hex(unsigned long val
, int width
, char pad
)
81 char *p
= buf
+ sizeof(buf
);
90 *--p
= (d
< 10 ? '0' : 'a' - 10) + d
;
96 return print_buf_pad(buf
, sizeof(buf
), p
, width
, pad
);
99 int printf(const char *fmt
, ...)
115 const char *percent
= fmt
;
116 bool is_long
= false;
135 long d
= va_arg (args
, long);
145 int d
= va_arg (args
, int);
158 val
= va_arg (args
, unsigned long);
160 val
= va_arg (args
, unsigned int);
163 r
+= print_decimal (val
, width
, pad
);
168 val
= va_arg (args
, unsigned long);
170 val
= va_arg (args
, unsigned int);
171 r
+= print_hex (val
, width
, pad
);
176 const char *s
= va_arg (args
, const char *);
187 while (fmt
[1] >= '0' && fmt
[1] <= '9')
188 width
= width
* 10 + *++fmt
- '0';
193 int len
= fmt
- percent
;
194 crb_puts(0, percent
, len
);