> On this machine, NVs are doubles; their pack_type is 'd', which is
> as it should be. So, a number (2.0) is inserted into the bytecode
> stream with pack('d', 2.0)

./perl -Ilib -MDevel::Peek -le 'Dump(pack("d",2))'
SV = PV(0x1d9ed4) at 0x1e4568
  REFCNT = 1
  FLAGS = (PADTMP,POK,pPOK)
  PV = 0x1de638 "@\0\0\0\0\0\0\0"\0
  CUR = 8
  LEN = 9

./perl -Ilib -MDevel::Peek -le 'Dump(pack("f",2))'
SV = PV(0x1d9ed4) at 0x1e4568
  REFCNT = 1
  FLAGS = (PADTMP,POK,pPOK)
  PV = 0x1d97a0 "@\0\0\0"\0
  CUR = 4
  LEN = 5

> However, when I try dereferencing this as an NV, I get a bus error.
> Here's the debug:
>
> Program received signal SIGBUS, Bus error.
> 0x15230 in Parrot_op_set_n_nc (cur_opcode=0x7001c044, interpreter=0x30028) at 
>basic_opcodes.c:276

Bus error indicates unaligned access, not malformed fp.

> 276       NUM_REG(cur_opcode[1]) = *(NV *)&cur_opcode[2];
> (gdb) x/dg &cur_opcode[2]
> 0x7001c04c:     4611686018427387904

You are trying to access doubles (which must be aligned by 8) at an offset
which is aligned only by 4, not by 8.

> (gdb) x/fg &cur_opcode[2]
> 0x7001c04c:     2
> 
> Now, notice that when I use the *float* pattern to dereference it, I get the
> right answer. This leads me to believe that pack("d") in Perl 5.6.1 on Sparc
> Linux is not packing "double-precision float in the native format" as it claims,
> but is actually packing a single-precision float.

Because floats are 4 bytes they can be forcibly dereferenced at
offsets divisible by 4.

By sheer luck dereferencing the first four bytes of (double)2 produces
a valid result also as a float.  Try, say, 2.1 and you won't be so lucky.

./perl -Ilib -MDevel::Peek -le 'Dump(pack("d",2.1))'
SV = PV(0x1d9ed4) at 0x1e4568
  REFCNT = 1
  FLAGS = (PADTMP,POK,pPOK)
  PV = 0x1de638 "@\0\314\314\314\314\314\315"\0
  CUR = 8
  LEN = 9
./perl -Ilib -MDevel::Peek -le 'Dump(pack("f",2.1))'
SV = PV(0x1d9ed4) at 0x1e4568
  REFCNT = 1
  FLAGS = (PADTMP,POK,pPOK)
  PV = 0x1d97a0 "@\6ff"\0
  CUR = 4
  LEN = 5

> I know there's been some discussion about pack on p5p recently, but I haven't
> been able to get my head around it. Jarkko, does this ring any bells?

-- 
$jhi++; # http://www.iki.fi/jhi/
        # There is this special biologist word we use for 'stable'.
        # It is 'dead'. -- Jack Cohen

Reply via email to