e576bb2002-10-11Martin Nilsson /* || This file is part of Pike. For copyright information see COPYRIGHT. || Pike is distributed under GPL, LGPL and MPL. See the file COPYING || for more information. */
24ddc71998-03-28Henrik Grubbström (Grubba) 
ad42b81999-02-27Henrik Grubbström (Grubba) #include "global.h"
5267b71995-08-09Fredrik Hübinette (Hubbe) #include "stuff.h"
559b5f2004-11-14Martin Stjernholm /* Used by is8bitalnum in pike_macros.h. */ PMOD_EXPORT const char Pike_is8bitalnum_vector[] = "0000000000000000" "0000000000000000" "0000000000000000" "1111111111000000" "0111111111111111" "1111111111100001" "0111111111111111" "1111111111100000" "0000000000000000" "0000000000000000" "1011110101100010" "1011011001101110" "1111111111111111" "1111111011111111" "1111111111111111" "1111111011111111";
75920f1997-12-28Fredrik Hübinette (Hubbe) /* Not all of these are primes, but they should be adequate */
c653912011-05-02Per Hedbor const INT32 hashprimes[32] =
75920f1997-12-28Fredrik Hübinette (Hubbe) { 31, /* ~ 2^0 = 1 */ 31, /* ~ 2^1 = 2 */ 31, /* ~ 2^2 = 4 */ 31, /* ~ 2^3 = 8 */ 31, /* ~ 2^4 = 16 */ 31, /* ~ 2^5 = 32 */ 61, /* ~ 2^6 = 64 */ 127, /* ~ 2^7 = 128 */ 251, /* ~ 2^8 = 256 */ 541, /* ~ 2^9 = 512 */ 1151, /* ~ 2^10 = 1024 */ 2111, /* ~ 2^11 = 2048 */ 4327, /* ~ 2^12 = 4096 */ 8803, /* ~ 2^13 = 8192 */ 17903, /* ~ 2^14 = 16384 */ 32321, /* ~ 2^15 = 32768 */ 65599, /* ~ 2^16 = 65536 */ 133153, /* ~ 2^17 = 131072 */ 270001, /* ~ 2^18 = 264144 */ 547453, /* ~ 2^19 = 524288 */ 1109891, /* ~ 2^20 = 1048576 */ 2000143, /* ~ 2^21 = 2097152 */ 4561877, /* ~ 2^22 = 4194304 */ 9248339, /* ~ 2^23 = 8388608 */ 16777215, /* ~ 2^24 = 16777216 */ 33554431, /* ~ 2^25 = 33554432 */ 67108863, /* ~ 2^26 = 67108864 */ 134217727, /* ~ 2^27 = 134217728 */ 268435455, /* ~ 2^28 = 268435456 */ 536870911, /* ~ 2^29 = 536870912 */ 1073741823,/* ~ 2^30 = 1073741824 */ 2147483647,/* ~ 2^31 = 2147483648 */ };
5267b71995-08-09Fredrik Hübinette (Hubbe) /* same thing as (int)floor(log((double)x) / log(2.0)) */ /* Except a bit quicker :) (hopefully) */
c7241b2000-08-10Henrik Grubbström (Grubba) PMOD_EXPORT int my_log2(size_t x)
5267b71995-08-09Fredrik Hübinette (Hubbe) {
f5bcae2014-05-05Per Hedbor #if (defined(__x86_64) || defined(__i386__)) && defined(__GNUC__)
520d142014-06-20Henrik Grubbström (Grubba)  if( x == 0 ) return 0;
cd7d552011-05-03Per Hedbor #if SIZEOF_CHAR_P > 4
f5bcae2014-05-05Per Hedbor  asm("bsrq %1, %0" :"+r"(x) :"rm"(x));
cd7d552011-05-03Per Hedbor #else
f5bcae2014-05-05Per Hedbor  asm("bsrl %1, %0" :"+r"(x) :"rm"(x)); #endif /* sizeof(char*) */ return x+1; #elif defined(HAS___BUILTIN_CLZL)
520d142014-06-20Henrik Grubbström (Grubba)  if( x == 0 ) return 0;
f5bcae2014-05-05Per Hedbor  return ((sizeof(unsigned long)*8)-__builtin_clzl(x))-1; #elif defined(HAS___BUILTIN_CLZ) /* on arm32 only this one exists. Happily enough sizeof(int) is * sizeof(size_t). */
520d142014-06-20Henrik Grubbström (Grubba)  if( x == 0 ) return 0;
f5bcae2014-05-05Per Hedbor  return ((sizeof(int)*8)-__builtin_clz(x))-1;
cd7d552011-05-03Per Hedbor #else
fc60d42004-06-01Martin Nilsson  static const signed char bit[256] =
5267b71995-08-09Fredrik Hübinette (Hubbe)  {
5de4c12002-08-30Martin Nilsson  -1, 0, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
5267b71995-08-09Fredrik Hübinette (Hubbe)  };
c7241b2000-08-10Henrik Grubbström (Grubba)  register size_t tmp;
520d142014-06-20Henrik Grubbström (Grubba)  if( x == 0 ) return 0;
c7241b2000-08-10Henrik Grubbström (Grubba) #if SIZEOF_CHAR_P > 4 if((tmp=(x>>32))) { if((x=(tmp>>16))) { if((tmp=(x>>8))) return bit[tmp]+56; return bit[x]+48; } if((x=(tmp>>8))) return bit[x]+40; return bit[tmp]+32; } #endif /* SIZEOF_CHAP_P > 4 */
9c6f7d1997-04-15Fredrik Hübinette (Hubbe)  if((tmp=(x>>16)))
5267b71995-08-09Fredrik Hübinette (Hubbe)  {
9c6f7d1997-04-15Fredrik Hübinette (Hubbe)  if((x=(tmp>>8))) return bit[x]+24;
5267b71995-08-09Fredrik Hübinette (Hubbe)  return bit[tmp]+16; }
9c6f7d1997-04-15Fredrik Hübinette (Hubbe)  if((tmp=(x>>8))) return bit[tmp]+8;
5267b71995-08-09Fredrik Hübinette (Hubbe)  return bit[x];
cd7d552011-05-03Per Hedbor #endif
5267b71995-08-09Fredrik Hübinette (Hubbe) }
1f21332000-07-28Fredrik Hübinette (Hubbe) PMOD_EXPORT double my_strtod(char *nptr, char **endptr)
5740881998-01-01Fredrik Hübinette (Hubbe) {
7661da2014-09-03Martin Nilsson  double tmp=strtod(nptr,endptr);
5740881998-01-01Fredrik Hübinette (Hubbe)  if(*endptr>nptr) { if(endptr[0][-1]=='.') endptr[0]--; } return tmp; }
a6b98a1999-02-27Henrik Grubbström (Grubba) 
9031422011-04-05Arne Goedeke /* * This rounds an integer up to the next power of two. For x a power * of two, this will just return the same again. */ unsigned INT32 find_next_power(unsigned INT32 x) {
f5bcae2014-05-05Per Hedbor  if( x == 0 ) return 1; return 1<<(my_log2(x-1)+1);
9031422011-04-05Arne Goedeke }