Beispiel #1
0
static void lfg_srand(AVLFG *c, unsigned int seed){
    uint32_t i, tmp = seed;

    for(i=0; i<64; i+=1){
        tmp = lcg_rand(tmp);
        c->state[i]= tmp;
    }
    c->index=0;
}
static void
initialize_palette (pixman_indexed_t *palette, uint32_t mask, int is_rgb)
{
    int i;

    for (i = 0; i < 32768; ++i)
        palette->ent[i] = lcg_rand() & mask;

    for (i = 0; i < mask + 1; ++i)
    {
        uint32_t rgba24;
        pixman_bool_t retry;
        uint32_t i15;

        /* We filled the rgb->index map with random numbers, but we
         * do need the ability to round trip, that is if some indexed
         * color expands to an argb24, then the 15 bit version of that
         * color must map back to the index. Anything else, we don't
         * care about too much.
         */
        do
        {
            uint32_t old_idx;

            rgba24 = lcg_rand();
            i15 = CONVERT_15 (rgba24, is_rgb);

            old_idx = palette->ent[i15];
            if (CONVERT_15 (palette->rgba[old_idx], is_rgb) == i15)
                retry = 1;
            else
                retry = 0;
        } while (retry);

        palette->rgba[i] = rgba24;
        palette->ent[i15] = i;
    }

    for (i = 0; i < mask + 1; ++i)
    {
        assert (palette->ent[CONVERT_15 (palette->rgba[i], is_rgb)] == i);
    }
}
    void onDraw(const int loops, SkCanvas* canvas) override {
        // Unlike blackhole, junk can and probably will be a register.
        uint32_t junk = 0;
        uint32_t seed = 0;
        for (int i = 0; i < loops; i++) {
            SkPMColor color;
        #ifdef SK_DEBUG
            // Our SkASSERTs will remind us that it's technically required that we premultiply.
            color = SkPreMultiplyColor(lcg_rand(&seed));
        #else
            // But it's a lot faster not to, and this code won't really mind the non-PM colors.
            color = lcg_rand(&seed);
        #endif

            auto f = SkPMFloat::FromPMColor(color);
            SkPMColor back = f.round();
            junk ^= back;
        }
        blackhole ^= junk;
    }
Beispiel #4
0
static inline short dithered_vol(short sample) {
  short rand_a, rand_b;
  long out;

  out = (long)sample * fix_volume;
  if (fix_volume < 0x10000) {
    rand_b = rand_a;
    rand_a = lcg_rand();
    out += rand_a;
    out -= rand_b;
  }
  return out >> 16;
}
Beispiel #5
0
 void onDraw(int loops, SkCanvas* canvas) override {
     // Unlike blackhole, junk can and probably will be a register.
     uint32_t junk = 0;
     uint32_t seed = 0;
     for (int i = 0; i < loops; i++) {
         uint32_t color = lcg_rand(&seed),
                  back;
         auto f = SkNx_cast<float>(Sk4b::Load(&color));
         SkNx_cast<uint8_t>(f).store(&back);
         junk ^= back;
     }
     blackhole ^= junk;
 }
Beispiel #6
0
 void onDraw(const int loops, SkCanvas* canvas) override {
     // Unlike blackhole, junk can and probably will be a register.
     uint32_t junk = 0;
     uint32_t seed = 0;
     for (int i = 0; i < loops; i++) {
         uint32_t color = lcg_rand(&seed),
                  back;
         auto f = Sk4f::FromBytes((const uint8_t*)&color);
         f.toBytes((uint8_t*)&back);
         junk ^= back;
     }
     blackhole ^= junk;
 }
Beispiel #7
0
/****************************************************************************
 * Given a range, calculate some possible constants for the LCG algorithm
 * for randomizing the order of the array.
 * @parm m
 *      The range for which we'll be finding random numbers. If we are
 *      looking for random numbers between [0..100), this number will
 *      be 100.
 * @parm a
 *      The LCG 'a' constant that will be the result of this function.
 * @param c
 *      The LCG 'c' constant that will be the result of this function. This
 *      should be set to 0 on the input to this function, or a suggested
 *      value.
 ****************************************************************************/
void
lcg_calculate_constants(uint64_t m, uint64_t *out_a, uint64_t *inout_c, int is_debug)
{
	uint64_t a;
	uint64_t c = *inout_c;
    double elapsed = 0.0; /* Benchmark of 'sieve' algorithm */
    PRIMEFACTORS factors; /* List of prime factors of 'm' */
	PRIMEFACTORS non_factors;
	unsigned i;

    /*
     * Find all the prime factors of the number. This step can take several
     * seconds for 48 bit numbers, which is why we benchmark how long it
     * takes.
     */
    sieve_prime_factors(m, factors, non_factors, &elapsed);
   
    /*
     * Calculate the 'a-1' constant. It must share all the prime factors
     * with the range, and if the range is a multiple of 4, must also
     * be a multiple of 4
     */
	if (factors[0] == m) {
		/* this number has no prime factors, so we can choose anything.
		 * Therefore, we are going to pick something at random */
		unsigned j;

		a = 1;
		for (j=0; non_factors[j] && j < 5; j++)
			a *= non_factors[j];
	} else {
		unsigned j;
		a = 1;
		for (i=0; factors[i]; i++)
			a = a * factors[i];
		if ((m % 4) == 0)
			a *= 2;
		
		for (j=0; j<0 && non_factors[j]; j++)
			a *= non_factors[j];
	}   
	a += 1;
   
    /*
     * Calculate the 'c' constant. It must have no prime factors in
     * common with the range.
     */
    if (c == 0)
        c = 2531011 ; /* something random */
    while (has_factors_in_common(c, factors))
        c++;

	if (is_debug) {
		/*
		 * print the results
		 */
		//printf("sizeof(int) = %llu-bits\n", (uint64_t)(sizeof(size_t)*8));
		printf("elapsed     = %5.3f-seconds\n", elapsed);
		printf("factors     = ");
		for (i=0; factors[i]; i++)
			printf("%llu ", factors[i]);
		printf("%s\n", factors[0]?"":"(none)");
		printf("m           = %-24llu (0x%llx)\n", m, m);
		printf("a           = %-24llu (0x%llx)\n", a, a);
		printf("c           = %-24llu (0x%llx)\n", c, c);
		printf("c%%m         = %-24llu (0x%llx)\n", c%m, c%m);
		printf("a%%m         = %-24llu (0x%llx)\n", a%m, a%m);
   
		if (m < 1000000000) {
			if (lcg_verify(a, c+1, m, 280))
				printf("verify      = success\n");
			else
				printf("verify      = failure\n");
		} else {
			printf("verify      = too big to check\n");
		}
       
   
		/*
		 * Print some first numbers. We use these to visually inspect whether
		 * the results are random or not.
		 */
		{
			unsigned count = 0;
			uint64_t x = 0;
			unsigned digits = count_digits(m);
       
			for (i=0; i<100 && i < m; i++) {
				x = lcg_rand(x, a, c, m);
				count += printf("%*llu ", digits, x);
				if (count >= 70) {
					count = 0;
					printf("\n");
				}
			}
			printf("\n");
		}
	}

	*out_a = a;
	*inout_c = c;
}
Beispiel #8
0
// pseudo-random in [0,1]
double randf(){
	return lcg_rand() / (double)0xFFFFFFFFUL;
}