BDE 4.14.0 Production release
Loading...
Searching...
No Matches
bdldfp_decimalconvertutil_inteldfp.h
Go to the documentation of this file.
1/// @file bdldfp_decimalconvertutil_inteldfp.h
2///
3/// The content of this file has been pre-processed for Doxygen.
4///
5
6
7// bdldfp_decimalconvertutil_inteldfp.h -*-C++-*-
8#ifndef INCLUDED_BDLDFP_DECIMALCONVERTUTIL_INTELDFP
9#define INCLUDED_BDLDFP_DECIMALCONVERTUTIL_INTELDFP
10
11#include <bsls_ident.h>
12BSLS_IDENT("$Id$")
13
14/// @defgroup bdldfp_decimalconvertutil_inteldfp bdldfp_decimalconvertutil_inteldfp
15/// @brief Provide decimal floating-point conversion functions for Intel DFP.
16/// @addtogroup bdl
17/// @{
18/// @addtogroup bdldfp
19/// @{
20/// @addtogroup bdldfp_decimalconvertutil_inteldfp
21/// @{
22///
23/// <h1> Outline </h1>
24/// * <a href="#bdldfp_decimalconvertutil_inteldfp-purpose"> Purpose</a>
25/// * <a href="#bdldfp_decimalconvertutil_inteldfp-classes"> Classes </a>
26/// * <a href="#bdldfp_decimalconvertutil_inteldfp-description"> Description </a>
27/// * <a href="#bdldfp_decimalconvertutil_inteldfp-usage"> Usage </a>
28/// * <a href="#bdldfp_decimalconvertutil_inteldfp-example-1-tbd"> Example 1: TBD </a>
29///
30/// # Purpose {#bdldfp_decimalconvertutil_inteldfp-purpose}
31/// Provide decimal floating-point conversion functions for Intel DFP.
32///
33/// # Classes {#bdldfp_decimalconvertutil_inteldfp-classes}
34///
35/// - bdldfp::DecimalConvertUtil_IntelDfp: Intel DFP conversion functions
36///
37/// @see bdldfp_decimal, bdldfp_decimalplatform
38///
39/// # Description {#bdldfp_decimalconvertutil_inteldfp-description}
40/// This component provides conversion operations between the
41/// decimal types supplied in this package (`Decimal32`, `Decimal64`,
42/// `Decimal128`) and various alternative representations. Some of the
43/// alternative representations that this component provides conversions for are
44/// IEEE-754 binary floating point (i.e., `float` and `double`) and a network
45/// format (big-endian, Densely Packed Decimal encoding).
46///
47/// ## Usage {#bdldfp_decimalconvertutil_inteldfp-usage}
48///
49///
50/// This section shows the intended use of this component.
51///
52/// ### Example 1: TBD {#bdldfp_decimalconvertutil_inteldfp-example-1-tbd}
53///
54///
55/// @}
56/** @} */
57/** @} */
58
59/** @addtogroup bdl
60 * @{
61 */
62/** @addtogroup bdldfp
63 * @{
64 */
65/** @addtogroup bdldfp_decimalconvertutil_inteldfp
66 * @{
67 */
68
69#include <bdlscm_version.h>
70
72
73#ifdef BDLDFP_DECIMALPLATFORM_INTELDFP
74#include <bdldfp_decimal.h>
78
79#include <bsls_assert.h>
80
81#include <bsl_cstring.h>
82
83
84namespace bdldfp {
85 // =================================
86 // class DecimalConvertUtil_IntelDfp
87 // =================================
88
89/// This `struct` provides a namespace for utility functions that convert
90/// between the decimal floating-point types of @ref bdldfp_decimal and various
91/// other formats.
92struct DecimalConvertUtil_IntelDfp {
93
94 // Convert to Binary Floating-Point from C++ Decimal TR
95
96 // CLASS METHODS
97
98 // decimalToDouble functions
99
100 static double decimal32ToDouble (Decimal32 decimal);
101 static double decimal64ToDouble (Decimal64 decimal);
102 static double decimal128ToDouble(Decimal128 decimal);
103
104 /// Return a `double` object having the value closest to the value of
105 /// the specified `decimal` object following the conversion rules
106 /// defined by IEEE-754:
107 ///
108 static double decimalToDouble (Decimal32 decimal);
109 static double decimalToDouble (Decimal64 decimal);
110 /// * If the `decimal` object is a NaN, return a NaN.
111 /// * Otherwise if `decimal` is positive or negative infinity, return
112 /// infinity of the same sign.
113 /// * Otherwise if `decimal` is positive or negative zero, return zero
114 /// of the same sign.
115 /// * Otherwise if `decimal` object has an absolute value that is
116 /// larger than `std::numeric_limits<double>::max()`, raise the
117 /// "overflow" floating-point exception and return infinity of the
118 /// same sign as `decimal`.
119 /// * Otherwise if `decimal` has an absolute value that is smaller than
120 /// `std::numeric_limits<double>::min()`, raise the "underflow"
121 /// floating-point exception and return zero of the same sign as
122 /// `decimal`.
123 /// * Otherwise if `decimal` has a value that has more significant
124 /// base-10 digits than `std::numeric_limits<double>::digits10`,
125 /// raise the "inexact" floating-point exception, round that value
126 /// according to the *binary* rounding direction setting of the
127 /// floating-point environment, and return the result of that.
128 /// * Otherwise if `decimal` has a significand that cannot be exactly
129 /// represented using binary floating-point, raise the "inexact"
130 /// floating-point exception, roundthat value according to the
131 /// *binary* rounding direction setting of the environment, and
132 /// return the result of that.
133 /// * Otherwise use the exact value of the `other` object for the
134 /// initialization if this object.
135 static double decimalToDouble (Decimal128 decimal);
136
137 // decimalToFloat functions
138
139 static float decimal32ToFloat (Decimal32 decimal);
140 static float decimal64ToFloat (Decimal64 decimal);
141 static float decimal128ToFloat(Decimal128 decimal);
142
143 /// Return a `float` object having the value closest to the value of the
144 /// specified `decimal` object following the conversion rules defined
145 /// by IEEE-754:
146 ///
147 static float decimalToFloat (Decimal32 decimal);
148 static float decimalToFloat (Decimal64 decimal);
149 /// * If the `decimal` object is a NaN, return a NaN.
150 /// * Otherwise if `decimal` is positive or negative infinity, return
151 /// infinity of the same sign.
152 /// * Otherwise if `decimal` is positive or negative zero, return zero
153 /// of the same sign.
154 /// * Otherwise if `decimal` object has an absolute value that is
155 /// larger than `std::numeric_limits<long double>::max()`, raise the
156 /// "overflow" floating-point exception and return infinity of the
157 /// same sign as `decimal`.
158 /// * Otherwise if `decimal` has an absolute value that is smaller than
159 /// `std::numeric_limits<float>::min()`, raise the "underflow"
160 /// floating-point exception and return zero of the same sign as
161 /// `decimal`.
162 /// * Otherwise if `decimal` has a value that has more significant
163 /// base-10 digits than `std::numeric_limits<float>::digits10`,
164 /// raise the "inexact" floating-point exception, round that value
165 /// according to the *binary* rounding direction setting of the
166 /// floating-point environment, and return the result of that.
167 /// * Otherwise if `decimal` has a significand that cannot be exactly
168 /// represented using binary floating-point, raise the "inexact"
169 /// floating-point exception, roundthat value according to the
170 /// *binary* rounding direction setting of the environment, and
171 /// return the result of that.
172 /// * Otherwise use the exact value of the `other` object for the
173 /// initialization if this object.
174 static float decimalToFloat (Decimal128 decimal);
175
176
177 // decimalToDPD functions
178
179 static void decimal32ToDPD( unsigned char *buffer,
180 Decimal32 decimal);
181 static void decimal64ToDPD( unsigned char *buffer,
182 Decimal64 decimal);
183 static void decimal128ToDPD(unsigned char *buffer,
184 Decimal128 decimal);
185
186 static void decimalToDPD( unsigned char *buffer,
187 Decimal32 decimal);
188 static void decimalToDPD( unsigned char *buffer,
189 Decimal64 decimal);
190 /// Populate the specified `buffer` with the Densely Packed Decimal
191 /// (DPD) representation of the specified `decimal` value. The DPD
192 /// representations of `Decimal32`, `Decimal64`, and `Decimal128`
193 /// require 4, 8, and 16 bytes respectively. The behavior is undefined
194 /// unless `buffer` points to a contiguous sequence of at least
195 /// `sizeof(decimal)` bytes. Note that the DPD representation is
196 /// defined in section 3.5 of IEEE 754-2008.
197 static void decimalToDPD( unsigned char *buffer,
198 Decimal128 decimal);
199
200 // decimalFromDPD functions
201
202 static Decimal32 decimal32FromDPD( const unsigned char *buffer);
203 static Decimal64 decimal64FromDPD( const unsigned char *buffer);
204
205 /// Return the native implementation representation of the value of the
206 /// same size base-10 floating-point value stored in Densely Packed
207 /// Decimal format at the specified `buffer` address. The behavior is
208 /// undefined unless `buffer` points to a memory area at least
209 /// `sizeof(decimal)` in size containing a value in DPD format.
210 static Decimal128 decimal128FromDPD(const unsigned char *buffer);
211
212 static void decimalFromDPD(Decimal32 *decimal,
213 const unsigned char *buffer);
214 static void decimalFromDPD(Decimal64 *decimal,
215 const unsigned char *buffer);
216 /// Store, into the specified `decimal`, the native implementation
217 /// representation of the value of the same size base-10 floating point
218 /// value represented in Densely Packed Decimal format, at the specified
219 /// `buffer` address. The behavior is undefined unless `buffer` points
220 /// to a memory area at least `sizeof(decimal)` in size containing a
221 /// value in DPD format.
222 static void decimalFromDPD(Decimal128 *decimal,
223 const unsigned char *buffer);
224
225 // decimalToBID functions
226
227 static void decimal32ToBID( unsigned char *buffer,
228 Decimal32 decimal);
229 static void decimal64ToBID( unsigned char *buffer,
230 Decimal64 decimal);
231 static void decimal128ToBID(unsigned char *buffer,
232 Decimal128 decimal);
233
234 static void decimalToBID( unsigned char *buffer,
235 Decimal32 decimal);
236 static void decimalToBID( unsigned char *buffer,
237 Decimal64 decimal);
238 /// Populate the specified `buffer` with the Binary Integer Decimal
239 /// (BID) representation of the specified `decimal` value. The BID
240 /// representations of `Decimal32`, `Decimal64`, and `Decimal128`
241 /// require 4, 8, and 16 bytes respectively. The behavior is undefined
242 /// unless `buffer` points to a contiguous sequence of at least
243 /// `sizeof(decimal)` bytes. Note that the BID representation is
244 /// defined in section 3.5 of IEEE 754-2008.
245 static void decimalToBID( unsigned char *buffer,
246 Decimal128 decimal);
247
248 // decimalFromBID functions
249
250 static Decimal32 decimal32FromBID( const unsigned char *buffer);
251 static Decimal64 decimal64FromBID( const unsigned char *buffer);
252
253 /// Return the native implementation representation of the value of the
254 /// same size base-10 floating-point value stored in Binary Integer
255 /// Decimal format at the specified `buffer` address. The behavior is
256 /// undefined unless `buffer` points to a memory area at least
257 /// `sizeof(decimal)` in size containing a value in BID format.
258 static Decimal128 decimal128FromBID(const unsigned char *buffer);
259
260 static void decimalFromBID(Decimal32 *decimal,
261 const unsigned char *buffer);
262 static void decimalFromBID(Decimal64 *decimal,
263 const unsigned char *buffer);
264 /// Store, into the specified `decimal`, the native implementation
265 /// representation of the value of the same size base-10 floating point
266 /// value represented in Binary Integer Decimal format, at the specified
267 /// `buffer` address. The behavior is undefined unless `buffer` points
268 /// to a memory area at least `sizeof(decimal)` in size containing a
269 /// value in BID format.
270 static void decimalFromBID(Decimal128 *decimal,
271 const unsigned char *buffer);
272};
273
274// ============================================================================
275// INLINE FUNCTION DEFINITIONS
276// ============================================================================
277
278 // decimalToDouble functions
279
280inline
281double
282DecimalConvertUtil_IntelDfp::decimal32ToDouble(Decimal32 decimal)
283{
284 return decimalToDouble(decimal);
285}
286
287inline
288double
289DecimalConvertUtil_IntelDfp::decimal64ToDouble(Decimal64 decimal)
290{
291 return decimalToDouble(decimal);
292}
293
294inline
295double
296DecimalConvertUtil_IntelDfp::decimal128ToDouble(Decimal128 decimal)
297{
298 return decimalToDouble(decimal);
299}
300
301inline
302double
303DecimalConvertUtil_IntelDfp::decimalToDouble(Decimal32 decimal)
304{
305 _IDEC_flags flags;
306 return __bid32_to_binary64(decimal.data()->d_raw, &flags);
307}
308
309inline
310double
311DecimalConvertUtil_IntelDfp::decimalToDouble(Decimal64 decimal)
312{
313 _IDEC_flags flags;
314 return __bid64_to_binary64(decimal.data()->d_raw, &flags);
315}
316
317inline
318double
319DecimalConvertUtil_IntelDfp::decimalToDouble(Decimal128 decimal)
320{
321 _IDEC_flags flags;
322 return __bid128_to_binary64(decimal.data()->d_raw, &flags);
323}
324
325 // decimalToFloat functions
326
327inline
328float
329DecimalConvertUtil_IntelDfp::decimal32ToFloat(Decimal32 decimal)
330{
331 return decimalToFloat(decimal);
332}
333
334inline float
335DecimalConvertUtil_IntelDfp::decimal64ToFloat(Decimal64 decimal)
336{
337 return decimalToFloat(decimal);
338}
339
340inline float
341DecimalConvertUtil_IntelDfp::decimal128ToFloat(Decimal128 decimal)
342{
343 return decimalToFloat(decimal);
344}
345
346inline
347float
348DecimalConvertUtil_IntelDfp::decimalToFloat(Decimal32 decimal)
349{
350 _IDEC_flags flags;
351 return __bid32_to_binary32(decimal.data()->d_raw, &flags);
352}
353
354inline
355float
356DecimalConvertUtil_IntelDfp::decimalToFloat(Decimal64 decimal)
357{
358 _IDEC_flags flags;
359 return __bid64_to_binary32(decimal.data()->d_raw, &flags);
360}
361
362inline
363float
364DecimalConvertUtil_IntelDfp::decimalToFloat(Decimal128 decimal)
365{
366 _IDEC_flags flags;
367 return __bid128_to_binary32(decimal.data()->d_raw, &flags);
368}
369
370 // decimalToDPD functions
371
372inline
373void DecimalConvertUtil_IntelDfp::decimal32ToDPD(unsigned char *buffer,
374 Decimal32 decimal)
375{
376 decimalToDPD(buffer, decimal);
377}
378
379inline
380void DecimalConvertUtil_IntelDfp::decimal64ToDPD(unsigned char *buffer,
381 Decimal64 decimal)
382{
383 decimalToDPD(buffer, decimal);
384}
385
386inline
387void DecimalConvertUtil_IntelDfp::decimal128ToDPD(unsigned char *buffer,
388 Decimal128 decimal)
389{
390 decimalToDPD(buffer, decimal);
391}
392
393inline
394void DecimalConvertUtil_IntelDfp::decimalToDPD(unsigned char *buffer,
395 Decimal32 decimal)
396{
397 decimal.data()->d_raw = __bid_to_dpd32(decimal.data()->d_raw);
398 bsl::memcpy(buffer, &decimal, sizeof(decimal));
399}
400
401inline
402void DecimalConvertUtil_IntelDfp::decimalToDPD(unsigned char *buffer,
403 Decimal64 decimal)
404{
405 decimal.data()->d_raw = __bid_to_dpd64(decimal.data()->d_raw);
406 bsl::memcpy(buffer, &decimal, sizeof(decimal));
407}
408
409inline
410void DecimalConvertUtil_IntelDfp::decimalToDPD(unsigned char *buffer,
411 Decimal128 decimal)
412{
413 decimal.data()->d_raw = __bid_to_dpd128(decimal.data()->d_raw);
414 bsl::memcpy(buffer, &decimal, sizeof(decimal));
415}
416
417 // decimalFromDPD functions
418
419inline
420Decimal32 DecimalConvertUtil_IntelDfp::decimal32FromDPD(
421 const unsigned char *buffer)
422{
423 BSLS_ASSERT(buffer);
424
426 bsl::memcpy(&value, buffer, sizeof(value));
427 value.d_raw = __bid_dpd_to_bid32(value.d_raw);
428
429 return Decimal32(value);
430}
431
432inline
433Decimal64 DecimalConvertUtil_IntelDfp::decimal64FromDPD(
434 const unsigned char *buffer)
435{
436 BSLS_ASSERT(buffer);
437
439 bsl::memcpy(&value, buffer, sizeof(value));
440 value.d_raw = __bid_dpd_to_bid64(value.d_raw);
441
442 return Decimal64(value);
443}
444
445inline
446Decimal128 DecimalConvertUtil_IntelDfp::decimal128FromDPD(
447 const unsigned char *buffer)
448{
449 BSLS_ASSERT(buffer);
450
452 bsl::memcpy(&value, buffer, sizeof(value));
453 value.d_raw = __bid_dpd_to_bid128(value.d_raw);
454
455 return Decimal128(value);
456}
457
458inline
459void DecimalConvertUtil_IntelDfp::decimalFromDPD(Decimal32 *decimal,
460 const unsigned char *buffer)
461{
462 BSLS_ASSERT(decimal);
463 BSLS_ASSERT(buffer);
464
465 *decimal = decimal32FromDPD(buffer);
466}
467
468inline
469void DecimalConvertUtil_IntelDfp::decimalFromDPD(Decimal64 *decimal,
470 const unsigned char *buffer)
471{
472 BSLS_ASSERT(decimal);
473 BSLS_ASSERT(buffer);
474
475 *decimal = decimal64FromDPD(buffer);
476}
477
478inline
479void DecimalConvertUtil_IntelDfp::decimalFromDPD(Decimal128 *decimal,
480 const unsigned char *buffer)
481{
482 BSLS_ASSERT(decimal);
483 BSLS_ASSERT(buffer);
484
485 *decimal = decimal128FromDPD(buffer);
486}
487
488 // decimalToBID functions
489
490inline
491void
492DecimalConvertUtil_IntelDfp::decimal32ToBID(unsigned char *buffer,
493 Decimal32 decimal)
494{
495 decimalToBID(buffer, decimal);
496}
497
498inline
499void
500DecimalConvertUtil_IntelDfp::decimal64ToBID(unsigned char *buffer,
501 Decimal64 decimal)
502{
503 decimalToBID(buffer, decimal);
504}
505
506inline
507void DecimalConvertUtil_IntelDfp::decimal128ToBID(unsigned char *buffer,
508 Decimal128 decimal)
509{
510 decimalToBID(buffer, decimal);
511}
512
513inline
514void DecimalConvertUtil_IntelDfp::decimalToBID(unsigned char *buffer,
515 Decimal32 decimal)
516{
517 bsl::memcpy(buffer, &decimal, sizeof(decimal));
518}
519
520inline
521void DecimalConvertUtil_IntelDfp::decimalToBID(unsigned char *buffer,
522 Decimal64 decimal)
523{
524 bsl::memcpy(buffer, &decimal, sizeof(decimal));
525}
526
527inline
528void DecimalConvertUtil_IntelDfp::decimalToBID(unsigned char *buffer,
529 Decimal128 decimal)
530{
531 bsl::memcpy(buffer, &decimal, sizeof(decimal));
532}
533
534 // decimalFromBID functions
535
536inline
537Decimal32 DecimalConvertUtil_IntelDfp::decimal32FromBID(
538 const unsigned char *buffer)
539{
540 BSLS_ASSERT(buffer);
541
543 bsl::memcpy(&value, buffer, sizeof(value));
544
545 return Decimal32(value);
546}
547
548inline
549Decimal64 DecimalConvertUtil_IntelDfp::decimal64FromBID(
550 const unsigned char *buffer)
551{
552 BSLS_ASSERT(buffer);
553
555 bsl::memcpy(&value, buffer, sizeof(value));
556
557 return Decimal64(value);
558}
559
560inline
561Decimal128 DecimalConvertUtil_IntelDfp::decimal128FromBID(
562 const unsigned char *buffer)
563{
564 BSLS_ASSERT(buffer);
565
567 bsl::memcpy(&value, buffer, sizeof(value));
568
569 return Decimal128(value);
570}
571
572inline
573void DecimalConvertUtil_IntelDfp::decimalFromBID(Decimal32 *decimal,
574 const unsigned char *buffer)
575{
576 BSLS_ASSERT(decimal);
577 BSLS_ASSERT(buffer);
578
579 *decimal = decimal32FromBID(buffer);
580}
581
582inline
583void DecimalConvertUtil_IntelDfp::decimalFromBID(Decimal64 *decimal,
584 const unsigned char *buffer)
585{
586 BSLS_ASSERT(decimal);
587 BSLS_ASSERT(buffer);
588
589 *decimal = decimal64FromBID(buffer);
590}
591
592inline
593void DecimalConvertUtil_IntelDfp::decimalFromBID(Decimal128 *decimal,
594 const unsigned char *buffer)
595{
596 BSLS_ASSERT(decimal);
597 BSLS_ASSERT(buffer);
598
599 *decimal = decimal128FromBID(buffer);
600}
601
602} // close package namespace
603
604
605#endif
606
607#endif
608
609// ----------------------------------------------------------------------------
610// Copyright 2014 Bloomberg Finance L.P.
611//
612// Licensed under the Apache License, Version 2.0 (the "License");
613// you may not use this file except in compliance with the License.
614// You may obtain a copy of the License at
615//
616// http://www.apache.org/licenses/LICENSE-2.0
617//
618// Unless required by applicable law or agreed to in writing, software
619// distributed under the License is distributed on an "AS IS" BASIS,
620// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
621// See the License for the specific language governing permissions and
622// limitations under the License.
623// ----------------------------- END-OF-FILE ----------------------------------
624
625/** @} */
626/** @} */
627/** @} */
Imp::ValueType64 ValueType64
Definition bdldfp_decimalimputil.h:250
Imp::ValueType128 ValueType128
Definition bdldfp_decimalimputil.h:251
Imp::ValueType32 ValueType32
Definition bdldfp_decimalimputil.h:249
#define BSLS_ASSERT(X)
Definition bsls_assert.h:1804
#define BSLS_IDENT(str)
Definition bsls_ident.h:195
Definition bdldfp_decimal.h:712
Decimal_Type128 Decimal128
Definition bdldfp_decimal.h:719
Decimal_Type32 Decimal32
Definition bdldfp_decimal.h:714
Decimal_Type64 Decimal64
Definition bdldfp_decimal.h:715