Main Page | Class List | Directories | File List | Class Members | File Members

slarrfun.inc

Go to the documentation of this file.
00001 /* -*- mode: C -*- */
00002 
00003 /* Some "inline" functions for generic scalar types */
00004 
00005 #ifdef TRANSPOSE_2D_ARRAY
00006 static SLang_Array_Type *TRANSPOSE_2D_ARRAY (SLang_Array_Type *at, SLang_Array_Type *bt)
00007 {
00008    GENERIC_TYPE *a_data, *b_data;
00009    SLindex_Type nr, nc, i;
00010 
00011    nr = at->dims[0];
00012    nc = at->dims[1];
00013 
00014    a_data = (GENERIC_TYPE *) at->data;
00015    b_data = (GENERIC_TYPE *) bt->data;
00016 
00017    for (i = 0; i < nr; i++)
00018      {
00019         GENERIC_TYPE *offset = b_data + i;
00020         int j;
00021         for (j = 0; j < nc; j++)
00022           {
00023              *offset = *a_data++;
00024              offset += nr;
00025           }
00026      }
00027    return bt;
00028 }
00029 #undef TRANSPOSE_2D_ARRAY
00030 #endif
00031 
00032 
00033 #ifdef INNERPROD_FUNCTION
00034 
00035 static void INNERPROD_FUNCTION
00036   (SLang_Array_Type *at, SLang_Array_Type *bt, SLang_Array_Type *ct,
00037    unsigned int a_loops, unsigned int a_stride,
00038    unsigned int b_loops, unsigned int b_inc,
00039    unsigned int inner_loops)
00040 {
00041    GENERIC_TYPE_A *a;
00042    GENERIC_TYPE_B *b;
00043    GENERIC_TYPE_C *c;
00044 
00045    c = (GENERIC_TYPE_C *) ct->data;
00046    b = (GENERIC_TYPE_B *) bt->data;
00047    a = (GENERIC_TYPE_A *) at->data;
00048    
00049    while (a_loops--)
00050      {
00051         GENERIC_TYPE_B *bb;
00052         unsigned int j;
00053 
00054         bb = b;
00055 
00056         for (j = 0; j < inner_loops; j++)
00057           {
00058              double x = (double) a[j];
00059 
00060              if (x != 0.0)
00061                {
00062                   unsigned int k;
00063 
00064                   for (k = 0; k < b_loops; k++)
00065                     c[k] += x * bb[k];
00066                }
00067              bb += b_inc;
00068           }
00069         c += b_loops;
00070         a += a_stride;
00071      }
00072 }
00073 #undef INNERPROD_FUNCTION
00074 
00075 #undef GENERIC_TYPE_A
00076 #undef GENERIC_TYPE_B
00077 #undef GENERIC_TYPE_C
00078 #endif
00079 
00080 #ifdef INNERPROD_COMPLEX_A
00081 static void INNERPROD_COMPLEX_A
00082   (SLang_Array_Type *at, SLang_Array_Type *bt, SLang_Array_Type *ct,
00083    unsigned int a_loops, unsigned int a_stride,
00084    unsigned int b_loops, unsigned int b_inc,
00085    unsigned int inner_loops)
00086 {
00087    double *a;
00088    GENERIC_TYPE *b;
00089    double *c;
00090 
00091    c = (double *) ct->data;
00092    b = (GENERIC_TYPE *) bt->data;
00093    a = (double *) at->data;
00094    
00095    a_stride *= 2;
00096 
00097    while (a_loops--)
00098      {
00099         GENERIC_TYPE *bb;
00100         unsigned int bb_loops;
00101 
00102         bb = b;
00103         bb_loops = b_loops;
00104         
00105         while (bb_loops--)
00106           {
00107              double real_sum;
00108              double imag_sum;
00109              unsigned int iloops;
00110              double *aa;
00111              GENERIC_TYPE *bbb;
00112              
00113              aa = a;
00114              bbb = bb;
00115              iloops = inner_loops;
00116 
00117              real_sum = 0.0;
00118              imag_sum = 0.0;
00119              while (iloops--)
00120                {
00121                   real_sum += aa[0] * (double)bbb[0];
00122                   imag_sum += aa[1] * (double)bbb[0];
00123                   aa += 2;
00124                   bbb += b_inc;
00125                }
00126 
00127              *c++ = real_sum;
00128              *c++ = imag_sum;
00129              bb++;
00130           }
00131 
00132         a += a_stride;
00133      }
00134 }
00135 
00136 static void INNERPROD_A_COMPLEX
00137   (SLang_Array_Type *at, SLang_Array_Type *bt, SLang_Array_Type *ct,
00138    unsigned int a_loops, unsigned int a_stride,
00139    unsigned int b_loops, unsigned int b_inc,
00140    unsigned int inner_loops)
00141 {
00142    GENERIC_TYPE *a;
00143    double *b;
00144    double *c;
00145 
00146    c = (double *) ct->data;
00147    b = (double *) bt->data;
00148    a = (GENERIC_TYPE *) at->data;
00149    
00150    b_inc *= 2;
00151 
00152    while (a_loops--)
00153      {
00154         double *bb;
00155         unsigned int bb_loops;
00156 
00157         bb = b;
00158         bb_loops = b_loops;
00159         
00160         while (bb_loops--)
00161           {
00162              double real_sum;
00163              double imag_sum;
00164              unsigned int iloops;
00165              GENERIC_TYPE *aa;
00166              double *bbb;
00167 
00168              aa = a;
00169              bbb = bb;
00170              iloops = inner_loops;
00171 
00172              real_sum = 0.0;
00173              imag_sum = 0.0;
00174              while (iloops--)
00175                {
00176                   real_sum += (double)aa[0] * bbb[0];
00177                   imag_sum += (double)aa[0] * bbb[1];
00178                   aa += 1;
00179                   bbb += b_inc;
00180                }
00181 
00182              *c++ = real_sum;
00183              *c++ = imag_sum;
00184              bb += 2;
00185           }
00186 
00187         a += a_stride;
00188      }
00189 }
00190 
00191 #undef INNERPROD_A_COMPLEX
00192 #undef INNERPROD_COMPLEX_A
00193 #endif                                 /* INNERPROD_COMPLEX_A */
00194 
00195 
00196 #ifdef INNERPROD_COMPLEX_COMPLEX
00197 static void INNERPROD_COMPLEX_COMPLEX
00198   (SLang_Array_Type *at, SLang_Array_Type *bt, SLang_Array_Type *ct,
00199    unsigned int a_loops, unsigned int a_stride,
00200    unsigned int b_loops, unsigned int b_inc,
00201    unsigned int inner_loops)
00202 {
00203    double *a;
00204    double *b;
00205    double *c;
00206 
00207    c = (double *) ct->data;
00208    b = (double *) bt->data;
00209    a = (double *) at->data;
00210    
00211    a_stride *= 2;
00212    b_inc *= 2;
00213 
00214    while (a_loops--)
00215      {
00216         double *bb;
00217         unsigned int bb_loops;
00218 
00219         bb = b;
00220         bb_loops = b_loops;
00221         
00222         while (bb_loops--)
00223           {
00224              double real_sum;
00225              double imag_sum;
00226              unsigned int iloops;
00227              double *aa;
00228              double *bbb;
00229 
00230              aa = a;
00231              bbb = bb;
00232              iloops = inner_loops;
00233 
00234              real_sum = 0.0;
00235              imag_sum = 0.0;
00236              while (iloops--)
00237                {
00238                   real_sum += aa[0]*bbb[0] - aa[1]*bbb[1];
00239                   imag_sum += aa[0]*bbb[1] + aa[1]*bbb[0];
00240                   aa += 2;
00241                   bbb += b_inc;
00242                }
00243 
00244              *c++ = real_sum;
00245              *c++ = imag_sum;
00246              bb += 2;
00247           }
00248 
00249         a += a_stride;
00250      }
00251 }
00252 #undef INNERPROD_COMPLEX_COMPLEX
00253 #endif
00254 
00255 #ifdef SUM_FUNCTION
00256 #if SLANG_HAS_FLOAT
00257 static int SUM_FUNCTION (VOID_STAR xp, unsigned int inc, unsigned int num, VOID_STAR yp)
00258 {
00259    GENERIC_TYPE *x, *xmax;
00260    double sum, sumerr;
00261    
00262    sum = 0.0;
00263    sumerr = 0.0;
00264 
00265    x = (GENERIC_TYPE *) xp;
00266    xmax = x + num;
00267 #if 0 && SLANG_OPTIMIZE_FOR_SPEED
00268    if (inc == 1)
00269      {
00270         while (x < xmax)
00271           {
00272              sum += (double) *x;
00273              x++;
00274           }
00275      }
00276    else
00277 #endif
00278    while (x < xmax)
00279      {
00280         double v = *x;
00281         double new_sum = sum + v;
00282         sumerr += v - (new_sum-sum);
00283         sum = new_sum;
00284         x += inc;
00285      }
00286    *(SUM_RESULT_TYPE *)yp = (SUM_RESULT_TYPE) (sum + sumerr);
00287    return 0;
00288 }
00289 #endif                                 /* SLANG_HAS_FLOAT */
00290 #undef SUM_FUNCTION
00291 #undef SUM_RESULT_TYPE
00292 #endif
00293 
00294 #ifdef MIN_FUNCTION
00295 static int 
00296 MIN_FUNCTION (VOID_STAR ip, unsigned int inc, unsigned int num, VOID_STAR sp)
00297 {
00298    unsigned int n, n0;
00299    GENERIC_TYPE m;
00300    GENERIC_TYPE *i = (GENERIC_TYPE *)ip;
00301 
00302    if (-1 == check_for_empty_array ("min", num))
00303      return -1;
00304 
00305 # ifdef IS_NAN_FUNCTION
00306    n0 = 0;
00307    do 
00308      {
00309         m = i[n0];
00310         n0 += inc;
00311      }
00312    while (IS_NAN_FUNCTION(m) && (n0 < num));
00313 # else
00314    m = i[0];
00315    n0 = inc;
00316 # endif
00317 
00318    for (n = n0; n < num; n += inc)
00319      if (m > i[n]) m = i[n];
00320    
00321    *(GENERIC_TYPE *)sp = m;
00322    return 0;
00323 }
00324 #undef MIN_FUNCTION
00325 #endif
00326 
00327 #ifdef MAX_FUNCTION
00328 static int
00329 MAX_FUNCTION (VOID_STAR ip, unsigned int inc, unsigned int num, VOID_STAR s)
00330 {
00331    unsigned int n, n0;
00332    GENERIC_TYPE m;
00333    GENERIC_TYPE *i = (GENERIC_TYPE *) ip;
00334 
00335    if (-1 == check_for_empty_array ("max", num))
00336      return -1;
00337 
00338 # ifdef IS_NAN_FUNCTION
00339    n0 = 0;
00340    do 
00341      {
00342         m = i[n0];
00343         n0 += inc;
00344      }
00345    while (IS_NAN_FUNCTION(m) && (n0 < num));
00346 # else
00347    m = i[0];
00348    n0 = inc;
00349 # endif
00350    
00351    for (n = n0; n < num; n += inc)
00352      if (m < i[n]) m = i[n];
00353    
00354    *(GENERIC_TYPE *)s = m;
00355    return 0;
00356 }
00357 #undef MAX_FUNCTION
00358 #endif
00359 
00360 #ifdef ANY_FUNCTION
00361 static int
00362 ANY_FUNCTION (VOID_STAR ip, unsigned int inc, unsigned int num, VOID_STAR s)
00363 {
00364    unsigned int n;
00365    GENERIC_TYPE *i = (GENERIC_TYPE *) ip;
00366 
00367    for (n = 0; n < num; n += inc)
00368      if (i[n] != 0)
00369        {
00370 #ifdef IS_NAN_FUNCTION
00371           if (IS_NAN_FUNCTION(i[n]))
00372             continue;
00373 #endif
00374           *(char *)s = 1;
00375           return 0;
00376        }
00377 
00378    *(char *)s = 0;
00379    return 0;
00380 }
00381 #undef ANY_FUNCTION
00382 #endif
00383 
00384 #ifdef ALL_FUNCTION
00385 static int
00386 ALL_FUNCTION (VOID_STAR ip, unsigned int inc, unsigned int num, VOID_STAR s)
00387 {
00388    unsigned int n;
00389    GENERIC_TYPE *i = (GENERIC_TYPE *) ip;
00390 
00391    if (num == 0)
00392      {
00393         *(char *)s = 0;
00394         return 0;
00395      }
00396    for (n = 0; n < num; n += inc)
00397      {
00398         if (i[n] == (GENERIC_TYPE)0)
00399           {
00400              *(char *)s = 0;
00401              return 0;
00402           }
00403 #ifdef IS_NAN_FUNCTION
00404         /* I really do not want to call this for all numbers, nor do I know
00405          * what makes most sense.  Doing nothing means that all(_NaN) is 1.
00406          * Such an interpretation is consistent with using 
00407          *   length(x) == length(where (x))
00408          */
00409 #endif
00410      }
00411 
00412    *(char *)s = 1;
00413    return 0;
00414 }
00415 #undef ALL_FUNCTION
00416 #endif
00417 
00418 #ifdef CUMSUM_FUNCTION
00419 #ifdef SLANG_HAS_FLOAT
00420 static int 
00421 CUMSUM_FUNCTION (SLtype xtype, VOID_STAR xp, unsigned int inc, 
00422                  unsigned int num,
00423                  SLtype ytype, VOID_STAR yp, VOID_STAR clientdata)
00424 {
00425    GENERIC_TYPE *x, *xmax;
00426    CUMSUM_RESULT_TYPE *y;
00427    double c;
00428    double cerr;
00429 
00430    (void) xtype;
00431    (void) ytype;
00432    (void) clientdata;
00433 
00434    x = (GENERIC_TYPE *) xp;
00435    y = (CUMSUM_RESULT_TYPE *) yp;
00436    xmax = x + num;
00437 
00438    c = 0.0;
00439    cerr = 0.0;
00440    while (x < xmax)
00441      {
00442         double d = (double) *x;
00443         double c1 = c + d;
00444         cerr += d - (c1 - c);
00445         c = c1;
00446         *y = (CUMSUM_RESULT_TYPE) (c + cerr);
00447         x += inc;
00448         y += inc;
00449      }
00450    return 0;
00451 }
00452 #endif                                 /* SLANG_HAS_FLOAT */
00453 #undef CUMSUM_FUNCTION
00454 #undef CUMSUM_RESULT_TYPE
00455 #endif
00456 
00457 #ifdef GENERIC_TYPE
00458 # undef GENERIC_TYPE
00459 #endif
00460 
00461 #ifdef IS_NAN_FUNCTION
00462 # undef IS_NAN_FUNCTION
00463 #endif

© sourcejam.com 2005-2008