Project

General

Profile

root / trunk / compiler / ooasCompiler / src / org / momut / ooas / math / SaturatedIntegerOperations.java @ 7

1
/**
2
  *
3
  *                      OOAS Compiler
4
  *
5
  *       Copyright 2015, AIT Austrian Institute of Technology.
6
  * This code is based on the C# Version of the OOAS Compiler, which is
7
  * copyright 2015 by the Institute of Software Technology, Graz University
8
  * of Technology with portions copyright by the AIT Austrian Institute of
9
  * Technology. All rights reserved.
10
  *
11
  * SEE THE "LICENSE" FILE FOR THE TERMS UNDER WHICH THIS FILE IS PROVIDED.
12
  *
13
  * If you modify the file please update the list of contributors below to in-
14
  * clude your name. Please also stick to the coding convention of using TABs
15
  * to do the basic (block-level) indentation and spaces for anything after
16
  * that. (Enable the display of special chars and it should be pretty obvious
17
  * what this means.) Also, remove all trailing whitespace.
18
  *
19
  * Contributors:
20
  *               Willibald Krenn (AIT)
21
  *               Stephan Zimmerer (AIT)
22
  *               Markus Demetz (AIT)
23
  *               Christoph Czurda (AIT)
24
  *
25
  */
26

    
27

    
28
package org.momut.ooas.math;
29

    
30

    
31
public final class SaturatedIntegerOperations extends Operations<Integer>{
32
        public static final class SatIntegerBasicOps extends BasicOps<Integer> {
33
                @Override
34
                public Integer unMinus(Integer input) {
35
                        return input == Integer.MIN_VALUE ? Integer.MAX_VALUE : -input;
36
                }
37
                @Override
38
                public Integer minus(Integer inputA, Integer inputB) {
39
                        if (inputB < 0)
40
                                return (inputA > Integer.MAX_VALUE + inputB) ? Integer.MAX_VALUE : inputA - inputB;
41
                        else
42
                                return (inputA < Integer.MIN_VALUE + inputB) ? Integer.MIN_VALUE : inputA - inputB;
43
                }
44
                @Override
45
                public Integer plus(Integer inputA, Integer inputB) {
46
                        if (inputB < 0)
47
                                return (inputA < Integer.MIN_VALUE - inputB) ? Integer.MIN_VALUE : inputA + inputB;
48
                        else
49
                                return (inputA > Integer.MAX_VALUE - inputB) ? Integer.MAX_VALUE : inputA + inputB;
50
                }
51
                @Override
52
                public Integer div(Integer inputA, Integer inputB) {
53
                        //FIXME: checked { return a / b; };
54
                        return inputA / inputB;
55
                }
56
                @Override
57
                public Integer pow(Integer inputA, Integer inputB) {
58
                        long tmp = (long) Math.pow(inputA, inputB);
59
                        if (tmp < Integer.MIN_VALUE) return Integer.MIN_VALUE;
60
                        else if (tmp > Integer.MAX_VALUE) return Integer.MAX_VALUE;
61
                        else return (int)tmp;
62
                }
63
                @Override
64
                public Integer prod(Integer inputA, Integer inputB) {
65
                        long tmp = (long)inputA * (long)inputB;
66
                        if (tmp < Integer.MIN_VALUE) return Integer.MIN_VALUE;
67
                        else if (tmp > Integer.MAX_VALUE) return Integer.MAX_VALUE;
68
                        else return (int)tmp;
69
                }
70
                @Override
71
                public Integer mod(Integer inputA, Integer inputB) {return inputA % inputB;}
72
                @Override
73
                public boolean equal(Integer inputA, Integer inputB) {return inputA.equals(inputB);}
74
                @Override
75
                public boolean smaller(Integer inputA, Integer inputB) {return inputA < inputB;}
76
                @Override
77
                public boolean greater(Integer inputA, Integer inputB){return inputA > inputB;}
78
                @Override
79
                public Integer getDefaultValue() {return 0;}
80
        }
81

    
82
        public SaturatedIntegerOperations() {
83
                super(new SatIntegerBasicOps());
84
        }
85
}