aboutsummaryrefslogtreecommitdiff
path: root/odb/oracle/details/number.cxx
blob: c097115e1a798d865d43af0ce4b162d3ffd42142 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
// file      : odb/oracle/details/number.cxx
// copyright : Copyright (c) 2005-2019 Code Synthesis Tools CC
// license   : ODB NCUEL; see accompanying LICENSE file

#include <cstddef>  // std::size_t
#include <cassert>

#include <odb/oracle/details/number.hxx>

using namespace std;

namespace odb
{
  namespace oracle
  {
    namespace details
    {
      // The NUMBER type's binary representation is made up of the following
      // bit fields, ordered in increasing memory address.
      //
      // 000 to 007: The base-100 exponent bits. The most significant bit is
      //             the sign bit of the number, which is set for positive
      //             numbers and cleared for negative numbers. For positive
      //             numbers, the exponent has a bias of 65 added to it.
      //
      // 008 to 167: The mantissa bits. Each byte of this field represents a
      //             single base-100 value.
      //
      //

      long long
      number_to_int64 (const char* b, size_t n)
      {
        // All bytes in the buffer are interpreted as unsigned.
        //
        const unsigned char* ub (reinterpret_cast<const unsigned char*> (b));

        // Zero is represented by zero significant bits and an exponent
        // set to 128.
        //
        if (n == 1)
        {
          assert (ub[0] == 128);
          return 0;
        }

        long long v (0);

        // Test the sign bit of the exponent.
        //
        if ((ub[0] & 0x80) != 0)
        {
          // The unbiased exponent of a positive number may be calculated as
          // ub[0 - 128 - 65. For an integer, this is the order of magnitude
          // of the number. Calculate the maximum weight, 100 ^ o, where o is
          // the order of magnitude of the number.
          //
          long long w (1);

          for (size_t i (0), o (ub[0] - 193); i < o; ++i)
            w *= 100;

          // Accumlate the sum of the significant base-100 terms.
          //
          for (const unsigned char* m (ub + 1), *e (ub + n); m < e; ++m)
          {
            v += (*m - 1) * w;
            w /= 100;
          }
        }
        else
        {
          // The unbiased exponent of a negative number is calculated as
          // (~ub[0] & 0x7F) - 193. For an integer, this is the order of
          // magnitude of the number. Calculate the maximum weight, 100 ^ o,
          // where o is the order of magnitude of the number.
          //
          long long w (1);

          for (size_t i (0), o ((~ub[0] & 0x7F) - 65); i < o; ++i)
            w *= 100;

          // Accumulate the sum of the significant base-100 terms. Note that
          // negative values will have a terminator byte which is included
          // in the length. This is ignored.
          //
          for (const unsigned char* m (ub + 1), *e (ub + n - 1); m < e; ++m)
          {
            v -= (101 - *m) * w;
            w /= 100;
          }
        }

        return v;
      }

      void
      int64_to_number (char* b, size_t& n, long long v)
      {
        // We assume that b is long enough to contain a long long NUMBER
        // representation, that being 12 bytes.
        //

        // All bytes in the buffer are interpreted as unsigned.
        //
        unsigned char* ub (reinterpret_cast<unsigned char*> (b));

        if (v == 0)
        {
          ub[0] = 128;
          n = 1;

          return;
        }

        bool sig (false);
        unsigned char t[11], *m (t);
        n = 0;

        if (v < 0)
        {
          // Termination marker for negative numbers.
          //
          *m++ = 102;

          while (v != 0)
          {
            int r (static_cast<int> (v % 100));
            sig = sig || r != 0;

            if (sig)
              *m++ = static_cast<unsigned char> (101 + r);

            v /= 100;
            ++n;
          }

          // The exponent is one less than the number of base 100 digits. It is
          // inverted for negative values.
          //
          ub[0] = static_cast<unsigned char> (~(n + 192));
        }
        else
        {
          while (v != 0)
          {
            int r (static_cast<int> (v % 100));
            sig = sig || r != 0;

            if (sig)
              *m++ = static_cast<unsigned char> (r + 1);

            v /= 100;
            ++n;
          }

          // Exponent is one less than the number of base 100 digits.
          //
          ub[0] = static_cast<unsigned char> (n + 192);
        }

        // Set the length.
        //
        n = static_cast<unsigned char> (m - t + 1);

        // Set the significant digits in big-endian byte order and the
        // terminator, if any.
        //
        for (size_t i (1); m > t; ++i)
          ub[i] = *--m;
      }

      unsigned long long
      number_to_uint64 (const char* b, size_t n)
      {
        // All bytes in the buffer are interpreted as unsigned.
        //
        const unsigned char* ub (reinterpret_cast<const unsigned char*> (b));

        // Zero is represented by zero significant bits and an exponent
        // set to 128.
        //
        if (n == 1)
        {
          assert (ub[0] == 128);
          return 0;
        }

        unsigned long long v (0);

        // Test the sign bit of the exponent.
        //
        if ((ub[0] & 0x80) == 0)
        {
          assert (false);
          return 0;
        }

        // The unbiased exponent of a positive number may be calculated as
        // ub[0] - 128 - 65. For an integer, this is the order of magnitude
        // of the number. Calculate the maximum weight, 100 ^ o, where o is
        // the order of magnitude of the number.
        //
        unsigned long long w (1);

        for (size_t i (0), o (ub[0] - 193); i < o; ++i)
          w *= 100;

        // Accumlate the sum of the significant base-100 terms.
        //
        for (const unsigned char* m (ub + 1), *e (ub + n); m < e; ++m)
        {
          v += (*m - 1) * w;
          w /= 100;
        }

        return v;
      }

      void
      uint64_to_number (char* b, size_t& n, unsigned long long v)
      {
        // We assume that b is long enough to contain an unsigned long long
        // NUMBER representation, that being 12 bytes.
        //

        // All bytes in the buffer are interpreted as unsigned.
        //
        unsigned char* ub (reinterpret_cast<unsigned char*> (b));

        if (v == 0)
        {
          ub[0] = 128;
          n = 1;

          return;
        }

        bool sig (false);
        unsigned char t[11], *m (t);
        n = 0;

        while (v != 0)
        {
          int r (static_cast<int> (v % 100));
          sig = sig || r != 0;

          if (sig)
            *m++ = static_cast<unsigned char> (r + 1);

          v /= 100;
          ++n;
        }

        // Exponent is one less than the number of base 100 digits.
        //
        ub[0] = static_cast<unsigned char> (n + 192);

        // Set the length.
        //
        n = static_cast<unsigned char> (m - t + 1);

        // Set the significant digits in big-endian byte order.
        //
        for (size_t i (1); m > t; ++i)
          ub[i] = *--m;
      }
    }
  }
}