decimal.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /*************************************************************************
  2. *
  3. * Licensed Materials - Property of IBM and/or HCL
  4. *
  5. * IBM Informix Dynamic Server
  6. * Copyright IBM Corporation 1996, 2012
  7. * (c) Copyright HCL Technologies Ltd. 2017. All Rights Reserved.
  8. *
  9. * Title: decimal.h
  10. * Description: Header file for decimal data type.
  11. *
  12. *************************************************************************/
  13. #ifndef _DECIMAL_H
  14. #define _DECIMAL_H
  15. #include "ifxtypes.h"
  16. #ifdef NT_MI_SAPI
  17. #ifdef NT_SERVER /* NT Server */
  18. #define MI_EXT_DECL __declspec(dllexport)
  19. #else /* NT Blade */
  20. #define MI_EXT_DECL __declspec(dllimport)
  21. #endif /* NT_SERVER */
  22. #else /* UNIX Blade & Server - Default */
  23. #define MI_EXT_DECL extern
  24. #endif /* !NT_MI_SAPI */
  25. #ifdef __cplusplus
  26. extern "C" {
  27. #endif
  28. /*
  29. * Unpacked Format (format for program usage)
  30. *
  31. * Signed exponent "dec_exp" ranging from -64 to +63
  32. * Separate sign of mantissa "dec_pos"
  33. * Base 100 digits (range 0 - 99) with decimal point
  34. * immediately to the left of first digit.
  35. */
  36. #define DECSIZE 16
  37. #define DECUNKNOWN -2
  38. struct decimal
  39. {
  40. int2 dec_exp; /* exponent base 100 */
  41. int2 dec_pos; /* sign: 1=pos, 0=neg, -1=null */
  42. int2 dec_ndgts; /* number of significant digits */
  43. char dec_dgts[DECSIZE]; /* actual digits base 100 */
  44. };
  45. typedef struct decimal dec_t;
  46. /*
  47. * A decimal null will be represented internally by setting dec_pos
  48. * equal to DECPOSNULL
  49. */
  50. #define DECPOSNULL (-1)
  51. #define DECPOSPOS +1
  52. #define DECPOSNEG 0
  53. #define DECEXPZERO -64
  54. #define DECEXPNULL 0
  55. #define DECEXPMAX +63
  56. #define DECEXPMIN -64
  57. /*
  58. * DECLEN calculates minumum number of bytes
  59. * necessary to hold a decimal(m,n)
  60. * where m = total # significant digits and
  61. * n = significant digits to right of decimal
  62. */
  63. #define DECLEN(m,n) (((m)+((n)&1)+3)/2)
  64. #define DECLENGTH(len) DECLEN(PRECTOT(len),PRECDEC(len))
  65. /*
  66. * DECPREC calculates a default precision given
  67. * number of bytes used to store number
  68. */
  69. #define DECPREC(size) (((size-1)<<9)+2)
  70. /* macros to look at and make encoded decimal precision
  71. *
  72. * PRECTOT(x) return total precision (digits total)
  73. * PRECDEC(x) return decimal precision (digits to right)
  74. * PRECMAKE(x,y) make precision from total and decimal
  75. */
  76. #define PRECTOT(x) (((x)>>8) & 0xff)
  77. #define PRECDEC(x) ((x) & 0xff)
  78. #define PRECMAKE(x,y) (((x)<<8) + (y))
  79. /* Floating point to decimal conversion.
  80. * precision 15 and 8 -> for Linux
  81. * precision 17 and 9 -> Default for all the other platforms.
  82. * precision 16 and 8 -> For clients when IFX_USE_PREC_16 is set.
  83. * precision 15 and 8 -> Which is supported by all platforms when
  84. * dotodecimal is called with OS_FLTPT_PREC
  85. */
  86. #define OS_FLTPT_PREC 3
  87. /*
  88. * Packed Format (format in records in files)
  89. *
  90. * First byte =
  91. * top 1 bit = sign 0=neg, 1=pos
  92. * low 7 bits = Exponent in excess 64 format
  93. * Rest of bytes = base 100 digits in 100 complement format
  94. * Notes -- This format sorts numerically with just a
  95. * simple byte by byte unsigned comparison.
  96. * Zero is represented as 80,00,00,... (hex).
  97. * Negative numbers have the exponent complemented
  98. * and the base 100 digits in 100's complement
  99. */
  100. /*
  101. ** DECIMALTYPE Functions
  102. */
  103. MI_EXT_DECL mint decadd(struct decimal const *n1, struct decimal const *n2, struct decimal *n3);
  104. MI_EXT_DECL mint decsub(struct decimal const *n1, struct decimal const *n2, struct decimal *n3);
  105. MI_EXT_DECL mint decmul(struct decimal const *n1, struct decimal const *n2, struct decimal *n3);
  106. MI_EXT_DECL mint decdiv(struct decimal const *n1, struct decimal const *n2, struct decimal *n3);
  107. MI_EXT_DECL mint deccmp(const struct decimal *n1, const struct decimal *n2);
  108. MI_EXT_DECL void deccopy(struct decimal *n1, struct decimal *n2);
  109. MI_EXT_DECL mint deccvasc(char *cp, mint len, struct decimal *np);
  110. MI_EXT_DECL mint deccvdbl(double dbl, struct decimal *np);
  111. MI_EXT_DECL mint deccvint(mint in, struct decimal *np);
  112. MI_EXT_DECL mint deccvlong(int4 lng, struct decimal *np);
  113. MI_EXT_DECL char *dececvt(struct decimal *np, mint ndigit, mint *decpt, mint *sign);
  114. MI_EXT_DECL char *decfcvt(struct decimal *np, mint ndigit, mint *decpt, mint *sign);
  115. MI_EXT_DECL void decround(struct decimal *np, mint dec_round);
  116. MI_EXT_DECL mint dectoasc(struct decimal *np, char *cp, mint len, mint right);
  117. MI_EXT_DECL mint dectodbl(struct decimal *np, double *dblp);
  118. MI_EXT_DECL mint dectoint(struct decimal *np, mint *ip);
  119. MI_EXT_DECL mint dectolong(struct decimal const *np, int4 *lngp);
  120. MI_EXT_DECL void dectrunc(struct decimal *np, mint trunc);
  121. MI_EXT_DECL mint deccvflt(double source, struct decimal *destination);
  122. MI_EXT_DECL mint dectoflt(struct decimal *source, float *destination);
  123. MI_EXT_DECL void stdecimal(dec_t *np, char *cp, mint len);
  124. MI_EXT_DECL mint ifx_stdecimal(dec_t *np, char *cp, mint len);
  125. MI_EXT_DECL mint lddecimal(char *cp, mint len, dec_t *np);
  126. #ifdef __cplusplus
  127. }
  128. #endif
  129. #endif /* _DECIMAL_H */