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.

bgemm_beta_neon.c 3.5 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /***************************************************************************
  2. * Copyright (c) 2025, 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
  21. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  22. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  23. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  24. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  25. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  26. * POSSIBILITY OF SUCH DAMAGE.
  27. * *****************************************************************************/
  28. #include "common.h"
  29. #include <arm_neon.h>
  30. int CNAME(BLASLONG m, BLASLONG n, BLASLONG dummy1, FLOAT beta_in, IFLOAT *dummy2,
  31. BLASLONG dummy3, IFLOAT *dummy4, BLASLONG dummy5, FLOAT *c,
  32. BLASLONG ldc) {
  33. BLASLONG i, j;
  34. BLASLONG chunk, remain;
  35. bfloat16_t *ptr_c, *ptr_c0;
  36. bfloat16x8_t x0, z0;
  37. float32x4_t y0, y1;
  38. float x;
  39. bfloat16_t z;
  40. bfloat16_t zero_bf16 = vcvth_bf16_f32(0.0f);
  41. bfloat16x8_t zeros = vdupq_n_bf16(zero_bf16);
  42. bfloat16_t beta_bf16;
  43. memcpy(&beta_bf16, &beta_in, sizeof(bfloat16_t));
  44. float beta = vcvtah_f32_bf16(beta_bf16);
  45. float32x4_t beta_neon = vdupq_n_f32(beta);
  46. ptr_c = (bfloat16_t *)c;
  47. chunk = m >> 3;
  48. remain = m & 7;
  49. if (beta == 0.0f){
  50. for (j = 0; j < n; j ++){
  51. ptr_c0 = ptr_c;
  52. ptr_c += ldc;
  53. for (i = 0; i < chunk; i ++){
  54. vst1q_bf16(ptr_c0, zeros);
  55. ptr_c0 += 8;
  56. }
  57. for (i = 0; i < remain; i ++){
  58. ptr_c0[0] = zero_bf16;
  59. ptr_c0 ++;
  60. }
  61. }
  62. } else {
  63. for (j = 0; j < n; j ++){
  64. ptr_c0 = ptr_c;
  65. ptr_c += ldc;
  66. for (i = 0; i < chunk; i ++){
  67. x0 = vld1q_bf16(ptr_c0);
  68. y0 = vcvtq_low_f32_bf16(x0);
  69. y1 = vcvtq_high_f32_bf16(x0);
  70. y0 = vmulq_f32(y0, beta_neon);
  71. y1 = vmulq_f32(y1, beta_neon);
  72. z0 = vcvtq_low_bf16_f32(y0);
  73. z0 = vcvtq_high_bf16_f32(z0, y1);
  74. vst1q_bf16(ptr_c0, z0);
  75. ptr_c0 += 8;
  76. }
  77. for (i = 0; i < remain; i ++){
  78. x = vcvtah_f32_bf16(ptr_c0[0]);
  79. z = vcvth_bf16_f32(x * beta);
  80. ptr_c0[0] = z;
  81. ptr_c0 ++;
  82. }
  83. }
  84. }
  85. return 0;
  86. };