quadbits.sa


Generated by gen_html_sa_files from ICSI. Contact gomes@icsi.berkeley.edu for details
 
------------------------->  GNU Sather - sourcefile  <-------------------------
-- Copyright (C) 2000 by K Hopper, University of Waikato, New Zealand        --
-- This file is part of the GNU Sather library. It is free software; you may --
-- redistribute  and/or modify it under the terms of the GNU Library General --
-- Public  License (LGPL)  as published  by the  Free  Software  Foundation; --
-- either version 2 of the license, or (at your option) any later version.   --
-- This  library  is distributed  in the  hope that it will  be  useful, but --
-- WITHOUT ANY WARRANTY without even the implied warranty of MERCHANTABILITY --
-- or FITNESS FOR A PARTICULAR PURPOSE. See Doc/LGPL for more details.       --
-- The license text is also available from:  Free Software Foundation, Inc., --
-- 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA                     --
-------------->  Please email comments to <bug-sather@gnu.org>  <--------------


immutable class QUADBITS < $BIT_PATTERN{QUADBITS}

immutable class QUADBITS < $BIT_PATTERN{QUADBITS} is -- This class is a primitive immutable class which has 32 bits, -- but otherwise may have any meaning. Only equality and bit operations -- are specified. -- It is intended for use when using codes of 32 bits (UCS4 in -- particular) and for accessing hardware devices of this 'width'. It -- is also likely that many implementations will use this for the NUM_BITS -- class. -- Version 1.0 Dec 96. Copyright K Hopper, U of Waikato -- Development History -- ------------------- -- Date Who By Detail -- ---- ------ ------ -- 19 Dec 96 kh Original include AVAL{BIT} asize-> ; include COMPARABLE ; include BINARY ; const asize : CARD := 32 ; const Quad_Bits : CARD := asize ; const Quad_Max : CARD := 0xFFFFFFFF ; const maxnum : CARD := Quad_Max ; const Octets_per_Quad : CARD := 4 ; -- This is the largest unsigned exact number representable using the -- number of bits contained. private const Masks : ARRAY{SAME} := | create(0x1), create(0x3), create(0x7), create(0xf), create(0x1f), create(0x3f), create(0x7f), create(0xff), create(0x1ff), create(0x3ff), create(0x7ff), create(0xfff), create(0x1fff), create(0x3fff), create(0x7fff), create(0xffff), create(0x1ffff), create(0x3ffff), create(0x7ffff), create(0xfffff), create(0x1fffff), create(0x3fffff), create(0x7fffff), create(0xffffff), create(0x1ffffff), create(0x3ffffff), create(0x7ffffff), create(0xfffffff), create(0x1fffffff), create(0x3fffffff), create(0x7fffffff), create(0xffffffff) | ; create : SAME is -- This creates with all bits zero! me : SAME := void ; return me end ; null : SAME is -- Merely a renamed version of create above! return create end ; private do_create( val : QUADBITS ) : SAME is -- This routine returns the value obtained as a result of taking the -- specific argument and returning it as SAME. return val end ; build( cursor : BIN_CURSOR ) : SAME pre ~void(cursor) and ~cursor.is_done post true -- beware side-effects of initial! is -- This creates a new object and advances the cursor by the appropriate -- number of octets. Note that if there are insufficient octets to completely -- fill the object the these are placed at the least significant end, high -- end octets being zero. res : SAME ; count : CARD := Octets_per_Quad ; if cursor.remaining < count then count := cursor.remaining end ; loop count.times! ; res := res.left(OCTET::Octet_Bits).bit_or( do_create(cursor.get_item.quad)) end ; return res end ; create( val : CARD ) : SAME is -- This produces a new object which takes the value val as a bit-pattern. -- It relies on the built-in conversion CARD_QUAD operation builtin CARD_QUAD end ; create( val : INT ) : SAME is -- This produces a new object which takes the value val as a bit-pattern. -- It relies on the built-in conversion CARD_QUAD operation which ignores any -- sign! builtin CARD_QUAD end ; create( val : CHAR ) : SAME is -- This produces a new object which takes the value val as a bit-pattern. -- It relies on the built-in conversion CARD_QUAD operation builtin CHAR_QUAD end ; binstr : BINSTR pre true post create(result) = self is -- This routine returns self as a binary string - MS octet first. res : FBINSTR := FBINSTR::create + self.right(HEXTET::Hextet_Bits + OCTET::Octet_Bits).octet + self.right(HEXTET::Hextet_Bits).octet + self.right(OCTET::Octet_Bits).octet + octet ; return res.binstr end ; octet : OCTET pre true post result = binstr[0] is -- This routine returns the octet which contains the low eight bits -- of self. builtin QUADBITS_OCTET end ; hextet : HEXTET pre true post (result.binstr[0] = binstr[0]) and (result.binstr[1] = binstr[1]) is -- This routine returns the hextet object which contains the low sixteen -- bits of self. builtin QUADBITS_HEXTET end ; quad : SAME pre true post result = self is -- This routine returns a copy of self. This is included here for -- symmetry with the other bit-pattern classes. return self end ; card : CARD is -- This converts self into an integer-sized numeric cardinal value. builtin QUAD_CARD end ; field : FIELD is -- This converts self into an integer-sized numeric field value. builtin QUAD_FIELD end ; char : CHAR is -- This routine returns the same bit-pattern as an object of type CHAR -- with which, however, no particular culture-dependent semantics are -- associated. builtin QUAD_CHAR end ; bool : BOOL is -- This feature converts the hextet into a truth value where no bits set -- is the value false. return ~card.is_zero end ; token : TOKEN is -- This converts self into a token value, acting as a bit-pattern type -- cast. builtin QUAD_CARD end ; hash : CARD pre true post true is -- This routine returns a hash value composed from the bit-pattern -- contained. return NUM_BITS::create(card).hash end ; int : INT is -- This converts self into an integer-sized numeric INTEGER value. return card.int end ; is_eq( other : SAME ) : BOOL is -- This and its inverse are the only two operations valid on self. builtin QUAD_IS_EQ end ; bit( index : CARD ) : BIT pre index < asize post result = [index] is -- This routine returns the value of the individual bit indicated. return aget(index) end ; alter( index : CARD, val : BIT ) : SAME pre index < asize post result[index] = val is -- This routine sets the value of the individual bit indicated. aset(index,val) ; return (self) end ; slice( lsb : CARD, count : CARD ) : SAME pre lsb < Quad_Bits and count > 0 and (lsb + count) <= Quad_Bits post true is -- This routine returns the octet containing the indicated slice in the -- lowest count bits. loc_mask : SAME := Masks[count - 1].left(lsb) ; return self.bit_and(loc_mask).right(lsb) end ; slice( lsb : CARD, count : CARD, val : SAME ) : SAME pre lsb < Quad_Bits and count > 0 and (lsb + count) <= Quad_Bits post true is -- This routine returns the hextet containing the indicated slice in the -- bits starting at lsb. loc_chunk : SAME := val.bit_and(Masks[count - 1]).left(lsb) ; loc_mask : SAME := Masks[count - 1].left(lsb).bit_invert ; return self.bit_and(loc_mask).bit_or(loc_chunk) end ; set( index : CARD ) : BOOL pre index < Quad_Bits post (result = ([index] = setbit)) is -- This returns true if and only if the indexed bit of self is set. return bit(index).set end ; clear( index : CARD ) : BOOL pre index < Quad_Bits post (result = ([index] = clearbit)) is -- This returns true if and only if the indexed bit of self is -- clear. return bit(index).clear end ; bit_and( other : SAME ) : SAME is -- This built-in primitive returns the result of anding together each bit -- of self and other builtin QUAD_AND -- compiler primitive end ; bit_or( other : SAME ) : SAME is -- This built-in primitive returns the result of oring together each bit -- of self and other builtin QUAD_OR -- compiler primitive end ; bit_invert : SAME is -- This built-in primitive returns the result of inverting self. builtin QUAD_INVERT -- compiler primitive end ; bit_xor( other : SAME ) : SAME is -- This built-in primitive returns the result of xoring together each bit -- of self and other builtin QUAD_XOR -- compiler primitive end ; private shift( places : INT ) : SAME is -- This built-in primitive returns the result of shifting the bits in -- self by the specified number of places (left if +ve). The necessary -- pre-conditions are satisfied by the public routines left and right. builtin QUAD_SHIFT -- compiler primitive end ; left( places : CARD ) : SAME pre places <= Quad_Bits post ((places = Quad_Bits) and (result = create)) or (result[places] = initial([0])) is -- This built-in primitive returns the result of shifting the bits in -- self left by the specified number of places. if places = Quad_Bits then return create else return shift(places.int) end end ; right( places : CARD ) : SAME pre places <= Quad_Bits post ((places = Quad_Bits) and (result = create)) or (result[Quad_Bits - 1 - places] = initial([Quad_Bits - 1])) is -- This built-in primitive returns the result of shifting the bits in -- self right by the specified number of places (which is not, of course, -- negative). if places = Quad_Bits then return void else return shift(-(places.int)) end end ; highest( val : BIT ) : CARD pre true post (result = CARD::nil) or (result < Quad_Bits) is -- This routine returns the index of the highest bit in self which has -- the value val. If no bits are set then the value CARD::nil is -- returned. loop index : CARD := (Quad_Bits - 1).downto!(0) ; if ([index] = val) then return index end end ; return CARD::nil end ; lowest( val : BIT ) : CARD pre true post (result = CARD::nil) or (result < Quad_Bits) is -- This routine returns the index of the lowest bit which has the same -- value as val - or CARD::nil if there are no such bits. loop index : CARD := 0.upto!(Quad_Bits - 1) ; if ([index] = val) then return index end end ; return CARD::nil end ; highest : CARD pre true post (result = CARD::nil) or (result < Quad_Bits) is -- This routine returns the index of the highest set bit in the bits. -- This is used in logarithmic and exponential routines for numbers. If -- no bits are set then the value CARD::nil is returned. loop index : CARD := (Quad_Bits - 1).downto!(0) ; if set(index) then return index end end ; return CARD::nil end ; lowest : CARD pre true post (result = CARD::nil) or (result < Quad_Bits) is -- This routine returns the index of the lowest set bit in the bits. -- This is used in logarithmic and exponential routines for numbers. If -- no bits are set then the value CARD::nil is returned. loop index : CARD := 0.upto!(Quad_Bits - 1) ; if set(index) then return index end end ; return CARD::nil end ; octet! : OCTET pre true post (result.card <= OCTET::Octet_Max) is -- This iter yields successive octets of self starting with the least -- significant bits. loc_val : SAME := self ; loop Octets_per_Quad.times! ; yield loc_val.octet ; loc_val := loc_val.right(OCTET::Octet_Bits) end end ; hex_str( lib : LIBCHARS ) : STR pre true post create(CARD::build_based(result.cursor,16)) = self is -- This routine yields a hexadecimal string representation of self -- using the specified encoding and repertoire. return self.card.hex_str(lib,8) end ; hex_str : STR pre true post create(CARD::build_based(result.cursor,16)) = self is -- This routine yields a hexadecimal string representation of self -- using the default encoding and repertoire. return self.card.hex_str(LIBCHARS::default,8) end ; str( lib : LIBCHARS ) : STR pre ~void(lib) post result.size = hex_str.size + 2 is -- This routine yields a string representation of self using the given -- encoding and repertoire. return STR::create(lib) + lib.digit(0).char + lib.Hex_Prefix.char + hex_str(lib) end ; str : STR pre true post result.size = hex_str.size + 2 is -- This routine yields a string representation of self using the default -- encoding and repertoire. return str(LIBCHARS::default) end ; end ; -- QUADBITS