开发者

Different sized structs for decNumberCopy() in libdecnumber?

I'm working with libdecnumber in a project and there's a peculiar little function in the library whose documentation has me a bit confused. The function is decNumberCopy()

decNumber  * decNumberCopy(decNumber *, const decNumber *);

Here's the definition of a decNumber

/* The size (integer data type) of each unit is determined by the   */
/* number of digits it will hold.                                   */
#if   DECDPUN<=2
  #define decNumberUnit uint8_t
#elif DECDPUN<=4
  #define decNumberUnit uint16_t
#else
  #define decNumberUnit uint32_t
#endif
/* The number of units needed is ceil(DECNUMDIGITS/DECDPUN)         */
#define DECNUMUNITS ((DECNUMDIGITS+DECDPUN-1)/DECDPUN)

/* The data structure... */
typedef struct {
  int32_t digits;      /* Count of digits in the coefficient; >0    */
  int32_t exponent;    /* Unadjusted exponent, unbiased, in         */
                       /* range: -1999999997 through 999999999      */
  uint8_t bits;        /* Indicator bits (see above)                */
                       /* Coefficient, from least significant unit  */
  decNumberUnit lsu[DECNUMUNITS];
} decNumber;

Here's what the libdecnumber documentation has to say about decNumberCopy()

decNumberCopy(number, source)

This function is used to copy the content of one decNumber structure to another. It is used when the structures may be of different sizes and hence a straightforward structure copy by C assignment is inappropriate. It also may have performance benefits when the number is short relative to the size of the structure, as only the units containing the digits in use in the source structure are copied.

So leaving aside the fact that simple structu开发者_JAVA百科re assignment would suffice for copying a decNumber, can anyone with more insight into how libdecnumber works tell me when the two decNumber structures would be a different size? Even assuming that you had brought together two different compilation modules that had different values for DECNUMUNITS (which is really a terrible thing to do), there's no size member in decNumber, so the function wouldn't be able to detect different size structs anyways.

Or, is it perhaps that decNumberCopy() is generalized for a future library version where individual decNumbers would be allowed to vary in size?


I'd guess that this comment from decNumber.h should explain things:

  /* Notes: */
  /* 1. If digits is > DECDPUN then there will be more than one */
  /*    decNumberUnits immediately following the first element of lsu. */
  /*    These contain the remaining (more significant) digits of the */
  /*    number, and may be in the lsu array, or may be guaranteed by */
  /*    some other mechanism (such as being contained in another */
  /*    structure, or being overlaid on dynamically allocated storage). */

In other words, the decNumber structure contains the decNumberUnit lsu[DECNUMUNITS] array to hold a 'default' number of digits, but the library will grow the array beyond that size if necessary.

Update:

A quick look at decNumber.c (I don't know how close it is to latest though) looks as if they haven't implemented any support for storing digits beyond what can be held in decNumber.lsu[DECNUMUNITS]. It looks like decNumberCopy() is more concerned with copying only the used lsu[DECNUMUNITS] elements instead of the whole thing (as an optimization and/or to allow testing to detect possible corruption of data).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜