// Sample program demonstrating the use of the Big Integer Library. // Standard libraries #include #include // `BigIntegerLibrary.hh' includes all of the library headers. #include "BigIntegerLibrary.hh" int main() { /* The library throws `const char *' error messages when things go * wrong. It's a good idea to catch them using a `try' block like this * one. Your C++ compiler might need a command-line option to compile * code that uses exceptions. */ try { BigInteger a; // a is 0 int b = 535; /* Any primitive integer can be converted implicitly to a * BigInteger. */ a = b; /* The reverse conversion requires a method call (implicit * conversions were previously supported but caused trouble). * If a were too big for an int, the library would throw an * exception. */ b = a.toInt(); BigInteger c(a); // Copy a BigInteger. // The int literal is converted to a BigInteger. BigInteger d(-314159265); /* This won't compile (at least on 32-bit machines) because the * number is too big to be a primitive integer literal, and * there's no such thing as a BigInteger literal. */ //BigInteger e(3141592653589793238462643383279); // Instead you can convert the number from a string. std::string s("3141592653589793238462643383279"); BigInteger f = stringToBigInteger(s); // You can convert the other way too. std::string s2 = bigIntegerToString(f); // f is implicitly stringified and sent to std::cout. std::cout << f << std::endl; /* Let's do some math! The library overloads most of the * mathematical operators (including assignment operators) to * work on BigIntegers. There are also ``copy-less'' * operations; see `BigUnsigned.hh' for details. */ // Arithmetic operators BigInteger g(314159), h(265); std::cout << (g + h) << '\n' << (g - h) << '\n' << (g * h) << '\n' << (g / h) << '\n' << (g % h) << std::endl; // Bitwise operators BigUnsigned i(0xFF0000FF), j(0x0000FFFF); // The library's << operator recognizes base flags. std::cout.flags(std::ios::hex | std::ios::showbase); std::cout << (i & j) << '\n' << (i | j) << '\n' << (i ^ j) << '\n' // Shift distances are ordinary unsigned ints. << (j << 21) << '\n' << (j >> 10) << '\n'; std::cout.flags(std::ios::dec); // Let's do some heavy lifting and calculate powers of 314. int maxPower = 10; BigUnsigned x(1), big314(314); for (int power = 0; power <= maxPower; power++) { std::cout << "314^" << power << " = " << x << std::endl; x *= big314; // A BigInteger assignment operator } // Some big-integer algorithms (albeit on small integers). std::cout << gcd(BigUnsigned(60), 72) << '\n' << modinv(BigUnsigned(7), 11) << '\n' << modexp(BigUnsigned(314), 159, 2653) << std::endl; // Add your own code here to experiment with the library. } catch(char const* err) { std::cout << "The library threw an exception:\n" << err << std::endl; } return 0; } /* The original sample program produces this output: 3141592653589793238462643383279 314424 313894 83252135 1185 134 0xFF 0xFF00FFFF 0xFF00FF00 0x1FFFE00000 0x3F 314^0 = 1 314^1 = 314 314^2 = 98596 314^3 = 30959144 314^4 = 9721171216 314^5 = 3052447761824 314^6 = 958468597212736 314^7 = 300959139524799104 314^8 = 94501169810786918656 314^9 = 29673367320587092457984 314^10 = 9317437338664347031806976 12 8 1931 */ id='n8' href='#n8'>8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
/*
 *  nextpnr -- Next Generation Place and Route
 *
 *  Copyright (C) 2021  Symbiflow Authors
 *
 *
 *  Permission to use, copy, modify, and/or distribute this software for any
 *  purpose with or without fee is hereby granted, provided that the above
 *  copyright notice and this permission notice appear in all copies.
 *
 *  THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 *  WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 *  MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 *  ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 *  WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 *  ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 *  OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 *
 */

#ifndef PSEUDO_PIP_MODEL_H
#define PSEUDO_PIP_MODEL_H

#include <tuple>

#include "dynamic_bitarray.h"
#include "hash_table.h"
#include "nextpnr_namespaces.h"
#include "nextpnr_types.h"
#include "site_router.h"

NEXTPNR_NAMESPACE_BEGIN

struct PseudoPipBel
{
    // Which BEL in the tile does the pseudo pip use?
    int32_t bel_index;

    // What is the index of the input BEL pin that the pseudo pip used?
    //
    // NOTE: This is **not** the name of the pin.
    int32_t input_bel_pin;

    // What is the index of the output BEL pin that the pseudo pip used?
    //
    // NOTE: This is **not** the name of the pin.
    int32_t output_bel_pin;
};

struct LogicBelKey
{
    int32_t tile_type;
    int32_t pip_index;
    int32_t site;

    std::tuple<int32_t, int32_t, int32_t> make_tuple() const { return std::make_tuple(tile_type, pip_index, site); }

    bool operator==(const LogicBelKey &other) const { return make_tuple() == other.make_tuple(); }

    bool operator<(const LogicBelKey &other) const { return make_tuple() < other.make_tuple(); }
};

NEXTPNR_NAMESPACE_END

namespace std {
template <> struct hash<NEXTPNR_NAMESPACE_PREFIX LogicBelKey>
{
    std::size_t operator()(const NEXTPNR_NAMESPACE_PREFIX LogicBelKey &key) const noexcept
    {
        std::size_t seed = 0;
        boost::hash_combine(seed, hash<int32_t>()(key.tile_type));
        boost::hash_combine(seed, hash<int32_t>()(key.pip_index));
        boost::hash_combine(seed, hash<int32_t>()(key.site));

        return seed;
    }
};

}; // namespace std

NEXTPNR_NAMESPACE_BEGIN

// Storage for tile type generic pseudo pip data and lookup.
struct PseudoPipData
{
    // Initial data for specified tile type, if not already initialized.
    void init_tile_type(const Context *ctx, int32_t tile_type);

    // Get the highest PipId::index found in a specified tile type.
    size_t get_max_pseudo_pip(int32_t tile_type) const;

    // Get the list of possible sites that a pseudo pip might be used in.
    const std::vector<size_t> &get_possible_sites_for_pip(const Context *ctx, PipId pip) const;

    // Get list of BELs the pseudo pip uses, and how it routes through them.
    //
    // This does **not** include site ports or site pips.
    const std::vector<PseudoPipBel> &get_logic_bels_for_pip(const Context *ctx, int32_t site, PipId pip) const;

    HashTables::HashMap<int32_t, size_t> max_pseudo_pip_for_tile_type;
    HashTables::HashMap<std::pair<int32_t, int32_t>, std::vector<size_t>, PairHash> possibles_sites_for_pip;
    HashTables::HashMap<LogicBelKey, std::vector<PseudoPipBel>> logic_bels_for_pip;
};

// Tile instance fast pseudo pip lookup.
struct PseudoPipModel
{
    int32_t tile;
    DynamicBitarray<> allowed_pseudo_pips;
    HashTables::HashMap<int32_t, size_t> pseudo_pip_sites;
    HashTables::HashMap<size_t, std::vector<int32_t>> site_to_pseudo_pips;
    HashTables::HashSet<int32_t> active_pseudo_pips;
    std::vector<int32_t> scratch;

    // Call when a tile is initialized.
    void init(Context *ctx, int32_t tile);

    // Call after placement but before routing to update which pseudo pips are
    // legal.  This call is important to ensure that checkPipAvail returns the
    // correct value.
    //
    // If the tile has no placed elements, then prepare_for_routing does not
    // need to be called after init.
    void prepare_for_routing(const Context *ctx, const std::vector<SiteRouter> &sites);

    // Returns true if the pseudo pip is allowed given current site placements
    // and other pseudo pips.
    bool checkPipAvail(const Context *ctx, PipId pip) const;

    // Enables a pseudo pip in the model.  May cause other pseudo pips to
    // become unavailable.
    void bindPip(const Context *ctx, PipId pip);

    // Removes a pseudo pip from the model.  May cause other pseudo pips to
    // become available.
    void unbindPip(const Context *ctx, PipId pip);

    // Internal method to update pseudo pips marked as part of a site.
    void update_site(const Context *ctx, size_t site);
};

NEXTPNR_NAMESPACE_END

#endif /* PSEUDO_PIP_MODEL_H */