Bits.c 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. /* vim: set expandtab ts=4 sw=4: */
  2. /*
  3. * You may redistribute this program and/or modify it under the terms of
  4. * the GNU General Public License as published by the Free Software Foundation,
  5. * either version 3 of the License, or (at your option) any later version.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. *
  12. * You should have received a copy of the GNU General Public License
  13. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. #include "util/Bits.h"
  16. int Bits_log2x64_stupid(uint64_t number)
  17. {
  18. int out = 0;
  19. while (number >>= 1) {
  20. out++;
  21. }
  22. return out;
  23. }
  24. void* Bits_memmem(const void* haystack, size_t haystackLen, const void* needle, size_t needleLen)
  25. {
  26. uint8_t* needleC = (uint8_t*) needle;
  27. uint8_t* haystackC = (uint8_t*) haystack;
  28. uint8_t* stopAt = haystackC + haystackLen - needleLen;
  29. if (!(haystack && needle && haystackLen && needleLen)) {
  30. return NULL;
  31. }
  32. while (haystackC <= stopAt) {
  33. if (*haystackC == *needleC && !Bits_memcmp(haystackC, needleC, needleLen)) {
  34. return haystackC;
  35. }
  36. haystackC++;
  37. }
  38. return NULL;
  39. }