lin
2025-08-14 dae8bad597b6607a449b32bf76c523423f7720ed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#ifndef _TCURANDOMVALUEITERATOR_HPP
#define _TCURANDOMVALUEITERATOR_HPP
/*-------------------------------------------------------------------------
 * drawElements Quality Program Tester Core
 * ----------------------------------------
 *
 * Copyright 2014 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 *//*!
 * \file
 * \brief Random value iterator.
 *//*--------------------------------------------------------------------*/
 
#include "tcuDefs.hpp"
#include "deRandom.hpp"
 
namespace tcu
{
 
template <typename T>
T getRandomValue (de::Random& rnd)
{
   // \note memcpy() is the only valid way to do cast from uint32 to float for instnance.
   deUint8 data[sizeof(T) + sizeof(T)%4];
   DE_STATIC_ASSERT(sizeof(data)%4 == 0);
   for (int vecNdx = 0; vecNdx < DE_LENGTH_OF_ARRAY(data)/4; vecNdx++)
   {
       deUint32 rval = rnd.getUint32();
       for (int compNdx = 0; compNdx < 4; compNdx++)
           data[vecNdx*4+compNdx] = ((const deUint8*)&rval)[compNdx];
   }
   return *(const T*)&data[0];
}
 
// Faster implementations for int types.
template <> inline deUint8    getRandomValue<deUint8>        (de::Random& rnd) { return (deUint8)rnd.getUint32();    }
template <> inline deUint16    getRandomValue<deUint16>    (de::Random& rnd) { return (deUint16)rnd.getUint32();    }
template <> inline deUint32    getRandomValue<deUint32>    (de::Random& rnd) { return rnd.getUint32();                }
template <> inline deUint64    getRandomValue<deUint64>    (de::Random& rnd) { return rnd.getUint64();                }
template <> inline deInt8    getRandomValue<deInt8>        (de::Random& rnd) { return (deInt8)rnd.getUint32();        }
template <> inline deInt16    getRandomValue<deInt16>        (de::Random& rnd) { return (deInt16)rnd.getUint32();    }
template <> inline deInt32    getRandomValue<deInt32>        (de::Random& rnd) { return (deInt32)rnd.getUint32();    }
template <> inline deInt64    getRandomValue<deInt64>        (de::Random& rnd) { return (deInt64)rnd.getUint64();    }
 
template <typename T>
class RandomValueIterator : public std::iterator<std::forward_iterator_tag, T>
{
public:
   static RandomValueIterator    begin                    (deUint32 seed, int numValues)    { return RandomValueIterator<T>(seed, numValues);    }
   static RandomValueIterator    end                        (void)                            { return RandomValueIterator<T>(0, 0);                }
 
   RandomValueIterator&        operator++                (void);
   RandomValueIterator            operator++                (int);
 
   const T&                    operator*                (void) const { return m_curVal; }
 
   bool                        operator==                (const RandomValueIterator<T>& other) const;
   bool                        operator!=                (const RandomValueIterator<T>& other) const;
 
private:
                               RandomValueIterator        (deUint32 seed, int numLeft);
 
   de::Random                    m_rnd;
   int                            m_numLeft;
   T                            m_curVal;
};
 
template <typename T>
RandomValueIterator<T>::RandomValueIterator (deUint32 seed, int numLeft)
   : m_rnd        (seed)
   , m_numLeft    (numLeft)
   , m_curVal    (numLeft > 0 ? getRandomValue<T>(m_rnd) : T())
{
}
 
template <typename T>
RandomValueIterator<T>& RandomValueIterator<T>::operator++ (void)
{
   DE_ASSERT(m_numLeft > 0);
 
   m_numLeft    -= 1;
   m_curVal     = getRandomValue<T>(m_rnd);
 
   return *this;
}
 
template <typename T>
RandomValueIterator<T> RandomValueIterator<T>::operator++ (int)
{
   RandomValueIterator copy(*this);
   ++(*this);
   return copy;
}
 
template <typename T>
bool RandomValueIterator<T>::operator== (const RandomValueIterator<T>& other) const
{
   return (m_numLeft == 0 && other.m_numLeft == 0) || (m_numLeft == other.m_numLeft && m_rnd == other.m_rnd);
}
 
template <typename T>
bool RandomValueIterator<T>::operator!= (const RandomValueIterator<T>& other) const
{
   return !(m_numLeft == 0 && other.m_numLeft == 0) && (m_numLeft != other.m_numLeft || m_rnd != other.m_rnd);
}
 
} // tcu
 
#endif // _TCURANDOMVALUEITERATOR_HPP