SIntFunc

Files CCore/inc/gadget/SIntFunc.h CCore/src/gadget/SIntFunc.cpp

SIntFunc is an Algorithm Package, parametrized by a signed integral type. Currently it is assumed, that the type uses 2's complement representation.


template <class SInt>
struct SIntFunc
 {
  // types

  using UInt = .... ; 

  // consts
  
  static const UInt MaxPositiveAbs = .... ;
  
  static const UInt MinNegativeAbs = .... ;
  
  static const SInt MaxPositive = .... ;
  
  static const SInt MinNegative = .... ;

UInt is the correspondent unsigned integral type.

MaxPositiveAbs is the absolute value of the maximum (positive) value of the SInt, represented as UInt type.

MinNegativeAbs is the absolute value of the minimum (negative) value of the SInt, represented as UInt type.

MaxPositive is the maximum (positive) value of the SInt.

MinNegative is the minimum (negative) value of the SInt.


  // abs
  
  static SInt PosAbs(UInt abs);
  
  static SInt NegAbs(UInt abs);

PosAbs() returns +abs as SInt value. It is assumed, that abs<=MaxPositiveAbs.

NegAbs() returns -abs as SInt value. It is assumed, that abs<=MinNegativeAbs.


  // shift

  static SInt LShift(SInt a,unsigned shift); // may overflow , UB for some arguments must be removed, operates as ASL

  static SInt RShift(SInt a,unsigned shift); // US for some arguments must be removed, operates as ASR

  // mask

  static SInt Mask(SInt a,UInt mask);

LShift() performs the arithmetic shift left. The result is a*2shift, if no overflow occurred.

RShift() performs the arithmetic shift right. The result is a/2shift rounded down.

Mask() performs the bitwise AND. The following is true: (UInt)Mask(a,mask) == UInt(a)&mask, if no overflow occurred.

All these functions expect the proper compiler behavior on the target type.

  
  // dist/move
  
  static UInt Dist(SInt a,SInt b); // a <= b 

  static SInt MovePos(SInt a,UInt delta); 
  
  static SInt MoveNeg(SInt a,UInt delta); 
  
  static SInt Move(SInt a,SInt e,UInt delta); 
 };

Dist() returns the distance from a to b, it is assumed, that a <= b. The return value is represented by the UInt.

MovePos() moves the argument a to the positive direction by the distance delta. It is assumed, that the upper SInt value bound is not crossed. I.e. if a <= b, then MovePos(a,Dist(a,b)) == b.

MoveNeg() moves the argument a to the negative direction by the distance delta. It is assumed, that the lower SInt value bound is not crossed. I.e. if a <= b, then MoveNeg(b,Dist(a,b)) == a.

Move() moves the argument a to the positive direction by the distance delta if e > 0, otherwise it moves a to the negative direction by the distance delta.