pot.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /*
  2. ** Command & Conquer Generals(tm)
  3. ** Copyright 2025 Electronic Arts Inc.
  4. **
  5. ** This program is free software: you can redistribute it and/or modify
  6. ** it under the terms of the GNU General Public License as published by
  7. ** the Free Software Foundation, either version 3 of the License, or
  8. ** (at your option) any later version.
  9. **
  10. ** This program is distributed in the hope that it will be useful,
  11. ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ** GNU General Public License for more details.
  14. **
  15. ** You should have received a copy of the GNU General Public License
  16. ** along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. /***************************************************************************
  19. *** C O N F I D E N T I A L --- W E S T W O O D S T U D I O S ***
  20. ***************************************************************************
  21. * *
  22. * Project Name : G *
  23. * *
  24. * $Archive:: /G/ww3d/POT.CPP $*
  25. * *
  26. * $Author:: Naty_h $*
  27. * *
  28. * $Modtime:: 12/23/98 7:58a $*
  29. * *
  30. * $Revision:: 3 $*
  31. * *
  32. *-------------------------------------------------------------------------*
  33. * Functions: *
  34. * Find_POT -- finds closest inclusive power of 2 to a value *
  35. * Find_POT_Log2 -- finds log2 of closest inclusive power of 2 to a value*
  36. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  37. #include "pot.h"
  38. /**************************************************************************
  39. * Find_POT -- finds closest inclusive power of 2 to a value *
  40. * *
  41. * INPUT: *
  42. * *
  43. * OUTPUT: *
  44. * *
  45. * WARNINGS: *
  46. * *
  47. * HISTORY: *
  48. * 10/20/1997 PWG : Created. *
  49. *========================================================================*/
  50. int Find_POT(int val)
  51. {
  52. // clear out the recorded position and the recorded count
  53. int recpos = 0;
  54. int reccnt = 0;
  55. // walk through the value shifting off bits and record the
  56. // position of the highest bit, and whether we have found
  57. // more than one bit.
  58. for (int lp = 0; val; lp++) {
  59. if (val & 1) {
  60. recpos = lp;
  61. reccnt++;
  62. }
  63. val >>= 1;
  64. }
  65. // if we have not found more than one bit then the number
  66. // was the power of two so return it.
  67. if (reccnt < 2) {
  68. return( 1 << recpos);
  69. }
  70. // if we found more than one bit, then the number needs to
  71. // be rounded up to the next highest power of 2.
  72. return( 1 << (recpos + 1));
  73. }
  74. /**************************************************************************
  75. * Find_POT_Log2 -- finds log2 of closest inclusive power of 2 to a value *
  76. * *
  77. * INPUT: *
  78. * *
  79. * OUTPUT: *
  80. * *
  81. * WARNINGS: *
  82. * *
  83. * HISTORY: *
  84. * 12/23/1998 NH : Created. *
  85. *========================================================================*/
  86. unsigned int Find_POT_Log2(unsigned int val)
  87. {
  88. // clear out the recorded position and the recorded count
  89. int recpos = 0;
  90. int reccnt = 0;
  91. // walk through the value shifting off bits and record the
  92. // position of the highest bit, and whether we have found
  93. // more than one bit.
  94. for (int lp = 0; val; lp++) {
  95. if (val & 1) {
  96. recpos = lp;
  97. reccnt++;
  98. }
  99. val >>= 1;
  100. }
  101. // if we have not found more than one bit then the number
  102. // was the power of two so return it.
  103. if (reccnt < 2) {
  104. return recpos;
  105. }
  106. // if we found more than one bit, then the number needs to
  107. // be rounded up to the next highest power of 2.
  108. return recpos + 1;
  109. }