2 * Jailhouse, a Linux-based partitioning hypervisor
4 * Copyright (c) Siemens AG, 2013
7 * Jan Kiszka <jan.kiszka@siemens.com>
9 * This work is licensed under the terms of the GNU GPL, version 2. See
10 * the COPYING file in the top-level directory.
13 #if BITS_PER_LONG < 64
15 static unsigned long long div_u64_u64(unsigned long long dividend,
16 unsigned long long divisor)
18 unsigned long long result = 0;
19 unsigned long long tmp_res, tmp_div;
21 while (dividend >= divisor) {
22 tmp_div = divisor << 1;
24 while (dividend >= tmp_div) {
26 if (tmp_div & (1ULL << 63))
30 dividend -= divisor * tmp_res;
36 #else /* BITS_PER_LONG >= 64 */
38 static inline unsigned long long div_u64_u64(unsigned long long dividend,
39 unsigned long long divisor)
41 return dividend / divisor;
44 #endif /* BITS_PER_LONG >= 64 */
46 static char *uint2str(unsigned long long value, char *buf)
48 unsigned long long digit, divisor = 10000000000000000000ULL;
52 digit = div_u64_u64(value, divisor);
53 value -= digit * divisor;
54 if (!first_digit || digit > 0 || divisor == 1) {
58 divisor = div_u64_u64(divisor, 10);
64 static char *int2str(long long value, char *buf)
70 return uint2str(value, buf);
73 static char *hex2str(unsigned long long value, char *buf,
74 unsigned long long leading_zero_mask)
76 static const char hexdigit[] = "0123456789abcdef";
77 unsigned long long digit, divisor = 0x1000000000000000ULL;
81 digit = div_u64_u64(value, divisor);
82 value -= digit * divisor;
83 if (!first_digit || digit > 0 || divisor == 1 ||
84 divisor & leading_zero_mask) {
85 *buf++ = hexdigit[digit];
94 static char *align(char *p1, char *p0, unsigned long width, char fill)
101 for (n = 1; p1 - n >= p0; n++)
102 *(p0 + width - n) = *(p1 - n);
103 memset(p0, fill, width - (p1 - p0));
107 static void __vprintk(const char *fmt, va_list ap)
112 unsigned long long v;
122 } else if (c == '%') {
131 fill = (c == '0') ? '0' : ' ';
132 while (c >= '0' && c <= '9') {
133 width = width * 10 + c - '0';
135 if (width >= sizeof(buf) - 1)
148 v = va_arg(ap, long);
152 p = align(p, p0, width, fill);
157 v = va_arg(ap, unsigned long);
158 p = hex2str(v, p, (unsigned long)-1);
161 console_write(va_arg(ap, const char *));
165 v = va_arg(ap, unsigned long);
167 v = va_arg(ap, unsigned int);
169 p = align(p, p0, width, fill);
173 v = va_arg(ap, unsigned long);
175 v = va_arg(ap, unsigned int);
176 p = hex2str(v, p, 0);
177 p = align(p, p0, width, fill);
187 if (p >= &buf[sizeof(buf) - 1]) {