import math
import struct


def _convertFloatTo32BitInt(x):
    """Convert 32-bit float to integer.
    
    Perform something akin to a C-style memory cast in order to achieve
    conversion of float value into integer. The souce paper has C code
    along the lines of:
    
      uint32_t v = *(long*)&x;
      if (v < 0)
      {
          v = 0x80000000 - v;
      }

    However, this is relying on the way 32-bit integer math works on a CPU.
    In the Python world, integers behave more mathmatically.

    So, what this does it map a 32-bit float as::

      < 0 --------->-0x80000001 ...

      -0.0 --.
              >----> 0x80000000
      +0.0 --'

      > 0 ---------> 0x7fffffff ....

    :Return:
        The integer representation as a long. For non-NaN values this is will
        be in the range 0 to 0xffffffff. If x is a NaN then the value -1 is
        returned.

    """
    if math.isnan(x):
        return -1
    ival = struct.unpack('I', struct.pack('f', x))[0]
    if ival == 0:
        ival = 0x80000000
    elif ival < 0x80000000:
        ival += 0x80000000
    else:
        ival = 0x100000000 - ival
    return ival