Ignore:
Timestamp:
Mar 3, 2010, 9:44:38 PM (16 years ago)
Author:
ansari
Message:

Prise en charge de float 128 bits (r_16, complex<r_16>) par les NDataBlock<T> et PPersist, controlee par le flag de compilation SO_LDBLE128 defini ds machdefs.h , Reza 03/03/2010

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/SophyaLib/BaseTools/ppfbinstream.cc

    r3619 r3750  
    2121
    2222
     23static inline void bswap16(void* p)
     24{
     25  uint_8* p8 = (uint_8*)p;
     26  uint_8 tmp1 = *(p8);
     27  uint_8 tmp = *(p8+1);
     28  *(p8)   = ((tmp >> (7*8)) & 0x000000FF) |
     29            ((tmp >> (5*8)) & 0x0000FF00) |
     30            ((tmp >> (3*8)) & 0x00FF0000) |
     31            ((tmp >> (1*8)) & 0xFF000000) |
     32            ((tmp & 0xFF000000) << (1*8))  |
     33            ((tmp & 0x00FF0000) << (3*8))  |
     34            ((tmp & 0x0000FF00) << (5*8))  |
     35            ((tmp & 0x000000FF) << (7*8));
     36  *(p8+1) = ((tmp1 >> (7*8)) & 0x000000FF) |
     37            ((tmp1 >> (5*8)) & 0x0000FF00) |
     38            ((tmp1 >> (3*8)) & 0x00FF0000) |
     39            ((tmp1 >> (1*8)) & 0xFF000000) |
     40            ((tmp1 & 0xFF000000) << (1*8))  |
     41            ((tmp1 & 0x00FF0000) << (3*8))  |
     42            ((tmp1 & 0x0000FF00) << (5*8))  |
     43            ((tmp1 & 0x000000FF) << (7*8));
     44}
     45
    2346static inline void bswap8(void* p)
    2447{
     
    4871  *(uint_2*)p = ((tmp >> 8) & 0x00FF) |
    4972                ((tmp & 0x00FF) << 8);
     73}
     74
     75// Convertit la taille de donnees (2 (int_2), 4 (int_4, r_4), 8 (r_8) ...) en taille codee dans le tag
     76static inline unsigned char datasize2tagsize( unsigned char sz)
     77{
     78  // Taille permises 1,2,4,8,16
     79  // Pour les tailles <= 8, c'est directement la taille
     80  if (sz<=8) return sz;
     81  else {
     82    if (sz==16) return 10;
     83    else return 0;  // signale une erreur
     84  }
     85}
     86// Convertit la taille codee dans le tag en taille de donnees (2 (int_2), 4 (int_4, r_4), 8 (r_8) ...) en
     87static inline unsigned char tagsize2datasize( unsigned char sz)
     88{
     89  // Taille permises 1,2,4,8,16
     90  // Pour les tailles <= 8, c'est directement la taille
     91  if (sz<=8) return sz;
     92  else {
     93    if (sz==10) return 10;
     94    else return 0;  // signale une erreur
     95  }
    5096}
    5197
     
    653699
    654700void
    655 PPFBinaryInputStream::CheckTag(short datasz, short datatype)
     701PPFBinaryInputStream::CheckTag(unsigned char datasz, unsigned char datatype)
    656702// datatype = PPS_DATATYPE_CHAR or PPS_DATATYPE_FLOAT or PPS_DATATYPE_INTEGER or PPS_DATATYPE_UNSIGNED
    657703{
    658704  unsigned char ppstype;
    659705  GetTypeTag(ppstype);
    660   if (ppstype != PPS_SIMPLE + datasz + datatype)
     706  if (ppstype != PPS_SIMPLE + datasize2tagsize(datasz) + datatype)
    661707    throw FileFormatExc("PPFBinaryInputStream::CheckTag   bad type in ppersist file");
    662708}
    663709
    664710void
    665 PPFBinaryInputStream::CheckArrayTag(short datasz, size_t sz, short datatype)
     711PPFBinaryInputStream::CheckArrayTag(unsigned char datasz, size_t sz, unsigned char datatype)
    666712// datatype = PPS_DATATYPE_CHAR or PPS_DATATYPE_FLOAT or PPS_DATATYPE_INTEGER or PPS_DATATYPE_UNSIGNED
    667713{
     
    670716  size_t filesz;
    671717  if (sz <= 0x7fffffff) {
    672     if (ppstype != PPS_SIMPLE_ARRAY4 + datasz + datatype)
     718    if (ppstype != PPS_SIMPLE_ARRAY4 + datasize2tagsize(datasz) + datatype)
    673719      throw FileFormatExc("PPFBinaryInputStream::CheckArrayTag   bad type in ppersist file");
    674720    int_4 ff;
    675721    GetRawI4(ff); filesz=ff;
    676722  } else {
    677     if (ppstype != PPS_SIMPLE_ARRAY8 + datasz + datatype)
     723    if (ppstype != PPS_SIMPLE_ARRAY8 + datasize2tagsize(datasz) + datatype)
    678724      throw FileFormatExc("PPFBinaryInputStream::CheckArrayTag   bad type in ppersist file");
    679725    uint_8 ff;
     
    691737}
    692738
    693 
    694739void
    695740PPFBinaryInputStream::GetBytes(void* ptr, size_t bytes)
     
    698743  GetRawBytes(ptr, bytes);
    699744}
     745
    700746void
    701747PPFBinaryInputStream::GetR4   (r_4& result)
     
    715761  if (bigEndian == IS_BIG_ENDIAN) return;
    716762
    717   for (unsigned int i=0; i<n; i++)
    718     bswap4(tab+i);
     763  for (size_t i=0; i<n; i++)  bswap4(tab+i);
    719764
    720765  return;
     
    737782  if (bigEndian == IS_BIG_ENDIAN) return;
    738783
    739   for (unsigned int i=0; i<n; i++)
    740     bswap8(tab+i);
     784  for (size_t i=0; i<n; i++)  bswap8(tab+i);
    741785
    742786  return;
     
    787831  if (bigEndian == IS_BIG_ENDIAN) return;
    788832
    789   for (unsigned int i=0; i<n; i++)
    790     bswap2(tab+i);
     833  for (size_t i=0; i<n; i++)  bswap2(tab+i);
    791834
    792835  return;
     
    809852  if (bigEndian == IS_BIG_ENDIAN) return;
    810853
    811   for (unsigned int i=0; i<n; i++)
    812     bswap2(tab+i);
     854  for (size_t i=0; i<n; i++)  bswap2(tab+i);
    813855
    814856  return;
     
    831873  if (bigEndian == IS_BIG_ENDIAN) return;
    832874
    833   for (unsigned int i=0; i<n; i++)
    834     bswap4(tab+i);
     875  for (size_t i=0; i<n; i++)  bswap4(tab+i);
    835876
    836877  return;
     
    853894  if (bigEndian == IS_BIG_ENDIAN) return;
    854895
    855   for (unsigned int i=0; i<n; i++)
    856     bswap4(tab+i);
     896  for (size_t i=0; i<n; i++)  bswap4(tab+i);
    857897
    858898  return;
     
    876916  if (bigEndian == IS_BIG_ENDIAN) return;
    877917
    878   for (unsigned int i=0; i<n; i++)
    879     bswap8(tab+i);
     918  for (size_t i=0; i<n; i++)  bswap8(tab+i);
    880919
    881920  return;
     
    898937  if (bigEndian == IS_BIG_ENDIAN) return;
    899938
    900   for (unsigned int i=0; i<n; i++)
    901     bswap8(tab+i);
     939  for (size_t i=0; i<n; i++)  bswap8(tab+i);
    902940
    903941  return;
     
    951989
    952990  r_4 * p = (r_4 *)tab;
    953   for (unsigned int i=0; i<n; i++) {
     991  for (size_t i=0; i<n; i++) {
    954992    bswap4(p);  p++;
    955993    bswap4(p);  p++;
     
    9791017
    9801018  r_8 * p = (r_8 *)tab;
    981   for (unsigned int i=0; i<n; i++) {
     1019  for (size_t i=0; i<n; i++) {
    9821020    bswap8(p);  p++;
    9831021    bswap8(p);  p++;
     
    9861024}
    9871025
     1026// Support pour les long double (128 bits floating point numbers)
     1027#ifdef SO_LDBLE128
     1028void
     1029PPFBinaryInputStream::GetR16  (r_16& result)
     1030{
     1031  CheckTag(16,PPS_DATATYPE_FLOAT);
     1032  GetRawBytes(&result, sizeof(r_16));
     1033  if (bigEndian != IS_BIG_ENDIAN)
     1034    bswap16(&result);
     1035}
     1036
     1037void
     1038PPFBinaryInputStream::GetR16s  (r_16* tab, size_t n)
     1039{
     1040  CheckArrayTag(16,n,PPS_DATATYPE_FLOAT);
     1041  GetRawBytes(tab, n*sizeof(r_16));
     1042  if (bigEndian == IS_BIG_ENDIAN) return;
     1043
     1044  for (size_t i=0; i<n; i++)  bswap16(tab+i);
     1045
     1046  return;
     1047}
     1048
     1049void
     1050PPFBinaryInputStream::GetZ16   (complex<r_16>& result)
     1051{
     1052  CheckTag(16,PPS_DATATYPE_COMPLEX);
     1053  r_16 reim[2];
     1054  GetRawBytes(reim, 2*sizeof(r_16));
     1055  if (bigEndian != IS_BIG_ENDIAN) {
     1056    bswap16(reim);
     1057    bswap16(reim+1);
     1058  }
     1059  result = complex<r_16>(reim[0], reim[1]);
     1060}
     1061
     1062void
     1063PPFBinaryInputStream::GetZ16s  (complex<r_16>* tab, size_t n)
     1064{
     1065  CheckArrayTag(16,n,PPS_DATATYPE_COMPLEX);
     1066  GetRawBytes(tab, n*2*sizeof(r_16));
     1067  if (bigEndian == IS_BIG_ENDIAN) return;
     1068
     1069  r_16 * p = (r_16 *)tab;
     1070  for (size_t i=0; i<n; i++) {
     1071    bswap16(p);  p++;
     1072    bswap16(p);  p++;
     1073  }
     1074  return;
     1075}
     1076#endif
    9881077
    9891078void
     
    11851274      else if (ppst3 == PPS_DATATYPE_INTEGER) dtype = "INTEGER x";
    11861275      else if (ppst3 == PPS_DATATYPE_UNSIGNED) dtype = "UNSIGNED x";
    1187       int_4 dsize = ppst1;
     1276      int_4 dsize = (int_4)tagsize2datasize(ppst1);
    11881277      int_8 dsizeskip = dsize;
    11891278      if (ppst30 == PPS_DATATYPE_COMPLEX) {
     
    12361325
    12371326
    1238 //++
    1239 // Class        POutPersist
    1240 // Lib          Outils++
    1241 // include      ppersist.h
    1242 //
    1243 //      Fichier d'objets persistants, en écriture.
    1244 //--
    1245 
    1246 
    1247 //++
    1248 //  POutPersist(string const& flnm, int endianness = PPersist::PPS_NATIVE)
    1249 //
    1250 //      Crée un nouveau fichier ppersist. Par défaut, il est petit=boutien
    1251 //      sur machines petit-boutiennes, et gros-boutien sur machines
    1252 //      gros-boutiennes. On peut explicitement spécifier PPersist::PPS_LITTLE_ENDIAN
    1253 //      ou PPersist::PPS_BIG_ENDIAN.
    1254 //--
    1255 
    12561327/*!
    12571328   \class SOPHYA::PPFBinaryInputStream
     
    13021373  version = 3;
    13031374  // Header
    1304   PutRawBytes("SOS-SOPHYA-PPersistFile V3               ",32);
     1375  PutRawBytes("SOS-SOPHYA-PPersistFile V4               ",32);
    13051376  PutRawBytes(bigEndian
    13061377           ? "BIG-ENDIAN                             "
     
    14821553
    14831554void
    1484 PPFBinaryOutputStream::PutArrayTag(short datasz, size_t sz, short datatype)
     1555PPFBinaryOutputStream::PutArrayTag(unsigned char datasz, size_t sz, unsigned char datatype)
    14851556// datatype = PPS_DATATYPE_CHAR or PPS_DATATYPE_FLOAT or PPS_DATATYPE_INTEGER or PPS_DATATYPE_UNSIGNED
    14861557{
    14871558  if (sz <= 0x7fffffff) {
    1488     PutRawUByte(PPS_SIMPLE_ARRAY4 + datasz + datatype);
     1559    PutRawUByte(PPS_SIMPLE_ARRAY4 + datasize2tagsize(datasz) + datatype);
    14891560    PutRawI4(sz);
    14901561  } else {
    1491     PutRawUByte(PPS_SIMPLE_ARRAY8 + datasz + datatype);
     1562    PutRawUByte(PPS_SIMPLE_ARRAY8 + datasize2tagsize(datasz) + datatype);
    14921563    PutRawU8(sz);
    14931564  }
     
    14971568PPFBinaryOutputStream::PutByte(char c)
    14981569{
    1499   PutRawByte(PPS_SIMPLE + 1 + PPS_DATATYPE_CHAR);
     1570  PutRawByte(PPS_SIMPLE + datasize2tagsize(1) + PPS_DATATYPE_CHAR);
    15001571  PutRawBytes(&c, 1);
    15011572}
     
    15131584PPFBinaryOutputStream::PutR4   (r_4 val)
    15141585{
    1515   PutRawUByte(PPS_SIMPLE + 4 + PPS_DATATYPE_FLOAT);
     1586  PutRawUByte(PPS_SIMPLE + datasize2tagsize(4) + PPS_DATATYPE_FLOAT);
    15161587 
    15171588  if (bigEndian != IS_BIG_ENDIAN)
     
    15291600    PutRawBytes(tab, n*sizeof(r_4));
    15301601  } else {
    1531     for (unsigned int i=0; i<n; i++) {
     1602    for (size_t i=0; i<n; i++) {
    15321603      r_4 val = tab[i];
    15331604      bswap4(&val);
     
    15401611PPFBinaryOutputStream::PutR8   (r_8 val)
    15411612{
    1542   PutRawUByte(PPS_SIMPLE + 8 + PPS_DATATYPE_FLOAT);
     1613  PutRawUByte(PPS_SIMPLE + datasize2tagsize(8) + PPS_DATATYPE_FLOAT);
    15431614
    15441615  if (bigEndian != IS_BIG_ENDIAN)
     
    15561627    PutRawBytes(tab, n*sizeof(r_8));
    15571628  } else {
    1558     for (unsigned int i=0; i<n; i++) {
     1629    for (size_t i=0; i<n; i++) {
    15591630      r_8 val = tab[i];
    15601631      bswap8(&val);
     
    15671638PPFBinaryOutputStream::PutI1   (int_1 val)
    15681639{
    1569   PutRawUByte(PPS_SIMPLE + 1 + PPS_DATATYPE_INTEGER);
     1640  PutRawUByte(PPS_SIMPLE + datasize2tagsize(1) + PPS_DATATYPE_INTEGER);
    15701641  PutRawBytes(&val, sizeof(int_1));
    15711642}
     
    15811652PPFBinaryOutputStream::PutU1   (uint_1 val)
    15821653{
    1583   PutRawUByte(PPS_SIMPLE + 1 + PPS_DATATYPE_UNSIGNED);
     1654  PutRawUByte(PPS_SIMPLE + datasize2tagsize(1) + PPS_DATATYPE_UNSIGNED);
    15841655  PutRawBytes(&val, sizeof(uint_1));
    15851656}
     
    15951666PPFBinaryOutputStream::PutI2   (int_2 val)
    15961667{
    1597   PutRawUByte(PPS_SIMPLE + 2 + PPS_DATATYPE_INTEGER);
     1668  PutRawUByte(PPS_SIMPLE + datasize2tagsize(2) + PPS_DATATYPE_INTEGER);
    15981669
    15991670  if (bigEndian != IS_BIG_ENDIAN)
     
    16111682    PutRawBytes(tab, n*sizeof(int_2));
    16121683  } else {
    1613     for (unsigned int i=0; i<n; i++) {
     1684    for (size_t i=0; i<n; i++) {
    16141685      int_2 val = tab[i];
    16151686      bswap2(&val);
     
    16221693PPFBinaryOutputStream::PutU2   (uint_2 val)
    16231694{
    1624   PutRawUByte(PPS_SIMPLE + 2 + PPS_DATATYPE_UNSIGNED);
     1695  PutRawUByte(PPS_SIMPLE + datasize2tagsize(2) + PPS_DATATYPE_UNSIGNED);
    16251696
    16261697  if (bigEndian != IS_BIG_ENDIAN)
     
    16381709    PutRawBytes(tab, n*sizeof(uint_2));
    16391710  } else {
    1640     for (unsigned int i=0; i<n; i++) {
     1711    for (size_t i=0; i<n; i++) {
    16411712      uint_2 val = tab[i];
    16421713      bswap2(&val);
     
    16491720PPFBinaryOutputStream::PutI4   (int_4 val)
    16501721{
    1651   PutRawUByte(PPS_SIMPLE + 4 + PPS_DATATYPE_INTEGER);
     1722  PutRawUByte(PPS_SIMPLE + datasize2tagsize(4) + PPS_DATATYPE_INTEGER);
    16521723
    16531724  if (bigEndian != IS_BIG_ENDIAN)
     
    16651736    PutRawBytes(tab, n*sizeof(int_4));
    16661737  } else {
    1667     for (unsigned int i=0; i<n; i++) {
     1738    for (size_t i=0; i<n; i++) {
    16681739      int_4 val = tab[i];
    16691740      bswap4(&val);
     
    16761747PPFBinaryOutputStream::PutU4   (uint_4 val)
    16771748{
    1678   PutRawUByte(PPS_SIMPLE + 4 + PPS_DATATYPE_UNSIGNED);
     1749  PutRawUByte(PPS_SIMPLE + datasize2tagsize(4) + PPS_DATATYPE_UNSIGNED);
    16791750
    16801751  if (bigEndian != IS_BIG_ENDIAN)
     
    16921763    PutRawBytes(tab, n*sizeof(uint_4));
    16931764  } else {
    1694     for (unsigned int i=0; i<n; i++) {
     1765    for (size_t i=0; i<n; i++) {
    16951766      uint_4 val = tab[i];
    16961767      bswap4(&val);
     
    17031774PPFBinaryOutputStream::PutI8   (int_8 val)
    17041775{
    1705   PutRawUByte(PPS_SIMPLE + 8 + PPS_DATATYPE_INTEGER);
     1776  PutRawUByte(PPS_SIMPLE + datasize2tagsize(8) + PPS_DATATYPE_INTEGER);
    17061777
    17071778  if (bigEndian != IS_BIG_ENDIAN)
     
    17191790    PutRawBytes(tab, n*sizeof(int_8));
    17201791  } else {
    1721     for (unsigned int i=0; i<n; i++) {
     1792    for (size_t i=0; i<n; i++) {
    17221793      int_8 val = tab[i];
    17231794      bswap8(&val);
     
    17301801PPFBinaryOutputStream::PutU8   (uint_8 val)
    17311802{
    1732   PutRawUByte(PPS_SIMPLE + 8 + PPS_DATATYPE_UNSIGNED);
     1803  PutRawUByte(PPS_SIMPLE + datasize2tagsize(8) + PPS_DATATYPE_UNSIGNED);
    17331804
    17341805  if (bigEndian != IS_BIG_ENDIAN)
     
    17461817    PutRawBytes(tab, n*sizeof(uint_8));
    17471818  } else {
    1748     for (unsigned int i=0; i<n; i++) {
     1819    for (size_t i=0; i<n; i++) {
    17491820      uint_8 val = tab[i];
    17501821      bswap8(&val);
     
    17731844PPFBinaryOutputStream::PutZ4   (complex<r_4> val)
    17741845{
    1775   PutRawUByte(PPS_SIMPLE + 4 + PPS_DATATYPE_COMPLEX);
     1846  PutRawUByte(PPS_SIMPLE + datasize2tagsize(4) + PPS_DATATYPE_COMPLEX);
    17761847  r_4 reim[2];
    17771848  reim[0] = val.real();
     
    17921863    PutRawBytes(tab, n*2*sizeof(r_4));
    17931864  } else {
    1794     for (unsigned int i=0; i<n; i++) {
     1865    for (size_t i=0; i<n; i++) {
    17951866      r_4 reim[2];
    17961867      reim[0] = tab[i].real();
     
    18061877PPFBinaryOutputStream::PutZ8   (complex<r_8> val)
    18071878{
    1808   PutRawUByte(PPS_SIMPLE + 8 + PPS_DATATYPE_COMPLEX);
     1879  PutRawUByte(PPS_SIMPLE + datasize2tagsize(8) + PPS_DATATYPE_COMPLEX);
    18091880  r_8 reim[2];
    18101881  reim[0] = val.real();
     
    18251896    PutRawBytes(tab, n*2*sizeof(r_8));
    18261897  } else {
    1827     for (unsigned int i=0; i<n; i++) {
     1898    for (size_t i=0; i<n; i++) {
    18281899      r_8 reim[2];
    18291900      reim[0] = tab[i].real();
     
    18361907}
    18371908
     1909// Support pour les long double (128 bits floating point numbers)
     1910#ifdef SO_LDBLE128
     1911void
     1912PPFBinaryOutputStream::PutR16   (r_16 val)
     1913{
     1914  PutRawUByte(PPS_SIMPLE + datasize2tagsize(16) + PPS_DATATYPE_FLOAT);
     1915
     1916  if (bigEndian != IS_BIG_ENDIAN)
     1917    bswap16(&val);
     1918
     1919  PutRawBytes(&val, sizeof(r_16));
     1920}
     1921
     1922void
     1923PPFBinaryOutputStream::PutR16s  (r_16 const* tab, size_t n)
     1924{
     1925  PutArrayTag(16, n, PPS_DATATYPE_FLOAT);
     1926
     1927  if (bigEndian == IS_BIG_ENDIAN) {
     1928    PutRawBytes(tab, n*sizeof(r_16));
     1929  } else {
     1930    for (size_t i=0; i<n; i++) {
     1931      r_16 val = tab[i];
     1932      bswap16(&val);
     1933      PutRawBytes(&val, sizeof(r_16));
     1934    }
     1935  }
     1936}
     1937
     1938void
     1939PPFBinaryOutputStream::PutZ16   (complex<r_16> val)
     1940{
     1941  PutRawUByte(PPS_SIMPLE + datasize2tagsize(16) + PPS_DATATYPE_COMPLEX);
     1942  r_16 reim[2];
     1943  reim[0] = val.real();
     1944  reim[1] = val.imag();
     1945  if (bigEndian != IS_BIG_ENDIAN) {
     1946    bswap16(reim);
     1947    bswap16(reim+1);
     1948  }
     1949  PutRawBytes(reim, 2*sizeof(r_16));
     1950}
     1951
     1952void
     1953PPFBinaryOutputStream::PutZ16s  (complex<r_16> const* tab, size_t n)
     1954{
     1955  PutArrayTag(16, n, PPS_DATATYPE_COMPLEX);
     1956
     1957  if (bigEndian == IS_BIG_ENDIAN) {
     1958    PutRawBytes(tab, n*2*sizeof(r_16));
     1959  } else {
     1960    for (size_t i=0; i<n; i++) {
     1961      r_16 reim[2];
     1962      reim[0] = tab[i].real();
     1963      reim[1] = tab[i].imag();
     1964      bswap16(reim);
     1965      bswap16(reim+1);
     1966      PutRawBytes(reim, 2*sizeof(r_16));
     1967    }
     1968  }
     1969}
     1970#endif
     1971
    18381972void
    18391973PPFBinaryOutputStream::PutPosTagTable(int_8 const * ptab, size_t sz)
Note: See TracChangeset for help on using the changeset viewer.