b25fa62003-04-12Martin Nilsson #pike __REAL_VERSION__ //! Returns the parity of the integer @[value]. If the //! parity is odd 1 is returned. If it is even 0 is //! returned. int(0..1) parity(int(0..) value) {
fe6bcb2003-08-07Martin Nilsson  if(value<0) error("Parity can not determined for negative values.\n"); return Gmp.mpz(value)->popcount()&1;
b25fa62003-04-12Martin Nilsson }
a63b362003-11-07Martin Stjernholm  constant NATIVE_MIN = __builtin.NATIVE_INT_MIN; constant NATIVE_MAX = __builtin.NATIVE_INT_MAX;
1d27c12003-11-12Martin Stjernholm  //! @decl constant NATIVE_MIN; //! @decl constant NATIVE_MAX; //!
a63b362003-11-07Martin Stjernholm //! The limits for using the native representation of integers on the //! current architecture. Any integer that is outside this range uses //! a more complex and slower representation. Also, some builtin //! functions that don't expect very large integers might start to //! complain about invalid argument type when given values outside //! this range (they typically say something like "Expected integer, //! got object"). //!
4495092003-11-08Martin Stjernholm //! @[NATIVE_MIN] is not greater than @expr{-2147483648@} //! (@expr{-0x80000000@}). //! //! @[NATIVE_MAX] is not less than @expr{2147483647@} //! (@expr{0x7fffffff@}). //!
a63b362003-11-07Martin Stjernholm //! @note //! The size of the native integers can be controlled when Pike is //! compiled with the configure flags @expr{--with-int-int@}, //! @expr{--with-long-int@}, and @expr{--with-long-long-int@}. The //! default is to use the longest available integer type that fits //! inside a pointer, which typically means that it's 64 bit on "true" //! 64 bit architectures. //!
48d0732004-03-15Martin Nilsson  //! Swaps the upper and lower byte in a word.
6177d02010-06-05Henrik Grubbström (Grubba) //! //! @seealso //! @[swap_long()]
48d0732004-03-15Martin Nilsson int(0..65535) swap_word(int(0..65535) i) { return ((i&255)<<8) | ((i&(255<<8))>>8); } //! Swaps the upper and lower word in a longword, and the upper and //! lower bytes in the words. Simply put, the bytes are reversed.
6177d02010-06-05Henrik Grubbström (Grubba) //! //! @seealso //! @[swap_word()]
48d0732004-03-15Martin Nilsson int(0..4294967295) swap_long(int(0..4294967295) i) { return ((i&255)<<24) | ((i&(255<<8))<<8) | ((i&(255<<16))>>8) | ((i&(255<<24))>>24); }
75bd5f2004-10-28Martin Nilsson 
6177d02010-06-05Henrik Grubbström (Grubba) //! Reverses the order of the low order @[bits] number of bits //! of the value @[value]. //! //! @note //! Any higher order bits of the value will be cleared. //! The returned value will thus be unsigned. //! //! @seealso //! @[reverse()], @[swap_word()], @[swap_long()] int(0..) reflect(int value, int(0..) bits) { int aligned_bits = bits; if (bits & (bits-1)) { // Find the closest larger even power of two. aligned_bits <<= 1; while (aligned_bits & (aligned_bits-1)) { aligned_bits -= (aligned_bits & ~(aligned_bits-1)); } } bits = aligned_bits - bits; // Perform pair-wise swapping of bit-sequences. int mask = (1<<aligned_bits)-1; int filter = mask; while (aligned_bits >>= 1) { filter ^= filter>>aligned_bits; value = (value & filter)>>aligned_bits | (value & (filter^mask))<<aligned_bits; } // Adjust the returned value in case we've swapped more bits // than needed. We then have junk in the lowest order bits. return value>>bits; }
31698c2011-04-04Martin Stjernholm //! The type of @[Int.inf]. Do not create more instances of this. class Inf {
75bd5f2004-10-28Martin Nilsson 
9eaf1d2008-06-28Martin Nilsson  protected constant neg = 0; protected int __hash() { return 17; } protected int(0..1) _equal(mixed arg) {
d896b12004-10-30Martin Nilsson  if(neg && arg==-Math.inf) return 1; if(!neg && arg==Math.inf) return 1; return arg==this; }
9eaf1d2008-06-28Martin Nilsson  protected int(0..1) _is_type(mixed type) { return (< "int", "object" >)[type]; }
4b51c12018-02-03Henrik Grubbström (Grubba)  protected mixed _random(function rnd_string, function rnd) { if (neg) return 0; return this; }
9eaf1d2008-06-28Martin Nilsson  protected mixed _sqrt() { return this; }
75bd5f2004-10-28Martin Nilsson  // % == nan // & == nan
509c8c2017-01-06Martin Nilsson  protected mixed `*(mixed arg) {
75bd5f2004-10-28Martin Nilsson  int n = neg;
509c8c2017-01-06Martin Nilsson  if(arg<0) n = !n;
75bd5f2004-10-28Martin Nilsson  if(n) return ninf; return inf; }
509c8c2017-01-06Martin Nilsson  protected mixed ``*(mixed arg) { return `*(arg); } protected mixed `+(mixed arg) { if(arg==`-()) error("NaN\n");
75bd5f2004-10-28Martin Nilsson  return this; }
509c8c2017-01-06Martin Nilsson  protected mixed ``+(mixed arg) { return ``+(arg); } protected mixed `-(void|mixed arg) { if(!query_num_arg()) {
75bd5f2004-10-28Martin Nilsson  if(neg) return inf; return ninf; }
509c8c2017-01-06Martin Nilsson  if(arg==inf || arg==ninf) error("NaN\n");
75bd5f2004-10-28Martin Nilsson  return this; }
9eaf1d2008-06-28Martin Nilsson  protected mixed ``-(mixed arg) {
75bd5f2004-10-28Martin Nilsson  if(arg==inf || arg==ninf) error("NaN\n"); return this; }
9eaf1d2008-06-28Martin Nilsson  protected int(0..1) `<(mixed arg) {
75bd5f2004-10-28Martin Nilsson  if(arg==this) return 0; return neg; }
9eaf1d2008-06-28Martin Nilsson  protected int(0..1) `>(mixed arg) {
75bd5f2004-10-28Martin Nilsson  if(arg==this) return 0; return !neg; }
9eaf1d2008-06-28Martin Nilsson  protected mixed `~() { return `-(); } protected mixed `<<(mixed arg) {
d896b12004-10-30Martin Nilsson  if(arg<0) error("Got negative shift count.\n"); return this; }
9eaf1d2008-06-28Martin Nilsson  protected mixed ``<<(mixed arg) {
d896b12004-10-30Martin Nilsson  if(arg<0) return ninf; return inf; }
9eaf1d2008-06-28Martin Nilsson  protected mixed `>>(mixed arg) {
d896b12004-10-30Martin Nilsson  if(arg<0) error("Got negative shift count.\n"); return this; }
9eaf1d2008-06-28Martin Nilsson  protected mixed ``>>(mixed arg) {
d896b12004-10-30Martin Nilsson  return 0; }
9eaf1d2008-06-28Martin Nilsson  protected mixed cast(string to) {
75bd5f2004-10-28Martin Nilsson  switch(to) { case "string": return "inf"; case "float": return Math.inf; default:
ec1a0f2014-08-16Martin Nilsson  return UNDEFINED;
75bd5f2004-10-28Martin Nilsson  } }
9eaf1d2008-06-28Martin Nilsson  protected string _sprintf(int t) {
75bd5f2004-10-28Martin Nilsson  return t=='O' && (neg?"-":"")+"Int.inf"; } } class NInf { inherit Inf; constant neg = 1; }
9eaf1d2008-06-28Martin Nilsson protected Inf ninf = NInf();
75bd5f2004-10-28Martin Nilsson 
31698c2011-04-04Martin Stjernholm //! An object that behaves like positive infinity.
75bd5f2004-10-28Martin Nilsson Inf inf = Inf();