diff --git a/components/nif/nifstream.hpp b/components/nif/nifstream.hpp index 35c042e7d..4d7e39ecb 100644 --- a/components/nif/nifstream.hpp +++ b/components/nif/nifstream.hpp @@ -41,6 +41,25 @@ template inline void readLittleEndianBufferO #endif } +template inline void readLittleEndianDynamicBufferOfType(Files::IStreamPtr &pIStream, T* dest, uint32_t numInstances) +{ +#if defined(__x86_64__) || defined(_M_X64) || defined(__i386) || defined(_M_IX86) + pIStream->read((char*)dest, numInstances * sizeof(T)); +#else + char buffer[numInstances * sizeof(T)]; + pIStream->read((char*)buffer, numInstances * sizeof(T)); + /* + Due to the loop iterations being known at compile time, + this nested loop will most likely be unrolled + */ + for (uint32_t i = 0; i < numInstances; i++) + { + dest[i] = 0; + for (uint32_t byte = 0; byte < sizeof(T); byte++) + dest[i] |= ((T)buffer[i * sizeof(T) + byte]) << (byte * 8); + } +#endif +} template type inline readLittleEndianType(Files::IStreamPtr &pIStream) { type val; @@ -146,28 +165,23 @@ public: void getUShorts(std::vector &vec, size_t size) { vec.resize(size); - for (size_t i = 0;i < vec.size();i++) - vec[i] = getUShort(); + readLittleEndianDynamicBufferOfType(inp, &vec.front(), size); } void getFloats(std::vector &vec, size_t size) { vec.resize(size); - for (size_t i = 0;i < vec.size();i++) - vec[i] = getFloat(); + readLittleEndianDynamicBufferOfType(inp, &vec.front(), size); } void getVector2s(std::vector &vec, size_t size) { vec.resize(size); - for (size_t i = 0;i < vec.size();i++) - vec[i] = getVector2(); + readLittleEndianDynamicBufferOfType(inp, &vec.front(), size); } void getVector3s(std::vector &vec, size_t size) { vec.resize(size); - for (size_t i = 0;i < vec.size();i++) - vec[i] = getVector3(); + readLittleEndianDynamicBufferOfType(inp, &vec.front(), size); } void getVector4s(std::vector &vec, size_t size) { vec.resize(size); - for (size_t i = 0;i < vec.size();i++) - vec[i] = getVector4(); + readLittleEndianDynamicBufferOfType(inp, &vec.front(), size); } void getQuaternions(std::vector &quat, size_t size) { quat.resize(size);