📄 cxrand.cpp
字号:
/*M///////////////////////////////////////////////////////////////////////////////////////
//
// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
//
// By downloading, copying, installing or using the software you agree to this license.
// If you do not agree to this license, do not download, install,
// copy or use the software.
//
//
// Intel License Agreement
// For Open Source Computer Vision Library
//
// Copyright (C) 2000, Intel Corporation, all rights reserved.
// Third party copyrights are property of their respective owners.
//
// Redistribution and use in source and binary forms, with or without modification,
// are permitted provided that the following conditions are met:
//
// * Redistribution's of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
//
// * Redistribution's in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation
// and/or other materials provided with the distribution.
//
// * The name of Intel Corporation may not be used to endorse or promote products
// derived from this software without specific prior written permission.
//
// This software is provided by the copyright holders and contributors "as is" and
// any express or implied warranties, including, but not limited to, the implied
// warranties of merchantability and fitness for a particular purpose are disclaimed.
// In no event shall the Intel Corporation or contributors be liable for any direct,
// indirect, incidental, special, exemplary, or consequential damages
// (including, but not limited to, procurement of substitute goods or services;
// loss of use, data, or profits; or business interruption) however caused
// and on any theory of liability, whether in contract, strict liability,
// or tort (including negligence or otherwise) arising in any way out of
// the use of this software, even if advised of the possibility of such damage.
//
//M*/
/* ////////////////////////////////////////////////////////////////////
//
// Filling CvMat/IplImage instances with random numbers
//
// */
#include "_cxcore.h"
///////////////////////////// Functions Declaration //////////////////////////////////////
/*
Multiply-with-carry generator is used here:
temp = ( A*X(n) + carry )
X(n+1) = temp mod (2^32)
carry = temp / (2^32)
*/
#define ICV_RNG_NEXT(x) ((uint64)(unsigned)(x)*1554115554 + ((x) >> 32))
#define ICV_CVT_FLT(x) (((unsigned)(x) >> 9)|CV_1F)
#define ICV_1D CV_BIG_INT(0x3FF0000000000000)
#define ICV_CVT_DBL(x) (((uint64)(unsigned)(x) << 20)|((x) >> 44)|ICV_1D)
/***************************************************************************************\
* Pseudo-Random Number Generators (PRNGs) *
\***************************************************************************************/
#define ICV_IMPL_RAND_BITS( flavor, arrtype, cast_macro ) \
static CvStatus CV_STDCALL \
icvRandBits_##flavor##_C1R( arrtype* arr, int step, CvSize size, \
uint64* state, const int* param ) \
{ \
uint64 temp = *state; \
int small_flag = (param[12]|param[13]|param[14]|param[15]) <= 255; \
step /= sizeof(arr[0]); \
\
for( ; size.height--; arr += step ) \
{ \
int i, k = 3; \
const int* p = param; \
\
if( !small_flag ) \
{ \
for( i = 0; i <= size.width - 4; i += 4 ) \
{ \
unsigned t0, t1; \
\
temp = ICV_RNG_NEXT(temp); \
t0 = ((unsigned)temp & p[i + 12]) + p[i]; \
temp = ICV_RNG_NEXT(temp); \
t1 = ((unsigned)temp & p[i + 13]) + p[i+1]; \
arr[i] = cast_macro((int)t0); \
arr[i+1] = cast_macro((int)t1); \
\
temp = ICV_RNG_NEXT(temp); \
t0 = ((unsigned)temp & p[i + 14]) + p[i+2]; \
temp = ICV_RNG_NEXT(temp); \
t1 = ((unsigned)temp & p[i + 15]) + p[i+3]; \
arr[i+2] = cast_macro((int)t0); \
arr[i+3] = cast_macro((int)t1); \
\
if( !--k ) \
{ \
k = 3; \
p -= 12; \
} \
} \
} \
else \
{ \
for( i = 0; i <= size.width - 4; i += 4 ) \
{ \
unsigned t0, t1, t; \
\
temp = ICV_RNG_NEXT(temp); \
t = (unsigned)temp; \
t0 = (t & p[i + 12]) + p[i]; \
t1 = ((t >> 8) & p[i + 13]) + p[i+1]; \
arr[i] = cast_macro((int)t0); \
arr[i+1] = cast_macro((int)t1); \
\
t0 = ((t >> 16) & p[i + 14]) + p[i + 2]; \
t1 = ((t >> 24) & p[i + 15]) + p[i + 3]; \
arr[i+2] = cast_macro((int)t0); \
arr[i+3] = cast_macro((int)t1); \
\
if( !--k ) \
{ \
k = 3; \
p -= 12; \
} \
} \
} \
\
for( ; i < size.width; i++ ) \
{ \
unsigned t0; \
temp = ICV_RNG_NEXT(temp); \
\
t0 = ((unsigned)temp & p[i + 12]) + p[i]; \
arr[i] = cast_macro((int)t0); \
} \
} \
\
*state = temp; \
return CV_OK; \
}
#define ICV_IMPL_RAND( flavor, arrtype, worktype, cast_macro1, cast_macro2 )\
static CvStatus CV_STDCALL \
icvRand_##flavor##_C1R( arrtype* arr, int step, CvSize size, \
uint64* state, const double* param ) \
{ \
uint64 temp = *state; \
step /= sizeof(arr[0]); \
\
for( ; size.height--; arr += step ) \
{ \
int i, k = 3; \
const double* p = param; \
\
for( i = 0; i <= size.width - 4; i += 4 ) \
{ \
worktype f0, f1; \
Cv32suf t0, t1; \
\
temp = ICV_RNG_NEXT(temp); \
t0.u = ICV_CVT_FLT(temp); \
temp = ICV_RNG_NEXT(temp); \
t1.u = ICV_CVT_FLT(temp); \
f0 = cast_macro1( t0.f * p[i + 12] + p[i] ); \
f1 = cast_macro1( t1.f * p[i + 13] + p[i + 1] ); \
arr[i] = cast_macro2(f0); \
arr[i+1] = cast_macro2(f1); \
\
temp = ICV_RNG_NEXT(temp); \
t0.u = ICV_CVT_FLT(temp); \
temp = ICV_RNG_NEXT(temp); \
t1.u = ICV_CVT_FLT(temp); \
f0 = cast_macro1( t0.f * p[i + 14] + p[i + 2] ); \
f1 = cast_macro1( t1.f * p[i + 15] + p[i + 3] ); \
arr[i+2] = cast_macro2(f0); \
arr[i+3] = cast_macro2(f1); \
\
if( !--k ) \
{ \
k = 3; \
p -= 12; \
} \
} \
\
for( ; i < size.width; i++ ) \
{ \
worktype f0; \
Cv32suf t0; \
\
temp = ICV_RNG_NEXT(temp); \
t0.u = ICV_CVT_FLT(temp); \
f0 = cast_macro1( t0.f * p[i + 12] + p[i] ); \
arr[i] = cast_macro2(f0); \
} \
} \
\
*state = temp; \
return CV_OK; \
}
static CvStatus CV_STDCALL
icvRand_64f_C1R( double* arr, int step, CvSize size,
uint64* state, const double* param )
{
uint64 temp = *state;
step /= sizeof(arr[0]);
for( ; size.height--; arr += step )
{
int i, k = 3;
const double* p = param;
for( i = 0; i <= size.width - 4; i += 4 )
{
double f0, f1;
Cv64suf t0, t1;
temp = ICV_RNG_NEXT(temp);
t0.u = ICV_CVT_DBL(temp);
temp = ICV_RNG_NEXT(temp);
t1.u = ICV_CVT_DBL(temp);
f0 = t0.f * p[i + 12] + p[i];
f1 = t1.f * p[i + 13] + p[i + 1];
arr[i] = f0;
arr[i+1] = f1;
temp = ICV_RNG_NEXT(temp);
t0.u = ICV_CVT_DBL(temp);
temp = ICV_RNG_NEXT(temp);
t1.u = ICV_CVT_DBL(temp);
f0 = t0.f * p[i + 14] + p[i + 2];
f1 = t1.f * p[i + 15] + p[i + 3];
arr[i+2] = f0;
arr[i+3] = f1;
if( !--k )
{
k = 3;
p -= 12;
}
}
for( ; i < size.width; i++ )
{
double f0;
Cv64suf t0;
temp = ICV_RNG_NEXT(temp);
t0.u = ICV_CVT_DBL(temp);
f0 = t0.f * p[i + 12] + p[i];
arr[i] = f0;
}
}
*state = temp;
return CV_OK;
}
/***************************************************************************************\
The code below implements algorithm from the paper:
G. Marsaglia and W.W. Tsang,
The Monty Python method for generating random variables,
ACM Transactions on Mathematical Software, Vol. 24, No. 3,
Pages 341-350, September, 1998.
\***************************************************************************************/
static CvStatus CV_STDCALL
icvRandn_0_1_32f_C1R( float* arr, int len, uint64* state )
{
uint64 temp = *state;
int i;
temp = ICV_RNG_NEXT(temp);
for( i = 0; i < len; i++ )
{
double x, y, v, ax, bx;
for(;;)
{
x = ((int)temp)*1.167239e-9;
temp = ICV_RNG_NEXT(temp);
ax = fabs(x);
v = 2.8658 - ax*(2.0213 - 0.3605*ax);
y = ((unsigned)temp)*2.328306e-10;
temp = ICV_RNG_NEXT(temp);
if( y < v || ax < 1.17741 )
break;
bx = x;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -