Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Need fast method to read/convert floats from disk

Need fast method to read/convert floats from disk

Scheduled Pinned Locked Moved C / C++ / MFC
graphicsgame-devalgorithmsdata-structuresperformance
4 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Offline
    N Offline
    nadiric
    wrote on last edited by
    #1

    I'm reading a bunch of raw numerical data (floats representing verticies) from disk in one endian format, and need to convert it to another (flip the bytes around). Currently I am reading the numbers one by one and bit-shifting them to the correct format, then placing them into their respective places in an array of verticies (float[3]'s). I am trying to speed up this whole operation, and may be able to do so with a better method/algorithm. The following also may help: - Each float is 4 bytes and needs a pure reversal of bytes - not bits (bytes 1234 become 4321) - the floats are contiguous on disk - I know ahead of time how many floats I will be loading into a given array - The array will be pre-allocated to hold the right number of verticies - I must keep (or end up with) the array two-dimensional (array of float[3]'s) for later OpenGL usage not sure if reading them all in at once, and applying a macro-bit-shift would be faster? Thanks for any help

    Z 1 Reply Last reply
    0
    • N nadiric

      I'm reading a bunch of raw numerical data (floats representing verticies) from disk in one endian format, and need to convert it to another (flip the bytes around). Currently I am reading the numbers one by one and bit-shifting them to the correct format, then placing them into their respective places in an array of verticies (float[3]'s). I am trying to speed up this whole operation, and may be able to do so with a better method/algorithm. The following also may help: - Each float is 4 bytes and needs a pure reversal of bytes - not bits (bytes 1234 become 4321) - the floats are contiguous on disk - I know ahead of time how many floats I will be loading into a given array - The array will be pre-allocated to hold the right number of verticies - I must keep (or end up with) the array two-dimensional (array of float[3]'s) for later OpenGL usage not sure if reading them all in at once, and applying a macro-bit-shift would be faster? Thanks for any help

      Z Offline
      Z Offline
      Zac Howland
      wrote on last edited by
      #2

      nadiric wrote:

      not sure if reading them all in at once, and applying a macro-bit-shift would be faster?

      Yes. Reading them in one at a time increases disk access (which is expensive). Reading them in as a block decreases the number of reads required to get your data. Something along the lines of the following should work:

      // for converting float
      float endian_shift(const float& f)
      {
      	float ret = 0.0;
      	ret |= (f << 24);
      	ret |= ((f >> 8) << 24) >> 8;
      	ret |= ((f >> 16) << 24) >> 16;
      	ret |= (f >> 24);
      	return ret;
      }
      
      // for reading data
      ifstream fin;
      fin.open("myfile.dat", ios::binary);
      vector<float> myData(SIZE);
      copy(istream_iterator<float>(fin), istream_iterator<float>(), back_inserter(myData));
      fin.close();
      transform(myData.begin(), myData.end(), myData.begin(), endian_shift);
      

      If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week Zac

      N 1 Reply Last reply
      0
      • Z Zac Howland

        nadiric wrote:

        not sure if reading them all in at once, and applying a macro-bit-shift would be faster?

        Yes. Reading them in one at a time increases disk access (which is expensive). Reading them in as a block decreases the number of reads required to get your data. Something along the lines of the following should work:

        // for converting float
        float endian_shift(const float& f)
        {
        	float ret = 0.0;
        	ret |= (f << 24);
        	ret |= ((f >> 8) << 24) >> 8;
        	ret |= ((f >> 16) << 24) >> 16;
        	ret |= (f >> 24);
        	return ret;
        }
        
        // for reading data
        ifstream fin;
        fin.open("myfile.dat", ios::binary);
        vector<float> myData(SIZE);
        copy(istream_iterator<float>(fin), istream_iterator<float>(), back_inserter(myData));
        fin.close();
        transform(myData.begin(), myData.end(), myData.begin(), endian_shift);
        

        If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week Zac

        N Offline
        N Offline
        nadiric
        wrote on last edited by
        #3

        Thanks for the reply, Just tossing a few other thoughts around... Given the following: - vertex is of type float[3] - verticies is an array of SIZE vertex (verticies = new vertex[SIZE];) - vertsfromfile is an array of (SIZE*3) floats are the floats in "verticies" aligned one after the other in this fashion: verticies[0][0]verticies[0][1]verticies[0][2]verticies[1][0]verticies[1][1]...and so on to allow for something like memcpy(verticies,vertsfromfile,(sizeof(vertex)*SIZE)) ? or is this just crazy talk? Thanks

        Z 1 Reply Last reply
        0
        • N nadiric

          Thanks for the reply, Just tossing a few other thoughts around... Given the following: - vertex is of type float[3] - verticies is an array of SIZE vertex (verticies = new vertex[SIZE];) - vertsfromfile is an array of (SIZE*3) floats are the floats in "verticies" aligned one after the other in this fashion: verticies[0][0]verticies[0][1]verticies[0][2]verticies[1][0]verticies[1][1]...and so on to allow for something like memcpy(verticies,vertsfromfile,(sizeof(vertex)*SIZE)) ? or is this just crazy talk? Thanks

          Z Offline
          Z Offline
          Zac Howland
          wrote on last edited by
          #4

          If the file is written out such that each vertex is written as float1float2float3, then writing out a series of vertices would just mean you would have vertex1vertex2vertex3... where each vertex matched the pervious pattern. Thus, you could read them in in such a fashion (though, I would still stick with using the copy algorithm).

          If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week Zac

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups