diff options
Diffstat (limited to 'linden/indra/llmath/llrand.h')
-rw-r--r-- | linden/indra/llmath/llrand.h | 88 |
1 files changed, 88 insertions, 0 deletions
diff --git a/linden/indra/llmath/llrand.h b/linden/indra/llmath/llrand.h new file mode 100644 index 0000000..db9f353 --- /dev/null +++ b/linden/indra/llmath/llrand.h | |||
@@ -0,0 +1,88 @@ | |||
1 | /** | ||
2 | * @file llrand.h | ||
3 | * @brief Some useful math functions. | ||
4 | * | ||
5 | * Copyright (c) 2000-2007, Linden Research, Inc. | ||
6 | * | ||
7 | * The source code in this file ("Source Code") is provided by Linden Lab | ||
8 | * to you under the terms of the GNU General Public License, version 2.0 | ||
9 | * ("GPL"), unless you have obtained a separate licensing agreement | ||
10 | * ("Other License"), formally executed by you and Linden Lab. Terms of | ||
11 | * the GPL can be found in doc/GPL-license.txt in this distribution, or | ||
12 | * online at http://secondlife.com/developers/opensource/gplv2 | ||
13 | * | ||
14 | * There are special exceptions to the terms and conditions of the GPL as | ||
15 | * it is applied to this Source Code. View the full text of the exception | ||
16 | * in the file doc/FLOSS-exception.txt in this software distribution, or | ||
17 | * online at http://secondlife.com/developers/opensource/flossexception | ||
18 | * | ||
19 | * By copying, modifying or distributing this software, you acknowledge | ||
20 | * that you have read and understood your obligations described above, | ||
21 | * and agree to abide by those obligations. | ||
22 | * | ||
23 | * ALL LINDEN LAB SOURCE CODE IS PROVIDED "AS IS." LINDEN LAB MAKES NO | ||
24 | * WARRANTIES, EXPRESS, IMPLIED OR OTHERWISE, REGARDING ITS ACCURACY, | ||
25 | * COMPLETENESS OR PERFORMANCE. | ||
26 | */ | ||
27 | |||
28 | #ifndef LL_LLRAND_H | ||
29 | #define LL_LLRAND_H | ||
30 | |||
31 | // As long as you #include "llviewerprecompiledheaders.h", | ||
32 | // you can use "gLindenLabRandomNumber.llfrand( range );" which returns a | ||
33 | // random number F32 ranging from 0.0f to range. | ||
34 | // -Ventrella - Sept 30, 2005 | ||
35 | |||
36 | // Slams Intel processors into Single Precision FP mode | ||
37 | // (which is not any faster on modern hardware) | ||
38 | void slamFPCW( void ); | ||
39 | |||
40 | class LLRand | ||
41 | { | ||
42 | public: | ||
43 | LLRand(U32 seed) : mSeed(seed) {} | ||
44 | ~LLRand() {} | ||
45 | |||
46 | void seed(U32 seed) { mSeed = seed; } | ||
47 | |||
48 | U32 llrand() | ||
49 | { | ||
50 | mSeed = U64L(1664525) * mSeed + U64L(1013904223); | ||
51 | return (U32)mSeed; | ||
52 | } | ||
53 | |||
54 | U32 llrand(U32 val) | ||
55 | { | ||
56 | mSeed = U64L(1664525) * mSeed + U64L(1013904223); | ||
57 | return (U32)(mSeed) % val; | ||
58 | } | ||
59 | |||
60 | // val is the maximum | ||
61 | F32 llfrand(F32 val) | ||
62 | { | ||
63 | const U32 FP_ONE = 0x3f800000; | ||
64 | const U32 FP_MASK = 0x007fffff; | ||
65 | U32 ir = llrand(); | ||
66 | |||
67 | ir = FP_ONE | (FP_MASK & ir); | ||
68 | |||
69 | // generate random float | ||
70 | F32 fr = (*(F32 *)&ir); | ||
71 | |||
72 | // correct to [0..1) | ||
73 | fr -= 1.f; | ||
74 | |||
75 | fr *= val; | ||
76 | |||
77 | return fr; | ||
78 | } | ||
79 | |||
80 | public: | ||
81 | U64 mSeed; | ||
82 | }; | ||
83 | |||
84 | F32 frand(F32 val); | ||
85 | |||
86 | extern LLRand gLindenLabRandomNumber; | ||
87 | |||
88 | #endif | ||