Monarch  v3.8.2
Project 8 Data File Format Library
M3IToA.hh
Go to the documentation of this file.
1 /*
2  * M3IToA.hh
3  *
4  * Created on: Dec 25, 2014
5  * Author: nsoblath
6  * Code is the "countlut" method from https://github.com/miloyip/itoa-benchmark
7  * Original code copyright: Copyright(c) 2014 Milo Yip (miloyip@gmail.com)
8  * License:
9  * Copyright (C) 2014 Milo Yip
10 
11  Permission is hereby granted, free of charge, to any person obtaining a copy
12  of this software and associated documentation files (the "Software"), to deal
13  in the Software without restriction, including without limitation the rights
14  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
15  copies of the Software, and to permit persons to whom the Software is
16  furnished to do so, subject to the following conditions:
17 
18  The above copyright notice and this permission notice shall be included in
19  all copies or substantial portions of the Software.
20 
21  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
26  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
27  THE SOFTWARE.
28  */
29 
30 #pragma once
31 
32 #include "M3Constants.hh"
33 
34 // from digitslut.h
35 
36 namespace monarch3
37 {
38  const char gDigitsLut[200] = {
39  '0','0','0','1','0','2','0','3','0','4','0','5','0','6','0','7','0','8','0','9',
40  '1','0','1','1','1','2','1','3','1','4','1','5','1','6','1','7','1','8','1','9',
41  '2','0','2','1','2','2','2','3','2','4','2','5','2','6','2','7','2','8','2','9',
42  '3','0','3','1','3','2','3','3','3','4','3','5','3','6','3','7','3','8','3','9',
43  '4','0','4','1','4','2','4','3','4','4','4','5','4','6','4','7','4','8','4','9',
44  '5','0','5','1','5','2','5','3','5','4','5','5','5','6','5','7','5','8','5','9',
45  '6','0','6','1','6','2','6','3','6','4','6','5','6','6','6','7','6','8','6','9',
46  '7','0','7','1','7','2','7','3','7','4','7','5','7','6','7','7','7','8','7','9',
47  '8','0','8','1','8','2','8','3','8','4','8','5','8','6','8','7','8','8','8','9',
48  '9','0','9','1','9','2','9','3','9','4','9','5','9','6','9','7','9','8','9','9'
49  };
50 }
51 
52 
53 // from countdecimaldigit.h
54 
55 #include <stdint.h>
56 #ifdef _MSC_VER
57 #include "intrin.h"
58 #endif
59 
60 namespace monarch3
61 {
62  //******************************
63  // Integer-to-string conversions
64  //******************************
65 
67  M3_API void u32toa( uint32_t value, char* buffer );
69  M3_API void i32toa( int32_t value, char* buffer );
71  M3_API void u64toa( uint64_t value, char* buffer );
73  M3_API void i64toa( int64_t value, char* buffer );
74 
75 
76 
77 
78  //******************
79  // Utility functions
80  //******************
81 
82  inline unsigned CountDecimalDigit32( uint32_t n )
83  {
84 #if defined(_MSC_VER) || defined(__GNUC__)
85  static const uint32_t powers_of_10[] =
86  {
87  0,
88  10,
89  100,
90  1000,
91  10000,
92  100000,
93  1000000,
94  10000000,
95  100000000,
96  1000000000
97  };
98 
99 #ifdef _MSC_VER
100  unsigned long i = 0;
101  _BitScanReverse(&i, n | 1);
102  uint32_t t = (i + 1) * 1233 >> 12;
103 #elif __GNUC__
104  uint32_t t = (32 - __builtin_clz(n | 1)) * 1233 >> 12;
105 #endif
106  return t - (n < powers_of_10[t]) + 1;
107 #else
108  // Simple pure C++ implementation
109  if (n < 10) return 1;
110  if (n < 100) return 2;
111  if (n < 1000) return 3;
112  if (n < 10000) return 4;
113  if (n < 100000) return 5;
114  if (n < 1000000) return 6;
115  if (n < 10000000) return 7;
116  if (n < 100000000) return 8;
117  if (n < 1000000000) return 9;
118  return 10;
119 #endif
120  }
121 
122  inline unsigned CountDecimalDigit64( uint64_t n )
123  {
124 #if defined(_MSC_VER) || defined(__GNUC__)
125  static const uint64_t powers_of_10[] =
126  {
127  0,
128  10,
129  100,
130  1000,
131  10000,
132  100000,
133  1000000,
134  10000000,
135  100000000,
136  1000000000,
137  10000000000,
138  100000000000,
139  1000000000000,
140  10000000000000,
141  100000000000000,
142  1000000000000000,
143  10000000000000000,
144  100000000000000000,
145  1000000000000000000,
146  10000000000000000000U
147  };
148 
149 #if _M_IX86
150  unsigned long i = 0;
151  uint64_t m = n | 1;
152  if (_BitScanReverse(&i, m >> 32))
153  i += 32;
154  else
155  _BitScanReverse(&i, m & 0xFFFFFFFF);
156  uint32_t t = (i + 1) * 1233 >> 12;
157 #elif _M_X64
158  unsigned long i = 0;
159  _BitScanReverse64(&i, n | 1);
160  uint32_t t = (i + 1) * 1233 >> 12;
161 #elif __GNUC__
162  uint32_t t = (64 - __builtin_clzll(n | 1)) * 1233 >> 12;
163 #endif
164  return t - (n < powers_of_10[t]) + 1;
165 #else
166  // Simple pure C++ implementation
167  if (n < 10) return 1;
168  if (n < 100) return 2;
169  if (n < 1000) return 3;
170  if (n < 10000) return 4;
171  if (n < 100000) return 5;
172  if (n < 1000000) return 6;
173  if (n < 10000000) return 7;
174  if (n < 100000000) return 8;
175  if (n < 1000000000) return 9;
176  if (n < 10000000000) return 10;
177  if (n < 100000000000) return 11;
178  if (n < 1000000000000) return 12;
179  if (n < 10000000000000) return 13;
180  if (n < 100000000000000) return 14;
181  if (n < 1000000000000000) return 15;
182  if (n < 10000000000000000) return 16;
183  if (n < 100000000000000000) return 17;
184  if (n < 1000000000000000000) return 18;
185  if (n < 10000000000000000000) return 19;
186  return 20;
187 #endif
188  }
189 } /* 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