summaryrefslogtreecommitdiff
path: root/indra/llcommon/lldefs.h
blob: ed075e8d96dd3454fec7186a5051e3abd00f597a (plain)
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
/**
 * @file lldefs.h
 * @brief Various generic constant definitions.
 *
 * $LicenseInfo:firstyear=2001&license=viewerlgpl$
 * Second Life Viewer Source Code
 * Copyright (C) 2010, Linden Research, Inc.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation;
 * version 2.1 of the License only.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 *
 * Linden Research, Inc., 945 Battery Street, San Francisco, CA  94111  USA
 * $/LicenseInfo$
 */

#ifndef LL_LLDEFS_H
#define LL_LLDEFS_H

#include "stdtypes.h"
#include <cassert>
#include <type_traits>

// Often used array indices
constexpr U32   VX          = 0;
constexpr U32   VY          = 1;
constexpr U32   VZ          = 2;
constexpr U32   VW          = 3;
constexpr U32   VS          = 3;

constexpr U32   VRED        = 0;
constexpr U32   VGREEN      = 1;
constexpr U32   VBLUE       = 2;
constexpr U32   VALPHA      = 3;

constexpr U32   INVALID_DIRECTION = 0xFFFFFFFF;
constexpr U32   EAST        = 0;
constexpr U32   NORTH       = 1;
constexpr U32   WEST        = 2;
constexpr U32   SOUTH       = 3;

constexpr U32   NORTHEAST   = 4;
constexpr U32   NORTHWEST   = 5;
constexpr U32   SOUTHWEST   = 6;
constexpr U32   SOUTHEAST   = 7;
constexpr U32   MIDDLE      = 8;

constexpr U8    EAST_MASK       = 0x1<<EAST;
constexpr U8    NORTH_MASK      = 0x1<<NORTH;
constexpr U8    WEST_MASK       = 0x1<<WEST;
constexpr U8    SOUTH_MASK      = 0x1<<SOUTH;

constexpr U8    NORTHEAST_MASK  = NORTH_MASK | EAST_MASK;
constexpr U8    NORTHWEST_MASK  = NORTH_MASK | WEST_MASK;
constexpr U8    SOUTHWEST_MASK  = SOUTH_MASK | WEST_MASK;
constexpr U8    SOUTHEAST_MASK  = SOUTH_MASK | EAST_MASK;

constexpr U32 gDirOpposite[8] = {2, 3, 0, 1, 6, 7, 4, 5};
constexpr U32 gDirAdjacent[8][2] =  {
                                    {4, 7},
                                    {4, 5},
                                    {5, 6},
                                    {6, 7},
                                    {0, 1},
                                    {1, 2},
                                    {2, 3},
                                    {0, 3}
                                    };

// Magnitude along the x and y axis
constexpr S32 gDirAxes[8][2] = {
                               { 1, 0}, // east
                               { 0, 1}, // north
                               {-1, 0}, // west
                               { 0,-1}, // south
                               { 1, 1}, // ne
                               {-1, 1}, // nw
                               {-1,-1}, // sw
                               { 1,-1}, // se
                               };

constexpr S32 gDirMasks[8] = {
                            EAST_MASK,
                            NORTH_MASK,
                            WEST_MASK,
                            SOUTH_MASK,
                            NORTHEAST_MASK,
                            NORTHWEST_MASK,
                            SOUTHWEST_MASK,
                            SOUTHEAST_MASK
                            };

// Sides of a box...
//                  . Z      __.Y
//                 /|\        /|       0 = NO_SIDE
//                  |        /         1 = FRONT_SIDE   = +x
//           +------|-----------+      2 = BACK_SIDE    = -x
//          /|      |/     /   /|      3 = LEFT_SIDE    = +y
//         / |     -5-   |/   / |      4 = RIGHT_SIDE   = -y
//        /  |     /|   -3-  /  |      5 = TOP_SIDE     = +z
//       +------------------+   |      6 = BOTTOM_SIDE  = -z
//       |   |      |  /    |   |
//       | |/|      | /     | |/|
//       | 2 |    | *-------|-1--------> X
//       |/| |   -4-        |/| |
//       |   +----|---------|---+
//       |  /        /      |  /
//       | /       -6-      | /
//       |/        /        |/
//       +------------------+
constexpr U32 NO_SIDE       = 0;
constexpr U32 FRONT_SIDE    = 1;
constexpr U32 BACK_SIDE     = 2;
constexpr U32 LEFT_SIDE     = 3;
constexpr U32 RIGHT_SIDE    = 4;
constexpr U32 TOP_SIDE      = 5;
constexpr U32 BOTTOM_SIDE   = 6;

constexpr U8 LL_SOUND_FLAG_NONE =         0x0;
constexpr U8 LL_SOUND_FLAG_LOOP =         1<<0;
constexpr U8 LL_SOUND_FLAG_SYNC_MASTER =  1<<1;
constexpr U8 LL_SOUND_FLAG_SYNC_SLAVE =   1<<2;
constexpr U8 LL_SOUND_FLAG_SYNC_PENDING = 1<<3;
constexpr U8 LL_SOUND_FLAG_QUEUE =        1<<4;
constexpr U8 LL_SOUND_FLAG_STOP =         1<<5;
constexpr U8 LL_SOUND_FLAG_SYNC_MASK = LL_SOUND_FLAG_SYNC_MASTER | LL_SOUND_FLAG_SYNC_SLAVE | LL_SOUND_FLAG_SYNC_PENDING;

//
// *NOTE: These values may be used as hard-coded numbers in scanf() variants.
//
// --------------
// DO NOT CHANGE.
// --------------
//
constexpr U32   LL_MAX_PATH     = 1024;     // buffer size of maximum path + filename string length

// For strings we send in messages
constexpr U32   STD_STRING_BUF_SIZE = 255;  // Buffer size
constexpr U32   STD_STRING_STR_LEN  = 254;  // Length of the string (not including \0)

// *NOTE: This value is used as hard-coded numbers in scanf() variants.
// DO NOT CHANGE.
constexpr U32   MAX_STRING          = STD_STRING_BUF_SIZE;  // Buffer size

constexpr U32   MAXADDRSTR      = 17;       // 123.567.901.345 = 15 chars + \0 + 1 for good luck

// C++ is our friend. . . use template functions to make life easier!

// specific inlines for basic types
//
// defined for all:
//   llmin(a,b)
//   llmax(a,b)
//   llclamp(a,minimum,maximum)
//
// defined for F32, F64:
//   llclampf(a)     // clamps a to [0.0 .. 1.0]
//
// defined for U16, U32, U64, S16, S32, S64, :
//   llclampb(a)     // clamps a to [0 .. 255]
//

// llless(d0, d1) safely compares d0 < d1 even if one is signed and the other
// is unsigned. A simple (d0 < d1) expression converts the signed operand to
// unsigned before comparing. If the signed operand is negative, that flips
// the negative value to a huge positive value, producing the wrong answer!
// llless() specifically addresses that case.
template <typename T0, typename T1>
inline bool llless(T0 d0, T1 d1)
{
    if constexpr (std::is_signed_v<T0> && ! std::is_signed_v<T1>)
    {
        // T0 signed, T1 unsigned: negative d0 is less than any unsigned d1
        if (d0 < 0)
            return true;
    }
    else if constexpr (! std::is_signed_v<T0> && std::is_signed_v<T1>)
    {
        // T0 unsigned, T1 signed: any unsigned d0 is greater than negative d1
        if (d1 < 0)
            return false;
    }
    // both T0 and T1 are signed, or both are unsigned, or both non-negative:
    // straightforward comparison works
    return d0 < d1;
}

// recursion tail
template <typename T>
inline auto llmax(T data)
{
    return data;
}

template <typename T0, typename T1, typename... Ts>
inline auto llmax(T0 d0, T1 d1, Ts... rest)
{
    auto maxrest = llmax(d1, rest...);
    return llless(maxrest, d0)? d0 : maxrest;
}

// recursion tail
template <typename T>
inline auto llmin(T data)
{
    return data;
}

template <typename T0, typename T1, typename... Ts>
inline auto llmin(T0 d0, T1 d1, Ts... rest)
{
    auto minrest = llmin(d1, rest...);
    return llless(d0, minrest) ? d0 : minrest;
}

template <typename A, typename MIN, typename MAX>
inline A llclamp(A a, MIN minval, MAX maxval)
{
    // The only troublesome case is if A is unsigned and either minval or
    // maxval is both signed and negative. Casting a negative number to
    // unsigned flips it to a huge positive number, making this llclamp() call
    // ineffective.
    if constexpr (! std::is_signed_v<A>)
    {
        if constexpr (std::is_signed_v<MIN>)
        {
            assert(minval >= 0);
        }
        if constexpr (std::is_signed_v<MAX>)
        {
            assert(maxval >= 0);
        }
    }

    A aminval{ static_cast<A>(minval) }, amaxval{ static_cast<A>(maxval) };
    if ( a < aminval )
    {
        return aminval;
    }
    else if ( a > amaxval )
    {
        return amaxval;
    }
    return a;
}

template <class LLDATATYPE>
inline LLDATATYPE llclampf(LLDATATYPE a)
{
    return llmin(llmax(a, LLDATATYPE(0)), LLDATATYPE(1));
}

template <class LLDATATYPE>
inline LLDATATYPE llclampb(LLDATATYPE a)
{
    return llmin(llmax(a, LLDATATYPE(0)), LLDATATYPE(255));
}

template <class LLDATATYPE>
inline void llswap(LLDATATYPE& lhs, LLDATATYPE& rhs)
{
    std::swap(lhs, rhs);
}

#endif // LL_LLDEFS_H