FastCoding icon indicating copy to clipboard operation
FastCoding copied to clipboard

alignment error in FCReadFloat64 on 32 bit devices

Open matt3 opened this issue 10 years ago • 4 comments

Occasionally get an alignment error on 32 bit devices. This fix works for me:

static id FCReadFloat64(__unsafe_unretained FCNSDecoder* decoder) {
  FC_ALIGN_INPUT(double_t, *decoder->_offset);
  FC_READ_VALUE(uint64_t, *decoder->_offset, decoder->_input, decoder->_total);
  __autoreleasing NSNumber* number = @(*(double_t*)&value);
  return number;
}

matt3 avatar Apr 02 '15 04:04 matt3

I don't understand how this could possibly work. Can you provide a sample project so I can verify before I merge the fix? Thanks.

nicklockwood avatar Apr 02 '15 10:04 nicklockwood

I checked and it looks like double_t isn't what I thought it was (it's defined as being a minimum of 8 bytes, rather than exactly 8 bytes). I've changed the definitions, so hopefully it should work now (version 3.2.1). Let me know if not.

nicklockwood avatar Apr 02 '15 10:04 nicklockwood

I found the issue occurs on the iPhone 4s when _input is misaligned. When cast as a double, the device tries to load 8 bytes into a floating point register. When cast as an int64, 8 bytes are loaded into a general purpose register. Unlike the float registers, the general purpose registers can work with misaligned data.

matt3 avatar Apr 02 '15 17:04 matt3

The data shouldn't be misaligned in the first place though: that's the point of the align statement.

nicklockwood avatar Apr 02 '15 18:04 nicklockwood