sgemm.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  1. /* sgemm.f -- translated by f2c (version 20061008).
  2. You must link the resulting object file with libf2c:
  3. on Microsoft Windows system, link with libf2c.lib;
  4. on Linux or Unix systems, link with .../path/to/libf2c.a -lm
  5. or, if you install libf2c.a in a standard place, with -lf2c -lm
  6. -- in that order, at the end of the command line, as in
  7. cc *.o -lf2c -lm
  8. Source for libf2c is in /netlib/f2c/libf2c.zip, e.g.,
  9. http://www.netlib.org/f2c/libf2c.zip
  10. */
  11. #include "f2c.h"
  12. #include "blaswrap.h"
  13. /* Subroutine */ int sgemm_(char *transa, char *transb, integer *m, integer *
  14. n, integer *k, real *alpha, real *a, integer *lda, real *b, integer *
  15. ldb, real *beta, real *c__, integer *ldc)
  16. {
  17. /* System generated locals */
  18. integer a_dim1, a_offset, b_dim1, b_offset, c_dim1, c_offset, i__1, i__2,
  19. i__3;
  20. /* Local variables */
  21. integer i__, j, l, info;
  22. logical nota, notb;
  23. real temp;
  24. integer ncola;
  25. extern logical lsame_(char *, char *);
  26. integer nrowa, nrowb;
  27. extern /* Subroutine */ int xerbla_(char *, integer *);
  28. /* .. Scalar Arguments .. */
  29. /* .. */
  30. /* .. Array Arguments .. */
  31. /* .. */
  32. /* Purpose */
  33. /* ======= */
  34. /* SGEMM performs one of the matrix-matrix operations */
  35. /* C := alpha*op( A )*op( B ) + beta*C, */
  36. /* where op( X ) is one of */
  37. /* op( X ) = X or op( X ) = X', */
  38. /* alpha and beta are scalars, and A, B and C are matrices, with op( A ) */
  39. /* an m by k matrix, op( B ) a k by n matrix and C an m by n matrix. */
  40. /* Arguments */
  41. /* ========== */
  42. /* TRANSA - CHARACTER*1. */
  43. /* On entry, TRANSA specifies the form of op( A ) to be used in */
  44. /* the matrix multiplication as follows: */
  45. /* TRANSA = 'N' or 'n', op( A ) = A. */
  46. /* TRANSA = 'T' or 't', op( A ) = A'. */
  47. /* TRANSA = 'C' or 'c', op( A ) = A'. */
  48. /* Unchanged on exit. */
  49. /* TRANSB - CHARACTER*1. */
  50. /* On entry, TRANSB specifies the form of op( B ) to be used in */
  51. /* the matrix multiplication as follows: */
  52. /* TRANSB = 'N' or 'n', op( B ) = B. */
  53. /* TRANSB = 'T' or 't', op( B ) = B'. */
  54. /* TRANSB = 'C' or 'c', op( B ) = B'. */
  55. /* Unchanged on exit. */
  56. /* M - INTEGER. */
  57. /* On entry, M specifies the number of rows of the matrix */
  58. /* op( A ) and of the matrix C. M must be at least zero. */
  59. /* Unchanged on exit. */
  60. /* N - INTEGER. */
  61. /* On entry, N specifies the number of columns of the matrix */
  62. /* op( B ) and the number of columns of the matrix C. N must be */
  63. /* at least zero. */
  64. /* Unchanged on exit. */
  65. /* K - INTEGER. */
  66. /* On entry, K specifies the number of columns of the matrix */
  67. /* op( A ) and the number of rows of the matrix op( B ). K must */
  68. /* be at least zero. */
  69. /* Unchanged on exit. */
  70. /* ALPHA - REAL . */
  71. /* On entry, ALPHA specifies the scalar alpha. */
  72. /* Unchanged on exit. */
  73. /* A - REAL array of DIMENSION ( LDA, ka ), where ka is */
  74. /* k when TRANSA = 'N' or 'n', and is m otherwise. */
  75. /* Before entry with TRANSA = 'N' or 'n', the leading m by k */
  76. /* part of the array A must contain the matrix A, otherwise */
  77. /* the leading k by m part of the array A must contain the */
  78. /* matrix A. */
  79. /* Unchanged on exit. */
  80. /* LDA - INTEGER. */
  81. /* On entry, LDA specifies the first dimension of A as declared */
  82. /* in the calling (sub) program. When TRANSA = 'N' or 'n' then */
  83. /* LDA must be at least max( 1, m ), otherwise LDA must be at */
  84. /* least max( 1, k ). */
  85. /* Unchanged on exit. */
  86. /* B - REAL array of DIMENSION ( LDB, kb ), where kb is */
  87. /* n when TRANSB = 'N' or 'n', and is k otherwise. */
  88. /* Before entry with TRANSB = 'N' or 'n', the leading k by n */
  89. /* part of the array B must contain the matrix B, otherwise */
  90. /* the leading n by k part of the array B must contain the */
  91. /* matrix B. */
  92. /* Unchanged on exit. */
  93. /* LDB - INTEGER. */
  94. /* On entry, LDB specifies the first dimension of B as declared */
  95. /* in the calling (sub) program. When TRANSB = 'N' or 'n' then */
  96. /* LDB must be at least max( 1, k ), otherwise LDB must be at */
  97. /* least max( 1, n ). */
  98. /* Unchanged on exit. */
  99. /* BETA - REAL . */
  100. /* On entry, BETA specifies the scalar beta. When BETA is */
  101. /* supplied as zero then C need not be set on input. */
  102. /* Unchanged on exit. */
  103. /* C - REAL array of DIMENSION ( LDC, n ). */
  104. /* Before entry, the leading m by n part of the array C must */
  105. /* contain the matrix C, except when beta is zero, in which */
  106. /* case C need not be set on entry. */
  107. /* On exit, the array C is overwritten by the m by n matrix */
  108. /* ( alpha*op( A )*op( B ) + beta*C ). */
  109. /* LDC - INTEGER. */
  110. /* On entry, LDC specifies the first dimension of C as declared */
  111. /* in the calling (sub) program. LDC must be at least */
  112. /* max( 1, m ). */
  113. /* Unchanged on exit. */
  114. /* Level 3 Blas routine. */
  115. /* -- Written on 8-February-1989. */
  116. /* Jack Dongarra, Argonne National Laboratory. */
  117. /* Iain Duff, AERE Harwell. */
  118. /* Jeremy Du Croz, Numerical Algorithms Group Ltd. */
  119. /* Sven Hammarling, Numerical Algorithms Group Ltd. */
  120. /* .. External Functions .. */
  121. /* .. */
  122. /* .. External Subroutines .. */
  123. /* .. */
  124. /* .. Intrinsic Functions .. */
  125. /* .. */
  126. /* .. Local Scalars .. */
  127. /* .. */
  128. /* .. Parameters .. */
  129. /* .. */
  130. /* Set NOTA and NOTB as true if A and B respectively are not */
  131. /* transposed and set NROWA, NCOLA and NROWB as the number of rows */
  132. /* and columns of A and the number of rows of B respectively. */
  133. /* Parameter adjustments */
  134. a_dim1 = *lda;
  135. a_offset = 1 + a_dim1;
  136. a -= a_offset;
  137. b_dim1 = *ldb;
  138. b_offset = 1 + b_dim1;
  139. b -= b_offset;
  140. c_dim1 = *ldc;
  141. c_offset = 1 + c_dim1;
  142. c__ -= c_offset;
  143. /* Function Body */
  144. nota = lsame_(transa, "N");
  145. notb = lsame_(transb, "N");
  146. if (nota) {
  147. nrowa = *m;
  148. ncola = *k;
  149. } else {
  150. nrowa = *k;
  151. ncola = *m;
  152. }
  153. if (notb) {
  154. nrowb = *k;
  155. } else {
  156. nrowb = *n;
  157. }
  158. /* Test the input parameters. */
  159. info = 0;
  160. if (! nota && ! lsame_(transa, "C") && ! lsame_(
  161. transa, "T")) {
  162. info = 1;
  163. } else if (! notb && ! lsame_(transb, "C") && !
  164. lsame_(transb, "T")) {
  165. info = 2;
  166. } else if (*m < 0) {
  167. info = 3;
  168. } else if (*n < 0) {
  169. info = 4;
  170. } else if (*k < 0) {
  171. info = 5;
  172. } else if (*lda < max(1,nrowa)) {
  173. info = 8;
  174. } else if (*ldb < max(1,nrowb)) {
  175. info = 10;
  176. } else if (*ldc < max(1,*m)) {
  177. info = 13;
  178. }
  179. if (info != 0) {
  180. xerbla_("SGEMM ", &info);
  181. return 0;
  182. }
  183. /* Quick return if possible. */
  184. if (*m == 0 || *n == 0 || (*alpha == 0.f || *k == 0) && *beta == 1.f) {
  185. return 0;
  186. }
  187. /* And if alpha.eq.zero. */
  188. if (*alpha == 0.f) {
  189. if (*beta == 0.f) {
  190. i__1 = *n;
  191. for (j = 1; j <= i__1; ++j) {
  192. i__2 = *m;
  193. for (i__ = 1; i__ <= i__2; ++i__) {
  194. c__[i__ + j * c_dim1] = 0.f;
  195. /* L10: */
  196. }
  197. /* L20: */
  198. }
  199. } else {
  200. i__1 = *n;
  201. for (j = 1; j <= i__1; ++j) {
  202. i__2 = *m;
  203. for (i__ = 1; i__ <= i__2; ++i__) {
  204. c__[i__ + j * c_dim1] = *beta * c__[i__ + j * c_dim1];
  205. /* L30: */
  206. }
  207. /* L40: */
  208. }
  209. }
  210. return 0;
  211. }
  212. /* Start the operations. */
  213. if (notb) {
  214. if (nota) {
  215. /* Form C := alpha*A*B + beta*C. */
  216. i__1 = *n;
  217. for (j = 1; j <= i__1; ++j) {
  218. if (*beta == 0.f) {
  219. i__2 = *m;
  220. for (i__ = 1; i__ <= i__2; ++i__) {
  221. c__[i__ + j * c_dim1] = 0.f;
  222. /* L50: */
  223. }
  224. } else if (*beta != 1.f) {
  225. i__2 = *m;
  226. for (i__ = 1; i__ <= i__2; ++i__) {
  227. c__[i__ + j * c_dim1] = *beta * c__[i__ + j * c_dim1];
  228. /* L60: */
  229. }
  230. }
  231. i__2 = *k;
  232. for (l = 1; l <= i__2; ++l) {
  233. if (b[l + j * b_dim1] != 0.f) {
  234. temp = *alpha * b[l + j * b_dim1];
  235. i__3 = *m;
  236. for (i__ = 1; i__ <= i__3; ++i__) {
  237. c__[i__ + j * c_dim1] += temp * a[i__ + l *
  238. a_dim1];
  239. /* L70: */
  240. }
  241. }
  242. /* L80: */
  243. }
  244. /* L90: */
  245. }
  246. } else {
  247. /* Form C := alpha*A'*B + beta*C */
  248. i__1 = *n;
  249. for (j = 1; j <= i__1; ++j) {
  250. i__2 = *m;
  251. for (i__ = 1; i__ <= i__2; ++i__) {
  252. temp = 0.f;
  253. i__3 = *k;
  254. for (l = 1; l <= i__3; ++l) {
  255. temp += a[l + i__ * a_dim1] * b[l + j * b_dim1];
  256. /* L100: */
  257. }
  258. if (*beta == 0.f) {
  259. c__[i__ + j * c_dim1] = *alpha * temp;
  260. } else {
  261. c__[i__ + j * c_dim1] = *alpha * temp + *beta * c__[
  262. i__ + j * c_dim1];
  263. }
  264. /* L110: */
  265. }
  266. /* L120: */
  267. }
  268. }
  269. } else {
  270. if (nota) {
  271. /* Form C := alpha*A*B' + beta*C */
  272. i__1 = *n;
  273. for (j = 1; j <= i__1; ++j) {
  274. if (*beta == 0.f) {
  275. i__2 = *m;
  276. for (i__ = 1; i__ <= i__2; ++i__) {
  277. c__[i__ + j * c_dim1] = 0.f;
  278. /* L130: */
  279. }
  280. } else if (*beta != 1.f) {
  281. i__2 = *m;
  282. for (i__ = 1; i__ <= i__2; ++i__) {
  283. c__[i__ + j * c_dim1] = *beta * c__[i__ + j * c_dim1];
  284. /* L140: */
  285. }
  286. }
  287. i__2 = *k;
  288. for (l = 1; l <= i__2; ++l) {
  289. if (b[j + l * b_dim1] != 0.f) {
  290. temp = *alpha * b[j + l * b_dim1];
  291. i__3 = *m;
  292. for (i__ = 1; i__ <= i__3; ++i__) {
  293. c__[i__ + j * c_dim1] += temp * a[i__ + l *
  294. a_dim1];
  295. /* L150: */
  296. }
  297. }
  298. /* L160: */
  299. }
  300. /* L170: */
  301. }
  302. } else {
  303. /* Form C := alpha*A'*B' + beta*C */
  304. i__1 = *n;
  305. for (j = 1; j <= i__1; ++j) {
  306. i__2 = *m;
  307. for (i__ = 1; i__ <= i__2; ++i__) {
  308. temp = 0.f;
  309. i__3 = *k;
  310. for (l = 1; l <= i__3; ++l) {
  311. temp += a[l + i__ * a_dim1] * b[j + l * b_dim1];
  312. /* L180: */
  313. }
  314. if (*beta == 0.f) {
  315. c__[i__ + j * c_dim1] = *alpha * temp;
  316. } else {
  317. c__[i__ + j * c_dim1] = *alpha * temp + *beta * c__[
  318. i__ + j * c_dim1];
  319. }
  320. /* L190: */
  321. }
  322. /* L200: */
  323. }
  324. }
  325. }
  326. return 0;
  327. /* End of SGEMM . */
  328. } /* sgemm_ */