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.

tobf16.c 5.9 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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. #if defined(COOPERLAKE) || defined(SAPPHIRERAPIDS)
  36. #if defined(DOUBLE)
  37. #include "dtobf16_microk_cooperlake.c"
  38. #elif defined(SINGLE)
  39. #include "stobf16_microk_cooperlake.c"
  40. #endif
  41. #endif
  42. /* Notes for algorithm:
  43. * - Round to Nearest Even used generally
  44. * - QNAN for NAN case
  45. * - Input denormals are treated as zero
  46. */
  47. static void tobf16_generic_kernel(BLASLONG n, const FLOAT_TYPE * in, BLASLONG inc_in, bfloat16 * out, BLASLONG inc_out)
  48. {
  49. BLASLONG register index_in = 0;
  50. BLASLONG register index_out = 0;
  51. BLASLONG register index = 0;
  52. float float_in = 0.0;
  53. uint32_t * uint32_in = (uint32_t *)(&float_in);
  54. uint16_t * uint16_in = (uint16_t *)(&float_in);
  55. while(index<n) {
  56. #if defined(DOUBLE)
  57. float_in = (float)(*(in+index_in));
  58. #else
  59. float_in = *(in+index_in);
  60. #endif
  61. switch((*uint32_in) & 0xff800000u) {
  62. case (0x00000000u): /* Type 1: Positive denormal */
  63. *(out+index_out) = 0x0000u;
  64. break;
  65. case (0x80000000u): /* Type 2: Negative denormal */
  66. *(out+index_out) = 0x8000u;
  67. break;
  68. case (0x7f800000u): /* Type 3: Positive infinity or NAN */
  69. case (0xff800000u): /* Type 4: Negative infinity or NAN */
  70. #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
  71. *(out+index_out) = uint16_in[1];
  72. #else
  73. *(out+index_out) = uint16_in[0];
  74. #endif
  75. /* Specific for NAN */
  76. if (((*uint32_in) & 0x007fffffu) != 0) {
  77. /* Force to be QNAN */
  78. *(out+index_out) |= 0x0040u;
  79. }
  80. break;
  81. default: /* Type 5: Normal case */
  82. (*uint32_in) += ((((*uint32_in) >> 16) & 0x1u) + 0x7fffu);
  83. #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
  84. *(out+index_out) = uint16_in[1];
  85. #else
  86. *(out+index_out) = uint16_in[0];
  87. #endif
  88. break;
  89. }
  90. index_in += inc_in;
  91. index_out += inc_out;
  92. index++;
  93. }
  94. }
  95. #ifndef HAVE_TOBF16_ACCL_KERNEL
  96. static void tobf16_accl_kernel(BLASLONG n, const FLOAT_TYPE * in, bfloat16 * out)
  97. {
  98. tobf16_generic_kernel(n, in, 1, out, 1);
  99. }
  100. #endif
  101. static void tobf16_compute(BLASLONG n, FLOAT_TYPE * in, BLASLONG inc_in, bfloat16 * out, BLASLONG inc_out)
  102. {
  103. if ((inc_in == 1) && (inc_out == 1)) {
  104. tobf16_accl_kernel(n, in, out);
  105. } else {
  106. tobf16_generic_kernel(n, in, inc_in, out, inc_out);
  107. }
  108. }
  109. #if defined(SMP)
  110. static int tobf16_thread_func(BLASLONG n, BLASLONG dummy0, BLASLONG dummy1, FLOAT_TYPE dummy2,
  111. FLOAT_TYPE *x, BLASLONG inc_x, bfloat16 *y, BLASLONG inc_y,
  112. FLOAT_TYPE *dummy3, BLASLONG dummy4)
  113. {
  114. tobf16_compute(n, x, inc_x, y, inc_y);
  115. return 0;
  116. }
  117. extern int blas_level1_thread(int mode, BLASLONG m, BLASLONG n, BLASLONG k, void *alpha,
  118. void *a, BLASLONG lda, void *b, BLASLONG ldb, void *c, BLASLONG ldc,
  119. int (*function)(), int nthreads);
  120. #endif
  121. void CNAME(BLASLONG n, FLOAT_TYPE * in, BLASLONG inc_in, bfloat16 * out, BLASLONG inc_out)
  122. {
  123. if (n <= 0) return;
  124. #if defined(SMP)
  125. int nthreads;
  126. FLOAT_TYPE dummy_alpha;
  127. FLOAT_TYPE dummy_c;
  128. #endif
  129. #if defined(SMP)
  130. if (inc_in == 0 || inc_out == 0 || n <= 100000) {
  131. nthreads = 1;
  132. } else {
  133. nthreads = num_cpu_avail(1);
  134. if (n/100000 < 100) {
  135. nthreads = MAX(nthreads,4);
  136. // } else {
  137. // nthreads = MAX(nthreads,16);
  138. }
  139. }
  140. if (nthreads == 1) {
  141. tobf16_compute(n, in, inc_in, out, inc_out);
  142. } else {
  143. #if defined(DOUBLE)
  144. int mode = BLAS_REAL | BLAS_DTOBF16;
  145. #elif defined(SINGLE)
  146. int mode = BLAS_REAL | BLAS_STOBF16;
  147. #endif
  148. blas_level1_thread(mode, n, 0, 0, &dummy_alpha,
  149. in, inc_in, out, inc_out, &dummy_c, 0,
  150. (void *)tobf16_thread_func, nthreads);
  151. }
  152. #else
  153. tobf16_compute(n, in, inc_in, out, inc_out);
  154. #endif
  155. }