GHC: Ticket #1005: Add Logical Shift Operators
http://ghc.haskell.org/trac/ghc/ticket/1005
<p>
This patch adds logical shift operators to complement the current arithmetic shift operators in Data.Bits.
</p>
<p>
The patch has been tested against GHC on Linux running on Intel 32bit architecture. I haven't had an opportunity to test other architectures but they should work fine.
</p>
<p>
The patch shouldn't break NHC or Hugs but I haven't been able to test them either. As it currently is, among the Int family, logical shift operators should work on these platforms for Int only. the Int* family of sized integers will currently treat logical shift operators as arithmetic shift operators as I couldn't figure out how to patch these.
</p>
<p>
Integer will raise an error if a logical right-shift operator is attempted since it doesn't seem to make any sense for this type.
</p>
<p>
By default, the logical shift operators will act as arithmetic shift operators and so should require no method definition for unsigned types.
</p>
<p>
The patch also includes quickcheck properties which I've taken the liberty of creating a Tests directory for. I thought it would make sense to keep quickcheck properties with the library they belong to for easier management.
</p>
en-usGHChttp://ghc.haskell.org/trac/ghc/chrome/site/ghc_logo.png
http://ghc.haskell.org/trac/ghc/ticket/1005
Trac 1.0.7rob@…Thu, 16 Nov 2006 06:51:01 GMTattachment set
http://ghc.haskell.org/trac/ghc/ticket/1005
http://ghc.haskell.org/trac/ghc/ticket/1005
<ul>
<li><strong>attachment</strong>
set to <em>log_shift_ops.patch</em>
</li>
</ul>
TicketguestThu, 16 Nov 2006 09:09:22 GMTattachment set
http://ghc.haskell.org/trac/ghc/ticket/1005
http://ghc.haskell.org/trac/ghc/ticket/1005
<ul>
<li><strong>attachment</strong>
set to <em>log_shift_ops_new.patch</em>
</li>
</ul>
TicketguestThu, 16 Nov 2006 09:09:50 GMTattachment set
http://ghc.haskell.org/trac/ghc/ticket/1005
http://ghc.haskell.org/trac/ghc/ticket/1005
<ul>
<li><strong>attachment</strong>
set to <em>bits_test.patch</em>
</li>
</ul>
Ticketmalcolm.wallace@…Thu, 16 Nov 2006 11:02:30 GMT
http://ghc.haskell.org/trac/ghc/ticket/1005#comment:1
http://ghc.haskell.org/trac/ghc/ticket/1005#comment:1
<p>
Could you describe briefly what is the intended difference between arithmetic shift and logical shift?
</p>
<p>
Also, I don't think it is easy to justify proposing a change to the base package, but only to test it on one implementation platform. Portability is vital!
</p>
TicketdonsThu, 16 Nov 2006 11:08:12 GMT
http://ghc.haskell.org/trac/ghc/ticket/1005#comment:2
http://ghc.haskell.org/trac/ghc/ticket/1005#comment:2
<p>
Check the code works in GHC and Hugs at the very least (the QuickCheck properties should run fine in both environments).
</p>
TicketrobThu, 16 Nov 2006 12:10:14 GMT
http://ghc.haskell.org/trac/ghc/ticket/1005#comment:3
http://ghc.haskell.org/trac/ghc/ticket/1005#comment:3
<p>
Ok. I'm getting Hugs and NHC. It'll probably take me a while. I think I've found some things that need fixing anyway, so standby for updated patches.
</p>
<p>
Malcolm: info about logical shift can be found here: <a class="ext-link" href="http://en.wikipedia.org/wiki/Logical_shift"><span class="icon"></span>http://en.wikipedia.org/wiki/Logical_shift</a>
</p>
Ticketmalcolm.wallace@…Thu, 16 Nov 2006 12:40:08 GMT
http://ghc.haskell.org/trac/ghc/ticket/1005#comment:4
http://ghc.haskell.org/trac/ghc/ticket/1005#comment:4
<p>
Thanks for the pointer to a description of logical vs arithmetic shift. However, this does flag up an assumption in the proposal - namely that the current specification requires arithmetic shifting. As far as I can tell, nothing in the interface documentation for Data.Bits says that the operations should be arithmetic, and indeed I couldn't guarantee that we do not _already_ have logical shifts in either Hugs or nhc98. It might be that it is the arithmetic variants that are missing.
</p>
<p>
Having said that, I do think it is good to propose that the specification should be more precise. And it would indeed be good to have separate functions for the separate styles of operation. (But implementing them efficiently might conceivably require some architecture-specific configuration.)
</p>
TicketRobFri, 17 Nov 2006 00:06:07 GMT
http://ghc.haskell.org/trac/ghc/ticket/1005#comment:5
http://ghc.haskell.org/trac/ghc/ticket/1005#comment:5
<p>
True; I hadn't verified whether the implementations were consistent... I'll have a look around at how other high level languages guarantee their shift operations are arithmetic and change this proposal into something like you suggest.
</p>
<p>
So how do I take this proposal off the "current" list and put it back on the drawing board?
</p>
TicketrossFri, 17 Nov 2006 00:39:20 GMT
http://ghc.haskell.org/trac/ghc/ticket/1005#comment:6
http://ghc.haskell.org/trac/ghc/ticket/1005#comment:6
<p>
The <a class="ext-link" href="http://www.cse.unsw.edu.au/~chak/haskell/ffi/"><span class="icon"></span>FFI Addendum</a> says:
</p>
<blockquote>
<p>
The function <tt>shift</tt> performs sign extension on signed number types; i.e., right shifts fill the top bits with 1 if the number is negative and with 0 otherwise.
</p>
</blockquote>
<p>
(This text was missing from Data.Bits)
</p>
<p>
Isn't that reasonable? That is, use <tt>Word</tt><em>n</em> to get logical shifts?
</p>
<p>
(If you want to scrub this ticket, resolve it as <tt>wontfix</tt>)
</p>
TicketrossTue, 19 Dec 2006 08:57:36 GMTstatus changed; resolution set
http://ghc.haskell.org/trac/ghc/ticket/1005#comment:7
http://ghc.haskell.org/trac/ghc/ticket/1005#comment:7
<ul>
<li><strong>status</strong>
changed from <em>new</em> to <em>closed</em>
</li>
<li><strong>resolution</strong>
set to <em>wontfix</em>
</li>
</ul>
<p>
Proposal seems abandoned, and may be unnecessary anyway.
</p>
TicketsimonmarTue, 30 Sep 2008 15:40:04 GMTarchitecture changed
http://ghc.haskell.org/trac/ghc/ticket/1005#comment:8
http://ghc.haskell.org/trac/ghc/ticket/1005#comment:8
<ul>
<li><strong>architecture</strong>
changed from <em>Unknown</em> to <em>Unknown/Multiple</em>
</li>
</ul>
TicketsimonmarTue, 30 Sep 2008 15:51:10 GMTos changed
http://ghc.haskell.org/trac/ghc/ticket/1005#comment:9
http://ghc.haskell.org/trac/ghc/ticket/1005#comment:9
<ul>
<li><strong>os</strong>
changed from <em>Unknown</em> to <em>Unknown/Multiple</em>
</li>
</ul>
Ticket