File: decode.c

package info (click to toggle)
libdfp 1.0.17-0.1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 16,172 kB
  • sloc: ansic: 255,724; sh: 4,225; makefile: 703; awk: 461; python: 432; cpp: 254
file content (142 lines) | stat: -rw-r--r-- 3,957 bytes parent folder | download
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
/* Functions to print the BID declets for use with printf %Z

   Copyright (C) 2014-2015 Free Software Foundation, Inc.

   This file is part of the Decimal Floating Point C Library.

   The Decimal Floating Point C Library is free software; you can
   redistribute it and/or modify it under the terms of the GNU Lesser
   General Public License version 2.1.

   The Decimal Floating Point C Library is distributed in the hope that
   it will be useful, but WITHOUT ANY WARRANTY; without even the implied
   warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
   the GNU Lesser General Public License version 2.1 for more details.

   You should have received a copy of the GNU Lesser General Public
   License version 2.1 along with the Decimal Floating Point C Library;
   if not, write to the Free Software Foundation, Inc., 51 Franklin
   Street, Fifth Floor, Boston, MA 02110-1301 USA.

   Please see dfp/COPYING.txt for more information.  */

#include <stdio.h>

#include <decode.h>
#include <numdigits.h>
#include <math_private.h>
#include <decimal128.h>

/* This version is used by all BID implementations.  */

#define BID_SIGN_MASK 0x80000000UL
/* BID_SPEC_MASK is the mask for _Decimal32 to be used with uint32_t and
   since we only work with uint32_t it works for all decoded.  */
#define BID_SPEC_MASK DEC32_INF_MASK

char *
__decoded32 (_Decimal32 a, char *str)
{
  union ieee754r_Decimal32 d = { .sd = a };
  int exp = -DECIMAL32_Bias;
  char sign;
  unsigned int coeff;
  unsigned int decunits[3]  = { 0 };
  int n;

  sign = (d.si & BID_SIGN_MASK ? '-' : '+');
  if ((d.si & BID_SPEC_MASK) != BID_SPEC_MASK)
    {
      if ((d.si & BID_EXPONENT_ENC_MASK) == BID_EXPONENT_ENC_MASK)
	coeff = 0x00800000 | (d.si & 0x001FFFFF);
      else
	coeff = d.si & 0x007FFFFF;

      for (n = 0; (coeff != 0) && (n < 3); ++n)
	{
	  decunits[n] = coeff % 1000;
	  coeff /= 1000;
	}

      exp = getexpd32 (a);
    }

  sprintf (str, "%c%01u,%03u,%03uE%+d",
           sign, decunits[2], decunits[1], decunits[0], exp);

  return str;
}
weak_alias (__decoded32, decoded32)
hidden_def (__decoded32)


char *
__decoded64 (_Decimal64 a, char *str)
{
  union ieee754r_Decimal64 d = { .dd = a };
  int exp = -DECIMAL64_Bias;
  char sign;
  unsigned long long int coeff;
  unsigned int decunits[6]  = { 0 };
  int n;

  sign = (d.di[1] & BID_SIGN_MASK ? '-' : '+');
  if ((d.di[1] & BID_SPEC_MASK) != BID_SPEC_MASK)
    {
      if ((d.di[1] & BID_EXPONENT_ENC_MASK) == BID_EXPONENT_ENC_MASK)
	coeff = 0x00200000 | (d.di[1] & 0x0007FFFF);
      else
	coeff = d.di[1] & 0x001FFFFF;
      coeff = (coeff << 32) | d.di[0];

      for (n = 0; (coeff != 0) && (n < 6); ++n)
	{
	  decunits[n] = coeff % 1000;
	  coeff /= 1000;
	}
      exp = getexpd64 (a);
    }

  sprintf (str, "%c%01u,%03u,%03u,%03u,%03u,%03uE%+d",
           sign, decunits[5], decunits[4], decunits[3],
           decunits[2], decunits[1], decunits[0], exp);

  return str;
}
weak_alias (__decoded64, decoded64)
hidden_def (__decoded64)

char *
__decoded128 (_Decimal128 a, char *str)
{
  union ieee754r_Decimal128 d = { .td = a };
  int exp = -DECIMAL128_Bias;
  char sign;
  unsigned int decunits[12]  = { 0 };
  int i = 0;

  sign = (d.ti[3] & BID_SIGN_MASK ? '-' : '+');
  if ((d.ti[3] & BID_SPEC_MASK) != BID_SPEC_MASK)
    {
    decNumber dn;
    decimal128ToNumber((decimal128*)&a, &dn);

    /* This only works if DECDPUN == 3, as is the default */
    for (int d = dn.digits; d > 0; d -= DECDPUN)
      {
        decunits[i] = dn.lsu[i];
        i++;
      }
    exp = dn.exponent;
    }

  sprintf (str, "%c%01u,%03u,%03u,%03u,%03u,%03u,%03u,%03u,%03u,%03u,%03u,%03uE%+d",
           sign, decunits[11], decunits[10], decunits[9],
           decunits[8], decunits[7], decunits[6],
           decunits[5], decunits[4], decunits[3],
           decunits[2], decunits[1], decunits[0], exp);

  return str;
}
weak_alias (__decoded128, decoded128)
hidden_def (__decoded128)