IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Ignore:
Timestamp:
Nov 26, 2019, 1:08:55 PM (7 years ago)
Author:
eugene
Message:

allow <~ and ~> as bitshift operators; better reporting of dvomath errors

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/eam_branches/ipp-20191011/Ohana/src/opihi/lib.shell/stack_math.c

    r41137 r41141  
    3434    case '?': SSS_FUNC(M1 ? M2: M3);
    3535    default:
    36       snprintf (line, 512, "error: op %c not defined!", op[0]);
     36      snprintf (line, 512, "error: op %c not defined as scalar trinary op!", op[0]);
    3737      push_error (line);
    3838      return (FALSE);
     
    162162    case '?': VVV_FUNC(*M1 ? *M2: *M3);
    163163    default:
    164       snprintf (line, 512, "error: op %c not defined!", op[0]);
     164      snprintf (line, 512, "error: op %c not defined as vector trinary op!", op[0]);
    165165      push_error (line);
    166166      return (FALSE);
     
    227227    case '?': MMM_FUNC(*M1 ? *M2: *M3);
    228228    default:
    229       snprintf (line, 512, "error: op %c not defined!", op[0]);
     229      snprintf (line, 512, "error: op %c not defined as matrix trinary op!", op[0]);
    230230      push_error (line);
    231231      return (FALSE);
     
    358358    case 'O': VV_FUNC(ST_SCALAR_INT, (*M1 || *M2) ? 1 : 0);
    359359
     360    // for the bitshift operators, we have to treat the INT and FLT values differently
     361    // this makes the operator incompatible with the macros used above
    360362    case 'l': {
     363      CopyVector (OUT[0].vector, V1[0].vector);
    361364      if ((V1->vector->type == OPIHI_FLT) || (V2->vector->type == OPIHI_FLT)) {
    362365        // bitshift is not valid with float valuess
    363         CopyVector (OUT[0].vector, V1[0].vector);
    364366        for (i = 0; i < Nx; i++) {
    365367          OUT[0].vector[0].elements.Flt[i] = NAN;
     
    378380
    379381    case 'r': {
     382      CopyVector (OUT[0].vector, V1[0].vector);
    380383      if ((V1->vector->type == OPIHI_FLT) || (V2->vector->type == OPIHI_FLT)) {
    381384        // bitshift is not valid with float valuess
     
    397400
    398401    default:
    399       snprintf (line, 512, "error: op %c not defined!", op[0]);
     402      snprintf (line, 512, "error: op %c not defined for (vector OP vector)!", op[0]);
    400403      push_error (line);
    401404      return (FALSE);
     
    509512    case 'O': SV_FUNC(ST_SCALAR_INT, (M1 || *M2) ? 1 : 0);
    510513
     514    // for the bitshift operators, we have to treat the INT and FLT values differently
     515    // this makes the operator incompatible with the macros used above
    511516    case 'l': {
    512       if (V2->vector->type == OPIHI_FLT) {
     517      CopyVector (OUT[0].vector, V2[0].vector);
     518      if ((V1->type == ST_SCALAR_FLT) || (V2->vector->type == OPIHI_FLT)) {
    513519        // bitshift is not valid with float valuess
    514         CopyVector (OUT[0].vector, V2[0].vector);
    515520        for (i = 0; i < Nx; i++) {
    516521          OUT[0].vector[0].elements.Flt[i] = NAN;
     
    528533
    529534    case 'r': {
    530       if (V2->vector->type == OPIHI_FLT) {
     535      CopyVector (OUT[0].vector, V2[0].vector);
     536      if ((V1->type == ST_SCALAR_FLT) || (V2->vector->type == OPIHI_FLT)) {
    531537        // bitshift is not valid with float valuess
    532         CopyVector (OUT[0].vector, V2[0].vector);
    533538        for (i = 0; i < Nx; i++) {
    534539          OUT[0].vector[0].elements.Flt[i] = NAN;
     
    546551
    547552    default:
    548       snprintf (line, 512, "error: op %c not defined!", op[0]);
     553      snprintf (line, 512, "error: op %c not defined for (scalar OP vector)!", op[0]);
    549554      push_error (line);
    550555      return (FALSE);
     
    655660
    656661    case 'l': {
    657       if (V1->vector->type == OPIHI_FLT) {
     662      CopyVector (OUT[0].vector, V1[0].vector);
     663      if ((V1->vector->type == OPIHI_FLT) || (V2->type == ST_SCALAR_FLT)) {
    658664        // bitshift is not valid with float valuess
    659         CopyVector (OUT[0].vector, V1[0].vector);
    660665        for (i = 0; i < Nx; i++) {
    661666          OUT[0].vector[0].elements.Flt[i] = NAN;
     
    673678
    674679    case 'r': {
    675       if (V1->vector->type == OPIHI_FLT) {
     680      CopyVector (OUT[0].vector, V1[0].vector);
     681      if ((V1->vector->type == OPIHI_FLT) || (V2->type == ST_SCALAR_FLT)) {
    676682        // bitshift is not valid with float valuess
    677683        CopyVector (OUT[0].vector, V1[0].vector);
     
    691697
    692698    default:
    693       snprintf (line, 512, "error: op %c not defined!", op[0]);
     699      snprintf (line, 512, "error: op %c not defined for (vector OP scalar)!", op[0]);
    694700      push_error (line);
    695701      return (FALSE);
     
    784790    case 'O': MV_FUNC((*M1 || *M2) ? 1 : 0);
    785791
    786     case 'l': {
    787       if (V1->vector->type == OPIHI_FLT) {
    788         // bitshift is not valid with float valuess
    789         CopyVector (OUT[0].vector, V1[0].vector);
    790         for (i = 0; i < Nx; i++) {
    791           OUT[0].vector[0].elements.Flt[i] = NAN;
    792         }
    793         break;
    794       }
    795       opihi_int *M1  =  V1[0].vector[0].elements.Int;
    796       opihi_int  M2  =  V2[0].IntValue;                                 \
    797       opihi_int *out = OUT[0].vector[0].elements.Int;
    798       for (i = 0; i < Nx; i++, out++, M1++) {
    799         *out = *M1 << M2;
    800       }
    801       break;
    802     }
    803 
    804     case 'r': {
    805       if (V1->vector->type == OPIHI_FLT) {
    806         // bitshift is not valid with float valuess
    807         CopyVector (OUT[0].vector, V1[0].vector);
    808         for (i = 0; i < Nx; i++) {
    809           OUT[0].vector[0].elements.Flt[i] = NAN;
    810         }
    811         break;
    812       }
    813       opihi_int *M1  =  V1[0].vector[0].elements.Int;
    814       opihi_int  M2  =  V2[0].IntValue;                                 \
    815       opihi_int *out = OUT[0].vector[0].elements.Int;
    816       for (i = 0; i < Nx; i++, out++, M1++) {
    817         *out = *M1 >> M2;
    818       }
    819       break;
    820     }
    821 
    822792    default:
    823       snprintf (line, 512, "error: op %c not defined!", op[0]);
     793      snprintf (line, 512, "error: op %c not defined for (matrix OP vector)!", op[0]);
    824794      push_error (line);
    825795      return (FALSE);
     
    918888    case 'O': VM_FUNC((*M1 || *M2) ? 1 : 0);
    919889    default:
    920       snprintf (line, 512, "error: op %c not defined!", op[0]);
     890      snprintf (line, 512, "error: op %c not defined for (vector OP matrix)!", op[0]);
    921891      push_error (line);
    922892      return (FALSE);
     
    999969    case 'O': MM_FUNC((*M1 || *M2) ? 1 : 0);
    1000970    default:
    1001       snprintf (line, 512, "error: op %c not defined!", op[0]);
     971      snprintf (line, 512, "error: op %c not defined for (matrix OP matrix)!", op[0]);
    1002972      push_error (line);
    1003973      return (FALSE);
     
    10851055    case 'O': MS_FUNC((*M1 || M2) ? 1 : 0);
    10861056    default:
    1087       snprintf (line, 512, "error: op %c not defined!", op[0]);
     1057      snprintf (line, 512, "error: op %c not defined for (matrix OP scalar)!", op[0]);
    10881058      push_error (line);
    10891059      return (FALSE);
     
    11621132    case 'O': SM_FUNC((M1 || *M2) ? 1 : 0);
    11631133    default:
    1164       snprintf (line, 512, "error: op %c not defined!", op[0]);
     1134      snprintf (line, 512, "error: op %c not defined for (scalar OP matrix)!", op[0]);
    11651135      push_error (line);
    11661136      return (FALSE);
     
    12441214    case 'A': SS_FUNC(ST_SCALAR_INT, (M1 && M2) ? 1 : 0);
    12451215    case 'O': SS_FUNC(ST_SCALAR_INT, (M1 || M2) ? 1 : 0);
     1216
     1217    // for the bitshift operators, we have to treat the INT and FLT values differently
     1218    // this makes the operator incompatible with the macros used above
     1219    case 'l': {
     1220      if ((V1->type == ST_SCALAR_FLT) || (V2->type == ST_SCALAR_FLT)) {
     1221        // bitshift is not valid with float valuess
     1222        OUT[0].type = ST_SCALAR_FLT;
     1223        OUT[0].FltValue = NAN;
     1224        break;
     1225      }
     1226      opihi_int M1 = V1[0].IntValue;
     1227      opihi_int M2 = V2[0].IntValue;
     1228      OUT[0].type = ST_SCALAR_INT;
     1229      OUT[0].IntValue = M1 << M2;
     1230      break;
     1231    }
     1232
     1233    case 'r': {
     1234      if ((V1->type == ST_SCALAR_FLT) || (V2->type == ST_SCALAR_FLT)) {
     1235        // bitshift is not valid with float valuess
     1236        OUT[0].type = ST_SCALAR_FLT;
     1237        OUT[0].FltValue = NAN;
     1238        break;
     1239      }
     1240      opihi_int M1 = V1[0].IntValue;
     1241      opihi_int M2 = V2[0].IntValue;
     1242      OUT[0].type = ST_SCALAR_INT;
     1243      OUT[0].IntValue = M1 >> M2;
     1244      break;
     1245    }
     1246
    12461247    default:
    1247       snprintf (line, 512, "error: op %c not defined!", op[0]);
     1248      snprintf (line, 512, "error: op %c not defined for (scalar OP scalar)!", op[0]);
    12481249      push_error (line);
    12491250      return (FALSE);
     
    13181319int S_unary (StackVar *OUT, StackVar *V1, char *op) {
    13191320
    1320   char line[512]; // this is only used to report an error
    1321  
    13221321# define S_FUNC(OP,FTYPE) {                                             \
    13231322    if (V1->type == ST_SCALAR_FLT) {                                    \
     
    13601359  if (!strcmp (op, "asinh"))  S_FUNC(asinh (M1), ST_SCALAR_FLT);
    13611360  if (!strcmp (op, "acosh"))  S_FUNC(acosh (M1), ST_SCALAR_FLT);
    1362   if (!strcmp (op, "lgamma")) S_FUNC(lgamma (M1), ST_SCALAR_FLT);
    13631361  if (!strcmp (op, "sin"))    S_FUNC(sin (M1), ST_SCALAR_FLT);
    13641362  if (!strcmp (op, "cos"))    S_FUNC(cos (M1), ST_SCALAR_FLT);
     
    13731371  if (!strcmp (op, "dacos"))  S_FUNC(acos (M1)*DEG_RAD, ST_SCALAR_FLT);
    13741372  if (!strcmp (op, "datan"))  S_FUNC(atan (M1)*DEG_RAD, ST_SCALAR_FLT);
     1373  if (!strcmp (op, "lgamma")) S_FUNC(lgamma (M1), ST_SCALAR_FLT);
    13751374  if (!strcmp (op, "rnd"))    S_FUNC(0.0*M1 + drand48(), ST_SCALAR_FLT);
    13761375  if (!strcmp (op, "drnd"))   S_FUNC(0.0*M1 + drand48(), ST_SCALAR_FLT);
     
    13851384
    13861385  clear_stack (V1);
    1387   snprintf (line, 512, "error: op %s not defined!", op);
     1386
     1387  char line[512]; // this is only used to report an error
     1388  snprintf (line, 512, "error: op %s not defined as unary scalar op!", op);
    13881389  push_error (line);
    13891390  return (FALSE);
    1390 
    13911391}
    13921392
     
    14451445  if (!strcmp (op, "asinh"))  V_FUNC(asinh(*M1), ST_SCALAR_FLT);
    14461446  if (!strcmp (op, "acosh"))  V_FUNC(acosh(*M1), ST_SCALAR_FLT);
    1447   if (!strcmp (op, "lgamma")) V_FUNC(lgamma(*M1), ST_SCALAR_FLT);
    14481447  if (!strcmp (op, "sin"))    V_FUNC(sin(*M1), ST_SCALAR_FLT);
    14491448  if (!strcmp (op, "cos"))    V_FUNC(cos(*M1), ST_SCALAR_FLT);
     
    14581457  if (!strcmp (op, "dacos"))  V_FUNC(acos(*M1)*DEG_RAD, ST_SCALAR_FLT);
    14591458  if (!strcmp (op, "datan"))  V_FUNC(atan(*M1)*DEG_RAD, ST_SCALAR_FLT);
     1459  if (!strcmp (op, "lgamma")) V_FUNC(lgamma(*M1), ST_SCALAR_FLT);
    14601460  if (!strcmp (op, "rnd"))    V_FUNC(drand48(), ST_SCALAR_FLT);
    14611461  if (!strcmp (op, "drnd"))   V_FUNC(drand48(), ST_SCALAR_FLT);
    14621462  if (!strcmp (op, "lrnd"))   V_FUNC(lrand48(), ST_SCALAR_INT);
    14631463  if (!strcmp (op, "mrnd"))   V_FUNC(mrand48(), ST_SCALAR_INT);
    1464   if (!strcmp (op, "ramp"))   V_FUNC(i, ST_SCALAR_INT);
    1465   if (!strcmp (op, "zero"))   V_FUNC(0, ST_SCALAR_INT);
    14661464  if (!strcmp (op, "not"))    V_FUNC(!(*M1), ST_SCALAR_INT);
    14671465  if (!strcmp (op, "--"))     V_FUNC(-1*(*M1), ST_SCALAR_INT); // NOTE: opihi_int is signed
    14681466  if (!strcmp (op, "isinf"))  V_FUNC(!finite(*M1), ST_SCALAR_FLT);
    14691467  if (!strcmp (op, "isnan"))  V_FUNC(isnan((opihi_flt)(*M1)), ST_SCALAR_FLT);
     1468  if (!strcmp (op, "ramp"))   V_FUNC(i, ST_SCALAR_INT);
    14701469  if (!strcmp (op, "xramp"))  V_FUNC(i, ST_SCALAR_INT);
    14711470  if (!strcmp (op, "yramp"))  V_FUNC(0, ST_SCALAR_INT);
    14721471  if (!strcmp (op, "zramp"))  V_FUNC(0, ST_SCALAR_INT);
     1472  if (!strcmp (op, "zero"))   V_FUNC(0, ST_SCALAR_INT);
    14731473  /* xramp, yramp, zramp above only make sense for matrices. for vectors, xramp = ramp, yramp = zero */
    14741474
    14751475# undef V_FUNC
    14761476
    1477 escape:
    1478 
     1477  // free the temp vector if needed
    14791478  if (V1[0].type == ST_VECTOR_TMP) {
    14801479    free (V1[0].vector[0].elements.Ptr);
     
    14841483
    14851484  clear_stack (V1);
     1485
     1486  char line[512]; // this is only used to report an error
     1487  snprintf (line, 512, "error: op %s not defined as unary vector op!", op);
     1488  push_error (line);
     1489  return (FALSE);
     1490
     1491escape:
     1492
     1493  if (V1[0].type == ST_VECTOR_TMP) {
     1494    free (V1[0].vector[0].elements.Ptr);
     1495    free (V1[0].vector);
     1496    V1[0].vector = NULL;
     1497  } 
     1498
     1499  clear_stack (V1);
    14861500  return (TRUE);
    14871501
    14881502}
    14891503
    1490 # define M_FUNC(OP) { for (i = 0; i < Npix; i++, out++, M1++) { *out = (OP); }}
     1504# define M_FUNC(OP) { for (i = 0; i < Npix; i++, out++, M1++) { *out = (OP); } goto escape; }
    14911505
    14921506int M_unary (StackVar *OUT, StackVar *V1, char *op) {
     
    15101524// if (!strcmp (op, "rint"))  { for (i = 0; i < Npix; i++, out++, M1++) { *out = nearbyint (*M1); }}
    15111525 
    1512   if (!strcmp (op, "="))     { }
     1526  if (!strcmp (op, "="))      { goto escape; }
    15131527  if (!strcmp (op, "abs"))    M_FUNC(fabs(*M1));
    15141528  if (!strcmp (op, "int"))    M_FUNC((opihi_flt)(long long)(*M1));
     
    15251539  if (!strcmp (op, "asinh"))  M_FUNC(asinh(*M1));
    15261540  if (!strcmp (op, "acosh"))  M_FUNC(acosh(*M1));
    1527   if (!strcmp (op, "lgamma")) M_FUNC(lgamma(*M1));
    15281541  if (!strcmp (op, "sin"))    M_FUNC(sin(*M1));
    15291542  if (!strcmp (op, "cos"))    M_FUNC(cos(*M1));
     
    15381551  if (!strcmp (op, "dacos"))  M_FUNC(acos(*M1)*DEG_RAD);
    15391552  if (!strcmp (op, "datan"))  M_FUNC(atan(*M1)*DEG_RAD);
    1540   if (!strcmp (op, "not"))    M_FUNC(!(*M1));
    1541   if (!strcmp (op, "--"))     M_FUNC(-(*M1));
     1553  if (!strcmp (op, "lgamma")) M_FUNC(lgamma(*M1));
    15421554  if (!strcmp (op, "rnd"))    M_FUNC(drand48());
    15431555  if (!strcmp (op, "drnd"))   M_FUNC(drand48());
    15441556  if (!strcmp (op, "lrnd"))   M_FUNC(lrand48());
    15451557  if (!strcmp (op, "mrnd"))   M_FUNC(mrand48());
     1558  if (!strcmp (op, "not"))    M_FUNC(!(*M1));
     1559  if (!strcmp (op, "--"))     M_FUNC(-(*M1));
    15461560  if (!strcmp (op, "ramp"))   M_FUNC(i);
    1547   if (!strcmp (op, "zero"))   M_FUNC(0);
    15481561  if (!strcmp (op, "isinf"))  M_FUNC(!finite(*M1));
    15491562  if (!strcmp (op, "isnan"))  M_FUNC(isnan(*M1));
     1563  if (!strcmp (op, "zero"))   M_FUNC(0);
    15501564
    15511565  /* xrm and yrm only make sense for 2D matrices. see special meaning for vectors */
     
    15611575      }
    15621576    }
     1577    goto escape;
    15631578  }
    15641579  if (!strcmp (op, "yramp")) {
     
    15731588      }
    15741589    }
     1590    goto escape;
    15751591  }
    15761592  if (!strcmp (op, "zramp")) {
     
    15851601      }
    15861602    }
    1587   }
    1588  
     1603    goto escape;
     1604  }
     1605 
     1606  if (V1[0].type == ST_MATRIX_TMP) {
     1607    free (V1[0].buffer[0].header.buffer);
     1608    free (V1[0].buffer[0].matrix.buffer);
     1609    free (V1[0].buffer);
     1610  }
     1611
     1612  clear_stack (V1);
     1613
     1614  char line[512]; // this is only used to report an error
     1615  snprintf (line, 512, "error: op %s not defined as unary matrix op!", op);
     1616  push_error (line);
     1617  return (FALSE);
     1618
     1619 escape:
     1620
    15891621  if (V1[0].type == ST_MATRIX_TMP) {
    15901622    free (V1[0].buffer[0].header.buffer);
Note: See TracChangeset for help on using the changeset viewer.