Ticket #7008: l64a.c

File l64a.c, 1.5 KB (added by TriEdgeAI, 7 years ago)

Implements l64a()

Line 
1/*
2 * Copyright 2012 Tri-Edge AI <triedgeai@gmail.com>
3 * All rights reserved. Distributed under the terms of the MIT license.
4 */
5
6char __l64a_result_empty[1] = "";
7char __l64a_result[7] = "......";
8
9char __l64a_map[] = {
10 '.','/',
11
12 '0','1','2','3','4','5','6','7','8','9',
13
14 'A','B','C','D','E','F','G','H','I','G','K','L','M','N','O','P','Q','R',
15 'S','T','U','V','W','X','Y','Z',
16
17 'a','b','c','d','e','f','g','h','i','g','k','l','m','n','o','p','q','r',
18 's','t','u','v','w','x','y','z'
19};
20
21char*
22l64a(long value)
23{
24 // Only the low-order 32 bits should be used. In case long > 4 bytes.
25
26 if (sizeof(long) > 4) {
27 value &= 0xFFFFFFFF;
28 }
29
30 // Unrolled for maximum performance.
31
32 if (value == 0) {
33 return __l64a_result_empty;
34 }
35
36 __l64a_result[0] = __l64a_map[value % 64];
37 value /= 64;
38
39 if (value == 0) {
40 __l64a_result[1] = '\0';
41 return __l64a_result;
42 }
43
44 __l64a_result[1] = __l64a_map[value % 64];
45 value /= 64;
46
47 if (value == 0) {
48 __l64a_result[2] = '\0';
49 return __l64a_result;
50 }
51
52 __l64a_result[2] = __l64a_map[value % 64];
53 value /= 64;
54
55 if (value == 0) {
56 __l64a_result[3] = '\0';
57 return __l64a_result;
58 }
59
60 __l64a_result[3] = __l64a_map[value % 64];
61 value /= 64;
62
63 if (value == 0) {
64 __l64a_result[4] = '\0';
65 return __l64a_result;
66 }
67
68 __l64a_result[4] = __l64a_map[value % 64];
69 value /= 64;
70
71 if (value == 0) {
72 __l64a_result[5] = '\0';
73 return __l64a_result;
74 }
75
76 __l64a_result[5] = __l64a_map[value % 64];
77
78 return __l64a_result;
79}