1 /* Copyright © 2017 Keith Packard
2    All rights reserved.
3 
4    Redistribution and use in source and binary forms, with or without
5    modification, are permitted provided that the following conditions are met:
6 
7    * Redistributions of source code must retain the above copyright
8      notice, this list of conditions and the following disclaimer.
9    * Redistributions in binary form must reproduce the above copyright
10      notice, this list of conditions and the following disclaimer in
11      the documentation and/or other materials provided with the
12      distribution.
13    * Neither the name of the copyright holders nor the names of
14      contributors may be used to endorse or promote products derived
15      from this software without specific prior written permission.
16 
17   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18   AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19   IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20   ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
21   LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22   CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23   SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24   INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25   CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26   ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27   POSSIBILITY OF SUCH DAMAGE. */
28 
29 #if PRINTF_LEVEL < PRINTF_DBL && defined(_PRINTF_SMALL_ULTOA)
30 
31 /*
32  * Enable fancy divmod for targets where we don't expect either
33  * hardware division support or that software will commonly be using
34  * the soft division code. That means platforms where 'long' is not at
35  * least 64-bits get the fancy code for 64-bit values and platforms
36  * where 'int' is not at least 32-bits also get the fancy code for
37  * 32-bit values
38  */
39 
40 #if SIZEOF_ULTOA > __SIZEOF_LONG__
41 
42 #define FANCY_DIVMOD
43 
udivmod10(ultoa_unsigned_t n,char * rp)44 static inline ultoa_unsigned_t udivmod10(ultoa_unsigned_t n, char *rp) {
45         ultoa_unsigned_t q;
46         char r;
47 
48         /* Compute n * 0x1999999999999999 / (2^64) ≃ n / 10 */
49 
50         /* q = n * 0xc >> 4 */
51 	q = (n >> 1) + (n >> 2);
52 
53         /*
54           q = q * 0x11 >> 4
55             = (n * 0xc >> 4) * 0x11 >> 4
56             ≂ n * 0xcc >> 8
57         */
58 	q = q + (q >> 4);
59 
60         /*
61           q = q * 0x101 >> 8
62             = ((n * 0xc >> 4) * 0x11 >> 4) * 0x101 >> 8
63             ≂ (n * 0xcc >> 8) * 0x101 >> 8
64             ≂ n * 0xcccc >> 16
65         */
66 	q = q + (q >> 8);
67 
68         /*
69           q = q * 0x10001 >> 16
70             = (((n * 0xc >> 4) * 0x11 >> 4) * 0x101 >> 8) * 0x10001 >> 16
71             ≂ (n * 0xcccc >> 16) * 0x10001 >> 16
72             ≂ n * 0xcccccccc >> 32
73          */
74 	q = q + (q >> 16);
75 
76 #if SIZEOF_ULTOA > 4
77         /*
78           q = q * 0x100000001 >> 32
79             = ((((n * 0xc >> 4) * 0x11 >> 4) * 0x101 >> 8) * 0x10001 >> 16) * 0x100000001 >> 32
80             ≂ (n * 0xcccccccc >> 32) * 0x100000001 >> 32
81             ≂ n * cccccccccccccccc >> 64
82         */
83         q = q + (q >> 32);
84 #endif
85 
86         /*
87           q = q >> 3
88             = (((((n * 0xc >> 4) * 0x11 >> 4) * 0x101 >> 8) * 0x10001 >> 16) * 0x100000001 >> 32) >> 3
89             ≂ (n * cccccccccccccccc >> 64) >> 3
90             ≂ n * 0x1999999999999999 >> 64
91         */
92 	q = q >> 3;
93 
94         /* r = n - q * 10 */
95 	r = (char) (n - (((q << 2) + q) << 1));
96 
97         /*
98          * Because of the approximations above, q will
99          * be +0/-1 of the real result. Check and adjust
100          */
101         if (r > 9) {
102             q++;
103             r -= 10;
104         }
105         *rp = r;
106 	return q;
107 }
108 
109 static inline ultoa_unsigned_t
udivmod(ultoa_unsigned_t val,int base,char * dig)110 udivmod(ultoa_unsigned_t val, int base, char *dig)
111 {
112     switch(base) {
113 #ifdef _WANT_IO_PERCENT_B
114     case 2:
115         *dig = val & 1;
116         return val >> 1;
117 #endif
118     case 8:
119         *dig = val & 7;
120         return val >> 3;
121     case 16:
122         *dig = val & 15;
123         return val >> 4;
124     }
125     return udivmod10(val, dig);
126 }
127 
128 #endif
129 #endif
130 
131 static __noinline char *
__ultoa_invert(ultoa_unsigned_t val,char * str,int base)132 __ultoa_invert(ultoa_unsigned_t val, char *str, int base)
133 {
134 	char hex = ('a' - '0' - 10 + 16) - base;
135 
136         base &= 31;
137 
138 	do {
139 		char	v;
140 
141 #ifdef FANCY_DIVMOD
142                 val = udivmod(val, base, &v);
143 #else
144                 v = val % base;
145                 val /= base;
146 #endif
147 		if (v > 9)
148                         v += hex;
149                 v += '0';
150 		*str++ = v;
151 	} while (val);
152 	return str;
153 }
154