2 * Copyright © 2014 Advanced Micro Devices, Inc.
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:
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.
22 * The above copyright notice and this permission notice (including the
23 * next paragraph) shall be included in all copies or substantial portions
28 ****************************************************************************************************
30 * @brief Contains the helper function and constants.
31 ****************************************************************************************************
34 #ifndef __ADDR_COMMON_H__
35 #define __ADDR_COMMON_H__
37 #include "addrinterface.h"
43 #if BRAHMA_BUILD && !defined(DEBUG)
51 ////////////////////////////////////////////////////////////////////////////////////////////////////
52 // Platform specific debug break defines
53 ////////////////////////////////////////////////////////////////////////////////////////////////////
56 #define ADDR_DBG_BREAK() assert(false)
57 #elif defined(__APPLE__)
58 #define ADDR_DBG_BREAK() { IOPanic("");}
60 #define ADDR_DBG_BREAK() { __debugbreak(); }
63 #define ADDR_DBG_BREAK()
65 ////////////////////////////////////////////////////////////////////////////////////////////////////
67 ////////////////////////////////////////////////////////////////////////////////////////////////////
68 // Debug assertions used in AddrLib
69 ////////////////////////////////////////////////////////////////////////////////////////////////////
70 #if defined(_WIN32) && (_MSC_VER >= 1400)
71 #define ADDR_ANALYSIS_ASSUME(expr) __analysis_assume(expr)
73 #define ADDR_ANALYSIS_ASSUME(expr) do { (void)(expr); } while (0)
77 #define ADDR_ASSERT(__e) assert(__e)
79 #define ADDR_ASSERT(__e) \
81 ADDR_ANALYSIS_ASSUME(__e); \
82 if ( !((__e) ? TRUE : FALSE)) { ADDR_DBG_BREAK(); } \
85 #define ADDR_ASSERT(__e) ADDR_ANALYSIS_ASSUME(__e)
88 #define ADDR_ASSERT_ALWAYS() ADDR_DBG_BREAK()
89 #define ADDR_UNHANDLED_CASE() ADDR_ASSERT(!"Unhandled case")
90 #define ADDR_NOT_IMPLEMENTED() ADDR_ASSERT(!"Not implemented");
91 ////////////////////////////////////////////////////////////////////////////////////////////////////
93 ////////////////////////////////////////////////////////////////////////////////////////////////////
94 // Debug print macro from legacy address library
95 ////////////////////////////////////////////////////////////////////////////////////////////////////
98 #define ADDR_PRNT(a) Object::DebugPrint a
100 /// @brief Macro for reporting informational messages
103 /// This macro optionally prints an informational message to stdout.
104 /// The first parameter is a condition -- if it is true, nothing is done.
105 /// The second pararmeter MUST be a parenthesis-enclosed list of arguments,
106 /// starting with a string. This is passed to printf() or an equivalent
107 /// in order to format the informational message. For example,
108 /// ADDR_INFO(0, ("test %d",3) ); prints out "test 3".
110 #define ADDR_INFO(cond, a) \
111 { if (!(cond)) { ADDR_PRNT(a); } }
114 /// @brief Macro for reporting error warning messages
117 /// This macro optionally prints an error warning message to stdout,
118 /// followed by the file name and line number where the macro was called.
119 /// The first parameter is a condition -- if it is true, nothing is done.
120 /// The second pararmeter MUST be a parenthesis-enclosed list of arguments,
121 /// starting with a string. This is passed to printf() or an equivalent
122 /// in order to format the informational message. For example,
123 /// ADDR_WARN(0, ("test %d",3) ); prints out "test 3" followed by
124 /// a second line with the file name and line number.
126 #define ADDR_WARN(cond, a) \
129 ADDR_PRNT((" WARNING in file %s, line %d\n", __FILE__, __LINE__)); \
133 /// @brief Macro for reporting fatal error conditions
136 /// This macro optionally stops execution of the current routine
137 /// after printing an error warning message to stdout,
138 /// followed by the file name and line number where the macro was called.
139 /// The first parameter is a condition -- if it is true, nothing is done.
140 /// The second pararmeter MUST be a parenthesis-enclosed list of arguments,
141 /// starting with a string. This is passed to printf() or an equivalent
142 /// in order to format the informational message. For example,
143 /// ADDR_EXIT(0, ("test %d",3) ); prints out "test 3" followed by
144 /// a second line with the file name and line number, then stops execution.
146 #define ADDR_EXIT(cond, a) \
148 { ADDR_PRNT(a); ADDR_DBG_BREAK();\
153 #define ADDRDPF 1 ? (void)0 : (void)
157 #define ADDR_DBG_BREAK()
159 #define ADDR_INFO(cond, a)
161 #define ADDR_WARN(cond, a)
163 #define ADDR_EXIT(cond, a)
166 ////////////////////////////////////////////////////////////////////////////////////////////////////
168 #define ADDR_C_ASSERT(__e) typedef char __ADDR_C_ASSERT__[(__e) ? 1 : -1]
175 ////////////////////////////////////////////////////////////////////////////////////////////////////
177 ////////////////////////////////////////////////////////////////////////////////////////////////////
178 static const UINT_32 MicroTileWidth
= 8; ///< Micro tile width, for 1D and 2D tiling
179 static const UINT_32 MicroTileHeight
= 8; ///< Micro tile height, for 1D and 2D tiling
180 static const UINT_32 ThickTileThickness
= 4; ///< Micro tile thickness, for THICK modes
181 static const UINT_32 XThickTileThickness
= 8; ///< Extra thick tiling thickness
182 static const UINT_32 PowerSaveTileBytes
= 64; ///< Nuber of bytes per tile for power save 64
183 static const UINT_32 CmaskCacheBits
= 1024; ///< Number of bits for CMASK cache
184 static const UINT_32 CmaskElemBits
= 4; ///< Number of bits for CMASK element
185 static const UINT_32 HtileCacheBits
= 16384; ///< Number of bits for HTILE cache 512*32
187 static const UINT_32 MicroTilePixels
= MicroTileWidth
* MicroTileHeight
;
189 static const INT_32 TileIndexInvalid
= TILEINDEX_INVALID
;
190 static const INT_32 TileIndexLinearGeneral
= TILEINDEX_LINEAR_GENERAL
;
191 static const INT_32 TileIndexNoMacroIndex
= -3;
197 ////////////////////////////////////////////////////////////////////////////////////////////////////
199 ////////////////////////////////////////////////////////////////////////////////////////////////////
200 static const UINT_32 MaxSurfaceHeight
= 16384;
204 ////////////////////////////////////////////////////////////////////////////////////////////////////
206 ////////////////////////////////////////////////////////////////////////////////////////////////////
207 #define BITS_PER_BYTE 8
208 #define BITS_TO_BYTES(x) ( ((x) + (BITS_PER_BYTE-1)) / BITS_PER_BYTE )
209 #define BYTES_TO_BITS(x) ( (x) * BITS_PER_BYTE )
211 /// Helper macros to select a single bit from an int (undefined later in section)
212 #define _BIT(v,b) (((v) >> (b) ) & 1)
215 ****************************************************************************************************
216 * @brief Enums to identify AddrLib type
217 ****************************************************************************************************
230 ****************************************************************************************************
234 * Neutral enums that specifies chip family.
236 ****************************************************************************************************
240 ADDR_CHIP_FAMILY_IVLD
, ///< Invalid family
241 ADDR_CHIP_FAMILY_R6XX
,
242 ADDR_CHIP_FAMILY_R7XX
,
243 ADDR_CHIP_FAMILY_R8XX
,
252 ****************************************************************************************************
256 * This structure is used to set configuration flags.
257 ****************************************************************************************************
263 /// These flags are set up internally thru AddrLib::Create() based on ADDR_CREATE_FLAGS
264 UINT_32 optimalBankSwap
: 1; ///< New bank tiling for RV770 only
265 UINT_32 noCubeMipSlicesPad
: 1; ///< Disables faces padding for cubemap mipmaps
266 UINT_32 fillSizeFields
: 1; ///< If clients fill size fields in all input and
268 UINT_32 ignoreTileInfo
: 1; ///< Don't use tile info structure
269 UINT_32 useTileIndex
: 1; ///< Make tileIndex field in input valid
270 UINT_32 useCombinedSwizzle
: 1; ///< Use combined swizzle
271 UINT_32 checkLast2DLevel
: 1; ///< Check the last 2D mip sub level
272 UINT_32 useHtileSliceAlign
: 1; ///< Do htile single slice alignment
273 UINT_32 allowLargeThickTile
: 1; ///< Allow 64*thickness*bytesPerPixel > rowSize
274 UINT_32 disableLinearOpt
: 1; ///< Disallow tile modes to be optimized to linear
275 UINT_32 reserved
: 22; ///< Reserved bits for future use
281 ////////////////////////////////////////////////////////////////////////////////////////////////////
282 // Misc helper functions
283 ////////////////////////////////////////////////////////////////////////////////////////////////////
286 ****************************************************************************************************
290 * Xor the right-side numberOfBits bits of x.
291 ****************************************************************************************************
293 static inline UINT_32
XorReduce(
295 UINT_32 numberOfBits
)
298 UINT_32 result
= x
& 1;
300 for (i
=1; i
<numberOfBits
; i
++)
302 result
^= ((x
>>i
) & 1);
309 ****************************************************************************************************
313 * Check if the size (UINT_32) is pow 2
314 ****************************************************************************************************
316 static inline UINT_32
IsPow2(
317 UINT_32 dim
) ///< [in] dimension of miplevel
319 ADDR_ASSERT(dim
> 0);
320 return !(dim
& (dim
- 1));
324 ****************************************************************************************************
328 * Check if the size (UINT_64) is pow 2
329 ****************************************************************************************************
331 static inline UINT_64
IsPow2(
332 UINT_64 dim
) ///< [in] dimension of miplevel
334 ADDR_ASSERT(dim
> 0);
335 return !(dim
& (dim
- 1));
339 ****************************************************************************************************
343 * Align UINT_32 "x" to "align" alignment, "align" should be power of 2
344 ****************************************************************************************************
346 static inline UINT_32
PowTwoAlign(
351 // Assert that x is a power of two.
353 ADDR_ASSERT(IsPow2(align
));
354 return (x
+ (align
- 1)) & (~(align
- 1));
358 ****************************************************************************************************
362 * Align UINT_64 "x" to "align" alignment, "align" should be power of 2
363 ****************************************************************************************************
365 static inline UINT_64
PowTwoAlign(
370 // Assert that x is a power of two.
372 ADDR_ASSERT(IsPow2(align
));
373 return (x
+ (align
- 1)) & (~(align
- 1));
377 ****************************************************************************************************
381 * Get the min value between two unsigned values
382 ****************************************************************************************************
384 static inline UINT_32
Min(
388 return ((value1
< (value2
)) ? (value1
) : value2
);
392 ****************************************************************************************************
396 * Get the min value between two signed values
397 ****************************************************************************************************
399 static inline INT_32
Min(
403 return ((value1
< (value2
)) ? (value1
) : value2
);
407 ****************************************************************************************************
411 * Get the max value between two unsigned values
412 ****************************************************************************************************
414 static inline UINT_32
Max(
418 return ((value1
> (value2
)) ? (value1
) : value2
);
422 ****************************************************************************************************
426 * Get the max value between two signed values
427 ****************************************************************************************************
429 static inline INT_32
Max(
433 return ((value1
> (value2
)) ? (value1
) : value2
);
437 ****************************************************************************************************
441 * Compute the mipmap's next level dim size
442 ****************************************************************************************************
444 static inline UINT_32
NextPow2(
445 UINT_32 dim
) ///< [in] dimension of miplevel
449 if (dim
> 0x7fffffff)
451 ADDR_ASSERT_ALWAYS();
466 ****************************************************************************************************
470 * Compute log of base 2 no matter the target is power of 2 or not
471 ****************************************************************************************************
473 static inline UINT_32
Log2NonPow2(
474 UINT_32 x
) ///< [in] the value should calculate log based 2
489 ****************************************************************************************************
493 * Compute log of base 2
494 ****************************************************************************************************
496 static inline UINT_32
Log2(
497 UINT_32 x
) ///< [in] the value should calculate log based 2
499 // Assert that x is a power of two.
500 ADDR_ASSERT(IsPow2(x
));
502 return Log2NonPow2(x
);
506 ****************************************************************************************************
510 * Compute log of base 2 quickly (<= 16)
511 ****************************************************************************************************
513 static inline UINT_32
QLog2(
514 UINT_32 x
) ///< [in] the value should calculate log based 2
516 ADDR_ASSERT(x
<= 16);
538 ADDR_ASSERT_ALWAYS();
545 ****************************************************************************************************
549 * NULL pointer safe assignment
550 ****************************************************************************************************
552 static inline VOID
SafeAssign(
553 UINT_32
* pLVal
, ///< [in] Pointer to left val
554 UINT_32 rVal
) ///< [in] Right value
563 ****************************************************************************************************
567 * NULL pointer safe assignment for 64bit values
568 ****************************************************************************************************
570 static inline VOID
SafeAssign(
571 UINT_64
* pLVal
, ///< [in] Pointer to left val
572 UINT_64 rVal
) ///< [in] Right value
581 ****************************************************************************************************
585 * NULL pointer safe assignment for AddrTileMode
586 ****************************************************************************************************
588 static inline VOID
SafeAssign(
589 AddrTileMode
* pLVal
, ///< [in] Pointer to left val
590 AddrTileMode rVal
) ///< [in] Right value
599 ****************************************************************************************************
604 ****************************************************************************************************
606 static inline UINT_32
RoundHalf(
607 UINT_32 x
) ///< [in] input value
612 return (x
>> 1) + (x
& 1);
619 ****************************************************************************************************
623 * Calculate sum of a geometric progression whose ratio is 1/2
624 ****************************************************************************************************
626 static inline UINT_32
SumGeo(
627 UINT_32 base
, ///< [in] First term in the geometric progression
628 UINT_32 num
) ///< [in] Number of terms to be added into sum
630 ADDR_ASSERT(base
> 0);
634 for (; (i
< num
) && (base
> 1); i
++)
637 base
= RoundHalf(base
);
645 ****************************************************************************************************
649 * Extract bit N value (0 or 1) of a UINT32 value.
650 ****************************************************************************************************
652 static inline UINT_32
GetBit(
653 UINT_32 u32
, ///< [in] UINT32 value
654 UINT_32 pos
) ///< [in] bit position from LSB, valid range is [0..31]
656 ADDR_ASSERT(pos
<= 31);
658 return (u32
>> pos
) & 0x1;
662 ****************************************************************************************************
666 * Copy 'bitsNum' bits from src start from srcStartPos into destination from dstStartPos
667 * srcStartPos: 0~31 for UINT_32
668 * bitsNum : 1~32 for UINT_32
669 * srcStartPos: 0~31 for UINT_32
672 * src : b[31] b[30] b[29] ... ... ... ... ... ... ... ... b[end]..b[beg] ... b[1] b[0]
673 * || Bits num || copy length || Bits num ||
674 * dst : b[31] b[30] b[29] ... b[end]..b[beg] ... ... ... ... ... ... ... ... b[1] b[0]
677 ****************************************************************************************************
679 static inline UINT_32
GetBits(
685 ADDR_ASSERT((srcStartPos
< 32) && (dstStartPos
< 32) && (bitsNum
> 0));
686 ADDR_ASSERT((bitsNum
+ dstStartPos
<= 32) && (bitsNum
+ srcStartPos
<= 32));
688 return ((src
>> srcStartPos
) << (32 - bitsNum
)) >> (32 - bitsNum
- dstStartPos
);
692 ****************************************************************************************************
696 * Generate 2D Morton interleave code with num lowest bits in each channel
697 ****************************************************************************************************
699 static inline UINT_32
MortonGen2d(
700 UINT_32 x
, ///< [in] First channel
701 UINT_32 y
, ///< [in] Second channel
702 UINT_32 num
) ///< [in] Number of bits extracted from each channel
706 for (UINT_32 i
= 0; i
< num
; i
++)
708 mort
|= (GetBit(y
, i
) << (2 * i
));
709 mort
|= (GetBit(x
, i
) << (2 * i
+ 1));
716 ****************************************************************************************************
720 * Generate 3D Morton interleave code with num lowest bits in each channel
721 ****************************************************************************************************
723 static inline UINT_32
MortonGen3d(
724 UINT_32 x
, ///< [in] First channel
725 UINT_32 y
, ///< [in] Second channel
726 UINT_32 z
, ///< [in] Third channel
727 UINT_32 num
) ///< [in] Number of bits extracted from each channel
731 for (UINT_32 i
= 0; i
< num
; i
++)
733 mort
|= (GetBit(z
, i
) << (3 * i
));
734 mort
|= (GetBit(y
, i
) << (3 * i
+ 1));
735 mort
|= (GetBit(x
, i
) << (3 * i
+ 2));
742 ****************************************************************************************************
746 * Return reversed lowest num bits of v: v[0]v[1]...v[num-2]v[num-1]
747 ****************************************************************************************************
749 static inline UINT_32
ReverseBitVector(
750 UINT_32 v
, ///< [in] Reverse operation base value
751 UINT_32 num
) ///< [in] Number of bits used in reverse operation
755 for (UINT_32 i
= 0; i
< num
; i
++)
757 reverse
|= (GetBit(v
, num
- 1 - i
) << i
);
764 ****************************************************************************************************
768 * 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]
769 ****************************************************************************************************
771 static inline UINT_32
FoldXor2d(
772 UINT_32 v
, ///< [in] Xor operation base value
773 UINT_32 num
) ///< [in] Number of bits used in fold xor operation
775 return (v
& ((1 << num
) - 1)) ^ ReverseBitVector(v
>> num
, num
);
779 ****************************************************************************************************
783 * Return v[0] | v[2] | v[4] | v[6]... | v[2*num - 2]
784 ****************************************************************************************************
786 static inline UINT_32
DeMort(
787 UINT_32 v
, ///< [in] DeMort operation base value
788 UINT_32 num
) ///< [in] Number of bits used in fold DeMort operation
792 for (UINT_32 i
= 0; i
< num
; i
++)
794 d
|= ((v
& (1 << (i
<< 1))) >> i
);
801 ****************************************************************************************************
805 * 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]
806 ****************************************************************************************************
808 static inline UINT_32
FoldXor3d(
809 UINT_32 v
, ///< [in] Xor operation base value
810 UINT_32 num
) ///< [in] Number of bits used in fold xor operation
812 UINT_32 t
= v
& ((1 << num
) - 1);
813 t
^= ReverseBitVector(DeMort(v
>> num
, num
), num
);
814 t
^= ReverseBitVector(DeMort(v
>> (num
+ 1), num
), num
);
820 ****************************************************************************************************
824 * Set channel initialization value via a return value
825 ****************************************************************************************************
827 static inline ADDR_CHANNEL_SETTING
InitChannel(
828 UINT_32 valid
, ///< [in] valid setting
829 UINT_32 channel
, ///< [in] channel setting
830 UINT_32 index
) ///< [in] index setting
832 ADDR_CHANNEL_SETTING t
;
841 ****************************************************************************************************
845 * Set channel initialization value via channel pointer
846 ****************************************************************************************************
848 static inline VOID
InitChannel(
849 UINT_32 valid
, ///< [in] valid setting
850 UINT_32 channel
, ///< [in] channel setting
851 UINT_32 index
, ///< [in] index setting
852 ADDR_CHANNEL_SETTING
*pChanSet
) ///< [out] channel setting to be initialized
854 pChanSet
->valid
= valid
;
855 pChanSet
->channel
= channel
;
856 pChanSet
->index
= index
;
861 ****************************************************************************************************
865 * Set channel initialization value via another channel
866 ****************************************************************************************************
868 static inline VOID
InitChannel(
869 ADDR_CHANNEL_SETTING
*pChanDst
, ///< [in] channel setting to be copied from
870 ADDR_CHANNEL_SETTING
*pChanSrc
) ///< [out] channel setting to be initialized
872 pChanDst
->valid
= pChanSrc
->valid
;
873 pChanDst
->channel
= pChanSrc
->channel
;
874 pChanDst
->index
= pChanSrc
->index
;
878 ****************************************************************************************************
879 * GetMaxValidChannelIndex
882 * Get max valid index for a specific channel
883 ****************************************************************************************************
885 static inline UINT_32
GetMaxValidChannelIndex(
886 const ADDR_CHANNEL_SETTING
*pChanSet
, ///< [in] channel setting to be initialized
887 UINT_32 searchCount
,///< [in] number of channel setting to be searched
888 UINT_32 channel
) ///< [in] channel to be searched
892 for (UINT_32 i
= 0; i
< searchCount
; i
++)
894 if (pChanSet
[i
].valid
&& (pChanSet
[i
].channel
== channel
))
896 index
= Max(index
, static_cast<UINT_32
>(pChanSet
[i
].index
));
904 ****************************************************************************************************
908 * Get bit mask which indicates which positions in the equation match the target coord
909 ****************************************************************************************************
911 static inline UINT_32
GetCoordActiveMask(
912 const ADDR_CHANNEL_SETTING
*pChanSet
, ///< [in] channel setting to be initialized
913 UINT_32 searchCount
,///< [in] number of channel setting to be searched
914 UINT_32 channel
, ///< [in] channel to be searched
915 UINT_32 index
) ///< [in] index to be searched
919 for (UINT_32 i
= 0; i
< searchCount
; i
++)
921 if ((pChanSet
[i
].valid
== TRUE
) &&
922 (pChanSet
[i
].channel
== channel
) &&
923 (pChanSet
[i
].index
== index
))
934 #endif // __ADDR_COMMON_H__