Libav
|
00001 /* 00002 * Lagged Fibonacci PRNG 00003 * Copyright (c) 2008 Michael Niedermayer 00004 * 00005 * This file is part of FFmpeg. 00006 * 00007 * FFmpeg is free software; you can redistribute it and/or 00008 * modify it under the terms of the GNU Lesser General Public 00009 * License as published by the Free Software Foundation; either 00010 * version 2.1 of the License, or (at your option) any later version. 00011 * 00012 * FFmpeg is distributed in the hope that it will be useful, 00013 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00015 * Lesser General Public License for more details. 00016 * 00017 * You should have received a copy of the GNU Lesser General Public 00018 * License along with FFmpeg; if not, write to the Free Software 00019 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 00020 */ 00021 00022 #include <inttypes.h> 00023 #include "lfg.h" 00024 #include "md5.h" 00025 #include "intreadwrite.h" 00026 #include "attributes.h" 00027 00028 void av_cold av_lfg_init(AVLFG *c, unsigned int seed){ 00029 uint8_t tmp[16]={0}; 00030 int i; 00031 00032 for(i=8; i<64; i+=4){ 00033 AV_WL32(tmp, seed); tmp[4]=i; 00034 av_md5_sum(tmp, tmp, 16); 00035 c->state[i ]= AV_RL32(tmp); 00036 c->state[i+1]= AV_RL32(tmp+4); 00037 c->state[i+2]= AV_RL32(tmp+8); 00038 c->state[i+3]= AV_RL32(tmp+12); 00039 } 00040 c->index=0; 00041 } 00042 00043 void av_bmg_get(AVLFG *lfg, double out[2]) 00044 { 00045 double x1, x2, w; 00046 00047 do { 00048 x1 = 2.0/UINT_MAX*av_lfg_get(lfg) - 1.0; 00049 x2 = 2.0/UINT_MAX*av_lfg_get(lfg) - 1.0; 00050 w = x1*x1 + x2*x2; 00051 } while (w >= 1.0); 00052 00053 w = sqrt((-2.0 * log(w)) / w); 00054 out[0] = x1 * w; 00055 out[1] = x2 * w; 00056 } 00057 00058 #ifdef TEST 00059 #include "log.h" 00060 #include "timer.h" 00061 00062 int main(void) 00063 { 00064 int x=0; 00065 int i, j; 00066 AVLFG state; 00067 00068 av_lfg_init(&state, 0xdeadbeef); 00069 for (j = 0; j < 10000; j++) { 00070 START_TIMER 00071 for (i = 0; i < 624; i++) { 00072 // av_log(NULL,AV_LOG_ERROR, "%X\n", av_lfg_get(&state)); 00073 x+=av_lfg_get(&state); 00074 } 00075 STOP_TIMER("624 calls of av_lfg_get"); 00076 } 00077 av_log(NULL, AV_LOG_ERROR, "final value:%X\n", x); 00078 00079 /* BMG usage example */ 00080 { 00081 double mean = 1000; 00082 double stddev = 53; 00083 00084 av_lfg_init(&state, 42); 00085 00086 for (i = 0; i < 1000; i += 2) { 00087 double bmg_out[2]; 00088 av_bmg_get(&state, bmg_out); 00089 av_log(NULL, AV_LOG_INFO, 00090 "%f\n%f\n", 00091 bmg_out[0] * stddev + mean, 00092 bmg_out[1] * stddev + mean); 00093 } 00094 } 00095 00096 return 0; 00097 } 00098 #endif