You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

bf16to.c 4.0 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /***************************************************************************
  2. Copyright (c) 2014, The OpenBLAS Project
  3. All rights reserved.
  4. Redistribution and use in source and binary forms, with or without
  5. modification, are permitted provided that the following conditions are
  6. met:
  7. 1. Redistributions of source code must retain the above copyright
  8. notice, this list of conditions and the following disclaimer.
  9. 2. Redistributions in binary form must reproduce the above copyright
  10. notice, this list of conditions and the following disclaimer in
  11. the documentation and/or other materials provided with the
  12. distribution.
  13. 3. Neither the name of the OpenBLAS project nor the names of
  14. its contributors may be used to endorse or promote products
  15. derived from this software without specific prior written permission.
  16. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  17. AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  18. IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  19. ARE DISCLAIMED. IN NO EVENT SHALL THE OPENBLAS PROJECT OR CONTRIBUTORS BE
  20. LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  21. DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  22. SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  23. CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  24. OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
  25. USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. *****************************************************************************/
  27. #include <stddef.h>
  28. #include "common.h"
  29. #if defined(DOUBLE)
  30. #define FLOAT_TYPE double
  31. #elif defined(SINGLE)
  32. #define FLOAT_TYPE float
  33. #else
  34. #endif
  35. /* Notes for algorithm:
  36. * - Input denormal treated as zero
  37. * - Force to be QNAN
  38. */
  39. static void bf16to_kernel_1(BLASLONG n, const bfloat16 * in, BLASLONG inc_in, FLOAT_TYPE * out, BLASLONG inc_out)
  40. {
  41. BLASLONG register index_in = 0;
  42. BLASLONG register index_out = 0;
  43. BLASLONG register index = 0;
  44. uint16_t * tmp = NULL;
  45. #if defined(DOUBLE)
  46. float float_out = 0.0;
  47. #endif
  48. while(index<n) {
  49. #if defined(DOUBLE)
  50. float_out = 0.0;
  51. tmp = (uint16_t*)(&float_out);
  52. #else
  53. *(out+index_out) = 0;
  54. tmp = (uint16_t*)(out+index_out);
  55. #endif
  56. switch((*(in+index_in)) & 0xff80u) {
  57. case (0x0000u): /* Type 1: Positive denormal */
  58. tmp[1] = 0x0000u;
  59. tmp[0] = 0x0000u;
  60. break;
  61. case (0x8000u): /* Type 2: Negative denormal */
  62. #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
  63. tmp[1] = 0x8000u;
  64. tmp[0] = 0x0000u;
  65. #else
  66. tmp[1] = 0x0000u;
  67. tmp[0] = 0x8000u;
  68. #endif
  69. break;
  70. case (0x7f80u): /* Type 3: Positive infinity or NAN */
  71. case (0xff80u): /* Type 4: Negative infinity or NAN */
  72. #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
  73. tmp[1] = *(in+index_in);
  74. #else
  75. tmp[0] = *(in+index_in);
  76. #endif
  77. /* Specific for NAN */
  78. if (((*(in+index_in)) & 0x007fu) != 0) {
  79. /* Force to be QNAN */
  80. #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
  81. tmp[1] |= 0x0040u;
  82. #else
  83. tmp[0] |= 0x0040u;
  84. #endif
  85. }
  86. break;
  87. default: /* Type 5: Normal case */
  88. #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
  89. tmp[1] = *(in+index_in);
  90. #else
  91. tmp[0] = *(in+index_in);
  92. #endif
  93. break;
  94. }
  95. #if defined(DOUBLE)
  96. *(out+index_out) = (double)float_out;
  97. #endif
  98. index_in += inc_in;
  99. index_out += inc_out;
  100. index++;
  101. }
  102. }
  103. void CNAME(BLASLONG n, bfloat16 * in, BLASLONG inc_in, FLOAT_TYPE * out, BLASLONG inc_out)
  104. {
  105. if (n <= 0) return;
  106. bf16to_kernel_1(n, in, inc_in, out, inc_out);
  107. }