/ tests / fuzz_util.cpp
fuzz_util.cpp
 1  /* This file is part of the dynarmic project.
 2   * Copyright (c) 2018 MerryMage
 3   * SPDX-License-Identifier: 0BSD
 4   */
 5  
 6  #include "./fuzz_util.h"
 7  
 8  #include <cstring>
 9  
10  #include <fmt/format.h>
11  #include <fmt/ostream.h>
12  #include <mcl/assert.hpp>
13  
14  #include "./rand_int.h"
15  #include "dynarmic/common/fp/fpcr.h"
16  #include "dynarmic/common/fp/rounding_mode.h"
17  
18  using namespace Dynarmic;
19  
20  std::ostream& operator<<(std::ostream& o, Vector vec) {
21      return o << fmt::format("{:016x}'{:016x}", vec[1], vec[0]);
22  }
23  
24  Vector RandomVector() {
25      return {RandInt<u64>(0, ~u64(0)), RandInt<u64>(0, ~u64(0))};
26  }
27  
28  u32 RandomFpcr() {
29      FP::FPCR fpcr;
30      fpcr.AHP(RandInt(0, 1) == 0);
31      fpcr.DN(RandInt(0, 1) == 0);
32      fpcr.FZ(RandInt(0, 1) == 0);
33      fpcr.RMode(static_cast<FP::RoundingMode>(RandInt(0, 3)));
34      fpcr.FZ16(RandInt(0, 1) == 0);
35      return fpcr.Value();
36  }
37  
38  InstructionGenerator::InstructionGenerator(const char* format) {
39      const size_t format_len = std::strlen(format);
40      ASSERT(format_len == 16 || format_len == 32);
41  
42      if (format_len == 16) {
43          // Begin with 16 zeros
44          mask |= 0xFFFF0000;
45      }
46  
47      for (size_t i = 0; i < format_len; i++) {
48          const u32 bit = 1u << (format_len - i - 1);
49          switch (format[i]) {
50          case '0':
51              mask |= bit;
52              break;
53          case '1':
54              bits |= bit;
55              mask |= bit;
56              break;
57          default:
58              // Do nothing
59              break;
60          }
61      }
62  }
63  
64  u32 InstructionGenerator::Generate() const {
65      const u32 random = RandInt<u32>(0, 0xFFFFFFFF);
66      return bits | (random & ~mask);
67  }