amd: update addrlib
[mesa.git] / src / amd / addrlib / src / core / addrcommon.h
1 /*
2 * Copyright © 2007-2019 Advanced Micro Devices, Inc.
3 * All Rights Reserved.
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining
6 * a copy of this software and associated documentation files (the
7 * "Software"), to deal in the Software without restriction, including
8 * without limitation the rights to use, copy, modify, merge, publish,
9 * distribute, sub license, and/or sell copies of the Software, and to
10 * permit persons to whom the Software is furnished to do so, subject to
11 * the following conditions:
12 *
13 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
14 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
15 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
16 * NON-INFRINGEMENT. IN NO EVENT SHALL THE COPYRIGHT HOLDERS, AUTHORS
17 * AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20 * USE OR OTHER DEALINGS IN THE SOFTWARE.
21 *
22 * The above copyright notice and this permission notice (including the
23 * next paragraph) shall be included in all copies or substantial portions
24 * of the Software.
25 */
26
27 /**
28 ****************************************************************************************************
29 * @file addrcommon.h
30 * @brief Contains the helper function and constants.
31 ****************************************************************************************************
32 */
33
34 #ifndef __ADDR_COMMON_H__
35 #define __ADDR_COMMON_H__
36
37 #include "addrinterface.h"
38
39 #if !defined(DEBUG)
40 #ifdef NDEBUG
41 #define DEBUG 0
42 #else
43 #define DEBUG 1
44 #endif
45 #endif
46
47 // ADDR_LNX_KERNEL_BUILD is for internal build
48 // Moved from addrinterface.h so __KERNEL__ is not needed any more
49 #if !defined(__APPLE__) || defined(HAVE_TSERVER)
50 #include <stdlib.h>
51 #include <string.h>
52 #include <assert.h>
53 #endif
54
55 ////////////////////////////////////////////////////////////////////////////////////////////////////
56 // Platform specific debug break defines
57 ////////////////////////////////////////////////////////////////////////////////////////////////////
58 #if DEBUG
59 #if defined(__GNUC__)
60 #define ADDR_DBG_BREAK() assert(false)
61 #elif defined(__APPLE__)
62 #define ADDR_DBG_BREAK() { IOPanic("");}
63 #else
64 #define ADDR_DBG_BREAK() { __debugbreak(); }
65 #endif
66 #else
67 #define ADDR_DBG_BREAK()
68 #endif
69 ////////////////////////////////////////////////////////////////////////////////////////////////////
70
71 ////////////////////////////////////////////////////////////////////////////////////////////////////
72 // Debug assertions used in AddrLib
73 ////////////////////////////////////////////////////////////////////////////////////////////////////
74 #if defined(_WIN32) && (_MSC_VER >= 1400)
75 #define ADDR_ANALYSIS_ASSUME(expr) __analysis_assume(expr)
76 #else
77 #define ADDR_ANALYSIS_ASSUME(expr) do { (void)(expr); } while (0)
78 #endif
79
80 #define ADDR_ASSERT(__e) assert(__e)
81 #define ADDR_ASSERT_ALWAYS() ADDR_DBG_BREAK()
82 #define ADDR_UNHANDLED_CASE() ADDR_ASSERT(!"Unhandled case")
83 #define ADDR_NOT_IMPLEMENTED() ADDR_ASSERT(!"Not implemented");
84 ////////////////////////////////////////////////////////////////////////////////////////////////////
85
86 ////////////////////////////////////////////////////////////////////////////////////////////////////
87 // Debug print macro from legacy address library
88 ////////////////////////////////////////////////////////////////////////////////////////////////////
89 #if DEBUG
90
91 #define ADDR_PRNT(a) Object::DebugPrint a
92
93 /// @brief Macro for reporting informational messages
94 /// @ingroup util
95 ///
96 /// This macro optionally prints an informational message to stdout.
97 /// The first parameter is a condition -- if it is true, nothing is done.
98 /// The second pararmeter MUST be a parenthesis-enclosed list of arguments,
99 /// starting with a string. This is passed to printf() or an equivalent
100 /// in order to format the informational message. For example,
101 /// ADDR_INFO(0, ("test %d",3) ); prints out "test 3".
102 ///
103 #define ADDR_INFO(cond, a) \
104 { if (!(cond)) { ADDR_PRNT(a); } }
105
106 /// @brief Macro for reporting error warning messages
107 /// @ingroup util
108 ///
109 /// This macro optionally prints an error warning message to stdout,
110 /// followed by the file name and line number where the macro was called.
111 /// The first parameter is a condition -- if it is true, nothing is done.
112 /// The second pararmeter MUST be a parenthesis-enclosed list of arguments,
113 /// starting with a string. This is passed to printf() or an equivalent
114 /// in order to format the informational message. For example,
115 /// ADDR_WARN(0, ("test %d",3) ); prints out "test 3" followed by
116 /// a second line with the file name and line number.
117 ///
118 #define ADDR_WARN(cond, a) \
119 { if (!(cond)) \
120 { ADDR_PRNT(a); \
121 ADDR_PRNT((" WARNING in file %s, line %d\n", __FILE__, __LINE__)); \
122 } }
123
124 /// @brief Macro for reporting fatal error conditions
125 /// @ingroup util
126 ///
127 /// This macro optionally stops execution of the current routine
128 /// after printing an error warning message to stdout,
129 /// followed by the file name and line number where the macro was called.
130 /// The first parameter is a condition -- if it is true, nothing is done.
131 /// The second pararmeter MUST be a parenthesis-enclosed list of arguments,
132 /// starting with a string. This is passed to printf() or an equivalent
133 /// in order to format the informational message. For example,
134 /// ADDR_EXIT(0, ("test %d",3) ); prints out "test 3" followed by
135 /// a second line with the file name and line number, then stops execution.
136 ///
137 #define ADDR_EXIT(cond, a) \
138 { if (!(cond)) \
139 { ADDR_PRNT(a); ADDR_DBG_BREAK();\
140 } }
141
142 #else // DEBUG
143
144 #define ADDRDPF 1 ? (void)0 : (void)
145
146 #define ADDR_PRNT(a)
147
148 #define ADDR_DBG_BREAK()
149
150 #define ADDR_INFO(cond, a)
151
152 #define ADDR_WARN(cond, a)
153
154 #define ADDR_EXIT(cond, a)
155
156 #endif // DEBUG
157 ////////////////////////////////////////////////////////////////////////////////////////////////////
158
159 #if defined(static_assert)
160 #define ADDR_C_ASSERT(__e) static_assert(__e, "")
161 #else
162 #define ADDR_C_ASSERT(__e) typedef char __ADDR_C_ASSERT__[(__e) ? 1 : -1]
163 #endif
164
165 namespace Addr
166 {
167
168 namespace V1
169 {
170 ////////////////////////////////////////////////////////////////////////////////////////////////////
171 // Common constants
172 ////////////////////////////////////////////////////////////////////////////////////////////////////
173 static const UINT_32 MicroTileWidth = 8; ///< Micro tile width, for 1D and 2D tiling
174 static const UINT_32 MicroTileHeight = 8; ///< Micro tile height, for 1D and 2D tiling
175 static const UINT_32 ThickTileThickness = 4; ///< Micro tile thickness, for THICK modes
176 static const UINT_32 XThickTileThickness = 8; ///< Extra thick tiling thickness
177 static const UINT_32 PowerSaveTileBytes = 64; ///< Nuber of bytes per tile for power save 64
178 static const UINT_32 CmaskCacheBits = 1024; ///< Number of bits for CMASK cache
179 static const UINT_32 CmaskElemBits = 4; ///< Number of bits for CMASK element
180 static const UINT_32 HtileCacheBits = 16384; ///< Number of bits for HTILE cache 512*32
181
182 static const UINT_32 MicroTilePixels = MicroTileWidth * MicroTileHeight;
183
184 static const INT_32 TileIndexInvalid = TILEINDEX_INVALID;
185 static const INT_32 TileIndexLinearGeneral = TILEINDEX_LINEAR_GENERAL;
186 static const INT_32 TileIndexNoMacroIndex = -3;
187
188 } // V1
189
190 namespace V2
191 {
192 ////////////////////////////////////////////////////////////////////////////////////////////////////
193 // Common constants
194 ////////////////////////////////////////////////////////////////////////////////////////////////////
195 static const UINT_32 MaxSurfaceHeight = 16384;
196
197 } // V2
198
199 ////////////////////////////////////////////////////////////////////////////////////////////////////
200 // Common macros
201 ////////////////////////////////////////////////////////////////////////////////////////////////////
202 #define BITS_PER_BYTE 8
203 #define BITS_TO_BYTES(x) ( ((x) + (BITS_PER_BYTE-1)) / BITS_PER_BYTE )
204 #define BYTES_TO_BITS(x) ( (x) * BITS_PER_BYTE )
205
206 /// Helper macros to select a single bit from an int (undefined later in section)
207 #define _BIT(v,b) (((v) >> (b) ) & 1)
208
209 /**
210 ****************************************************************************************************
211 * @brief Enums to identify AddrLib type
212 ****************************************************************************************************
213 */
214 enum LibClass
215 {
216 BASE_ADDRLIB = 0x0,
217 R600_ADDRLIB = 0x6,
218 R800_ADDRLIB = 0x8,
219 SI_ADDRLIB = 0xa,
220 CI_ADDRLIB = 0xb,
221 AI_ADDRLIB = 0xd,
222 };
223
224 /**
225 ****************************************************************************************************
226 * ChipFamily
227 *
228 * @brief
229 * Neutral enums that specifies chip family.
230 *
231 ****************************************************************************************************
232 */
233 enum ChipFamily
234 {
235 ADDR_CHIP_FAMILY_IVLD, ///< Invalid family
236 ADDR_CHIP_FAMILY_R6XX,
237 ADDR_CHIP_FAMILY_R7XX,
238 ADDR_CHIP_FAMILY_R8XX,
239 ADDR_CHIP_FAMILY_NI,
240 ADDR_CHIP_FAMILY_SI,
241 ADDR_CHIP_FAMILY_CI,
242 ADDR_CHIP_FAMILY_VI,
243 ADDR_CHIP_FAMILY_AI,
244 };
245
246 /**
247 ****************************************************************************************************
248 * ConfigFlags
249 *
250 * @brief
251 * This structure is used to set configuration flags.
252 ****************************************************************************************************
253 */
254 union ConfigFlags
255 {
256 struct
257 {
258 /// These flags are set up internally thru AddrLib::Create() based on ADDR_CREATE_FLAGS
259 UINT_32 optimalBankSwap : 1; ///< New bank tiling for RV770 only
260 UINT_32 noCubeMipSlicesPad : 1; ///< Disables faces padding for cubemap mipmaps
261 UINT_32 fillSizeFields : 1; ///< If clients fill size fields in all input and
262 /// output structure
263 UINT_32 ignoreTileInfo : 1; ///< Don't use tile info structure
264 UINT_32 useTileIndex : 1; ///< Make tileIndex field in input valid
265 UINT_32 useCombinedSwizzle : 1; ///< Use combined swizzle
266 UINT_32 checkLast2DLevel : 1; ///< Check the last 2D mip sub level
267 UINT_32 useHtileSliceAlign : 1; ///< Do htile single slice alignment
268 UINT_32 allowLargeThickTile : 1; ///< Allow 64*thickness*bytesPerPixel > rowSize
269 UINT_32 disableLinearOpt : 1; ///< Disallow tile modes to be optimized to linear
270 UINT_32 use32bppFor422Fmt : 1; ///< View 422 formats as 32 bits per pixel element
271 UINT_32 forceDccAndTcCompat : 1; ///< Force enable DCC and TC compatibility
272 UINT_32 reserved : 20; ///< Reserved bits for future use
273 };
274
275 UINT_32 value;
276 };
277
278 ////////////////////////////////////////////////////////////////////////////////////////////////////
279 // Misc helper functions
280 ////////////////////////////////////////////////////////////////////////////////////////////////////
281
282 /**
283 ****************************************************************************************************
284 * AddrXorReduce
285 *
286 * @brief
287 * Xor the right-side numberOfBits bits of x.
288 ****************************************************************************************************
289 */
290 static inline UINT_32 XorReduce(
291 UINT_32 x,
292 UINT_32 numberOfBits)
293 {
294 UINT_32 i;
295 UINT_32 result = x & 1;
296
297 for (i=1; i<numberOfBits; i++)
298 {
299 result ^= ((x>>i) & 1);
300 }
301
302 return result;
303 }
304
305 /**
306 ****************************************************************************************************
307 * IsPow2
308 *
309 * @brief
310 * Check if the size (UINT_32) is pow 2
311 ****************************************************************************************************
312 */
313 static inline UINT_32 IsPow2(
314 UINT_32 dim) ///< [in] dimension of miplevel
315 {
316 ADDR_ASSERT(dim > 0);
317 return !(dim & (dim - 1));
318 }
319
320 /**
321 ****************************************************************************************************
322 * IsPow2
323 *
324 * @brief
325 * Check if the size (UINT_64) is pow 2
326 ****************************************************************************************************
327 */
328 static inline UINT_64 IsPow2(
329 UINT_64 dim) ///< [in] dimension of miplevel
330 {
331 ADDR_ASSERT(dim > 0);
332 return !(dim & (dim - 1));
333 }
334
335 /**
336 ****************************************************************************************************
337 * ByteAlign
338 *
339 * @brief
340 * Align UINT_32 "x" to "align" alignment, "align" should be power of 2
341 ****************************************************************************************************
342 */
343 static inline UINT_32 PowTwoAlign(
344 UINT_32 x,
345 UINT_32 align)
346 {
347 //
348 // Assert that x is a power of two.
349 //
350 ADDR_ASSERT(IsPow2(align));
351 return (x + (align - 1)) & (~(align - 1));
352 }
353
354 /**
355 ****************************************************************************************************
356 * ByteAlign
357 *
358 * @brief
359 * Align UINT_64 "x" to "align" alignment, "align" should be power of 2
360 ****************************************************************************************************
361 */
362 static inline UINT_64 PowTwoAlign(
363 UINT_64 x,
364 UINT_64 align)
365 {
366 //
367 // Assert that x is a power of two.
368 //
369 ADDR_ASSERT(IsPow2(align));
370 return (x + (align - 1)) & (~(align - 1));
371 }
372
373 /**
374 ****************************************************************************************************
375 * Min
376 *
377 * @brief
378 * Get the min value between two unsigned values
379 ****************************************************************************************************
380 */
381 static inline UINT_32 Min(
382 UINT_32 value1,
383 UINT_32 value2)
384 {
385 return ((value1 < (value2)) ? (value1) : value2);
386 }
387
388 /**
389 ****************************************************************************************************
390 * Min
391 *
392 * @brief
393 * Get the min value between two signed values
394 ****************************************************************************************************
395 */
396 static inline INT_32 Min(
397 INT_32 value1,
398 INT_32 value2)
399 {
400 return ((value1 < (value2)) ? (value1) : value2);
401 }
402
403 /**
404 ****************************************************************************************************
405 * Max
406 *
407 * @brief
408 * Get the max value between two unsigned values
409 ****************************************************************************************************
410 */
411 static inline UINT_32 Max(
412 UINT_32 value1,
413 UINT_32 value2)
414 {
415 return ((value1 > (value2)) ? (value1) : value2);
416 }
417
418 /**
419 ****************************************************************************************************
420 * Max
421 *
422 * @brief
423 * Get the max value between two signed values
424 ****************************************************************************************************
425 */
426 static inline INT_32 Max(
427 INT_32 value1,
428 INT_32 value2)
429 {
430 return ((value1 > (value2)) ? (value1) : value2);
431 }
432
433 /**
434 ****************************************************************************************************
435 * NextPow2
436 *
437 * @brief
438 * Compute the mipmap's next level dim size
439 ****************************************************************************************************
440 */
441 static inline UINT_32 NextPow2(
442 UINT_32 dim) ///< [in] dimension of miplevel
443 {
444 UINT_32 newDim = 1;
445
446 if (dim > 0x7fffffff)
447 {
448 ADDR_ASSERT_ALWAYS();
449 newDim = 0x80000000;
450 }
451 else
452 {
453 while (newDim < dim)
454 {
455 newDim <<= 1;
456 }
457 }
458
459 return newDim;
460 }
461
462 /**
463 ****************************************************************************************************
464 * Log2NonPow2
465 *
466 * @brief
467 * Compute log of base 2 no matter the target is power of 2 or not
468 ****************************************************************************************************
469 */
470 static inline UINT_32 Log2NonPow2(
471 UINT_32 x) ///< [in] the value should calculate log based 2
472 {
473 UINT_32 y;
474
475 y = 0;
476 while (x > 1)
477 {
478 x >>= 1;
479 y++;
480 }
481
482 return y;
483 }
484
485 /**
486 ****************************************************************************************************
487 * Log2
488 *
489 * @brief
490 * Compute log of base 2
491 ****************************************************************************************************
492 */
493 static inline UINT_32 Log2(
494 UINT_32 x) ///< [in] the value should calculate log based 2
495 {
496 // Assert that x is a power of two.
497 ADDR_ASSERT(IsPow2(x));
498
499 return Log2NonPow2(x);
500 }
501
502 /**
503 ****************************************************************************************************
504 * QLog2
505 *
506 * @brief
507 * Compute log of base 2 quickly (<= 16)
508 ****************************************************************************************************
509 */
510 static inline UINT_32 QLog2(
511 UINT_32 x) ///< [in] the value should calculate log based 2
512 {
513 ADDR_ASSERT(x <= 16);
514
515 UINT_32 y = 0;
516
517 switch (x)
518 {
519 case 1:
520 y = 0;
521 break;
522 case 2:
523 y = 1;
524 break;
525 case 4:
526 y = 2;
527 break;
528 case 8:
529 y = 3;
530 break;
531 case 16:
532 y = 4;
533 break;
534 default:
535 ADDR_ASSERT_ALWAYS();
536 }
537
538 return y;
539 }
540
541 /**
542 ****************************************************************************************************
543 * SafeAssign
544 *
545 * @brief
546 * NULL pointer safe assignment
547 ****************************************************************************************************
548 */
549 static inline VOID SafeAssign(
550 UINT_32* pLVal, ///< [in] Pointer to left val
551 UINT_32 rVal) ///< [in] Right value
552 {
553 if (pLVal)
554 {
555 *pLVal = rVal;
556 }
557 }
558
559 /**
560 ****************************************************************************************************
561 * SafeAssign
562 *
563 * @brief
564 * NULL pointer safe assignment for 64bit values
565 ****************************************************************************************************
566 */
567 static inline VOID SafeAssign(
568 UINT_64* pLVal, ///< [in] Pointer to left val
569 UINT_64 rVal) ///< [in] Right value
570 {
571 if (pLVal)
572 {
573 *pLVal = rVal;
574 }
575 }
576
577 /**
578 ****************************************************************************************************
579 * SafeAssign
580 *
581 * @brief
582 * NULL pointer safe assignment for AddrTileMode
583 ****************************************************************************************************
584 */
585 static inline VOID SafeAssign(
586 AddrTileMode* pLVal, ///< [in] Pointer to left val
587 AddrTileMode rVal) ///< [in] Right value
588 {
589 if (pLVal)
590 {
591 *pLVal = rVal;
592 }
593 }
594
595 /**
596 ****************************************************************************************************
597 * RoundHalf
598 *
599 * @brief
600 * return (x + 1) / 2
601 ****************************************************************************************************
602 */
603 static inline UINT_32 RoundHalf(
604 UINT_32 x) ///< [in] input value
605 {
606 ADDR_ASSERT(x != 0);
607
608 #if 1
609 return (x >> 1) + (x & 1);
610 #else
611 return (x + 1) >> 1;
612 #endif
613 }
614
615 /**
616 ****************************************************************************************************
617 * SumGeo
618 *
619 * @brief
620 * Calculate sum of a geometric progression whose ratio is 1/2
621 ****************************************************************************************************
622 */
623 static inline UINT_32 SumGeo(
624 UINT_32 base, ///< [in] First term in the geometric progression
625 UINT_32 num) ///< [in] Number of terms to be added into sum
626 {
627 ADDR_ASSERT(base > 0);
628
629 UINT_32 sum = 0;
630 UINT_32 i = 0;
631 for (; (i < num) && (base > 1); i++)
632 {
633 sum += base;
634 base = RoundHalf(base);
635 }
636 sum += num - i;
637
638 return sum;
639 }
640
641 /**
642 ****************************************************************************************************
643 * GetBit
644 *
645 * @brief
646 * Extract bit N value (0 or 1) of a UINT32 value.
647 ****************************************************************************************************
648 */
649 static inline UINT_32 GetBit(
650 UINT_32 u32, ///< [in] UINT32 value
651 UINT_32 pos) ///< [in] bit position from LSB, valid range is [0..31]
652 {
653 ADDR_ASSERT(pos <= 31);
654
655 return (u32 >> pos) & 0x1;
656 }
657
658 /**
659 ****************************************************************************************************
660 * GetBits
661 *
662 * @brief
663 * Copy 'bitsNum' bits from src start from srcStartPos into destination from dstStartPos
664 * srcStartPos: 0~31 for UINT_32
665 * bitsNum : 1~32 for UINT_32
666 * srcStartPos: 0~31 for UINT_32
667 * src start position
668 * |
669 * src : b[31] b[30] b[29] ... ... ... ... ... ... ... ... b[end]..b[beg] ... b[1] b[0]
670 * || Bits num || copy length || Bits num ||
671 * dst : b[31] b[30] b[29] ... b[end]..b[beg] ... ... ... ... ... ... ... ... b[1] b[0]
672 * |
673 * dst start position
674 ****************************************************************************************************
675 */
676 static inline UINT_32 GetBits(
677 UINT_32 src,
678 UINT_32 srcStartPos,
679 UINT_32 bitsNum,
680 UINT_32 dstStartPos)
681 {
682 ADDR_ASSERT((srcStartPos < 32) && (dstStartPos < 32) && (bitsNum > 0));
683 ADDR_ASSERT((bitsNum + dstStartPos <= 32) && (bitsNum + srcStartPos <= 32));
684
685 return ((src >> srcStartPos) << (32 - bitsNum)) >> (32 - bitsNum - dstStartPos);
686 }
687
688 /**
689 ****************************************************************************************************
690 * MortonGen2d
691 *
692 * @brief
693 * Generate 2D Morton interleave code with num lowest bits in each channel
694 ****************************************************************************************************
695 */
696 static inline UINT_32 MortonGen2d(
697 UINT_32 x, ///< [in] First channel
698 UINT_32 y, ///< [in] Second channel
699 UINT_32 num) ///< [in] Number of bits extracted from each channel
700 {
701 UINT_32 mort = 0;
702
703 for (UINT_32 i = 0; i < num; i++)
704 {
705 mort |= (GetBit(y, i) << (2 * i));
706 mort |= (GetBit(x, i) << (2 * i + 1));
707 }
708
709 return mort;
710 }
711
712 /**
713 ****************************************************************************************************
714 * MortonGen3d
715 *
716 * @brief
717 * Generate 3D Morton interleave code with num lowest bits in each channel
718 ****************************************************************************************************
719 */
720 static inline UINT_32 MortonGen3d(
721 UINT_32 x, ///< [in] First channel
722 UINT_32 y, ///< [in] Second channel
723 UINT_32 z, ///< [in] Third channel
724 UINT_32 num) ///< [in] Number of bits extracted from each channel
725 {
726 UINT_32 mort = 0;
727
728 for (UINT_32 i = 0; i < num; i++)
729 {
730 mort |= (GetBit(z, i) << (3 * i));
731 mort |= (GetBit(y, i) << (3 * i + 1));
732 mort |= (GetBit(x, i) << (3 * i + 2));
733 }
734
735 return mort;
736 }
737
738 /**
739 ****************************************************************************************************
740 * ReverseBitVector
741 *
742 * @brief
743 * Return reversed lowest num bits of v: v[0]v[1]...v[num-2]v[num-1]
744 ****************************************************************************************************
745 */
746 static inline UINT_32 ReverseBitVector(
747 UINT_32 v, ///< [in] Reverse operation base value
748 UINT_32 num) ///< [in] Number of bits used in reverse operation
749 {
750 UINT_32 reverse = 0;
751
752 for (UINT_32 i = 0; i < num; i++)
753 {
754 reverse |= (GetBit(v, num - 1 - i) << i);
755 }
756
757 return reverse;
758 }
759
760 /**
761 ****************************************************************************************************
762 * FoldXor2d
763 *
764 * @brief
765 * Xor bit vector v[num-1]v[num-2]...v[1]v[0] with v[num]v[num+1]...v[2*num-2]v[2*num-1]
766 ****************************************************************************************************
767 */
768 static inline UINT_32 FoldXor2d(
769 UINT_32 v, ///< [in] Xor operation base value
770 UINT_32 num) ///< [in] Number of bits used in fold xor operation
771 {
772 return (v & ((1 << num) - 1)) ^ ReverseBitVector(v >> num, num);
773 }
774
775 /**
776 ****************************************************************************************************
777 * DeMort
778 *
779 * @brief
780 * Return v[0] | v[2] | v[4] | v[6]... | v[2*num - 2]
781 ****************************************************************************************************
782 */
783 static inline UINT_32 DeMort(
784 UINT_32 v, ///< [in] DeMort operation base value
785 UINT_32 num) ///< [in] Number of bits used in fold DeMort operation
786 {
787 UINT_32 d = 0;
788
789 for (UINT_32 i = 0; i < num; i++)
790 {
791 d |= ((v & (1 << (i << 1))) >> i);
792 }
793
794 return d;
795 }
796
797 /**
798 ****************************************************************************************************
799 * FoldXor3d
800 *
801 * @brief
802 * v[0]...v[num-1] ^ v[3*num-1]v[3*num-3]...v[num+2]v[num] ^ v[3*num-2]...v[num+1]v[num-1]
803 ****************************************************************************************************
804 */
805 static inline UINT_32 FoldXor3d(
806 UINT_32 v, ///< [in] Xor operation base value
807 UINT_32 num) ///< [in] Number of bits used in fold xor operation
808 {
809 UINT_32 t = v & ((1 << num) - 1);
810 t ^= ReverseBitVector(DeMort(v >> num, num), num);
811 t ^= ReverseBitVector(DeMort(v >> (num + 1), num), num);
812
813 return t;
814 }
815
816 /**
817 ****************************************************************************************************
818 * InitChannel
819 *
820 * @brief
821 * Set channel initialization value via a return value
822 ****************************************************************************************************
823 */
824 static inline ADDR_CHANNEL_SETTING InitChannel(
825 UINT_32 valid, ///< [in] valid setting
826 UINT_32 channel, ///< [in] channel setting
827 UINT_32 index) ///< [in] index setting
828 {
829 ADDR_CHANNEL_SETTING t;
830 t.valid = valid;
831 t.channel = channel;
832 t.index = index;
833
834 return t;
835 }
836
837 /**
838 ****************************************************************************************************
839 * InitChannel
840 *
841 * @brief
842 * Set channel initialization value via channel pointer
843 ****************************************************************************************************
844 */
845 static inline VOID InitChannel(
846 UINT_32 valid, ///< [in] valid setting
847 UINT_32 channel, ///< [in] channel setting
848 UINT_32 index, ///< [in] index setting
849 ADDR_CHANNEL_SETTING *pChanSet) ///< [out] channel setting to be initialized
850 {
851 pChanSet->valid = valid;
852 pChanSet->channel = channel;
853 pChanSet->index = index;
854 }
855
856 /**
857 ****************************************************************************************************
858 * InitChannel
859 *
860 * @brief
861 * Set channel initialization value via another channel
862 ****************************************************************************************************
863 */
864 static inline VOID InitChannel(
865 ADDR_CHANNEL_SETTING *pChanDst, ///< [in] channel setting to be copied from
866 ADDR_CHANNEL_SETTING *pChanSrc) ///< [out] channel setting to be initialized
867 {
868 pChanDst->valid = pChanSrc->valid;
869 pChanDst->channel = pChanSrc->channel;
870 pChanDst->index = pChanSrc->index;
871 }
872
873 /**
874 ****************************************************************************************************
875 * GetMaxValidChannelIndex
876 *
877 * @brief
878 * Get max valid index for a specific channel
879 ****************************************************************************************************
880 */
881 static inline UINT_32 GetMaxValidChannelIndex(
882 const ADDR_CHANNEL_SETTING *pChanSet, ///< [in] channel setting to be initialized
883 UINT_32 searchCount,///< [in] number of channel setting to be searched
884 UINT_32 channel) ///< [in] channel to be searched
885 {
886 UINT_32 index = 0;
887
888 for (UINT_32 i = 0; i < searchCount; i++)
889 {
890 if (pChanSet[i].valid && (pChanSet[i].channel == channel))
891 {
892 index = Max(index, static_cast<UINT_32>(pChanSet[i].index));
893 }
894 }
895
896 return index;
897 }
898
899 /**
900 ****************************************************************************************************
901 * GetCoordActiveMask
902 *
903 * @brief
904 * Get bit mask which indicates which positions in the equation match the target coord
905 ****************************************************************************************************
906 */
907 static inline UINT_32 GetCoordActiveMask(
908 const ADDR_CHANNEL_SETTING *pChanSet, ///< [in] channel setting to be initialized
909 UINT_32 searchCount,///< [in] number of channel setting to be searched
910 UINT_32 channel, ///< [in] channel to be searched
911 UINT_32 index) ///< [in] index to be searched
912 {
913 UINT_32 mask = 0;
914
915 for (UINT_32 i = 0; i < searchCount; i++)
916 {
917 if ((pChanSet[i].valid == TRUE) &&
918 (pChanSet[i].channel == channel) &&
919 (pChanSet[i].index == index))
920 {
921 mask |= (1 << i);
922 }
923 }
924
925 return mask;
926 }
927
928 } // Addr
929
930 #endif // __ADDR_COMMON_H__
931