DataConverter.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /*
  2. * ________________________________________________________________________________________________________
  3. * Copyright (c) 2015-2015 InvenSense Inc. All rights reserved.
  4. *
  5. * This software, related documentation and any modifications thereto (collectively “Software”) is subject
  6. * to InvenSense and its licensors' intellectual property rights under U.S. and international copyright
  7. * and other intellectual property rights laws.
  8. *
  9. * InvenSense and its licensors retain all intellectual property and proprietary rights in and to the Software
  10. * and any use, reproduction, disclosure or distribution of the Software without an express license agreement
  11. * from InvenSense is strictly prohibited.
  12. *
  13. * EXCEPT AS OTHERWISE PROVIDED IN A LICENSE AGREEMENT BETWEEN THE PARTIES, THE SOFTWARE IS
  14. * PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  15. * TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
  16. * EXCEPT AS OTHERWISE PROVIDED IN A LICENSE AGREEMENT BETWEEN THE PARTIES, IN NO EVENT SHALL
  17. * INVENSENSE BE LIABLE FOR ANY DIRECT, SPECIAL, INDIRECT, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, OR ANY
  18. * DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
  19. * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
  20. * OF THE SOFTWARE.
  21. * ________________________________________________________________________________________________________
  22. */
  23. #include "dataconverter.h"
  24. uint8_t * inv_dc_int32_to_little8(int32_t x, uint8_t * little8)
  25. {
  26. little8[3] = (uint8_t)((x >> 24) & 0xff);
  27. little8[2] = (uint8_t)((x >> 16) & 0xff);
  28. little8[1] = (uint8_t)((x >> 8) & 0xff);
  29. little8[0] = (uint8_t)(x & 0xff);
  30. return little8;
  31. }
  32. uint8_t * inv_dc_int16_to_little8(int16_t x, uint8_t * little8)
  33. {
  34. little8[0] = (uint8_t)(x & 0xff);
  35. little8[1] = (uint8_t)((x >> 8) & 0xff);
  36. return little8;
  37. }
  38. uint8_t * inv_dc_int32_to_big8(int32_t x, uint8_t * big8)
  39. {
  40. big8[0] = (uint8_t)((x >> 24) & 0xff);
  41. big8[1] = (uint8_t)((x >> 16) & 0xff);
  42. big8[2] = (uint8_t)((x >> 8) & 0xff);
  43. big8[3] = (uint8_t)(x & 0xff);
  44. return big8;
  45. }
  46. uint8_t * inv_dc_int16_to_big8(int16_t x, uint8_t * big8)
  47. {
  48. big8[0] = (uint8_t)((x >> 8) & 0xff);
  49. big8[1] = (uint8_t)(x & 0xff);
  50. return big8;
  51. }
  52. int32_t inv_dc_little8_to_int32(const uint8_t * little8)
  53. {
  54. int32_t x = 0;
  55. x |= ((int32_t)little8[3] << 24);
  56. x |= ((int32_t)little8[2] << 16);
  57. x |= ((int32_t)little8[1] << 8);
  58. x |= ((int32_t)little8[0]);
  59. return x;
  60. }
  61. int16_t inv_dc_big16_to_int16(uint8_t * data)
  62. {
  63. int16_t result;
  64. result = (*data << 8);
  65. data++;
  66. result |= *data;
  67. return result;
  68. }
  69. int16_t inv_dc_le_to_int16(const uint8_t * little8)
  70. {
  71. uint16_t x = 0;
  72. x |= ((uint16_t)little8[0]);
  73. x |= ((uint16_t)little8[1] << 8);
  74. return (int16_t)x;
  75. }
  76. void inv_dc_sfix32_to_float(const int32_t * in, uint32_t len, uint8_t qx, float * out)
  77. {
  78. uint8_t i;
  79. for(i = 0; i < len; ++i) {
  80. out[i] = (float)in[i] / (1 << qx);
  81. }
  82. }
  83. void inv_dc_float_to_sfix32(const float * in, uint32_t len, uint8_t qx, int32_t * out)
  84. {
  85. uint8_t i;
  86. for(i = 0; i < len; ++i) {
  87. out[i] = (int32_t)((in[i] * (1 << qx)) + ((in[i] >= 0) - 0.5f));
  88. }
  89. }