pack("l"), 64 bit question

D

Daniel Berger

Hi all,

Ruby 1.8.4
Solaris 10

Is this correct?

# 32 bit
irb(main):002:0> [-1].pack("l")
=> "\377\377\377\377"
irb(main):003:0> "\377\377\377\377".unpack("l")
=> [-1]

# 64 bit
irb(main):002:0> [-1].pack("l")
=> "\377\377\377\377"
irb(main):003:0> "\377\377\377\377".unpack("l")
=> [4294967295]

Regards,

Dan
 
A

ara.t.howard

Hi all,

Ruby 1.8.4
Solaris 10

Is this correct?

# 32 bit
irb(main):002:0> [-1].pack("l")
=> "\377\377\377\377"
irb(main):003:0> "\377\377\377\377".unpack("l")
=> [-1]

# 64 bit
irb(main):002:0> [-1].pack("l")
=> "\377\377\377\377"
irb(main):003:0> "\377\377\377\377".unpack("l")
=> [4294967295]

Regards,

Dan

yes. 'l' is unsigned. i think you may want 'i'.

regards.

-a
 
A

ara.t.howard

Hi all,

Ruby 1.8.4
Solaris 10

Is this correct?

# 32 bit
irb(main):002:0> [-1].pack("l")
=> "\377\377\377\377"
irb(main):003:0> "\377\377\377\377".unpack("l")
=> [-1]

# 64 bit
irb(main):002:0> [-1].pack("l")
=> "\377\377\377\377"
irb(main):003:0> "\377\377\377\377".unpack("l")
=> [4294967295]

Regards,

Dan

btw. this is useful for me

irb(main):008:0> to_bin = lambda{|s| '[ ' << s.unpack('c*').map{|b| '%8.8b' % b}.join(' ') << ' ]'}
=> #<Proc:0xb7569f90@(irb):8>

irb(main):009:0> to_bin[ [-1].pack("l") ]
=> "[ 11111111 11111111 11111111 11111111 ]"

irb(main):010:0> to_bin[ [-1].pack("i") ]
=> "[ 11111111 11111111 11111111 11111111 ]"

irb(main):011:0> to_bin[ [1].pack("l") ]
=> "[ 00000001 00000000 00000000 00000000 ]"

irb(main):012:0> to_bin[ [1].pack("i") ]
=> "[ 00000001 00000000 00000000 00000000 ]"


-a
 
Y

Yukihiro Matsumoto

Hi,

In message "Re: pack("l"), 64 bit question"

|Ruby 1.8.4
|Solaris 10
|
|Is this correct?
|
|# 32 bit
|irb(main):002:0> [-1].pack("l")
|=> "\377\377\377\377"
|irb(main):003:0> "\377\377\377\377".unpack("l")
|=> [-1]
|
|# 64 bit
|irb(main):002:0> [-1].pack("l")
|=> "\377\377\377\377"
|irb(main):003:0> "\377\377\377\377".unpack("l")
|=> [4294967295]

Hmm, unpack("l") should have returned negative value unless "_" suffix
is supplied.

matz.
 
D

Daniel Berger

Yukihiro said:
List-Unsubscribe: <mailto:[email protected]?body=unsubscribe>

Hi,

In message "Re: pack("l"), 64 bit question"

|Ruby 1.8.4
|Solaris 10
|
|Is this correct?
|
|# 32 bit
|irb(main):002:0> [-1].pack("l")
|=> "\377\377\377\377"
|irb(main):003:0> "\377\377\377\377".unpack("l")
|=> [-1]
|
|# 64 bit
|irb(main):002:0> [-1].pack("l")
|=> "\377\377\377\377"
|irb(main):003:0> "\377\377\377\377".unpack("l")
|=> [4294967295]

Hmm, unpack("l") should have returned negative value unless "_" suffix
is supplied.

matz.

Here's some more info that may or may not be useful:

# 64 bit
irb(main):004:0> "\377\377\377\377".unpack("l_")
=> [nil]
irb(main):005:0> [-1].pack("l_")
=> "\377\377\377\377\377\377\377\377"
irb(main):006:0> "\377\377\377\377\377\377\377\377".unpack("l_")
=> [-1]

Regards,

Dan
 
Y

Yukihiro Matsumoto

Hi,

In message "Re: pack("l"), 64 bit question"

||irb(main):003:0> "\377\377\377\377".unpack("l")
||=> [4294967295]
|
|Hmm, unpack("l") should have returned negative value unless "_" suffix
|is supplied.

This means EXTEND32() macro in pack.c is not working on 64bit Solaris
(and perhaps on other 64bit systems neither). I have no 64bit machine
at hand. Could somebody confirm?

matz.
 
H

H.Yamamoto

Hello.
This means EXTEND32() macro in pack.c is not working on 64bit Solaris
(and perhaps on other 64bit systems neither). I have no 64bit machine
at hand. Could somebody confirm?

Me neigher. But we can use HP TestDrive :)

Probably this patch will solve the problem.

Index: pack.c
===================================================================
RCS file: /src/ruby/pack.c,v
retrieving revision 1.62.2.12
diff -u -w -b -p -r1.62.2.12 pack.c
--- pack.c 13 Oct 2005 14:30:49 -0000 1.62.2.12
+++ pack.c 16 Feb 2006 06:01:07 -0000
@@ -347,11 +347,11 @@ num2i32(x)
return 0; /* not reached */
}

-#if SIZEOF_LONG == SIZE32 || SIZEOF_INT == SIZE32
+#if SIZEOF_LONG == SIZE32
# define EXTEND32(x)
#else
/* invariant in modulo 1<<31 */
-# define EXTEND32(x) do {if (!natint) {(x) = (I32)(((1<<31)-1-(x))^~(~0<<31));}} while(0)
+# define EXTEND32(x) do { if (!natint) {(x) = (((1L<<31)-1-(x))^~(~0L<<31));}} while(0)
#endif
#if SIZEOF_SHORT == SIZE16
# define EXTEND16(x)
 
Y

Yukihiro Matsumoto

Hi,

In message "Re: pack("l"), 64 bit question"
|
|>This means EXTEND32() macro in pack.c is not working on 64bit Solaris
|>(and perhaps on other 64bit systems neither). I have no 64bit machine
|>at hand. Could somebody confirm?
|
|Me neigher. But we can use HP TestDrive :)
|
|Probably this patch will solve the problem.

Daniel, could you try this patch on your 64bit box?

matz.

|--- pack.c 13 Oct 2005 14:30:49 -0000 1.62.2.12
|+++ pack.c 16 Feb 2006 06:01:07 -0000
|@@ -347,11 +347,11 @@ num2i32(x)
| return 0; /* not reached */
| }
|
|-#if SIZEOF_LONG == SIZE32 || SIZEOF_INT == SIZE32
|+#if SIZEOF_LONG == SIZE32
| # define EXTEND32(x)
| #else
| /* invariant in modulo 1<<31 */
|-# define EXTEND32(x) do {if (!natint) {(x) = (I32)(((1<<31)-1-(x))^~(~0<<31));}} while(0)
|+# define EXTEND32(x) do { if (!natint) {(x) = (((1L<<31)-1-(x))^~(~0L<<31));}} while(0)
| #endif
| #if SIZEOF_SHORT == SIZE16
| # define EXTEND16(x)
 
V

Ville Mattila

Yukihiro Matsumoto said:
Hi,

In message "Re: pack("l"), 64 bit question"
|
|>This means EXTEND32() macro in pack.c is not working on 64bit Solaris
|>(and perhaps on other 64bit systems neither). I have no 64bit machine
|>at hand. Could somebody confirm?
|
|Me neigher. But we can use HP TestDrive :)
|
|Probably this patch will solve the problem.

Daniel, could you try this patch on your 64bit box?

The pach works for me on my opteron 64 box. Here is another.
The sun studio cc, warned
cc: Warning: -fsimple option is ignored.
"../pack.c", line 1954: warning: integer overflow detected: op "<<"
"../pack.c", line 1954: warning: initializer does not fit or is out of range: -144115188075855872

Is the patch correct?

index: pack.c
===================================================================
RCS file: /src/ruby/pack.c,v
retrieving revision 1.62.2.12
@@ -1951,7 +1951,7 @@ pack_unpack(str, fmt)
case 'w':
{
unsigned long ul = 0;
- unsigned long ulmask = 0xfeL << ((sizeof(unsigned long) - 1) * 8);
+ unsigned long ulmask = 0xfeUL << ((sizeof(unsigned long) - 1UL) * 8UL);

while (len > 0 && s < send) {
ul <<= 7;
 
D

Daniel Berger

Yukihiro said:
Hi,

In message "Re: pack("l"), 64 bit question"
|
|>This means EXTEND32() macro in pack.c is not working on 64bit Solaris
|>(and perhaps on other 64bit systems neither). I have no 64bit machine
|>at hand. Could somebody confirm?
|
|Me neigher. But we can use HP TestDrive :)
|
|Probably this patch will solve the problem.

Daniel, could you try this patch on your 64bit box?

matz.

|--- pack.c 13 Oct 2005 14:30:49 -0000 1.62.2.12
|+++ pack.c 16 Feb 2006 06:01:07 -0000
|@@ -347,11 +347,11 @@ num2i32(x)
| return 0; /* not reached */
| }
|
|-#if SIZEOF_LONG == SIZE32 || SIZEOF_INT == SIZE32
|+#if SIZEOF_LONG == SIZE32
| # define EXTEND32(x)
| #else
| /* invariant in modulo 1<<31 */
|-# define EXTEND32(x) do {if (!natint) {(x) = (I32)(((1<<31)-1-(x))^~(~0<<31));}} while(0)
|+# define EXTEND32(x) do { if (!natint) {(x) = (((1L<<31)-1-(x))^~(~0L<<31));}} while(0)
| #endif
| #if SIZEOF_SHORT == SIZE16
| # define EXTEND16(x)

Looks good. The only one I found odd was the [nil] returned by
"\377\377\377\377".unpack("l_"). Is that expected? The 32 bit version returns
[-1].

# 64 bit Ruby

irb(main):001:0> [-1].pack("l")
=> "\377\377\377\377"
irb(main):002:0> "\377\377\377\377".unpack("l")
=> [-1]
irb(main):003:0> "\377\377\377\377".unpack("l_")
=> [nil]
irb(main):004:0> [-1].pack("l_")
=> "\377\377\377\377\377\377\377\377"
irb(main):005:0> "\377\377\377\377\377\377\377\377".unpack("l")
=> [-1]
irb(main):006:0> "\377\377\377\377\377\377\377\377".unpack("l_")
=> [-1]

Thanks,

Dan
 
Y

Yukihiro Matsumoto

Hi,

In message "Re: pack("l"), 64 bit question"

|> Daniel, could you try this patch on your 64bit box?

|Looks good. The only one I found odd was the [nil] returned by
|"\377\377\377\377".unpack("l_"). Is that expected? The 32 bit version returns
|[-1].

No, it should return [4294967295]. How about a patch from Ville
Mattila in [ruby-talk:180126]?

matz.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,143
Latest member
SterlingLa
Top