Monarch  v3.8.2
Project 8 Data File Format Library
M3IToA.cc
Go to the documentation of this file.
1 /*
2  * M3IToA.cc
3  *
4  * Created on: Dec 25, 2014
5  * Author: nsoblath
6  *
7  * Code is the "countlut" method from https://github.com/miloyip/itoa-benchmark
8  * Original code copyright: Copyright(c) 2014 Milo Yip (miloyip@gmail.com)
9  * License:
10  * Copyright (C) 2014 Milo Yip
11 
12  Permission is hereby granted, free of charge, to any person obtaining a copy
13  of this software and associated documentation files (the "Software"), to deal
14  in the Software without restriction, including without limitation the rights
15  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
16  copies of the Software, and to permit persons to whom the Software is
17  furnished to do so, subject to the following conditions:
18 
19  The above copyright notice and this permission notice shall be included in
20  all copies or substantial portions of the Software.
21 
22  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
25  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
28  THE SOFTWARE.
29  */
30 
31 
32 #define M3_API_EXPORTS
33 
34 #include "M3IToA.hh"
35 
36 // Additional count number of digit pass
37 // Use lookup table of two gDigitsLut
38 
39 namespace monarch3
40 {
41 
42  M3_API void u32toa( uint32_t value, char* buffer )
43  {
44  unsigned digit = CountDecimalDigit32(value);
45  buffer += digit;
46  *buffer = '\0';
47 
48  while ( value >= 100 )
49  {
50  const unsigned i = (value % 100) << 1;
51  value /= 100;
52  *--buffer = gDigitsLut[i + 1];
53  *--buffer = gDigitsLut[i];
54  }
55 
56  if ( value < 10 )
57  {
58  *--buffer = char(value) + '0';
59  }
60  else
61  {
62  const unsigned i = value << 1;
63  *--buffer = gDigitsLut[i + 1];
64  *--buffer = gDigitsLut[i];
65  }
66  }
67 
68  M3_API void i32toa( int32_t value, char* buffer )
69  {
70  if (value < 0) {
71  *buffer++ = '-';
72  value = -value;
73  }
74 
75  u32toa( static_cast<uint32_t>(value), buffer );
76  }
77 
78  M3_API void u64toa( uint64_t value, char* buffer )
79  {
80  unsigned digit = CountDecimalDigit64(value);
81  buffer += digit;
82  *buffer = '\0';
83 
84  while ( value >= 100000000 )
85  {
86  const uint32_t a = static_cast<uint32_t>(value % 100000000);
87  value /= 100000000;
88 
89  const uint32_t b = a / 10000;
90  const uint32_t c = a % 10000;
91 
92  const uint32_t b1 = (b / 100) << 1;
93  const uint32_t b2 = (b % 100) << 1;
94  const uint32_t c1 = (c / 100) << 1;
95  const uint32_t c2 = (c % 100) << 1;
96 
97  buffer -= 8;
98 
99  buffer[0] = gDigitsLut[b1];
100  buffer[1] = gDigitsLut[b1 + 1];
101  buffer[2] = gDigitsLut[b2];
102  buffer[3] = gDigitsLut[b2 + 1];
103  buffer[4] = gDigitsLut[c1];
104  buffer[5] = gDigitsLut[c1 + 1];
105  buffer[6] = gDigitsLut[c2];
106  buffer[7] = gDigitsLut[c2 + 1];
107  }
108 
109  uint32_t value32 = static_cast<uint32_t>(value);
110  while ( value32 >= 100 )
111  {
112  const unsigned i = static_cast<unsigned>(value32 % 100) << 1;
113  value32 /= 100;
114  *--buffer = gDigitsLut[i + 1];
115  *--buffer = gDigitsLut[i];
116  }
117 
118  if ( value32 < 10 )
119  {
120  *--buffer = char(value32) + '0';
121  }
122  else
123  {
124  const unsigned i = static_cast<unsigned>(value32) << 1;
125  *--buffer = gDigitsLut[i + 1];
126  *--buffer = gDigitsLut[i];
127  }
128  }
129 
130  M3_API void i64toa( int64_t value, char* buffer )
131  {
132  if ( value < 0 )
133  {
134  *buffer++ = '-';
135  value = -value;
136  }
137 
138  u64toa( static_cast<uint64_t>(value), buffer );
139  }
140 
141 } /* namespace monarch3 */
void i32toa(int32_t value, char *buffer)
Quickly convert a 32-bit signed integer to a char array (buffer should already be allocated) ...
Definition: M3IToA.cc:68
unsigned CountDecimalDigit32(uint32_t n)
Definition: M3IToA.hh:82
void u32toa(uint32_t value, char *buffer)
Quickly convert a 32-bit unsigned integer to a char array (buffer should already be allocated) ...
Definition: M3IToA.cc:42
const char gDigitsLut[200]
Definition: M3IToA.hh:38
void u64toa(uint64_t value, char *buffer)
Quickly convert a 64-bit unsigned integer to a char array (buffer should already be allocated) ...
Definition: M3IToA.cc:78
#define M3_API
Definition: M3Constants.hh:21
unsigned CountDecimalDigit64(uint64_t n)
Definition: M3IToA.hh:122
void i64toa(int64_t value, char *buffer)
Quickly convert a 64-bit signed integer to a char array (buffer should already be allocated) ...
Definition: M3IToA.cc:130