Skip to content
Snippets Groups Projects
Select Git revision
  • eb7c996e529a49b5eedeb064df275378486987d7
  • master default protected
  • hpke
  • ppc-chacha-4core
  • delete-internal-name-mangling
  • master-updates
  • ppc-gcm
  • ppc-chacha-2core
  • refactor-ecc-mod
  • ppc-chacha-core
  • use-mpn_cnd-functions
  • optimize-ecc-invert
  • default-m4-quote-char
  • power-asm-wip
  • test-fat
  • chacha-3core-neon
  • x86_64-salsa20-2core
  • salsa20-2core-neon
  • bcrypt
  • arm-salsa20-chacha-vsra
  • test-shlib-dir
  • nettle_3.6_release_20200429
  • nettle_3.6rc3
  • nettle_3.6rc2
  • nettle_3.6rc1
  • nettle_3.5.1_release_20190627
  • nettle_3.5_release_20190626
  • nettle_3.5rc1
  • nettle_3.4.1_release_20181204
  • nettle_3.4.1rc1
  • nettle_3.4_release_20171119
  • nettle_3.4rc2
  • nettle_3.4rc1
  • nettle_3.3_release_20161001
  • nettle_3.2_release_20160128
  • nettle_3.1.1_release_20150424
  • nettle_3.1_release_20150407
  • nettle_3.1rc3
  • nettle_3.1rc2
  • nettle_3.1rc1
  • nettle_3.0_release_20140607
41 results

bignum-random.c

Blame
  • Forked from Nettle / nettle
    Source project has a limited visibility.
    stuff.c 2.23 KiB
    /*\
    ||| This file a part of Pike, and is copyright by Fredrik Hubinette
    ||| Pike is distributed as GPL (General Public License)
    ||| See the files COPYING and DISCLAIMER for more information.
    \*/
    #include "stuff.h"
    
    /* same thing as (int)floor(log((double)x) / log(2.0)) */
    /* Except a bit quicker :) (hopefully) */
    
    int my_log2(unsigned INT32 x)
    {
      static char bit[256] =
      {
        -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, 
      };
      register unsigned int tmp;
      if(tmp=(x>>16))
      {
        if(x=(tmp>>8)) return bit[x]+24;
        return bit[tmp]+16;
      }
      if(tmp=(x>>8)) return bit[tmp]+8;
      return bit[x];
    }
    
    
    /* Return the number of bits in a 32-bit integer */
    int count_bits(unsigned INT32 x)
    {
    #define B(X) X+0,X+1,X+1,X+2,\
                 X+1,X+2,X+2,X+3,\
                 X+1,X+2,X+2,X+3,\
                 X+2,X+3,X+3,X+4
      static char bits[256] =
      {
        B(0), B(1), B(1), B(2),
        B(1), B(2), B(2), B(3),
        B(1), B(2), B(2), B(3),
        B(2), B(3), B(3), B(4)
      };
    
      return (bits[x & 255] +
    	  bits[(x>>8) & 255] +
    	  bits[(x>>16) & 255] +
    	  bits[(x>>24) & 255]);
    }
    
    /* Return true for integers with more than one bit set */
    int is_more_than_one_bit(unsigned INT32 x)
    {
      return ((x & 0xaaaaaaaa) && (x & 0x55555555)) ||
             ((x & 0xcccccccc) && (x & 0x33333333)) ||
             ((x & 0xf0f0f0f0) && (x & 0x0f0f0f0f)) ||
             ((x & 0xff00ff00) && (x & 0x00ff00ff)) ||
             ((x & 0xff00ff00) && (x & 0x00ff00ff)) ||
             ((x & 0xffff0000) && (x & 0x0000ffff));
    }