github.com/FUSIONFoundation/efsn@v3.6.2-0.20200916075423-dbb5dd5d2cc7+incompatible/common/overflow/overflow_template.sh (about)

     1  #!/bin/sh
     2  
     3  exec > overflow_impl.go
     4  
     5  echo "package overflow
     6  
     7  // This is generated code, created by overflow_template.sh executed
     8  // by \"go generate\"
     9  
    10  "
    11  
    12  
    13  for SIZE in 8 16 32 64
    14  do
    15  echo "
    16  
    17  // Add${SIZE} performs + operation on two int${SIZE} operands
    18  // returning a result and status
    19  func Add${SIZE}(a, b int${SIZE}) (int${SIZE}, bool) {
    20          c := a + b
    21          if (c > a) == (b > 0) {
    22                  return c, true
    23          }
    24          return c, false
    25  }
    26  
    27  // Add${SIZE}p is the unchecked panicing version of Add${SIZE}
    28  func Add${SIZE}p(a, b int${SIZE}) int${SIZE} {
    29          r, ok := Add${SIZE}(a, b)
    30          if !ok {
    31                  panic(\"addition overflow\")
    32          }
    33          return r
    34  }
    35  
    36  
    37  // Sub${SIZE} performs - operation on two int${SIZE} operands
    38  // returning a result and status
    39  func Sub${SIZE}(a, b int${SIZE}) (int${SIZE}, bool) {
    40          c := a - b
    41          if (c < a) == (b > 0) {
    42                  return c, true
    43          }
    44          return c, false
    45  }
    46  
    47  // Sub${SIZE}p is the unchecked panicing version of Sub${SIZE}
    48  func Sub${SIZE}p(a, b int${SIZE}) int${SIZE} {
    49          r, ok := Sub${SIZE}(a, b)
    50          if !ok {
    51                  panic(\"subtraction overflow\")
    52          }
    53          return r
    54  }
    55  
    56  
    57  // Mul${SIZE} performs * operation on two int${SIZE} operands
    58  // returning a result and status
    59  func Mul${SIZE}(a, b int${SIZE}) (int${SIZE}, bool) {
    60          if a == 0 || b == 0 {
    61                  return 0, true
    62          }
    63          c := a * b
    64          if (c < 0) == ((a < 0) != (b < 0)) {
    65                  if c/b == a {
    66                          return c, true
    67                  }
    68          }
    69          return c, false
    70  }
    71  
    72  // Mul${SIZE}p is the unchecked panicing version of Mul${SIZE}
    73  func Mul${SIZE}p(a, b int${SIZE}) int${SIZE} {
    74          r, ok := Mul${SIZE}(a, b)
    75          if !ok {
    76                  panic(\"multiplication overflow\")
    77          }
    78          return r
    79  }
    80  
    81  
    82  
    83  // Div${SIZE} performs / operation on two int${SIZE} operands
    84  // returning a result and status
    85  func Div${SIZE}(a, b int${SIZE}) (int${SIZE}, bool) {
    86          q, _, ok := Quotient${SIZE}(a, b)
    87          return q, ok
    88  }
    89  
    90  // Div${SIZE}p is the unchecked panicing version of Div${SIZE}
    91  func Div${SIZE}p(a, b int${SIZE}) int${SIZE} {
    92          r, ok := Div${SIZE}(a, b)
    93          if !ok {
    94                  panic(\"division failure\")
    95          }
    96          return r
    97  }
    98  
    99  // Quotient${SIZE} performs + operation on two int${SIZE} operands
   100  // returning a quotient, a remainder and status
   101  func Quotient${SIZE}(a, b int${SIZE}) (int${SIZE}, int${SIZE}, bool) {
   102          if b == 0 {
   103                  return 0, 0, false
   104          }
   105          c := a / b
   106          status := (c < 0) == ((a < 0) != (b < 0))
   107          return c, a % b, status
   108  }
   109  "
   110  done