Skip to content

Instantly share code, notes, and snippets.

@Xliff
Last active April 28, 2016 03:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Xliff/b4c6675c7f3b89a21e640adb05e8269c to your computer and use it in GitHub Desktop.
Save Xliff/b4c6675c7f3b89a21e640adb05e8269c to your computer and use it in GitHub Desktop.

SOLVED -- See below.

I have the following code, in C:

buffer[0][i]=((readbuffer[i*4+1]<<8)|
    (0x00ff&(int)readbuffer[i*4]))/32768.f;
buffer[1][i]=((readbuffer[i*4+3]<<8)|
    (0x00ff&(int)readbuffer[i*4+2]))/32768.f;

Here is the perl6 translit:

@buffer0[$i] = Num(
	(($readbuff[$i * 4 + 1] +< 8) +|
	(0x00ff +& $readbuff[$i * 4].Int)) / 32768e0
);

@buffer1[$i] = Num(
	(($readbuff[$i * 4 + 3] +< 8) +|
	(0x00ff +& $readbuff[$i * 4 + 2].Int)) / 32768e0
);

The problem is, I am not getting the same results and it looks like it might be due to some weird conversions.

The C output:

Buffer0 is 0
Buffer1 is 0
Byte1 is 0
Byte2 is 0
Byte3 is 255
Byte4 is 255

Buffer0 is 0
Buffer1 is 0
Byte1 is 0
Byte2 is 0
Byte3 is 1
Byte4 is 0

The equivalent Perl6 output:

Buffer0 is 0
Buffer1 is 1.99996948242188
Byte1 is 0
Byte2 is 0
Byte3 is 255
Byte4 is 255

Buffer0 is 0
Buffer1 is 3.0517578125e-05
Byte1 is 0
Byte2 is 0
Byte3 is 1
Byte4 is 0

Here's what solved the problem. The weird thing about some libs is that they will define their read buffers as signed char. By default, Perl6 uses unsigned char Blobs with IO::Handler::read().

The solution was simple. I changed:

my $readbuff = $fh.read(READ * 4);

to:

my Blob[int8] $readbuff = Blob[int8].new($fh.read(READ * 4)); # This could probably be simplified.

Thanks again to sortiz++ on freenode:#perl6 for all of the help.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment