How about <<< and >>> ops?

This has probably come up before, but I think it would be good if perl
had two additional arithemetic operators:

	>>>	would be a right shift _without_ sign extension
		under use integer (currently, under use integer,
		>> is at the mercy of the underlying C lib).
	<<<	would be a left roll ($x <<< $y would be equivalent
		to ( ($x << $y) | (2**$y-1) & ($x >> (32-$y)) )
		
these two ops would come in handy when implementing cryptographic
algorithms.  I guess you could argue that >>> should be a right roll,
but then I don't see a good way to get >> not to sign extend under
use integer, other than making it _never_ sign extend.

There is probably some aberrent code that adding these ops would break,
but I think it would be worth it.

Any comments?

John.

0
allen
1/22/2001 5:32:52 PM
perl.perl5.porters 48287 articles. 1 followers. Follow

9 Replies
1094 Views

Similar Articles

[PageSpeed] 8
Get it on Google Play
Get it on Apple App Store

On 22 Jan 01, at 12:32, John L. Allen wrote:

> 	>>>	would be a right shift _without_ sign extension
> 		under use integer (currently, under use integer,
> 		>> is at the mercy of the underlying C lib).
> 	<<<	would be a left roll ($x <<< $y would be equivalent
> 		to ( ($x << $y) | (2**$y-1) & ($x >> (32-$y)) )

To my mind, this would make these two things nastily asymmetric, since 
they represent fairly different things (rotate vs. shift). Oh, and 
where did the number "32" come from in your description of <<<? (Hint, 
hint.)

> these two ops would come in handy when implementing cryptographic
> algorithms.

Depends. Most algorithms that use rotate, I imagine, specify how many 
bits to rotate. I'm not sure whether you can guarantee that in Perl 
(not if you want to be portable across different Perls, anyway).

> There is probably some aberrent code that adding these ops would break,
> but I think it would be worth it.

Example: <IN>>>4 is now <IN>  >> 4. With maximal munch (does Perl's 
tokeniser use that?), this'd be < IN >>> 4 "unmatched '<'". (Hm, 
obviously not; <IN>>4 is not <IN >> 4 but <IN> > 4. Still. How about 
4<<<IN>, which is now 4 << <IN> -- note that 4<<IN> is a syntax error 
rather than 4 < <IN> (at least, in 5.005_03).

Cheers,
Philip
-- 
Philip Newton <Philip.Newton@gmx.net>
0
Philip
1/23/2001 5:57:11 AM
On Tue, 23 Jan 2001 06:57:11 +0100, "Philip Newton" <Philip.Newton@gmx.net> wrote:
> > There is probably some aberrent code that adding these ops would break,
> > but I think it would be worth it.
> 
> Example: <IN>>>4 is now <IN>  >> 4. With maximal munch (does Perl's 
> tokeniser use that?), this'd be < IN >>> 4 "unmatched '<'". (Hm, 
> obviously not; <IN>>4 is not <IN >> 4 but <IN> > 4. Still. How about 

where <IN > would probably be parsed as glob '("IN")' giving back a list which
will be tested against a number, so the test is if <IN > (as a glob) returns
more than 4 elements (which is very unlikely for <IN > ;-))

> 4<<<IN>, which is now 4 << <IN> -- note that 4<<IN> is a syntax error 
> rather than 4 < <IN> (at least, in 5.005_03).

-- 
H.Merijn Brand           Amsterdam Perl Mongers (http://www.amsterdam.pm.org/)
using perl-5.005.03, 5.6.0, 5.6.1, 5.7.1 & 623 on HP-UX 10.20 & 11.00, AIX 4.2
   AIX 4.3, WinNT 4.0 SP-6a, and Win2000pro often with Tk800.022 &/| DBD-Unify
ftp://ftp.funet.fi/pub/languages/perl/CPAN/authors/id/H/HM/HMBRAND/

0
h
1/23/2001 11:52:53 AM

On Tue, 23 Jan 2001, Philip Newton wrote:
> On 22 Jan 01, at 12:32, John L. Allen wrote:
> 
> > 	>>>	would be a right shift _without_ sign extension
> > 		under use integer (currently, under use integer,
> > 		>> is at the mercy of the underlying C lib).
> > 	<<<	would be a left roll ($x <<< $y would be equivalent
> > 		to ( ($x << $y) | (2**$y-1) & ($x >> (32-$y)) )
> 
> To my mind, this would make these two things nastily asymmetric, since 
> they represent fairly different things (rotate vs. shift). Oh, and 
> where did the number "32" come from in your description of <<<? (Hint, 
> hint.)

I guess I could live _with_ the symmetry. :-)  And I guess you're hinting
about machines with 64 bit integers.

> > these two ops would come in handy when implementing cryptographic
> > algorithms.
> 
> Depends. Most algorithms that use rotate, I imagine, specify how many 
> bits to rotate. I'm not sure whether you can guarantee that in Perl 
> (not if you want to be portable across different Perls, anyway).

Are you referring to 64 bit machines again here?  Things like MD5, for which
I have pure perl that has stuff like this in it:

    $a+=($b&($c^$d)^$d)+$W[ 0]+0xd76aa478;
	$a=($a<< 7|0x0000007f&($a>>25))+$b;
    $d+=($a&($b^$c)^$c)+$W[ 1]+0xe8c7b756; 
	$d=($d<<12|0x00000fff&($d>>20))+$a;
    $c+=($d&($a^$b)^$b)+$W[ 2]+0x242070db; 
	$c=($c<<17|0x0001ffff&($c>>15))+$d;
  
definitely needs specific shift values (like 7 and 12) and these will 
definitely not work on 64 bit int machines.  Hmmm, is there a 
``use integer32;'' ?

John.
0
allen
1/23/2001 9:18:59 PM
John L . Allen <allen@grumman.com> writes:
>Are you referring to 64 bit machines again here?  Things like MD5, for which
>I have pure perl that has stuff like this in it:
>
>    $a+=($b&($c^$d)^$d)+$W[ 0]+0xd76aa478;
>	$a=($a<< 7|0x0000007f&($a>>25))+$b;
>    $d+=($a&($b^$c)^$c)+$W[ 1]+0xe8c7b756; 
>	$d=($d<<12|0x00000fff&($d>>20))+$a;
>    $c+=($d&($a^$b)^$b)+$W[ 2]+0x242070db; 
>	$c=($c<<17|0x0001ffff&($c>>15))+$d;
>  
>definitely needs specific shift values (like 7 and 12) and these will 
>definitely not work on 64 bit int machines.  Hmmm, is there a 
>``use integer32;'' ?

It seems to me that the example MD5 code is more portable and better
documents what is going on that a mystical rotate operator and a pragma
to make the machine behave in an un-natural width. Invent a 3-operand 

    rotate($val,$amount,$word_size) 

or similar.

>
>John.
-- 
Nick Ing-Simmons

0
nick
1/23/2001 10:05:22 PM

On Wed, 24 Jan 2001, Philip Newton wrote:
> On 23 Jan 01, at 16:18, John L. Allen wrote:
> > On Tue, 23 Jan 2001, Philip Newton wrote:
> > > To my mind, this would make these two things nastily asymmetric, since 
> > > they represent fairly different things (rotate vs. shift). Oh, and 
> > > where did the number "32" come from in your description of <<<? (Hint, 
> > > hint.)
> > 
> > I guess I could live _with_ the symmetry. :-)  And I guess you're hinting
> > about machines with 64 bit integers.
> 
> Yes. Or who-knows-how-many bit integers. (Not all the world's a <insert 
> type of machine with which you're familiar here>.)
 
> > 	$c=($c<<17|0x0001ffff&($c>>15))+$d;
> >   
> > definitely needs specific shift values (like 7 and 12) and these will 
> > definitely not work on 64 bit int machines.
> 
> The right shifts will work fine, since you're masking off the higher 
> bits that might result. (At first, I thought that the mask applied to 
> the left shift, but the relative precedence of | and & got me. I would 
> have used more parentheses.) If you want that to work on 64 bit 
> machines, you probably only need an "&0xffffffff" in each line to chop 
> of "extra" bits resulting from left shifts. (Or, alternatively, mask 
> off extra bits before left shifting.) Then you only need >= 32 bits 
> rather than = 32 bits.

Do'h!  What a blunder on my part.  So then, algorithms that emulate 
rolls by shifting right and left and masking _will_ work without change on 
greater int size machines, but if there were a builtin roll operator that 
took the int size into account, algorithms that used it would _fail_ 
unless they were changed to use different roll amounts.  Thanks for 
catching that.  But I still think having <<< and >>> ops, along with 
``use integer 32|64|...'' to govern their operation, would be nice, but I 
gather I'm alone in this view.

Hmmm, doesn't gcc optimize the construct "(a>>n)|(a<<(32-n))" to a single 
machine instruction on those architectures that have one?  Could perl be 
made to optimize the same thing?  

John.


0
allen
1/24/2001 4:26:31 PM

On Tue, 23 Jan 2001, Jarkko Hietaniemi wrote:

> > It seems to me that the example MD5 code is more portable and better
> > documents what is going on that a mystical rotate operator and a pragma
> > to make the machine behave in an un-natural width. Invent a 3-operand 
> > 
> >     rotate($val,$amount,$word_size) 
> > 
> > or similar.
> 
> Agreed.  There is a slew of bit operations we don't support in the
> core (as operators).  For example you cannot shift a bitvector.  You
> can't reverse a bitvector.  You can't find the highest or the lowest
> bit of a bitvector.

One man's precedent is another man's deficiency :-) but I see your point.

John.
0
allen
1/24/2001 4:29:33 PM
John L . Allen <allen@grumman.com> writes:
>
>Hmmm, doesn't gcc optimize the construct "(a>>n)|(a<<(32-n))" to a single 
>machine instruction on those architectures that have one?  

I seem to recall it does attempt to - but was very picky about 
exactly how it was written.

-- 
Nick Ing-Simmons <nik@tiuk.ti.com>
Via, but not speaking for: Texas Instruments Ltd.

0
nik
1/24/2001 5:30:01 PM
> I don't know what you'd do about operator overloading, though. 

It would also fail if the argument were tied.  (A << C1) + (A >> C2)
should perform two FETCH calls on A, and the optimized version would
perform only one.

0
mjd
1/25/2001 4:43:36 PM

On Wed, 24 Jan 2001, Stephen McCamant wrote:

> >>>>> "JLA" == John L Allen <allen@grumman.com> writes:
> 
> JLA> Hmmm, doesn't gcc optimize the construct "(a>>n)|(a<<(32-n))" to
> JLA> a single machine instruction on those architectures that have
> JLA> one?
.... 
> JLA> Could perl be made to optimize the same thing?
> 
> Anything is possible. This in fact wouldn't be all that difficult, say
> in a ck_ routine; you'd just have to have a long sequence of ifs. It
> strikes me as a little odd in light of how little other numeric
> optimization perl does, thoug (constant folding is about the only
> thing). You wouldn't need to bother to determine whether your
> architecture really had a rotate instruction, since the big speedup
> wouldn't come from saving bit operations -- it would be in reduced OP
> overhead.

I've now got this odd notion in my head that we could get much better
and more general results by creating an XS Math::Fast module that would
essentially emulate a simple CPU in C code: you'd pass it a string with your
minimalist "program" in it, and it would rip through it maybe 10 times 
faster than perl could, but still maybe 10 times slower than native C.  
For example,

	use Math::Fast;
	$x = 0x12345678; $y = 0x9abcdef0;
	Math::Fast::Do '
		int x, y, t, i;	 # declare some integers
		x = $x; y = $y;  # retrieve scalars from perl
		for (i = 0; i < 1000000; i++) {
		  t += x ^ (y << i); u = y ^ ((x << i) | (x >> (32-i)));
		}
		$t = t;		 # set a perl var when done
	';
	print "t = $t\n";

I guess you could handle ints and floats equally easily, and you would be 
limited to fortran-77-like flow control structures.

This would be a sort of poor man's Inline module, slower, no doubt, but 
not needing a C compiler at runtime.  But maybe I'm overestimating how
much faster than perl such a thing could be.

Ok, maybe I've been smoking something funny...

John.
0
allen
1/25/2001 7:15:24 PM
Reply:

Similar Artilces:

>>>> Heads up <<<<
I just got a warning from Norton that "PamelaSetup-Basic.exe" has a virus in it. The name is "VirusBurst" Luckily, I did not install this software and Norton's quarantined it so I could delte it, which I have done. Symantec has not completed analysis of this particular piece of garbage but it did catch the sig. If you have installed Pamela, you may be in trouble. Duffy wrote: > I just got a warning from Norton that "PamelaSetup-Basic.exe" has a virus > in it. The name is "VirusBurst" > > Luckily, I did not install...

>>>> ROOT Exploit in SAMBA <<<<<<
"A flaw has been detected in the Samba main smbd code which could allow an external attacker to remotely and anonymously gain Super User (root) privileges on a server running a Samba server. This flaw exists in previous versions of Samba from 2.0.x to 2.2.7a inclusive. This is a serious problem and all sites should either upgrade to Samba 2.2.8 immediately or prohibit access to TCP ports 139 and 445." http://us3.samba.org/samba/samba.html Binaries are available from Samba for RedHat, and some other distributions. So far as I can tell, the RedHat update mirrors I norm...

C<< >> vs C<< >> vs C<< x >>
Ugh. So we all know that there's this syntax for formatting codes (n=E9s "interio= r sequences") like C<< x >>. And that tokenizes as three tokens: "C<< ", open-C code "x", content " >>" close-code matching the C open-code And this is explicated by what I wrote in perlpodspec where I say that such a code... * starts with a capital letter (just US-ASCII [A-Z]) followed by two or more "<"'s, one or more whitespace characters, * any number of characters * one or more whit...

>>>> CAPITALS GAMES <<<<
.. ~~~!!!~~~ ================================================== ================================================== CLICK HERE TO ENTER: >>> http://web-paradise.cn/3/capitals-games <<< ================================================== ================================================== .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. ....

>>>> BUY RAM <<<<
.. ~~~*@@@*~~~ ================================================== ================================================== ENTER HERE: >>> http://web-for-you.cn/about/buy-ram <<< ================================================== ================================================== .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. ...

>>>> BLU-RAY MOVIES <<<<
.. ~~~!!!~~~ ================================================== ================================================== CLICK HERE TO ENTER: >>> http://web-paradise.cn/2/blu-ray-movies <<< ================================================== ================================================== .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. ....

[PATCH] correctly handle C<< >> and C<<< >>> in diagnostics
This is just a quick hack; ideally someone would make it use an actual pod parser. --- perl/lib/diagnostics.pm.orig 2003-12-30 15:48:47.000000000 -0800 +++ perl/lib/diagnostics.pm 2004-05-25 01:54:31.735904000 -0700 @@ -314,10 +314,10 @@ sub noop { return $_[0] } # spensive for a noop sub bold { my $str =$_[0]; $str =~ s/(.)/$1\b$1/g; return $str; } sub italic { my $str = $_[0]; $str =~ s/(.)/_\b$1/g; return $str; } - s/[BC]<(.*?)>/bold($1)/ges; + s/C<<< (.*?) >>>|C<< (.*?) >>|[BC]<(.*?)>/bold($+)/ges; ...

How to strip a string of <html>, ,</html>, <body>, </body>, <form ... >, </form> tags?
I have stream which is the HTML input of a page. Now I want to use only that part of this page, that is within <form .....> and </form> tags, and excluding these tags.How would I go about stripping <html>, ,</html>, <body>, </body>, <form ... >, </form>, <head> and </head> tags? I have to make sure that <head ...javascript..> and its corresponding </head> tags are not stripped in this process.sun21170    Wait... you said you wanted to strip the head tags, and you're also saying you need to NOT strip the ...

The Man Who Debunks Virus Myths <<<hero???>>>R.Rosenberger Vmyths.com!!!<<<SIGH>>>
http://www.securitynewsportal.com/article.php?sid=1368&mode=thread&order=0 -- Regard: Joh@nnes´┐Ż 1216771 Ont.Inc. "Nothing is more damaging to a new truth than an old error" Take a look at the following from the article: (Begin quote) "Rosenberger is not just a random ornery writer with a website and a bone to pick. He's an experienced programmer, a systems administrator and a man of mystery with high-level CIA security clearance. Information about Rosenberger's status with the CIA was confirmed by an inquiry to a government office, and Ro...

<<<THUD>>>
http://www.novell.com/support/search.do?cmd=displayKC&docType=kc&externalId=3882364&sliceId=1&docTypeID=DT_TID_1_1&dialogID=104807193&stateId=0%200%20104803654 Novell actually gave a projected release date for something? <ponders if this date is before or after the new maintenance policy kicks in> -----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 First, terrible subject line. Second, the offender has been shot. ;-) Not really.... this guy is awesome just because he is crazy like that. Third, before the patch date stuff (February I think). Goo...

Difference between <% %> <%# %> <%= %> ?
I have some server controls to which I want to assign the visible property using the server tags instead of code behind, but I can't manage to do it, so far I'm using:     Public Shared Function accesible(ByVal user As USERRow) As Boolean        Select Case user.rol            Case "Administrator"                Return True            Cas...

>>>BOOKMARKS<<<
I dont understand the hep page for Lsot Bookmarks - can someone guide me please? I need to know what to look for (instructions in how to do it) As I dont know what im doing im new to using PC's RAJA SINGH wrote: > I dont understand the hep page for Lsot Bookmarks - can someone guide me > please? I need to know what to look for (instructions in how to do it) As I > dont know what im doing im new to using PC's please keep your posting together so we can follow whats going on. Thanks. Close FF. Now, using your file manager, locate the FF profile. To l...

[PATCH] Fix POD: C<...->...> => C<< ...-> ... >>
--=-0nPiZliXhb80VRfJ/8qX Content-Type: text/plain Content-Transfer-Encoding: 7bit See the attached patch, it fixes some POD which gets rendered wrong by newer POD rendering tools. Thanks, Frank --=-0nPiZliXhb80VRfJ/8qX Content-Disposition: attachment; filename="0001-Fix-POD-C-.-.-C.patch" Content-Type: text/x-patch; name="0001-Fix-POD-C-.-.-C.patch"; charset="UTF-8" Content-Transfer-Encoding: 7bit From ed46d8dd56e57d51347cb0a7a6397687ee15a950 Mon Sep 17 00:00:00 2001 From: Frank Wiegand <frank.wiegand@gmail.com> Date: Thu, 19 Nov 2009 1...

Use of <code>, <var>, <kbd>, and <samp>
Is there any policy as to whether <code>, <var>, <kbd> and <samp> can all be used, and if so, whether they should only be used for XUL, etc. or other code documented at the site, or whether they can also be used to refer to code which is not intended as the subject of any documentation, but which is used as an example. For example, the template tutorial indicates that <content>'s @uri attribute can be set to any variable name, such as "?start" or "?uri". Could these be referenced with <var> too, or is it the policy to co...

Web resources about - How about <<< and >>> ops? - perl.perl5.porters

Resources last updated: 11/21/2015 2:22:39 PM