Changeset 2524


Ignore:
Timestamp:
Feb 27, 2013, 7:13:22 PM (10 years ago)
Author:
sam
Message:

math: move rand() to its own header, ensure it only returns
positive values, and add a unit test for rand<int>.

Location:
trunk
Files:
2 added
8 edited
6 moved

Legend:

Unmodified
Added
Removed
  • trunk/src/Makefile.am

    r2500 r2524  
    3232
    3333liblolcore_headers = \
    34     lol/base/base.h \
     34    lol/base/all.h \
    3535    lol/base/log.h lol/base/array.h lol/base/types.h lol/base/array.h \
    3636    lol/base/assert.h lol/base/string.h lol/base/hash.h lol/base/map.h \
    3737    \
    38     lol/math/math.h \
     38    lol/math/all.h \
    3939    lol/math/functions.h lol/math/vector.h lol/math/half.h lol/math/real.h \
    4040    lol/math/remez.h lol/math/math.h lol/math/geometry.h lol/math/interp.h \
     41    lol/math/rand.h \
    4142    \
    42     lol/sys/sys.h \
     43    lol/sys/all.h \
    4344    lol/sys/init.h lol/sys/file.h lol/sys/thread.h lol/sys/atomic.h \
    4445    lol/sys/timer.h \
    4546    \
    46     lol/image/image.h \
     47    lol/image/all.h \
    4748    lol/image/color.h \
    4849    \
    49     lol/gpu/gpu.h \
     50    lol/gpu/all.h \
    5051    lol/gpu/shader.h lol/gpu/indexbuffer.h lol/gpu/vertexbuffer.h \
    5152    lol/gpu/framebuffer.h lol/gpu/texture.h lol/gpu/lolfx.h \
    5253    \
    53     lol/debug/debug.h \
     54    lol/debug/all.h \
    5455    lol/debug/line.h \
    5556    \
  • trunk/src/core.h

    r2506 r2524  
    9292
    9393// Base types
    94 #include <lol/base/base.h>
    95 #include <lol/math/math.h>
    96 #include <lol/sys/sys.h>
    97 #include <lol/image/image.h>
    98 #include <lol/gpu/gpu.h>
    99 #include <lol/debug/debug.h>
     94#include <lol/base/all.h>
     95#include <lol/math/all.h>
     96#include <lol/sys/all.h>
     97#include <lol/image/all.h>
     98#include <lol/gpu/all.h>
     99#include <lol/debug/all.h>
    100100
    101101#include "numeric.h"
  • trunk/src/lol/base/all.h

    r2523 r2524  
    99//
    1010
    11 #if !defined __LOL_BASE_BASE_H__
    12 #define __LOL_BASE_BASE_H__
     11#if !defined __LOL_BASE_ALL_H__
     12#define __LOL_BASE_ALL_H__
    1313
    1414#include <lol/base/types.h>
     
    2020#include <lol/base/map.h>
    2121
    22 #endif // __LOL_BASE_BASE_H__
     22#endif // __LOL_BASE_ALL_H__
    2323
  • trunk/src/lol/debug/all.h

    r2523 r2524  
    99//
    1010
    11 #if !defined __LOL_DEBUG_DEBUG_H__
    12 #define __LOL_DEBUG_DEBUG_H__
     11#if !defined __LOL_DEBUG_ALL_H__
     12#define __LOL_DEBUG_ALL_H__
    1313
    1414#include <lol/debug/lines.h>
    1515
    16 #endif // __LOL_DEBUG_DEBUG_H__
     16#endif // __LOL_DEBUG_ALL_H__
    1717
  • trunk/src/lol/gpu/all.h

    r2523 r2524  
    99//
    1010
    11 #if !defined __LOL_GPU_GPU_H__
    12 #define __LOL_GPU_GPU_H__
     11#if !defined __LOL_GPU_ALL_H__
     12#define __LOL_GPU_ALL_H__
    1313
    1414#include <lol/gpu/shader.h>
     
    1919#include <lol/gpu/lolfx.h>
    2020
    21 #endif // __LOL_GPU_GPU_H__
     21#endif // __LOL_GPU_ALL_H__
    2222
  • trunk/src/lol/image/all.h

    r2523 r2524  
    99//
    1010
    11 #if !defined __LOL_IMAGE_IMAGE_H__
    12 #define __LOL_IMAGE_IMAGE_H__
     11#if !defined __LOL_IMAGE_ALL_H__
     12#define __LOL_IMAGE_ALL_H__
    1313
    1414#include <lol/image/color.h>
    1515
    16 #endif // __LOL_IMAGE_IMAGE_H__
     16#endif // __LOL_IMAGE_ALL_H__
    1717
  • trunk/src/lol/math/all.h

    r2523 r2524  
    99//
    1010
    11 #if !defined __LOL_MATH_MATH_H__
    12 #define __LOL_MATH_MATH_H__
     11#if !defined __LOL_MATH_ALL_H__
     12#define __LOL_MATH_ALL_H__
    1313
    1414#include <lol/math/functions.h>
     
    1818#include <lol/math/geometry.h>
    1919#include <lol/math/interp.h>
     20#include <lol/math/rand.h>
    2021
    21 #endif // __LOL_MATH_MATH_H__
     22#endif // __LOL_MATH_ALL_H__
    2223
  • trunk/src/lol/math/vector.h

    r2348 r2524  
    2020#include <ostream>
    2121
    22 #include <lol/math/math.h>
    2322#include <lol/math/half.h>
    2423#include <lol/math/real.h>
  • trunk/src/lol/sys/all.h

    r2523 r2524  
    99//
    1010
    11 #if !defined __LOL_SYS_SYS_H__
    12 #define __LOL_SYS_SYS_H__
     11#if !defined __LOL_SYS_ALL_H__
     12#define __LOL_SYS_ALL_H__
    1313
    1414#include <lol/sys/thread.h>
     
    1818#include <lol/sys/timer.h>
    1919
    20 #endif // __LOL_SYS_SYS_H__
     20#endif // __LOL_SYS_ALL_H__
    2121
  • trunk/src/lolcore.vcxproj

    r2500 r2524  
    197197    <ClInclude Include="loldebug.h" />
    198198    <ClInclude Include="lolgl.h" />
     199    <ClInclude Include="lol\base\all.h" />
    199200    <ClInclude Include="lol\base\array.h" />
    200201    <ClInclude Include="lol\base\assert.h" />
    201     <ClInclude Include="lol\base\base.h" />
    202202    <ClInclude Include="lol\base\hash.h" />
    203203    <ClInclude Include="lol\base\log.h" />
     
    205205    <ClInclude Include="lol\base\string.h" />
    206206    <ClInclude Include="lol\base\types.h" />
    207     <ClInclude Include="lol\debug\debug.h" />
     207    <ClInclude Include="lol\debug\all.h" />
    208208    <ClInclude Include="lol\debug\lines.h" />
     209    <ClInclude Include="lol\gpu\all.h" />
    209210    <ClInclude Include="lol\gpu\framebuffer.h" />
    210     <ClInclude Include="lol\gpu\gpu.h" />
    211211    <ClInclude Include="lol\gpu\indexbuffer.h" />
    212212    <ClInclude Include="lol\gpu\lolfx.h" />
     
    214214    <ClInclude Include="lol\gpu\texture.h" />
    215215    <ClInclude Include="lol\gpu\vertexbuffer.h" />
     216    <ClInclude Include="lol\image\all.h" />
    216217    <ClInclude Include="lol\image\color.h" />
    217     <ClInclude Include="lol\image\image.h" />
     218    <ClInclude Include="lol\math\all.h" />
    218219    <ClInclude Include="lol\math\functions.h" />
    219220    <ClInclude Include="lol\math\geometry.h" />
    220221    <ClInclude Include="lol\math\half.h" />
    221222    <ClInclude Include="lol\math\interp.h" />
    222     <ClInclude Include="lol\math\math.h" />
     223    <ClInclude Include="lol\math\rand.h" />
    223224    <ClInclude Include="lol\math\real.h" />
    224225    <ClInclude Include="lol\math\remez.h" />
    225226    <ClInclude Include="lol\math\vector.h" />
     227    <ClInclude Include="lol\sys\all.h" />
    226228    <ClInclude Include="lol\sys\atomic.h" />
    227229    <ClInclude Include="lol\sys\file.h" />
  • trunk/src/lolcore.vcxproj.filters

    r2500 r2524  
    327327      <Filter>lol\math</Filter>
    328328    </ClInclude>
    329     <ClInclude Include="lol\math\math.h">
     329    <ClInclude Include="lol\math\all.h">
     330      <Filter>lol\math</Filter>
     331    </ClInclude>
     332    <ClInclude Include="lol\math\rand.h">
    330333      <Filter>lol\math</Filter>
    331334    </ClInclude>
     
    522525      <Filter>easymesh</Filter>
    523526    </ClInclude>
    524     <ClInclude Include="lol\base\base.h">
     527    <ClInclude Include="lol\base\all.h">
    525528      <Filter>lol\base</Filter>
    526529    </ClInclude>
     
    540543      <Filter>lol\gpu</Filter>
    541544    </ClInclude>
    542     <ClInclude Include="lol\image\image.h">
     545    <ClInclude Include="lol\image\all.h">
    543546      <Filter>lol\image</Filter>
    544547    </ClInclude>
    545     <ClInclude Include="lol\gpu\gpu.h">
     548    <ClInclude Include="lol\gpu\all.h">
    546549      <Filter>lol\gpu</Filter>
    547550    </ClInclude>
     
    567570      <Filter>lol\gpu</Filter>
    568571    </ClInclude>
    569     <ClInclude Include="lol\sys\sys.h">
     572    <ClInclude Include="lol\sys\all.h">
    570573      <Filter>lol\sys</Filter>
    571574    </ClInclude>
  • trunk/src/numeric.h

    r2520 r2524  
    2222namespace lol
    2323{
    24 
    25 /* Random number generators */
    26 template<typename T> static inline T rand();
    27 template<typename T> static inline T rand(T a);
    28 template<typename T> static inline T rand(T a, T b);
    29 
    30 /* One-value random number generators */
    31 template<typename T> static inline T rand(T a)
    32 {
    33     return rand<T>() % a;
    34 }
    35 
    36 template<>
    37 inline half rand<half>(half a) { return a * std::rand() / RAND_MAX; }
    38 template<>
    39 inline float rand<float>(float a) { return a * std::rand() / RAND_MAX; }
    40 template<>
    41 inline double rand<double>(double a) { return a * std::rand() / RAND_MAX; }
    42 template<>
    43 inline ldouble rand<ldouble>(ldouble a) { return a * std::rand() / RAND_MAX; }
    44 
    45 /* Two-value random number generator -- no need for specialisation */
    46 template<typename T> static inline T rand(T a, T b)
    47 {
    48     return a + rand<T>(b - a);
    49 }
    50 
    51 /* Default random number generator */
    52 template<typename T> static inline T rand()
    53 {
    54     switch (sizeof(T))
    55     {
    56     case 1:
    57         return static_cast<T>(std::rand() & 0xff);
    58     case 2:
    59         if (RAND_MAX >= 0xffff)
    60             return static_cast<T>(std::rand());
    61         else
    62             return static_cast<T>((std::rand() << 8) ^ std::rand());
    63     case 4:
    64         if (RAND_MAX >= 0xffff)
    65             return static_cast<T>((std::rand() << 16) ^ std::rand());
    66         else
    67             return static_cast<T>((std::rand() << 24) ^ (std::rand() << 16)
    68                                  ^ (std::rand() << 8) ^ std::rand());
    69     case 8:
    70         if (RAND_MAX >= 0xffff)
    71             return static_cast<T>(((uint64_t)std::rand() << 48)
    72                                 ^ ((uint64_t)std::rand() << 32)
    73                                 ^ ((uint64_t)std::rand() << 16)
    74                                 ^ ((uint64_t)std::rand()));
    75         else
    76             return static_cast<T>(((uint64_t)std::rand() << 56)
    77                                 ^ ((uint64_t)std::rand() << 48)
    78                                 ^ ((uint64_t)std::rand() << 40)
    79                                 ^ ((uint64_t)std::rand() << 32)
    80                                 ^ ((uint64_t)std::rand() << 24)
    81                                 ^ ((uint64_t)std::rand() << 16)
    82                                 ^ ((uint64_t)std::rand() << 8)
    83                                 ^ ((uint64_t)std::rand()));
    84     default:
    85         ASSERT(false, "rand() doesn’t support types of size %d\n",
    86                (int)sizeof(T));
    87     }
    88 }
    89 
    90 template<> inline half rand<half>() { return rand<half>(1.f); }
    91 template<> inline float rand<float>() { return rand<float>(1.f); }
    92 template<> inline double rand<double>() { return rand<double>(1.0); }
    93 template<> inline ldouble rand<ldouble>() { return rand<ldouble>(1.0); }
    94 
    9524
    9625/* Next power of two. */
  • trunk/test/Makefile.am

    r2468 r2524  
    2222    unit/build.cpp unit/real.cpp unit/image.cpp unit/quat.cpp unit/cmplx.cpp \
    2323    unit/array.cpp unit/rotation.cpp unit/string.cpp unit/map.cpp \
    24     unit/color.cpp unit/atomic.cpp unit/interp.cpp unit/box.cpp
     24    unit/color.cpp unit/atomic.cpp unit/interp.cpp unit/box.cpp \
     25    unit/rand.cpp
    2526testsuite_CPPFLAGS = $(AM_CPPFLAGS)
    2627testsuite_DEPENDENCIES = @LOL_DEPENDENCIES@
  • trunk/test/testsuite.vcxproj

    r2468 r2524  
    4848    <ClCompile Include="unit\matrix.cpp" />
    4949    <ClCompile Include="unit\quat.cpp" />
     50    <ClCompile Include="unit\rand.cpp" />
    5051    <ClCompile Include="unit\real.cpp" />
    5152    <ClCompile Include="unit\rotation.cpp" />
Note: See TracChangeset for help on using the changeset viewer.