gsl_cblas__source_gemm_c.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /* blas/source_gemm_c.h
  2. *
  3. * Copyright (C) 2001, 2007 Brian Gough
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 3 of the License, or (at
  8. * your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  18. */
  19. {
  20. INDEX i, j, k;
  21. INDEX n1, n2;
  22. INDEX ldf, ldg;
  23. int conjF, conjG, TransF, TransG;
  24. const BASE *F, *G;
  25. const BASE alpha_real = CONST_REAL0(alpha);
  26. const BASE alpha_imag = CONST_IMAG0(alpha);
  27. const BASE beta_real = CONST_REAL0(beta);
  28. const BASE beta_imag = CONST_IMAG0(beta);
  29. if ((alpha_real == 0.0 && alpha_imag == 0.0)
  30. && (beta_real == 1.0 && beta_imag == 0.0))
  31. return;
  32. if (Order == CblasRowMajor) {
  33. n1 = M;
  34. n2 = N;
  35. F = (const BASE *)A;
  36. ldf = lda;
  37. conjF = (TransA == CblasConjTrans) ? -1 : 1;
  38. TransF = (TransA == CblasNoTrans) ? CblasNoTrans : CblasTrans;
  39. G = (const BASE *)B;
  40. ldg = ldb;
  41. conjG = (TransB == CblasConjTrans) ? -1 : 1;
  42. TransG = (TransB == CblasNoTrans) ? CblasNoTrans : CblasTrans;
  43. } else {
  44. n1 = N;
  45. n2 = M;
  46. F = (const BASE *)B;
  47. ldf = ldb;
  48. conjF = (TransB == CblasConjTrans) ? -1 : 1;
  49. TransF = (TransB == CblasNoTrans) ? CblasNoTrans : CblasTrans;
  50. G = (const BASE *)A;
  51. ldg = lda;
  52. conjG = (TransA == CblasConjTrans) ? -1 : 1;
  53. TransG = (TransA == CblasNoTrans) ? CblasNoTrans : CblasTrans;
  54. }
  55. /* form y := beta*y */
  56. if (beta_real == 0.0 && beta_imag == 0.0) {
  57. for (i = 0; i < n1; i++) {
  58. for (j = 0; j < n2; j++) {
  59. REAL(C, ldc * i + j) = 0.0;
  60. IMAG(C, ldc * i + j) = 0.0;
  61. }
  62. }
  63. } else if (!(beta_real == 1.0 && beta_imag == 0.0)) {
  64. for (i = 0; i < n1; i++) {
  65. for (j = 0; j < n2; j++) {
  66. const BASE Cij_real = REAL(C, ldc * i + j);
  67. const BASE Cij_imag = IMAG(C, ldc * i + j);
  68. REAL(C, ldc * i + j) = beta_real * Cij_real - beta_imag * Cij_imag;
  69. IMAG(C, ldc * i + j) = beta_real * Cij_imag + beta_imag * Cij_real;
  70. }
  71. }
  72. }
  73. if (alpha_real == 0.0 && alpha_imag == 0.0)
  74. return;
  75. if (TransF == CblasNoTrans && TransG == CblasNoTrans) {
  76. /* form C := alpha*A*B + C */
  77. for (k = 0; k < K; k++) {
  78. for (i = 0; i < n1; i++) {
  79. const BASE Fik_real = CONST_REAL(F, ldf * i + k);
  80. const BASE Fik_imag = conjF * CONST_IMAG(F, ldf * i + k);
  81. const BASE temp_real = alpha_real * Fik_real - alpha_imag * Fik_imag;
  82. const BASE temp_imag = alpha_real * Fik_imag + alpha_imag * Fik_real;
  83. if (!(temp_real == 0.0 && temp_imag == 0.0)) {
  84. for (j = 0; j < n2; j++) {
  85. const BASE Gkj_real = CONST_REAL(G, ldg * k + j);
  86. const BASE Gkj_imag = conjG * CONST_IMAG(G, ldg * k + j);
  87. REAL(C, ldc * i + j) += temp_real * Gkj_real - temp_imag * Gkj_imag;
  88. IMAG(C, ldc * i + j) += temp_real * Gkj_imag + temp_imag * Gkj_real;
  89. }
  90. }
  91. }
  92. }
  93. } else if (TransF == CblasNoTrans && TransG == CblasTrans) {
  94. /* form C := alpha*A*B' + C */
  95. for (i = 0; i < n1; i++) {
  96. for (j = 0; j < n2; j++) {
  97. BASE temp_real = 0.0;
  98. BASE temp_imag = 0.0;
  99. for (k = 0; k < K; k++) {
  100. const BASE Fik_real = CONST_REAL(F, ldf * i + k);
  101. const BASE Fik_imag = conjF * CONST_IMAG(F, ldf * i + k);
  102. const BASE Gjk_real = CONST_REAL(G, ldg * j + k);
  103. const BASE Gjk_imag = conjG * CONST_IMAG(G, ldg * j + k);
  104. temp_real += Fik_real * Gjk_real - Fik_imag * Gjk_imag;
  105. temp_imag += Fik_real * Gjk_imag + Fik_imag * Gjk_real;
  106. }
  107. REAL(C, ldc * i + j) += alpha_real * temp_real - alpha_imag * temp_imag;
  108. IMAG(C, ldc * i + j) += alpha_real * temp_imag + alpha_imag * temp_real;
  109. }
  110. }
  111. } else if (TransF == CblasTrans && TransG == CblasNoTrans) {
  112. for (k = 0; k < K; k++) {
  113. for (i = 0; i < n1; i++) {
  114. const BASE Fki_real = CONST_REAL(F, ldf * k + i);
  115. const BASE Fki_imag = conjF * CONST_IMAG(F, ldf * k + i);
  116. const BASE temp_real = alpha_real * Fki_real - alpha_imag * Fki_imag;
  117. const BASE temp_imag = alpha_real * Fki_imag + alpha_imag * Fki_real;
  118. if (!(temp_real == 0.0 && temp_imag == 0.0)) {
  119. for (j = 0; j < n2; j++) {
  120. const BASE Gkj_real = CONST_REAL(G, ldg * k + j);
  121. const BASE Gkj_imag = conjG * CONST_IMAG(G, ldg * k + j);
  122. REAL(C, ldc * i + j) += temp_real * Gkj_real - temp_imag * Gkj_imag;
  123. IMAG(C, ldc * i + j) += temp_real * Gkj_imag + temp_imag * Gkj_real;
  124. }
  125. }
  126. }
  127. }
  128. } else if (TransF == CblasTrans && TransG == CblasTrans) {
  129. for (i = 0; i < n1; i++) {
  130. for (j = 0; j < n2; j++) {
  131. BASE temp_real = 0.0;
  132. BASE temp_imag = 0.0;
  133. for (k = 0; k < K; k++) {
  134. const BASE Fki_real = CONST_REAL(F, ldf * k + i);
  135. const BASE Fki_imag = conjF * CONST_IMAG(F, ldf * k + i);
  136. const BASE Gjk_real = CONST_REAL(G, ldg * j + k);
  137. const BASE Gjk_imag = conjG * CONST_IMAG(G, ldg * j + k);
  138. temp_real += Fki_real * Gjk_real - Fki_imag * Gjk_imag;
  139. temp_imag += Fki_real * Gjk_imag + Fki_imag * Gjk_real;
  140. }
  141. REAL(C, ldc * i + j) += alpha_real * temp_real - alpha_imag * temp_imag;
  142. IMAG(C, ldc * i + j) += alpha_real * temp_imag + alpha_imag * temp_real;
  143. }
  144. }
  145. } else {
  146. BLAS_ERROR("unrecognized operation");
  147. }
  148. }