develooper Front page | perl.cvs.parrot | Postings from December 2008

[svn:parrot] r34190 - in trunk: . languages/lua/config/makefiles languages/lua/doc languages/lua/src/lib languages/lua/t

From:
fperrad
Date:
December 21, 2008 02:34
Subject:
[svn:parrot] r34190 - in trunk: . languages/lua/config/makefiles languages/lua/doc languages/lua/src/lib languages/lua/t
Message ID:
20081221103444.51E52CBA12@x12.develooper.com
Author: fperrad
Date: Sun Dec 21 02:34:41 2008
New Revision: 34190

Added:
   trunk/languages/lua/src/lib/bit.pir   (contents, props changed)
   trunk/languages/lua/t/bit.t   (contents, props changed)
Modified:
   trunk/MANIFEST
   trunk/MANIFEST.SKIP
   trunk/languages/lua/config/makefiles/root.in
   trunk/languages/lua/doc/status.pod

Log:
[Lua] bit
- an another bitwise operation library

Modified: trunk/MANIFEST
==============================================================================
--- trunk/MANIFEST	(original)
+++ trunk/MANIFEST	Sun Dec 21 02:34:41 2008
@@ -1,7 +1,7 @@
 # ex: set ro:
 # $Id$
 #
-# generated by tools\dev\mk_manifest_and_skip.pl Fri Dec 19 13:11:06 2008 UT
+# generated by tools/dev/mk_manifest_and_skip.pl Sun Dec 21 10:31:45 2008 UT
 #
 # See tools/dev/install_files.pl for documentation on the
 # format of this file.
@@ -1754,6 +1754,7 @@
 languages/lua/src/lib/alarm.pir                             [lua]
 languages/lua/src/lib/base64.pir                            [lua]
 languages/lua/src/lib/bc.pir                                [lua]
+languages/lua/src/lib/bit.pir                               [lua]
 languages/lua/src/lib/bitlib.pir                            [lua]
 languages/lua/src/lib/complex.pir                           [lua]
 languages/lua/src/lib/gl.lua                                [lua]
@@ -1814,6 +1815,7 @@
 languages/lua/t/base64.t                                    [lua]
 languages/lua/t/basic.t                                     [lua]
 languages/lua/t/bc.t                                        [lua]
+languages/lua/t/bit.t                                       [lua]
 languages/lua/t/bitlib.t                                    [lua]
 languages/lua/t/boolean.t                                   [lua]
 languages/lua/t/closure.t                                   [lua]
@@ -2971,9 +2973,9 @@
 src/jit/amd64/jit_defs.c                                    []
 src/jit/amd64/jit_emit.h                                    []
 src/jit/arm/core.jit                                        []
-src/jit/arm/jit_defs.c                                      []
 src/jit/arm/exec_dep.c                                      []
 src/jit/arm/exec_dep.h                                      []
+src/jit/arm/jit_defs.c                                      []
 src/jit/arm/jit_emit.h                                      []
 src/jit/hppa/core.jit                                       []
 src/jit/hppa/jit_defs.c                                     []
@@ -3053,6 +3055,7 @@
 src/pmc/callsignature.pmc                                   []
 src/pmc/capture.pmc                                         []
 src/pmc/class.pmc                                           []
+src/pmc/closure.pmc                                         []
 src/pmc/codestring.pmc                                      []
 src/pmc/complex.pmc                                         []
 src/pmc/continuation.pmc                                    []

Modified: trunk/MANIFEST.SKIP
==============================================================================
--- trunk/MANIFEST.SKIP	(original)
+++ trunk/MANIFEST.SKIP	Sun Dec 21 02:34:41 2008
@@ -1,6 +1,6 @@
 # ex: set ro:
 # $Id$
-# generated by tools\dev\mk_manifest_and_skip.pl Fri Dec 19 13:05:26 2008 UT
+# generated by tools/dev/mk_manifest_and_skip.pl Sun Dec 21 10:31:45 2008 UT
 #
 # This file should contain a transcript of the svn:ignore properties
 # of the directories in the Parrot subversion repository. (Needed for
@@ -1481,6 +1481,8 @@
 ^src/install_config\.c/
 ^src/jit_cpu\.c$
 ^src/jit_cpu\.c/
+^src/jit_defs\.c$
+^src/jit_defs\.c/
 ^src/jit_emit\.h$
 ^src/jit_emit\.h/
 ^src/nci\.c$

Modified: trunk/languages/lua/config/makefiles/root.in
==============================================================================
--- trunk/languages/lua/config/makefiles/root.in	(original)
+++ trunk/languages/lua/config/makefiles/root.in	Sun Dec 21 02:34:41 2008
@@ -102,6 +102,7 @@
  alarm.pbc \
  base64.pbc \
  bc.pbc \
+ bit.pbc \
  bitlib.pbc \
  complex.pbc \
  $(LIBPATH)/gl.pbc \
@@ -206,6 +207,9 @@
 bc.pbc: $(LIBPATH)/bc.pir
 	-$(PARROT) --output=bc.pbc $(LIBPATH)/bc.pir
 
+bit.pbc: $(LIBPATH)/bit.pir
+	-$(PARROT) --output=bit.pbc $(LIBPATH)/bit.pir
+
 bitlib.pbc: $(LIBPATH)/bitlib.pir
 	-$(PARROT) --output=bitlib.pbc $(LIBPATH)/bitlib.pir
 

Modified: trunk/languages/lua/doc/status.pod
==============================================================================
--- trunk/languages/lua/doc/status.pod	(original)
+++ trunk/languages/lua/doc/status.pod	Sun Dec 21 02:34:41 2008
@@ -218,6 +218,8 @@
 
 =item * big number library F<languages/lua/src/lib/bc.pir>
 
+=item * bitwise operation library F<languages/lua/src/lib/bit.pir>
+
 =item * bitwise operation library F<languages/lua/src/lib/bitlib.pir>
 
 =item * complex library F<languages/lua/src/lib/complex.pir>

Added: trunk/languages/lua/src/lib/bit.pir
==============================================================================
--- (empty file)
+++ trunk/languages/lua/src/lib/bit.pir	Sun Dec 21 02:34:41 2008
@@ -0,0 +1,268 @@
+# Copyright (C) 2008, The Perl Foundation.
+# $Id$
+
+=head1 NAME
+
+lib/bit.pir - bitwise operations library
+
+=head1 DESCRIPTION
+
+bit is a library for Lua 5.1 that provides bitwise operations on number.
+
+See original on L<http://bitop.luajit.org/>
+
+=over 4
+
+=cut
+
+.HLL 'Lua'
+.loadlib 'lua_group'
+.namespace [ 'bit' ]
+
+.sub '__onload' :anon :load
+#    print "__onload bit\n"
+    .const 'Sub' entry = 'luaopen_bit'
+    set_hll_global 'luaopen_bit', entry
+.end
+
+.const string MYNAME = 'bit'
+
+.sub 'luaopen_bit'
+
+#    print "luaopen_bit\n"
+
+    .local pmc _lua__GLOBAL
+    _lua__GLOBAL = get_hll_global '_G'
+
+    .local pmc _bit
+    new _bit, 'LuaTable'
+
+    new $P1, 'LuaString'
+    set $P1, MYNAME
+    _lua__GLOBAL[$P1] = _bit
+
+    $P2 = split "\n", <<'LIST'
+tobit
+bnot
+band
+bor
+bxor
+lshift
+rshift
+arshift
+rol
+ror
+bswap
+LIST
+    lua_register($P1, _bit, $P2)
+
+    .return (_bit)
+.end
+
+
+.macro BRET(b)
+    .local pmc res
+    new res, 'LuaNumber'
+    set res, .b
+    .return (res)
+.endm
+
+.macro BIT_OP(op, x, vararg)
+    $I1 = lua_checknumber(1, .x)
+    .local int i
+    i = 2
+  L1:
+    unless .vararg goto L2
+    $P0 = shift .vararg
+    $I0 = lua_checknumber(i, $P0)
+    .op $I1, $I0
+    inc i
+    goto L1
+  L2:
+    .BRET($I1)
+.endm
+
+.macro BIT_SH(op, x, n)
+    $I1 = lua_checknumber(1, .x)
+    $I2 = lua_checknumber(2, .n)
+    band $I2, 31
+    .op $I0, $I1, $I2
+    .BRET($I0)
+.endm
+
+
+=item C<bit.tobit (x)>
+
+Normalizes a number to the numeric range for bit operations and returns it.
+This function is usually not needed since all bit operations already normalize
+all of their input arguments.
+
+=cut
+
+.sub 'tobit'
+    .param pmc x :optional
+    .param pmc extra :slurpy
+    $I1 = lua_checknumber(1, x)
+    .BRET($I1)
+.end
+
+
+=item C<bit.bnot (x)>
+
+Returns the bitwise B<not> of its argument.
+
+=cut
+
+.sub 'bnot'
+    .param pmc x :optional
+    .param pmc extra :slurpy
+    $I1 = lua_checknumber(1, x)
+    $I0 = bnot $I1
+    .BRET($I0)
+.end
+
+
+=item C<bit.band (x1 [,x2...])>
+
+=item C<bit.bor (x1 [,x2...])>
+
+=item C<bit.bxor (x1 [,x2...])>
+
+Returns either the bitwise B<or>, bitwise B<and>, or bitwise B<xor> of all of
+its arguments. Note that more than two arguments are allowed.
+
+=cut
+
+.sub 'band'
+    .param pmc x :optional
+    .param pmc vararg :slurpy
+    .BIT_OP(band, x, vararg)
+.end
+
+.sub 'bor'
+    .param pmc x :optional
+    .param pmc vararg :slurpy
+    .BIT_OP(bor, x, vararg)
+.end
+
+.sub 'bxor'
+    .param pmc x :optional
+    .param pmc vararg :slurpy
+    .BIT_OP(bxor, x, vararg)
+.end
+
+
+=item C<bit.lshift (x, n)>
+
+=item C<bit.rshift (x, n)>
+
+=item C<bit.arshift (x, n)>
+
+Returns either the bitwise B<logical left-shift>, bitwise B<logical right-shift>,
+or bitwise B<arithmetic right-shift> of its first argument by the number of
+bits given by the second argument.
+
+Logical shifts treat the first argument as an unsigned number and shift in 0-bits.
+Arithmetic right-shift treats the most-significant bit as a sign bit and
+replicates it.
+
+Only the lower 5 bits of the shift count are used (reduces to the range [0..31]).
+
+=cut
+
+.sub 'lshift'
+    .param pmc x :optional
+    .param pmc n :optional
+    .param pmc extra :slurpy
+    .BIT_SH(shl, x, n)
+.end
+
+.sub 'rshift'
+    .param pmc x :optional
+    .param pmc n :optional
+    .param pmc extra :slurpy
+    .BIT_SH(lsr, x, n)
+.end
+
+.sub 'arshift'
+    .param pmc x :optional
+    .param pmc n :optional
+    .param pmc extra :slurpy
+    .BIT_SH(shr, x, n)
+.end
+
+
+=item C<bit.rol (x, n)>
+
+=item C<bit.ror (x, n)>
+
+Returns either the bitwise B<left rotation>, or bitwise B<right rotation> of its
+first argument by the number of bits given by the second argument. Bits shifted
+out on one side are shifted back in on the other side.
+
+Only the lower 5 bits of the rotate count are used (reduces to the range [0..31]).
+
+=cut
+
+.sub 'rol'
+    .param pmc x :optional
+    .param pmc n :optional
+    .param pmc extra :slurpy
+    $I1 = lua_checknumber(1, x)
+    $I2 = lua_checknumber(2, n)
+    band $I2, 31
+    neg $I2
+    rot $I0, $I1, $I2, 32
+    .BRET($I0)
+.end
+
+.sub 'ror'
+    .param pmc x :optional
+    .param pmc n :optional
+    .param pmc extra :slurpy
+    $I1 = lua_checknumber(1, x)
+    $I2 = lua_checknumber(2, n)
+    band $I2, 31
+    rot $I0, $I1, $I2, 32
+    .BRET($I0)
+.end
+
+
+=item C<bit.bswap (x)>
+
+Swaps the bytes of its argument and returns it. This can be used to convert
+little-endian 32 bit numbers to big-endian 32 bit numbers or vice versa.
+
+=cut
+
+.sub 'bswap'
+    .param pmc x :optional
+    .param pmc extra :slurpy
+    $I1 = lua_checknumber(1, x)
+    $I0 = $I1 >> 24
+    $I2 = $I1 >> 8
+    $I2 &= 0x0000ff00
+    $I0 |= $I2
+    $I3 = $I1 & 0x0000ff00
+    $I3 <<= 8
+    $I0 |= $I3
+    $I4 = $I1 << 24
+    $I0 |= $I4
+    .BRET($I0)
+.end
+
+
+=back
+
+=head1 AUTHORS
+
+Francois Perrad
+
+=cut
+
+
+# Local Variables:
+#   mode: pir
+#   fill-column: 100
+# End:
+# vim: expandtab shiftwidth=4 ft=pir:

Added: trunk/languages/lua/t/bit.t
==============================================================================
--- (empty file)
+++ trunk/languages/lua/t/bit.t	Sun Dec 21 02:34:41 2008
@@ -0,0 +1,81 @@
+#! perl
+# Copyright (C) 2008, The Perl Foundation.
+# $Id$
+
+=head1 NAME
+
+t/bit.t - bitwise operations library
+
+=head1 SYNOPSIS
+
+    % perl -I../lib -Ilua/t lua/t/bit.t
+
+=head1 DESCRIPTION
+
+Tests bit
+(implemented in F<languages/lua/src/lib/bit.pir>).
+
+=cut
+
+use strict;
+use warnings;
+use FindBin;
+use lib "$FindBin::Bin";
+
+use Parrot::Test;
+use Test::More;
+use Parrot::Test::Lua;
+use Config;
+
+my $test_prog = Parrot::Test::Lua::get_test_prog();
+if ( $test_prog eq 'lua' ) {
+    plan skip_all => "parrot only";
+}
+else {
+    plan tests => 6;
+}
+
+language_output_is( 'lua', << 'CODE', << "OUTPUT", 'require' );
+m = require "bit"
+print(type(m))
+CODE
+table
+OUTPUT
+
+language_output_is( 'lua', << 'CODE', << "OUTPUT", 'bit.tobit' );
+m = require "bit"
+assert(bit.tobit(1) == 1)
+CODE
+OUTPUT
+
+language_output_is( 'lua', << 'CODE', << "OUTPUT", 'bit.band' );
+m = require "bit"
+assert(bit.band(1) == 1)
+CODE
+OUTPUT
+
+language_output_is( 'lua', << 'CODE', << "OUTPUT", 'bit.bxor' );
+m = require "bit"
+assert(bit.bxor(1, 2) == 3)
+CODE
+OUTPUT
+
+language_output_is( 'lua', << 'CODE', << "OUTPUT", 'bit.bor' );
+m = require "bit"
+assert(bit.bor(1,2,4,8,16,32,64,128) == 255)
+CODE
+OUTPUT
+
+language_output_is( 'lua', << 'CODE', << "OUTPUT", 'bit.bswap' );
+m = require "bit"
+assert(bit.bswap(0x12345678) == 0x78563412)
+CODE
+OUTPUT
+
+
+# Local Variables:
+#   mode: cperl
+#   cperl-indent-level: 4
+#   fill-column: 100
+# End:
+# vim: expandtab shiftwidth=4:



nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About