qs_fp.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /**
  2. * @file
  3. * @brief QS floating point output implementation
  4. * @ingroup qs
  5. * @cond
  6. ******************************************************************************
  7. * Last updated for version 5.3.0
  8. * Last updated on 2014-03-27
  9. *
  10. * Q u a n t u m L e a P s
  11. * ---------------------------
  12. * innovating embedded systems
  13. *
  14. * Copyright (C) Quantum Leaps, www.state-machine.com.
  15. *
  16. * This program is open source software: you can redistribute it and/or
  17. * modify it under the terms of the GNU General Public License as published
  18. * by the Free Software Foundation, either version 3 of the License, or
  19. * (at your option) any later version.
  20. *
  21. * Alternatively, this program may be distributed and modified under the
  22. * terms of Quantum Leaps commercial licenses, which expressly supersede
  23. * the GNU General Public License and are specifically designed for
  24. * licensees interested in retaining the proprietary status of their code.
  25. *
  26. * This program is distributed in the hope that it will be useful,
  27. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  28. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  29. * GNU General Public License for more details.
  30. *
  31. * You should have received a copy of the GNU General Public License
  32. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  33. *
  34. * Contact information:
  35. * Web: www.state-machine.com
  36. * Email: info@state-machine.com
  37. ******************************************************************************
  38. * @endcond
  39. */
  40. #include "qs_port.h" /* QS port */
  41. #include "qs_pkg.h"
  42. /****************************************************************************/
  43. /**
  44. * @note This function is only to be used through macros, never in the
  45. * client code directly.
  46. */
  47. void QS_f32(uint8_t format, float32_t f) {
  48. union F32Rep {
  49. float32_t f;
  50. uint32_t u;
  51. } fu32; /* the internal binary representation */
  52. uint8_t chksum = QS_priv_.chksum; /* put in a temporary (register) */
  53. uint8_t *buf = QS_priv_.buf; /* put in a temporary (register) */
  54. QSCtr head = QS_priv_.head; /* put in a temporary (register) */
  55. QSCtr end = QS_priv_.end; /* put in a temporary (register) */
  56. int_fast8_t i;
  57. fu32.f = f; /* assign the binary representation */
  58. QS_priv_.used += (QSCtr)5; /* 5 bytes about to be added */
  59. QS_INSERT_ESC_BYTE(format) /* insert the format byte */
  60. /* insert 4 bytes... */
  61. for (i = (int_fast8_t)4; i != (int_fast8_t)0; --i) {
  62. format = (uint8_t)fu32.u;
  63. QS_INSERT_ESC_BYTE(format)
  64. fu32.u >>= 8;
  65. }
  66. QS_priv_.head = head; /* save the head */
  67. QS_priv_.chksum = chksum; /* save the checksum */
  68. }
  69. /****************************************************************************/
  70. /**
  71. * @description
  72. * This function is only to be used through macros, never in the
  73. * client code directly.
  74. */
  75. void QS_f64(uint8_t format, float64_t d) {
  76. union F64Rep {
  77. float64_t d;
  78. struct UInt2 {
  79. uint32_t u1;
  80. uint32_t u2;
  81. } i;
  82. } fu64; /* the internal binary representation */
  83. uint8_t chksum = QS_priv_.chksum;
  84. uint8_t *buf = QS_priv_.buf;
  85. QSCtr head = QS_priv_.head;
  86. QSCtr end = QS_priv_.end;
  87. int_fast8_t i;
  88. fu64.d = d; /* assign the binary representation */
  89. QS_priv_.used += (QSCtr)9; /* 9 bytes about to be added */
  90. QS_INSERT_ESC_BYTE(format) /* insert the format byte */
  91. /* output 4 bytes from fu64.i.u1 ... */
  92. for (i = (int_fast8_t)4; i != (int_fast8_t)0; --i) {
  93. format = (uint8_t)fu64.i.u1;
  94. QS_INSERT_ESC_BYTE(format)
  95. fu64.i.u1 >>= 8;
  96. }
  97. /* output 4 bytes from fu64.i.u2 ... */
  98. for (i = (int_fast8_t)4; i != (int_fast8_t)0; --i) {
  99. format = (uint8_t)fu64.i.u2;
  100. QS_INSERT_ESC_BYTE(format)
  101. fu64.i.u2 >>= 8;
  102. }
  103. QS_priv_.head = head; /* save the head */
  104. QS_priv_.chksum = chksum; /* save the checksum */
  105. }