001/* An actor that converts an IntToken to 32 consecutive BooleanTokens.
002
003 Copyright (c) 1998-2014 The Regents of the University of California.
004 All rights reserved.
005 Permission is hereby granted, without written agreement and without
006 license or royalty fees, to use, copy, modify, and distribute this
007 software and its documentation for any purpose, provided that the above
008 copyright notice and the following two paragraphs appear in all copies
009 of this software.
010
011 IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY
012 FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
013 ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF
014 THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF
015 SUCH DAMAGE.
016
017 THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
018 INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
019 MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE
020 PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF
021 CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES,
022 ENHANCEMENTS, OR MODIFICATIONS.
023
024 PT_COPYRIGHT_VERSION_2
025 COPYRIGHTENDKEY
026
027 */
028package ptolemy.domains.sdf.lib;
029
030import ptolemy.data.BooleanToken;
031import ptolemy.data.IntToken;
032import ptolemy.data.expr.Parameter;
033import ptolemy.data.type.BaseType;
034import ptolemy.kernel.CompositeEntity;
035import ptolemy.kernel.util.Attribute;
036import ptolemy.kernel.util.IllegalActionException;
037import ptolemy.kernel.util.NameDuplicationException;
038
039///////////////////////////////////////////////////////////////////
040/// IntToBits
041
042/**
043 This actor converts an IntToken into a sequence of Boolean tokens.
044 The number of Boolean tokens is specified by the <i>numberOfBits</i>
045 parameter. It should be a positive integer not bigger than 32.
046 The most significant bit (the sign bit) is the first boolean
047 token send out. It is "false" if the input integer is non-negative,
048 otherwise it is "true". The least significant bit is the last boolean
049 token send out.
050
051 Let <i>k</i> denotes the value of the <i>numberOfBits</i> parameter.
052 An exception is thrown if the input integer is smaller than
053 -2<sup><i>k</i></sup> or greater 2<sup><i>k</i></sup> - 1.
054
055 @author Michael Leung
056 @version $Id$
057 @since Ptolemy II 0.4
058 @Pt.ProposedRating Yellow (neuendor)
059 @Pt.AcceptedRating Yellow (neuendor)
060 */
061public class IntToBits extends SDFConverter {
062    /** Construct an actor with the given container and name.
063     *  @param container The container.
064     *  @param name The name of this actor.
065     *  @exception IllegalActionException If the actor cannot be contained
066     *   by the proposed container.
067     *  @exception NameDuplicationException If the container already has an
068     *   actor with this name.
069     */
070    public IntToBits(CompositeEntity container, String name)
071            throws NameDuplicationException, IllegalActionException {
072        super(container, name);
073
074        input.setTypeEquals(BaseType.INT);
075
076        numberOfBits = new Parameter(this, "numberOfBits");
077        numberOfBits.setExpression("32");
078
079        output_tokenProductionRate.setExpression("numberOfBits");
080        output.setTypeEquals(BaseType.BOOLEAN);
081    }
082
083    ///////////////////////////////////////////////////////////////////
084    ////                         parameters                        ////
085
086    /** The number of Boolean tokens that the input integer is converted to.
087     */
088    public Parameter numberOfBits;
089
090    ///////////////////////////////////////////////////////////////////
091    ////                         public methods                    ////
092
093    /** If the argument is the <i>numberOfBits</i> parameter, then
094     *  set the production rate of the output port.
095     *  @param attribute The attribute that has changed.
096     *  @exception IllegalActionException If the parameter is out of range.
097     */
098    @Override
099    public void attributeChanged(Attribute attribute)
100            throws IllegalActionException {
101        if (attribute == numberOfBits) {
102            int rate = ((IntToken) numberOfBits.getToken()).intValue();
103
104            if (rate < 1 || rate > 32) {
105                throw new IllegalActionException(this,
106                        "Invalid number of bits: " + rate);
107            }
108        } else {
109            super.attributeChanged(attribute);
110        }
111    }
112
113    /** Consume a single IntToken on the input. Produce <i>numberOfBits</i>
114     *  BooleanTokens on the output port which is the bitwise
115     *  representation of the input IntToken.
116     *  The most significant bit (the sign bit) is the first boolean
117     *  token send out. The least significant bit is the last
118     *  boolean token send out.
119     *
120     *  @exception IllegalActionException If there is no director.
121     *  or if the input integer is out of range.
122     */
123    @Override
124    public final void fire() throws IllegalActionException {
125        super.fire();
126
127        int rate = ((IntToken) numberOfBits.getToken()).intValue();
128        BooleanToken[] bits = new BooleanToken[rate];
129        IntToken token = (IntToken) input.get(0);
130        int integer = token.intValue();
131
132        if (integer < 0) {
133            if (integer < -(1 << rate - 1)) {
134                throw new IllegalActionException(this,
135                        "integer is out of range.");
136            }
137
138            bits[0] = new BooleanToken(true);
139
140            //integer = (int)(2147483648L + integer);
141            integer = (1 << rate - 1) + integer;
142        } else {
143            if (integer > (1 << rate - 1) - 1) {
144                throw new IllegalActionException(this,
145                        "integer is out of range.");
146            }
147
148            bits[0] = new BooleanToken(false);
149        }
150
151        for (int i = rate - 1; i > 0; i--) {
152            int remainder = integer % 2;
153            integer = integer / 2;
154
155            if (remainder == 0) {
156                bits[i] = new BooleanToken(false);
157            } else {
158                bits[i] = new BooleanToken(true);
159            }
160        }
161
162        output.send(0, bits, bits.length);
163    }
164}