home *** CD-ROM | disk | FTP | other *** search
/ ARM Club 1 / ARM_CLUB_CD.iso / contents / apps / clib / progs / meschach / !Meschach / c / extras < prev    next >
Encoding:
Text File  |  1994-05-18  |  10.7 KB  |  293 lines

  1.  
  2. /**************************************************************************
  3. **
  4. ** Copyright (C) 1993 David E. Steward & Zbigniew Leyk, all rights reserved.
  5. **
  6. **                 Meschach Library
  7. ** 
  8. ** This Meschach Library is provided "as is" without any express 
  9. ** or implied warranty of any kind with respect to this software. 
  10. ** In particular the authors shall not be liable for any direct, 
  11. ** indirect, special, incidental or consequential damages arising 
  12. ** in any way from use of the software.
  13. ** 
  14. ** Everyone is granted permission to copy, modify and redistribute this
  15. ** Meschach Library, provided:
  16. **  1.  All copies contain this copyright notice.
  17. **  2.  All modified copies shall carry a notice stating who
  18. **      made the last modification and the date of such modification.
  19. **  3.  No charge is made for this software or works derived from it.  
  20. **      This clause shall not be construed as constraining other software
  21. **      distributed on the same medium as this software, nor is a
  22. **      distribution fee considered a charge.
  23. **
  24. ***************************************************************************/
  25.  
  26.  
  27. /*
  28.     Memory port routines: MEM_COPY and MEM_ZERO
  29. */
  30.  
  31. /* For BSD 4.[23] environments: using bcopy() and bzero() */
  32.  
  33. #include "machine.h"
  34.  
  35. #ifndef MEM_COPY
  36. void    MEM_COPY(from,to,len)
  37. char    *from, *to;
  38. int    len;
  39. {
  40.     int        i;
  41.  
  42.     if ( from < to )
  43.     {
  44.     for ( i = 0; i < len; i++ )
  45.         *to++ = *from++;
  46.     }
  47.     else
  48.     {
  49.     from += len;    to += len;
  50.     for ( i = 0; i < len; i++ )
  51.         *(--to) = *(--from);
  52.     }
  53. }
  54. #endif
  55.  
  56. #ifndef MEM_ZERO
  57. void    MEM_ZERO(ptr,len)
  58. char    *ptr;
  59. int    len;
  60. {
  61.     int        i;
  62.  
  63.     for ( i = 0; i < len; i++ )
  64.     *(ptr++) = '\0';
  65. }
  66. #endif
  67.  
  68. /*
  69.     This file contains versions of something approximating the well-known
  70.     BLAS routines in C, suitable for Meschach (hence the `m').
  71.     These are "vanilla" implementations, at least with some consideration
  72.     of the effects of caching and paging, and maybe some loop unrolling
  73.     for register-rich machines
  74. */
  75.  
  76. /*
  77.     Organisation of matrices: it is assumed that matrices are represented
  78.     by Real **'s. To keep flexibility, there is also an "initial
  79.     column" parameter j0, so that the actual elements used are
  80.         A[0][j0],   A[0][j0+1],   ..., A[0][j0+n-1]
  81.         A[1][j0],   A[1][j0+1],   ..., A[1][j0+n-1]
  82.            ..         ..          ...      ..
  83.         A[m-1][j0], A[m-1][j0+1], ..., A[m-1][j0+n-1]
  84. */
  85.  
  86. static char    rcsid[] = "$Id: extras.c,v 1.3 1994/01/13 05:45:36 des Exp $";
  87.  
  88. #include    <math.h>
  89.  
  90. #define    REGISTER_RICH    1
  91.  
  92. /* mblar-1 routines */
  93.  
  94. /* Mscale -- sets x <- alpha.x */
  95. void    Mscale(len,alpha,x)
  96. int    len;
  97. double    alpha;
  98. Real    *x;
  99. {
  100.     register int    i;
  101.  
  102.     for ( i = 0; i < len; i++ )
  103.     x[i] *= alpha;
  104. }
  105.  
  106. /* Mswap -- swaps x and y */
  107. void    Mswap(len,x,y)
  108. int    len;
  109. Real    *x, *y;
  110. {
  111.     register int    i;
  112.     register Real    tmp;
  113.  
  114.     for ( i = 0; i < len; i++ )
  115.     {
  116.     tmp = x[i];
  117.     x[i] = y[i];
  118.     y[i] = tmp;
  119.     }
  120. }
  121.  
  122. /* Mcopy -- copies x to y */
  123. void    Mcopy(len,x,y)
  124. int    len;
  125. Real    *x, *y;
  126. {
  127.     register int    i;
  128.  
  129.     for ( i = 0; i < len; i++ )
  130.     y[i] = x[i];
  131. }
  132.  
  133. /* Maxpy -- y <- y + alpha.x */
  134. void    Maxpy(len,alpha,x,y)
  135. int    len;
  136. double    alpha;
  137. Real    *x, *y;
  138. {
  139.     register int    i, len4;
  140.  
  141.     /****************************************
  142.     for ( i = 0; i < len; i++ )
  143.     y[i] += alpha*x[i];
  144.     ****************************************/
  145.  
  146. #ifdef REGISTER_RICH
  147.     len4 = len / 4;
  148.     len  = len % 4;
  149.     for ( i = 0; i < len4; i++ )
  150.     {
  151.     y[4*i]   += alpha*x[4*i];
  152.     y[4*i+1] += alpha*x[4*i+1];
  153.     y[4*i+2] += alpha*x[4*i+2];
  154.     y[4*i+3] += alpha*x[4*i+3];
  155.     }
  156.     x += 4*len4;    y += 4*len4;
  157. #endif
  158.     for ( i = 0; i < len; i++ )
  159.     y[i] += alpha*x[i];
  160. }
  161.  
  162. /* Mdot -- returns x'.y */
  163. double    Mdot(len,x,y)
  164. int    len;
  165. Real    *x, *y;
  166. {
  167.     register int    i, len4;
  168.     register Real    sum;
  169.  
  170. #ifndef REGISTER_RICH
  171.     sum = 0.0;
  172. #endif
  173.  
  174. #ifdef REGISTER_RICH
  175.     register Real    sum0, sum1, sum2, sum3;
  176.     
  177.     sum0 = sum1 = sum2 = sum3 = 0.0;
  178.     
  179.     len4 = len / 4;
  180.     len  = len % 4;
  181.     
  182.     for ( i = 0; i < len4; i++ )
  183.     {
  184.     sum0 += x[4*i  ]*y[4*i  ];
  185.     sum1 += x[4*i+1]*y[4*i+1];
  186.     sum2 += x[4*i+2]*y[4*i+2];
  187.     sum3 += x[4*i+3]*y[4*i+3];
  188.     }
  189.     sum = sum0 + sum1 + sum2 + sum3;
  190.     x += 4*len4;    y += 4*len4;
  191. #endif
  192.  
  193.     for ( i = 0; i < len; i++ )
  194.     sum += x[i]*y[i];
  195.  
  196.     return sum;
  197. }
  198.  
  199. #ifndef ABS
  200. #define    ABS(x)    ((x) >= 0 ? (x) : -(x))
  201. #endif
  202.  
  203. /* Mnorminf -- returns ||x||_inf */
  204. double    Mnorminf(len,x)
  205. int    len;
  206. Real    *x;
  207. {
  208.     register int    i;
  209.     register Real    tmp, max_val;
  210.  
  211.     max_val = 0.0;
  212.     for ( i = 0; i < len; i++ )
  213.   < n; k++ )
  214.     {
  215.         /* tmp = A_me[j][k]; */
  216.         tmp = m_entry(A,j,k);
  217.         /* A_me[j][k] = A_me[i][k]; */
  218.         m_set_val(A,j,k,m_entry(A,i,k));
  219.         /* A_me[i][k] = tmp; */
  220.         m_set_val(A,i,k,tmp);
  221.     }
  222.     for ( k = i+1; k < j; k++ )
  223.     {
  224.         /* tmp = A_me[k][j]; */
  225.         tmp = m_entry(A,k,j);
  226.         /* A_me[k][j] = A_me[i][k]; */
  227.         m_set_val(A,k,j,m_entry(A,i,k));
  228.         /* A_me[i][k] = tmp; */
  229.         m_set_val(A,i,k,tmp);
  230.     }
  231.     /* tmp = A_me[i][i]; */
  232.     tmp = m_entry(A,i,i);
  233.     /* A_me[i][i] = A_me[j][j]; */
  234.     m_set_val(A,i,i,m_entry(A,j,j));
  235.     /* A_me[qÉ• Ø:ð!0"P"†€`E€€+€ —!Ü("u€ ~A0DÀ ‰À€$¤o☓&€'€(—"¨"¨!Y€3æÐEà⇧# „À¶`…€2 ™€€q€$`É4OD¦ ® €]6[@–ø2€03€2☓H3€ZCœÀœ⇦3À³!Ú Ó¨3À‡y ÏÃ⇩
  236. ø3h4 ÐÀ¬Ŵ[$€*€,h4@²"
  237. —4T–@¦†b
  238. r
  239. F
  240. ¥pj¤ÀŒÀ¢•ìÀ¡€9À§€_Ç€fÀ¦«Z
  241. ¯ ×X6€€²€0€1PŷΠ
  242. `
  243. m§p™⇦C—C@"’°èCðD
  244. À'À!€DÄ"Ð⇦¸CÀð⇦0⇦"&T"¸D@&6‡B0⇩(E@*ç°⇩´"xE°‰fP⇧àÈE»èEÀ/  #…#€vÀ;€xp…ÉËhFÏ€h`¨F@6€m€nÀ7á—G¨GåÅ €€•€<ö@fl
  245. ¨TŶ⇩C$*™
  246. €G"`R€JàRd*`SÑ©øT☓*F¥
  247. pªR¬*€V€W«³
  248. Ô*€[Ŷy€ŵv½
  249. ¿
  250. Á
  251. rVÅ
  252. ØX$+„€fÑ
  253. Ç
  254. €i0®àZ’°`[Ñ­À· \°°¨X€r€sEºb]Ñ®€z@Àð®⇦WP­€gd+‘¯Z^öô+à_ ,@Ã"e±²ð°±`b€{EÆB⇧•€’ÅÉ¢d¸X€€⇦€H—d¨dÀ0
  255. ¸d− Ødt2ðÉÊ€Q€rÈh€S© À*heðÊÄ2À,Ô2À- –`– “zà– —PÌÀ1É À2ÐÌxfÑ Ó ª¸f€lØfÝ øf@8À8À9‘ÎÖí À;@<0ÏÔ3€{ flÀ>ÐÏ€  €Ŵ€ŵ€◰HhXhÐÐÀC@D&*6
  256. €€îÕŷTÀª£Õðê⇦u—uÔ:n¹‰]G¯ã×0ïvÅàØ’ìXv4;fiÁðí׉ll;ªÝŵñ¸ww“;Îé‰u@´Óðî8zPï€|ØwýàßðÀÀ•◰GÂÀ•⇦ÇÄ âqñÈx`ãÑñÀÇ@ÈcäQòÀÉðQ€“ÇÊ@Ëàå⇦y`æQó¸yä<v=~íÂ0ô⇩@Ò@ØgîÐô<=¢Sä=¹•Ý[•®¾Y•±@Ù£î1û‘öj·o•¸ÇÜ£í@æcõÑ÷ü=◰
  257. K°öðøD>L>*–•« öàóŴÐÇè£ôì=@â€ÅÐøl>t>8}©h}ðú€€j€a€ffi!
  258. P
  259. Æ!åÅ‘c€d„! ´Cøŷ€⇦×Èŷ@=÷‘à!¢ À9 04DÖ!lC&"ÄCÀ>
  260. "€|` èðLCàpX⇦\D2"’‰ÈG$$2)⇨†DR"+’„x⇨ &2©⇨ÜD9’‡Ðù⇨ E⇩"G@RÀR¤)ÀSd0rQ—⇩TE®"‘Ù⇩tE€¯☓E`,BYä,‹±i⇧€· .—⇧@]ä.‹Ŵ½⇦¾È_@`(…H…°i…<F"#›”’Ë@fd3‡’Ï@hd4“F8™‘Y™´F^#±’ÙHmä6‹Ù™ôF~#Á’áHqÇ‘X‰4Gfi#⇦‰ª#LG€€€lɶ¤[ò-    –…K(–à\‹.Y–íÞ%ñ‹yI½ä^‹/À¾ú%à_0—L&    `aÒ0y—DL&&›⇧IÆdc@Çäc@ÈddR29−¤LV&-›–IÌdfR3¹−äLv&=›flIÐdhM‰&I,MŒ&<M€¨ÉÔ¤j€«    €¬    €­    tM¾&a›±IÙälBÚdmÒ6yœ€¸Éõd{r7’7Ùœ`n uÀê¤oâo8†N$Nj'½•@ädrBåär‹9°9éfi|O☓O…NJ'p:!zÒ8Aݦ'¯;³€êÉí@îdw²8áw<fiO‰'É›åIóäy€Î    >'@è­@ö0>±A@údŶŵJ
  261. Ŷõ    Ð>@ù$~°?¡ŶòBQ?¹fl€üÉEe☓RB9¡H €þÉûç P€
  262. øflX ÌO(DP&(\P
  263. “ JŶ€Ó€x
  264. `fi@=÷ȧûôSø§ TTÀA€☓
  265. ÐP€⇦
  266. 0Q**€⇧
  267. €…
  268. ÀF:*ÀG%¤2R)©pRI©¬TZ*^*1—©ÔT7”†ÊN¥§òSTŷ*€¢
  269. pT$U„*M”§JTŶ©JU媋UÙª «òU@Xe¬RV9«¤UÖ*m”·J\e®RW¹«äUö*ôUðW    ¬ V
  270. +8¬‘XY¬4V+’”ÉJe岋YÙ¬tV>+¡”ÑJiÅi%µ²Zi­¼Vb+³”ÚÊm‘[Ù­ôV~+Á”áJq帋\Y® ¹ò\⇨®LW¨®\W²+Û”îÊw%¼2^€€]),C,Cj0);
  271. }
  272.  
  273. /* Mmmtr -- C <- C + alpha.A.B^T */
  274. void    Mmmtr(m,n,p,alpha,A,Aj0,B,Bj0,C,Cj0)
  275. int    m, n, p;    /* C is m x n */
  276. double  alpha;
  277. Real    **A, **B, **C;
  278. int    Aj0, Bj0, Cj0;
  279. {
  280.     register int    i, j, k;
  281.  
  282.     /****************************************
  283.     for ( i = 0; i < m; i++ )
  284.     for ( j = 0; j < n; j++ )
  285.         C[i][Cj0+j] += alpha*Mdot(p,&(A[i][Aj0]),&(B[j][Bj0]));
  286.     ****************************************/
  287.     for ( i = 0; i < m; i++ )
  288.     Mmv(n,p,alpha,&(A[i][Aj0]),B,Bj0,&(C[i][Cj0]));
  289. }
  290.  
  291. /* Mmtrmtr -- C <- C + alpha.A^T.B^T */
  292. void    Mmtrmtr(m,n,p,alpha,A,Aj0,B,Bj0,C,Cj0)
  293. int    m, n, p;    /* C is m x n */
  294. double  alpha;
  295. Real    **A, **B, **C;
  296. int    Aj0, Bj0, Cj0;
  297. {
  298.     register int    i, j, k;
  299.  
  300.     for ( i = 0; i < m; i++ )
  301.     for ( j = 0; j < n; j++ )
  302.         for ( k = 0; k < p; k++ )
  303.         C[i][Cj0+j] += A[i][Aj0+k]*B[k][Bj0+j];
  304. }
  305.  
  306.