Commit 3be218a1 authored by Kento HASEGAWA's avatar Kento HASEGAWA

Add the ADC2017-version solver that is for HLS.

parent e4e68357
...@@ -4,5 +4,16 @@ ...@@ -4,5 +4,16 @@
*.txt *.txt
# Object files
*.o
# Executables
*.exe
*.stackdump
*.out
sim
solver
# Python files
*.pyc *.pyc
__pycache__/ __pycache__/
TARGET = sim
OBJS = $(CPPS:.cpp=.o)
CPPS = $(wildcard *.cpp)
CXX = g++
CXXFLAGS = -O3 -Wall -Wno-unknown-pragmas -Wno-unused-label -DSOFTWARE -DCALCTIME
all: $(TARGET)
$(TARGET): $(OBJS)
$(CXX) -O3 -o $@ $(OBJS)
run:
python3 ../NLGenerator.py -x 20 -y 20 -z 6 -l 100;\
python3 ./gen_boardstr.py Q-20x20x5_100_10.txt |\
./$(TARGET) -
clean:
rm *.o
rm $(TARGET)
TARGET = sim
OBJS = $(CPPS:.cpp=.o)
CPPS = $(wildcard *.cpp)
CXX = g++
CXXFLAGS = -O3 -Wall -Wno-unknown-pragmas -Wno-unused-label -DSOFTWARE -DCALCTIME
all: $(TARGET)
$(TARGET): $(OBJS)
$(CXX) -O3 -Wl,--stack,16777216 -o $@ $(OBJS)
clean:
rm *.o
rm $(TARGET)
# pynq-router (sw)
Vivado HLS 用 pynq-router
## メモ
* Vivado HLS 2016.1
* Vivado 2016.1
高位合成オプション:
* Part: xc7z020clg400-1
* Clock Period: 10.0 ns
* Clock Uncertainty: 3.0 ns, 3.5 ns // 2017/08/04
論理合成・配置配線のオプション:
* Synthesis strategy: ~~Vivado Synthesis Defaults~~ Flow_PerfOptimized_high
* Implementation strategy: ~~Vivado Implementation Defaults~~ Performance_Explore
最適化記録:
* 2017/08/04: クリティカルパス遅延の最適化
* (3.0) Estimated clock: 6.78, Max latency: 1,667,151,745 (1.00)
* (3.5) Estimated clock: 6.38, Max latency: 1,692,438,002 (1.00)
* 2017/08/05: キューpop処理の動作改善
* (3.0) Estimated clock: 6.78, Max latency: 1,530,952,491 (0.92)
* (3.5) Estimated clock: 6.38, Max latency: 1,556,238,748 (0.92)
* 2017/08/07: 剰余演算の書き換え,隣接ノードの探索ループ展開
* (3.0) Estimated clock: 6.78, Max latency: 1,423,592,713 (0.85)
* (3.5) Estimated clock: 6.38, Max latency: 1,428,235,716 (0.84)
* 2017/08/07: 高位合成パラメータの調整
* (3.0) Estimated clock: 6.78, Max latency: 1,284,364,290 (0.77)
* (3.5) Estimated clock: 6.38, Max latency: 1,289,003,291 (0.76)
* 2017/08/11: 隣接ノードの探索ループ展開を戻した (そうしないと論理合成・配置配線でタイミング満たさない)
* (3.0) Estimated clock: 6.78, Max latency: 1,329,761,290 (0.80)
* (3.5) Estimated clock: 6.38, Max latency: 1,355,035,291 (0.80)
* 2017/08/11: starts と goals を FF パーティションした
* (3.0) Estimated clock: 6.78, Max latency: 1,329,245,034 (0.797)
* (3.5) Estimated clock: 6.45, Max latency: 1,354,519,035 (0.800)
* 2017/08/16: MAX_LINES を 256 に増やした
* (3.0) Estimated clock: 6.78, Max latency: 1,905,588,970 (1.143)
* (3.5) Estimated clock: 6.38, Max latency: 1,933,166,972 (1.142)
* 2017/08/16: MAX_LINES を 128 に戻した (そうしないと論理合成・配置配線でタイミング満たさない)
* (3.0) Estimated clock: 6.78, Max latency: 1,329,245,034 (0.797)
* (3.5) Estimated clock: 6.45, Max latency: 1,354,519,035 (0.800)
* 2017/08/16: コスト関数の波形をのこぎり型にした
* (3.0) Estimated clock: 6.78, Max latency: 1,329,245,034 (13.29 sec) (0.797)
* (3.25) Estimated clock: 6.52, Max latency: 1,329,249,035 (13.29 sec)
* (3.5) Estimated clock: 6.45, Max latency: 1,354,519,035 (13.55 sec) (0.800)
* 2017/08/18: 対象ライン選択に剰余演算を用いない方法を試してみた ※あまり変わらないからこれはやらずに元に戻す
* (3.0) Estimated clock: 6.52, Max latency: 1,329,101,007 (13.29 sec) (0.797)
* (3.5) Estimated clock: 6.45, Max latency: 1,354,371,007 (13.54 sec) (0.800)
* 2017/08/18: 対象ラインが連続して同じだったらルーティングスキップする
* (3.0) Estimated clock: 6.78, Max latency: 1,329,245,034 (13.29 sec)
* (3.25) Estimated clock: 6.52, Max latency: 1,329,249,035 (13.29 sec) **←今これ**
* (3.5) Estimated clock: 6.45, Max latency: 1,354,519,035 (13.55 sec)
## 入出力
入力は boardstr という問題ファイルをコンパクトに記述したようなワンラインの文字列。
出力は char 型の配列で、各要素に解答ファイルに相当する数値が入っている。
/*
* Copyright 2012 Xilinx, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef __AESL_AP_SIM_H__
#define __AESL_AP_SIM_H__
#ifndef __cplusplus
#error C++ is required to include this header file
#else
#include "etc/ap_int_sim.h"
#include "etc/ap_fixed_sim.h"
//Forward declaration
template<int _AP_W, int _AP_I, ap_q_mode _AP_Q,
ap_o_mode _AP_O, int _AP_N> class ap_fixed;
template<int _AP_W, int _AP_I, ap_q_mode _AP_Q,
ap_o_mode _AP_O, int _AP_N> class ap_ufixed;
template<int _AP_W> class ap_int;
template<int _AP_W> class ap_uint;
//AP_INT
//--------------------------------------------------------
template<int _AP_W>
class ap_int : public ap_private<_AP_W, true> {
#ifdef _MSC_VER
#pragma warning(disable: 4521 4522)
#endif /* #ifdef _MSC_VER */
public:
typedef ap_private<_AP_W, true> Base;
//Constructor
INLINE ap_int(): Base() {}
template<int _AP_W2>
INLINE ap_int(const volatile ap_int<_AP_W2> &op):Base((const ap_private<_AP_W2,true> &)(op)) {}
template<int _AP_W2>
INLINE ap_int(const ap_int<_AP_W2> &op):Base((const ap_private<_AP_W2,true> &)(op)) {}
template<int _AP_W2>
INLINE ap_int(const ap_uint<_AP_W2> &op):Base((const ap_private<_AP_W2,false> &)(op)) {}
template<int _AP_W2>
INLINE ap_int(const volatile ap_uint<_AP_W2> &op):Base((const ap_private<_AP_W2,false> &)(op)) {}
template<int _AP_W2, bool _AP_S2>
INLINE ap_int(const ap_range_ref<_AP_W2, _AP_S2>& ref):Base(ref) {}
template<int _AP_W2, bool _AP_S2>
INLINE ap_int(const ap_bit_ref<_AP_W2, _AP_S2>& ref):Base(ref) {}
template<int _AP_W2, typename _AP_T2, int _AP_W3, typename _AP_T3>
INLINE ap_int(const ap_concat_ref<_AP_W2, _AP_T2, _AP_W3, _AP_T3>& ref):Base(ref) {}
template<int _AP_W2, int _AP_I2, ap_q_mode _AP_Q2,
ap_o_mode _AP_O2, int _AP_N2>
INLINE ap_int(const ap_fixed<_AP_W2, _AP_I2, _AP_Q2, _AP_O2, _AP_N2>& op)
:Base(op.to_ap_private()) {}
template<int _AP_W2, int _AP_I2, ap_q_mode _AP_Q2,
ap_o_mode _AP_O2, int _AP_N2>
INLINE ap_int(const ap_ufixed<_AP_W2, _AP_I2, _AP_Q2, _AP_O2, _AP_N2>& op)
:Base(op.to_ap_private()) {}
template<int _AP_W2, int _AP_I2, ap_q_mode _AP_Q2,
ap_o_mode _AP_O2, int _AP_N2>
INLINE ap_int(const volatile ap_fixed<_AP_W2, _AP_I2, _AP_Q2, _AP_O2, _AP_N2>& op)
:Base(op.to_ap_private()) {}
template<int _AP_W2, int _AP_I2, ap_q_mode _AP_Q2,
ap_o_mode _AP_O2, int _AP_N2>
INLINE ap_int(const volatile ap_ufixed<_AP_W2, _AP_I2, _AP_Q2, _AP_O2, _AP_N2>& op)
:Base(op.to_ap_private()) {}
template<int _AP_W2, bool _AP_S2>
INLINE ap_int(const ap_private<_AP_W2, _AP_S2>& op):Base(op) {}
template<int _AP_W2, int _AP_I2, bool _AP_S2, ap_q_mode _AP_Q2,
ap_o_mode _AP_O2, int _AP_N2>
INLINE ap_int(const af_bit_ref<_AP_W2, _AP_I2, _AP_S2, _AP_Q2, _AP_O2,
_AP_N2>& op):Base(op) {}
template<int _AP_W2, int _AP_I2, bool _AP_S2, ap_q_mode _AP_Q2,
ap_o_mode _AP_O2, int _AP_N2>
INLINE ap_int(const af_range_ref<_AP_W2, _AP_I2, _AP_S2, _AP_Q2, _AP_O2,
_AP_N2>& op):Base(op) {}
template<int _AP_W2, int _AP_I2, bool _AP_S2, ap_q_mode _AP_Q2,
ap_o_mode _AP_O2, int _AP_N2>
INLINE ap_int(const ap_fixed_base<_AP_W2, _AP_I2, _AP_S2,
_AP_Q2, _AP_O2, _AP_N2>& op):Base(op.to_ap_private()) {}
#define CTOR(TYPE) \
INLINE ap_int(TYPE v):Base(v) {}
CTOR(bool)
CTOR(signed char)
CTOR(unsigned char)
CTOR(short)
CTOR(unsigned short)
CTOR(int)
CTOR(unsigned int)
CTOR(long)
CTOR(unsigned long)
CTOR(unsigned long long)
CTOR(long long)
CTOR(float)
CTOR(double)
CTOR(const char*)
CTOR(const std::string&)
#undef CTOR
INLINE ap_int(const char* str, signed char rd):Base(str, rd) {}
//Assignment
//Another form of "write"
INLINE void operator = (const ap_int<_AP_W>& op2) volatile {
const_cast<ap_int*>(this)->operator = (op2);
}
INLINE void operator = (const volatile ap_int<_AP_W>& op2) volatile {
const_cast<Base*>(this)->operator = (op2);
}
INLINE ap_int<_AP_W>& operator = (const volatile ap_int<_AP_W>& op2) {
Base::operator = (const_cast<ap_int<_AP_W>& >(op2));
return *this;
}
INLINE ap_int<_AP_W>& operator = (const ap_int<_AP_W>& op2) {
Base::operator = ((const ap_private<_AP_W, true>&)op2);
return *this;
}
};
//AP_UINT
//---------------------------------------------------------------
template<int _AP_W>
class ap_uint: public ap_private<_AP_W, false> {
#ifdef _MSC_VER
#pragma warning( disable : 4521 4522 )
#endif
public:
typedef ap_private<_AP_W, false> Base;
//Constructor
INLINE ap_uint(): Base() {}
INLINE ap_uint(const ap_uint<_AP_W>& op) :Base(dynamic_cast<const ap_private<_AP_W, false>&>(op)) {}
INLINE ap_uint(const volatile ap_uint<_AP_W>& op):Base(dynamic_cast<const volatile ap_private<_AP_W, false>&>(op)){}
template<int _AP_W2>
INLINE ap_uint(const volatile ap_uint<_AP_W2> &op):Base((const ap_private<_AP_W2, false>&)(op)) {}
template<int _AP_W2>
INLINE ap_uint(const ap_uint<_AP_W2> &op) : Base((const ap_private<_AP_W2, false>&)(op)){}
template<int _AP_W2>
INLINE ap_uint(const ap_int<_AP_W2> &op) : Base((const ap_private<_AP_W2, true>&)(op)) {}
template<int _AP_W2>
INLINE ap_uint(const volatile ap_int<_AP_W2> &op) : Base((const ap_private<_AP_W2, false>&)(op)) {}
template<int _AP_W2, bool _AP_S2>
INLINE ap_uint(const ap_range_ref<_AP_W2, _AP_S2>& ref):Base(ref) {}
template<int _AP_W2, bool _AP_S2>
INLINE ap_uint(const ap_bit_ref<_AP_W2, _AP_S2>& ref):Base(ref) {}
template<int _AP_W2, typename _AP_T2, int _AP_W3, typename _AP_T3>
INLINE ap_uint(const ap_concat_ref<_AP_W2, _AP_T2, _AP_W3, _AP_T3>& ref):Base(ref) {}
template<int _AP_W2, int _AP_I2, ap_q_mode _AP_Q2,
ap_o_mode _AP_O2, int _AP_N2>
INLINE ap_uint(const ap_fixed<_AP_W2, _AP_I2, _AP_Q2, _AP_O2, _AP_N2>& op)
:Base(op.to_ap_private()) {}
template<int _AP_W2, int _AP_I2, ap_q_mode _AP_Q2,
ap_o_mode _AP_O2, int _AP_N2>
INLINE ap_uint(const ap_ufixed<_AP_W2, _AP_I2, _AP_Q2, _AP_O2, _AP_N2>& op)
:Base(op.to_ap_private()) {}
template<int _AP_W2, int _AP_I2, ap_q_mode _AP_Q2,
ap_o_mode _AP_O2, int _AP_N2>
INLINE ap_uint(const volatile ap_fixed<_AP_W2, _AP_I2, _AP_Q2, _AP_O2, _AP_N2>& op)
:Base(op.to_ap_private()) {}
template<int _AP_W2, int _AP_I2, ap_q_mode _AP_Q2,
ap_o_mode _AP_O2, int _AP_N2>
INLINE ap_uint(const volatile ap_ufixed<_AP_W2, _AP_I2, _AP_Q2, _AP_O2, _AP_N2>& op)
:Base(op) {}
template<int _AP_W2, bool _AP_S2>
INLINE ap_uint(const ap_private<_AP_W2, _AP_S2>& op):Base(op) {}
template<int _AP_W2, int _AP_I2, bool _AP_S2, ap_q_mode _AP_Q2,
ap_o_mode _AP_O2, int _AP_N2>
INLINE ap_uint(const af_bit_ref<_AP_W2, _AP_I2, _AP_S2, _AP_Q2, _AP_O2,
_AP_N2>& op):Base(op) {}
template<int _AP_W2, int _AP_I2, bool _AP_S2, ap_q_mode _AP_Q2,
ap_o_mode _AP_O2, int _AP_N2>
INLINE ap_uint(const af_range_ref<_AP_W2, _AP_I2, _AP_S2, _AP_Q2, _AP_O2,
_AP_N2>& op):Base(op) {}
template<int _AP_W2, int _AP_I2, bool _AP_S2, ap_q_mode _AP_Q2,
ap_o_mode _AP_O2, int _AP_N2>
INLINE ap_uint(const ap_fixed_base<_AP_W2, _AP_I2, _AP_S2,
_AP_Q2, _AP_O2, _AP_N2>& op):Base(op.to_ap_private()) {}
#define CTOR(TYPE) \
INLINE ap_uint(TYPE v):Base(v) {}
CTOR(bool)
CTOR(signed char)
CTOR(unsigned char)
CTOR(short)
CTOR(unsigned short)
CTOR(int)
CTOR(unsigned int)
CTOR(long)
CTOR(unsigned long)
CTOR(unsigned long long)
CTOR(long long)
CTOR(float)
CTOR(double)
CTOR(const char*)
CTOR(const std::string&)
#undef CTOR
INLINE ap_uint(const char* str, signed char rd):Base(str, rd) {}
//Assignment
//Another form of "write"
INLINE void operator = (const ap_uint<_AP_W>& op2) volatile {
Base::operator = (op2);
}
INLINE void operator = (const volatile ap_uint<_AP_W>& op2) volatile {
Base::operator = (op2);
}
INLINE ap_uint<_AP_W>& operator = (const volatile ap_uint<_AP_W>& op2) {
Base::operator = (op2);
return *this;
}
INLINE ap_uint<_AP_W>& operator = (const ap_uint<_AP_W>& op2) {
Base::operator = ((const ap_private<_AP_W, false>&)(op2));
return *this;
}
};
#define ap_bigint ap_int
#define ap_biguint ap_uint
//AP_FIXED
//---------------------------------------------------------------------
template<int _AP_W, int _AP_I, ap_q_mode _AP_Q = AP_TRN,
ap_o_mode _AP_O = AP_WRAP, int _AP_N = 0>
class ap_fixed: public ap_fixed_base<_AP_W, _AP_I, true, _AP_Q, _AP_O, _AP_N> {
#ifdef _MSC_VER
#pragma warning( disable : 4521 4522 )
#endif
public:
typedef ap_fixed_base<_AP_W, _AP_I, true, _AP_Q, _AP_O, _AP_N> Base;
//Constructor
INLINE ap_fixed():Base() {}
template<int _AP_W2, int _AP_I2, ap_q_mode _AP_Q2,
ap_o_mode _AP_O2, int _AP_N2>
INLINE ap_fixed(const ap_fixed<_AP_W2, _AP_I2, _AP_Q2, _AP_O2,
_AP_N2>& op): Base(op) {}
template<int _AP_W2, int _AP_I2, ap_q_mode _AP_Q2,
ap_o_mode _AP_O2, int _AP_N2>
INLINE ap_fixed(const ap_ufixed<_AP_W2, _AP_I2, _AP_Q2, _AP_O2,
_AP_N2>& op): Base(ap_fixed_base<_AP_W2, _AP_I2,
false, _AP_Q2, _AP_O2, _AP_N2>(op)) {}
template<int _AP_W2>
INLINE ap_fixed(const ap_int<_AP_W2>& op):
Base(ap_private<_AP_W2, true>(op)) {}
template<int _AP_W2>
INLINE ap_fixed(const ap_uint<_AP_W2>& op):Base(ap_private<_AP_W2, false>(op)) {}
template<int _AP_W2, int _AP_I2, ap_q_mode _AP_Q2,
ap_o_mode _AP_O2, int _AP_N2>
INLINE ap_fixed(const volatile ap_fixed<_AP_W2, _AP_I2, _AP_Q2, _AP_O2,
_AP_N2>& op): Base(ap_fixed_base<_AP_W2, _AP_I2,
true, _AP_Q2, _AP_O2, _AP_N2>(op)) {}
template<int _AP_W2, int _AP_I2, ap_q_mode _AP_Q2,
ap_o_mode _AP_O2, int _AP_N2>
INLINE ap_fixed(const volatile ap_ufixed<_AP_W2, _AP_I2, _AP_Q2, _AP_O2,
_AP_N2>& op): Base(ap_fixed_base<_AP_W2, _AP_I2,
false, _AP_Q2, _AP_O2, _AP_N2>(op)) {}
template<int _AP_W2>
INLINE ap_fixed(const volatile ap_int<_AP_W2>& op):
Base(ap_private<_AP_W2, true>(op)) {}
template<int _AP_W2>
INLINE ap_fixed(const volatile ap_uint<_AP_W2>& op):Base(op) {}
template<int _AP_W2, int _AP_I2, bool _AP_S2, ap_q_mode _AP_Q2,
ap_o_mode _AP_O2, int _AP_N2>
INLINE ap_fixed(const ap_fixed_base<_AP_W2, _AP_I2, _AP_S2,
_AP_Q2, _AP_O2, _AP_N2>& op):Base(op) {}
template<int _AP_W2, bool _AP_S2>
INLINE ap_fixed(const ap_bit_ref<_AP_W2, _AP_S2>& op):
Base(op) {}
template<int _AP_W2, bool _AP_S2>
INLINE ap_fixed(const ap_range_ref<_AP_W2, _AP_S2>& op):
Base(op) {}
template<int _AP_W2, typename _AP_T2, int _AP_W3, typename _AP_T3>
INLINE ap_fixed(const ap_concat_ref<_AP_W2, _AP_T2, _AP_W3, _AP_T3>& op):
Base(op) {}
template<int _AP_W2, int _AP_I2, bool _AP_S2, ap_q_mode _AP_Q2,
ap_o_mode _AP_O2, int _AP_N2>
INLINE ap_fixed(const af_bit_ref<_AP_W2, _AP_I2, _AP_S2,
_AP_Q2, _AP_O2, _AP_N2>& op): Base(op) {}
template<int _AP_W2, int _AP_I2, bool _AP_S2, ap_q_mode _AP_Q2,
ap_o_mode _AP_O2, int _AP_N2>
INLINE ap_fixed(const af_range_ref<_AP_W2, _AP_I2, _AP_S2,
_AP_Q2, _AP_O2, _AP_N2>& op): Base(op) {}
template<int _AP_W2, bool _AP_S2>
INLINE ap_fixed(const ap_private<_AP_W2, _AP_S2>& op):Base(op) {}
#define CTOR(TYPE) \
INLINE ap_fixed(TYPE v):Base(v) {}
CTOR(bool)
CTOR(signed char)
CTOR(unsigned char)
CTOR(short)
CTOR(unsigned short)
CTOR(int)
CTOR(unsigned int)
CTOR(long)
CTOR(unsigned long)
CTOR(unsigned long long)
CTOR(long long)
CTOR(float)
CTOR(double)
CTOR(const char*)
CTOR(const std::string&)
#undef CTOR
INLINE ap_fixed(const char* str, signed char rd):Base(str, rd) {}
//Assignment
INLINE ap_fixed& operator = (const ap_fixed<_AP_W, _AP_I,
_AP_Q, _AP_O, _AP_N>& op) {
Base::operator = (op);
return *this;
}
INLINE ap_fixed& operator = (const volatile ap_fixed<_AP_W, _AP_I,
_AP_Q, _AP_O, _AP_N>& op) {
Base::operator = (op);
return *this;
}
};
//AP_ UFIXED
//--- ----------------------------------------------------------------
template<int _AP_W, int _AP_I, ap_q_mode _AP_Q = AP_TRN,
ap_o_mode _AP_O = AP_WRAP, int _AP_N = 0>
class ap_ufixed : public ap_fixed_base<_AP_W, _AP_I, false, _AP_Q, _AP_O, _AP_N> {
#ifdef _MSC_VER
#pragma warning(disable: 4521 4522)
#endif /* #ifdef _MSC_VER */
public:
typedef ap_fixed_base<_AP_W, _AP_I, false, _AP_Q, _AP_O, _AP_N> Base;
//Constructor
INLINE ap_ufixed():Base() {}
template<int _AP_W2, int _AP_I2, ap_q_mode _AP_Q2,
ap_o_mode _AP_O2, int _AP_N2>
INLINE ap_ufixed(const ap_fixed<_AP_W2, _AP_I2, _AP_Q2,
_AP_O2, _AP_N2>& op) : Base(ap_fixed_base<_AP_W2,
_AP_I2, true, _AP_Q2, _AP_O2, _AP_N2>(op)) {}
template<int _AP_W2, int _AP_I2, ap_q_mode _AP_Q2,
ap_o_mode _AP_O2, int _AP_N2>
INLINE ap_ufixed(const ap_ufixed<_AP_W2, _AP_I2, _AP_Q2,
_AP_O2, _AP_N2>& op): Base(ap_fixed_base<_AP_W2, _AP_I2,
false, _AP_Q2, _AP_O2, _AP_N2>(op)) {}
template<int _AP_W2>
INLINE ap_ufixed(const ap_int<_AP_W2>& op):
Base((const ap_private<_AP_W2, true>&)(op)) {}
template<int _AP_W2>
INLINE ap_ufixed(const ap_uint<_AP_W2>& op):
Base((const ap_private<_AP_W2, false>&)(op)) {}
template<int _AP_W2, int _AP_I2, ap_q_mode _AP_Q2,
ap_o_mode _AP_O2, int _AP_N2>
INLINE ap_ufixed(const volatile ap_fixed<_AP_W2, _AP_I2, _AP_Q2,
_AP_O2, _AP_N2>& op) : Base(ap_fixed_base<_AP_W2,
_AP_I2, true, _AP_Q2, _AP_O2, _AP_N2>(op)) {}
template<int _AP_W2, int _AP_I2, ap_q_mode _AP_Q2,
ap_o_mode _AP_O2, int _AP_N2>
INLINE ap_ufixed(const volatile ap_ufixed<_AP_W2, _AP_I2, _AP_Q2,
_AP_O2, _AP_N2>& op): Base(ap_fixed_base<_AP_W2, _AP_I2,
false, _AP_Q2, _AP_O2, _AP_N2>(op)) {}
template<int _AP_W2>
INLINE ap_ufixed(const volatile ap_int<_AP_W2>& op):
Base(ap_private<_AP_W2, true>(op)) {}
template<int _AP_W2>
INLINE ap_ufixed(const volatile ap_uint<_AP_W2>& op):
Base(ap_private<_AP_W2, false>(op)) {}
template<int _AP_W2, int _AP_I2, bool _AP_S2, ap_q_mode _AP_Q2,
ap_o_mode _AP_O2, int _AP_N2>
INLINE ap_ufixed(const ap_fixed_base<_AP_W2, _AP_I2, _AP_S2, _AP_Q2,
_AP_O2, _AP_N2>& op):Base(op) {}
template<int _AP_W2, bool _AP_S2>
INLINE ap_ufixed(const ap_bit_ref<_AP_W2, _AP_S2>& op):
Base(op) {}
template<int _AP_W2, bool _AP_S2>
INLINE ap_ufixed(const ap_range_ref<_AP_W2, _AP_S2>& op):
Base(op) {}
template<int _AP_W2, typename _AP_T2, int _AP_W3, typename _AP_T3>
INLINE ap_ufixed(const ap_concat_ref<_AP_W2, _AP_T2, _AP_W3, _AP_T3>& op):
Base(op) {}
template<int _AP_W2, int _AP_I2, bool _AP_S2, ap_q_mode _AP_Q2,
ap_o_mode _AP_O2, int _AP_N2>
INLINE ap_ufixed(const af_bit_ref<_AP_W2, _AP_I2, _AP_S2,
_AP_Q2, _AP_O2, _AP_N2>& op): Base(op) {}
template<int _AP_W2, int _AP_I2, bool _AP_S2, ap_q_mode _AP_Q2,
ap_o_mode _AP_O2, int _AP_N2>
INLINE ap_ufixed(const af_range_ref<_AP_W2, _AP_I2, _AP_S2,
_AP_Q2, _AP_O2, _AP_N2>& op): Base(op) {}
template<int _AP_W2, bool _AP_S2>
INLINE ap_ufixed(const ap_private<_AP_W2, _AP_S2>& op):Base(op) {}
#define CTOR(TYPE) \
INLINE ap_ufixed(TYPE v):Base(v) {}
CTOR(bool)
CTOR(signed char)
CTOR(unsigned char)
CTOR(short)
CTOR(unsigned short)
CTOR(int)
CTOR(unsigned int)
CTOR(long)
CTOR(unsigned long)
CTOR(unsigned long long)
CTOR(long long)
CTOR(float)
CTOR(double)
CTOR(const char*)
CTOR(const std::string&)
#undef CTOR
INLINE ap_ufixed(const char* str, signed char rd):Base(str, rd) {}
//Assignment
INLINE ap_ufixed& operator = (const ap_ufixed<_AP_W, _AP_I,
_AP_Q, _AP_O, _AP_N>& op) {
Base::operator = (op);
return *this;
}
INLINE ap_ufixed& operator = (const volatile ap_ufixed<_AP_W, _AP_I,
_AP_Q, _AP_O, _AP_N>& op) {
Base::V = const_cast<ap_ufixed&>(op);
return *this;
}
};
#if defined(SYSTEMC_H) || defined(SYSTEMC_INCLUDED)
template<int _AP_W>
INLINE void sc_trace(sc_core::sc_trace_file *tf, const ap_int<_AP_W> &op,
const std::string &name) {
if (tf)
tf->trace(sc_dt::sc_lv<_AP_W>(op.to_string(2).c_str()), name);
}
template<int _AP_W>
INLINE void sc_trace(sc_core::sc_trace_file *tf, const ap_uint<_AP_W> &op,
const std::string &name) {
if (tf)
tf->trace(sc_dt::sc_lv<_AP_W>(op.to_string(2).c_str()), name);
}
template<int _AP_W, int _AP_I, ap_q_mode _AP_Q,
ap_o_mode _AP_O, int _AP_N>
INLINE void sc_trace(sc_core::sc_trace_file *tf, const ap_fixed<_AP_W, _AP_I, _AP_Q, _AP_O, _AP_N >&op, const std::string &name) {
tf->trace(sc_dt::sc_lv<_AP_W>(op.to_string(2).c_str()), name);
}
template<int _AP_W, int _AP_I, ap_q_mode _AP_Q,
ap_o_mode _AP_O, int _AP_N>
INLINE void sc_trace(sc_core::sc_trace_file *tf, const ap_ufixed<_AP_W, _AP_I, _AP_Q, _AP_O, _AP_N >&op, const std::string &name) {
tf->trace(sc_dt::sc_lv<_AP_W>(op.to_string(2).c_str()), name);
}
#endif /* #if defined(SYSTEMC_H) || defined(SYSTEMC_INCLUDED) */
#endif /* #ifndef __cplusplus */
#endif /* #ifndef __AESL_AP_SIM_H__ */
\ No newline at end of file
/*
* Copyright 2012 Xilinx, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef __AESL_GCC_AP_FIXED_H__
#define __AESL_GCC_AP_FIXED_H__
#ifndef __cplusplus
#error C++ is required to include this header file
#endif /* #ifndef __cplusplus */
#include <math.h>
#ifndef __AESL_APDT_IN_SCFLOW__
#include "etc/ap_int_sim.h"
#else
#include "../etc/ap_private.h"
#endif /* #ifndef __AESL_APDT_IN_SCFLOW__ */
#define FLOAT_MAN 23
#define FLOAT_EXP 8
#define DOUBLE_MAN 52
#define DOUBLE_EXP 11
// #define DOUBLE_MAN_MASK (~0ULL >> (64-DOUBLE_MAN-2))
#define DOUBLE_MAN_MASK 0x3fffffffffffffULL
#define BIAS(e) ((1ULL<<(e-1))-1)
#define FLOAT_BIAS BIAS(FLOAT_EXP)
#define DOUBLE_BIAS BIAS(DOUBLE_EXP)
/// Forward declaration.
template<int _AP_W, int _AP_I, bool _AP_S, ap_q_mode _AP_Q, ap_o_mode _AP_O, int _AP_N> struct ap_fixed_base;
///Proxy class, which allows bit selection to be used as both rvalue(for reading) and
//lvalue(for writing)
template<int _AP_W, int _AP_I, bool _AP_S, ap_q_mode _AP_Q, ap_o_mode _AP_O, int _AP_N>
struct af_bit_ref {
#ifdef _MSC_VER
#pragma warning(disable: 4521 4522)
#endif /* #ifdef _MSC_VER */
ap_fixed_base<_AP_W,_AP_I,_AP_S,_AP_Q,_AP_O, _AP_N>& d_bv;
int d_index;
public:
INLINE af_bit_ref(const af_bit_ref<_AP_W, _AP_I, _AP_S,
_AP_Q, _AP_O, _AP_N>& ref):
d_bv(ref.d_bv), d_index(ref.d_index) {}
INLINE af_bit_ref(ap_fixed_base<_AP_W,_AP_I,_AP_S,_AP_Q,_AP_O, _AP_N>* bv, int index=0):
d_bv(*bv),d_index(index) {}
INLINE operator bool() const {
return d_bv.V[d_index];
}
INLINE af_bit_ref& operator=(unsigned long long val) {
if (val)
d_bv.V.set(d_index);
else
d_bv.V.clear(d_index);
return *this;
}
template<int _AP_W2, bool _AP_S2>
INLINE af_bit_ref& operator =(const ap_private<_AP_W2,_AP_S2>& val) {
return operator=(val!=0);
}
INLINE af_bit_ref& operator =(const af_bit_ref<_AP_W, _AP_I, _AP_S,
_AP_Q, _AP_O, _AP_N>& val) {
return operator=((unsigned long long)(bool)val);
}
template<int _AP_W2, int _AP_I2, bool _AP_S2, ap_q_mode _AP_Q2, ap_o_mode _AP_O2, int _AP_N2>
INLINE af_bit_ref operator=(const af_bit_ref<_AP_W2,_AP_I2,_AP_S2,_AP_Q2,_AP_O2, _AP_N2>& val) {
return operator=((unsigned long long)(bool)val);
}
template<int _AP_W2, bool _AP_S2>
INLINE af_bit_ref& operator = ( const ap_bit_ref<_AP_W2, _AP_S2> &val) {
return operator =((unsigned long long) (bool) val);
}
template<int _AP_W2, bool _AP_S2>
INLINE af_bit_ref& operator = ( const ap_range_ref<_AP_W2,_AP_S2>& val) {
return operator =((const ap_private<_AP_W2, false>) val);
}
template<int _AP_W2, int _AP_I2, bool _AP_S2,
ap_q_mode _AP_Q2, ap_o_mode _AP_O2, int _AP_N2>
INLINE af_bit_ref& operator= (const af_range_ref<_AP_W2, _AP_I2, _AP_S2,
_AP_Q2, _AP_O2, _AP_N2>& val) {
return operator=((const ap_private<_AP_W2, false>)(val));
}
template<int _AP_W2, typename _AP_T2, int _AP_W3, typename _AP_T3>
INLINE af_bit_ref& operator= (const ap_concat_ref<_AP_W2, _AP_T3, _AP_W3, _AP_T3>& val) {
return operator=((const ap_private<_AP_W2 + _AP_W3, false>)(val));
}
template<int _AP_W2, int _AP_S2>
INLINE ap_concat_ref<1, af_bit_ref, _AP_W2, ap_private<_AP_W2, _AP_S2> >
operator, (ap_private<_AP_W2, _AP_S2>& op) {
return ap_concat_ref<1, af_bit_ref, _AP_W2,
ap_private<_AP_W2, _AP_S2> >(*this, op);
}
template<int _AP_W2, int _AP_S2>
INLINE ap_concat_ref<1, af_bit_ref, 1, ap_bit_ref<_AP_W2, _AP_S2> >
operator, (const ap_bit_ref<_AP_W2, _AP_S2> &op) {
return ap_concat_ref<1, af_bit_ref, 1,
ap_bit_ref<_AP_W2, _AP_S2> >(*this,
const_cast<ap_bit_ref<_AP_W2, _AP_S2>& >(op));
}
template<int _AP_W2, int _AP_S2>
INLINE ap_concat_ref<1, af_bit_ref, _AP_W2, ap_range_ref<_AP_W2, _AP_S2> >
operator, (const ap_range_ref<_AP_W2, _AP_S2> &op) {
return ap_concat_ref<1, af_bit_ref, _AP_W2,
ap_range_ref<_AP_W2, _AP_S2> >(*this,
const_cast<ap_range_ref<_AP_W2, _AP_S2>& >(op));
}
template<int _AP_W2, typename _AP_T2, int _AP_W3, typename _AP_T3>
INLINE ap_concat_ref<1, af_bit_ref, _AP_W2 + _AP_W3,
ap_concat_ref<_AP_W2, _AP_T2, _AP_W3, _AP_T3> >
operator, (const ap_concat_ref<_AP_W2, _AP_T2, _AP_W3, _AP_T3> &op) {
return ap_concat_ref<1, af_bit_ref, _AP_W2 + _AP_W3,
ap_concat_ref<_AP_W2, _AP_T2, _AP_W3, _AP_T3> >(*this,
const_cast<ap_concat_ref<_AP_W2, _AP_T2, _AP_W3, _AP_T3>& >(op));
}
template<int _AP_W2, int _AP_I2, bool _AP_S2,
ap_q_mode _AP_Q2, ap_o_mode _AP_O2, int _AP_N2>
INLINE ap_concat_ref<1, af_bit_ref, _AP_W2,
af_range_ref<_AP_W2, _AP_I2, _AP_S2, _AP_Q2, _AP_O2, _AP_N2> >
operator, (const af_range_ref<_AP_W2, _AP_I2, _AP_S2, _AP_Q2,
_AP_O2, _AP_N2> &op) {
return ap_concat_ref<1, af_bit_ref, _AP_W2,
af_range_ref<_AP_W2, _AP_I2, _AP_S2, _AP_Q2, _AP_O2,
_AP_N2> >(*this, const_cast<af_range_ref<_AP_W2, _AP_I2,
_AP_S2, _AP_Q2, _AP_O2,_AP_N2>& >(op));
}
template<int _AP_W2, int _AP_I2, bool _AP_S2,
ap_q_mode _AP_Q2, ap_o_mode _AP_O2, int _AP_N2>
INLINE ap_concat_ref<1, af_bit_ref, 1,
af_bit_ref<_AP_W2, _AP_I2, _AP_S2, _AP_Q2, _AP_O2, _AP_N2> >
operator, (const af_bit_ref<_AP_W2, _AP_I2, _AP_S2, _AP_Q2,
_AP_O2, _AP_N2> &op) {
return ap_concat_ref<1, af_bit_ref, 1,
af_bit_ref<_AP_W2, _AP_I2, _AP_S2, _AP_Q2, _AP_O2, _AP_N2> >(*this,
const_cast<af_bit_ref<_AP_W2, _AP_I2, _AP_S2, _AP_Q2,
_AP_O2, _AP_N2>& >(op));
}
template<int _AP_W2, int _AP_I2, bool _AP_S2,
ap_q_mode _AP_Q2, ap_o_mode _AP_O2, int _AP_N2>
INLINE bool operator == (const af_bit_ref<_AP_W2, _AP_I2,
_AP_S2, _AP_Q2, _AP_O2, _AP_N2>& op) {
return get() == op.get();
}
template<int _AP_W2, int _AP_I2, bool _AP_S2,
ap_q_mode _AP_Q2, ap_o_mode _AP_O2, int _AP_N2>
INLINE bool operator != (const af_bit_ref<_AP_W2, _AP_I2,
_AP_S2, _AP_Q2, _AP_O2, _AP_N2>& op) {
return get() != op.get();
}
INLINE bool operator ~ () const {
bool bit = (d_bv.V)[d_index];
return bit ? false : true;
}
INLINE int length() const {
return 1;
}
INLINE bool get() {
return d_bv.V[d_index];
}
INLINE bool get() const {
return d_bv.V[d_index];
}
INLINE std::string to_string() const {
return d_bv.V[d_index] ? "1" : "0";
}
};
///Range(slice) reference
//------------------------------------------------------------
template<int _AP_W, int _AP_I, bool _AP_S, ap_q_mode _AP_Q, ap_o_mode _AP_O, int _AP_N>
struct af_range_ref {
#ifdef _MSC_VER
#pragma warning(disable: 4521 4522)
#endif /* #ifdef _MSC_VER */
ap_fixed_base<_AP_W, _AP_I, _AP_S, _AP_Q, _AP_O, _AP_N> &d_bv;
int l_index;
int h_index;
public:
INLINE af_range_ref(const af_range_ref<_AP_W, _AP_I, _AP_S,
_AP_Q, _AP_O, _AP_N>& ref):
d_bv(ref.d_bv), l_index(ref.l_index), h_index(ref.h_index) {}
INLINE af_range_ref(ap_fixed_base<_AP_W, _AP_I, _AP_S, _AP_Q, _AP_O, _AP_N>* bv, int h, int l):
d_bv(*bv),l_index(l),h_index(h) {
//if (h < l)
// fprintf(stderr,
//"Warning! The bits selected will be returned in reverse order\n");
}
INLINE operator ap_private<_AP_W, false> () const {
if (h_index >= l_index) {
ap_private<_AP_W, false> val(d_bv.V);
ap_private<_AP_W,false> mask(-1);
mask>>=_AP_W-(h_index-l_index+1);
val>>=l_index;
return val&=mask;
} else {
ap_private<_AP_W, false> val = 0;
for (int i=0, j=l_index;j>=0&&j>=h_index;j--,i++)
if ((d_bv.V)[j]) val.set(i);
return val;
}
}
INLINE operator unsigned long long() const {
return get().to_uint64();
}
template<int _AP_W2,bool _AP_S2>
INLINE af_range_ref& operator =(const ap_private<_AP_W2,_AP_S2>& val) {
ap_private<_AP_W, false> vval= ap_private<_AP_W, false>(val);
if (l_index > h_index) {
for (int i=0, j=l_index;j>=0&&j>=h_index;j--,i++)
vval[i]? d_bv.V.set(j):d_bv.V.clear(j);
} else {
ap_private<_AP_W,false> mask(-1);
if (l_index>0) {
mask<<=l_index;
vval<<=l_index;
}
if (h_index<_AP_W-1) {
ap_private<_AP_W,false> mask2(-1);
mask2>>=_AP_W-h_index-1;
mask&=mask2;
vval&=mask2;
}
mask.flip();
d_bv.V &= mask;
d_bv.V |= vval;
}
return *this;
}
INLINE af_range_ref& operator = (unsigned long long val) {
const ap_private<_AP_W, false> tmpVal(val);
return operator = (tmpVal);
}
template<int _AP_W3, typename _AP_T3, int _AP_W4, typename _AP_T4>
INLINE af_range_ref& operator =
(const ap_concat_ref <_AP_W3, _AP_T3, _AP_W4, _AP_T4>& val) {
const ap_private<_AP_W, false> tmpVal(val);
return operator = (tmpVal);
}
template <int _AP_W3, bool _AP_S3>
INLINE af_range_ref& operator =(const ap_bit_ref<_AP_W3, _AP_S3>& val) {
const ap_private<_AP_W, false> tmpVal(val);
return operator = (tmpVal);
}
template <int _AP_W3, bool _AP_S3>
INLINE af_range_ref& operator =(const ap_range_ref<_AP_W3,_AP_S3>& val) {
const ap_private<_AP_W, false> tmpVal(val);
return operator =(tmpVal);
}
template<int _AP_W2, int _AP_I2, bool _AP_S2, ap_q_mode _AP_Q2,
ap_o_mode _AP_O2, int _AP_N2>
INLINE af_range_ref& operator= (const af_range_ref<_AP_W2, _AP_I2, _AP_S2, _AP_Q2, _AP_O2, _AP_N2>& val) {
const ap_private<_AP_W2, false> tmp= val.get();
return operator = (tmp);
}
INLINE af_range_ref& operator= (const af_range_ref<_AP_W, _AP_I, _AP_S,
_AP_Q, _AP_O, _AP_N>& val) {
const ap_private<_AP_W, false> tmp= val.get();
return operator = (tmp);
}
template<int _AP_W2, int _AP_I2, bool _AP_S2,
ap_q_mode _AP_Q2, ap_o_mode _AP_O2, int _AP_N2>
INLINE af_range_ref& operator= (const ap_fixed_base<_AP_W2,
_AP_I2, _AP_S2, _AP_Q2, _AP_O2, _AP_N2>& val) {
return operator=(val.to_ap_private());
}
template<int _AP_W2, bool _AP_S2>
INLINE bool operator == (const ap_range_ref<_AP_W2, _AP_S2>& op2) {
ap_private<_AP_W,false> lhs=get();
ap_private<_AP_W2,false> rhs=op2.get();
return lhs==rhs;
}
template<int _AP_W2, bool _AP_S2>
INLINE bool operator != (const ap_range_ref<_AP_W2, _AP_S2>& op2) {
ap_private<_AP_W,false> lhs=get();
ap_private<_AP_W2,false> rhs=op2.get();
return lhs!=rhs;
}
template<int _AP_W2, bool _AP_S2>
INLINE bool operator > (const ap_range_ref<_AP_W2, _AP_S2>& op2) {
ap_private<_AP_W,false> lhs=get();
ap_private<_AP_W2,false> rhs=op2.get();
return lhs>rhs;
}
template<int _AP_W2, bool _AP_S2>
INLINE bool operator >= (const ap_range_ref<_AP_W2, _AP_S2>& op2) {
ap_private<_AP_W,false> lhs=get();
ap_private<_AP_W2,false> rhs=op2.get();
return lhs>=rhs;
}
template<int _AP_W2, bool _AP_S2>
INLINE bool operator < (const ap_range_ref<_AP_W2, _AP_S2>& op2) {
ap_private<_AP_W,false> lhs=get();
ap_private<_AP_W2,false> rhs=op2.get();
return lhs<rhs;
}
template<int _AP_W2, bool _AP_S2>
INLINE bool operator <= (const ap_range_ref<_AP_W2, _AP_S2>& op2) {
ap_private<_AP_W,false> lhs=get();
ap_private<_AP_W2,false> rhs=op2.get();
return lhs<=rhs;
}
template<int _AP_W2, int _AP_I2, bool _AP_S2, ap_q_mode _AP_Q2, ap_o_mode _AP_O2, int _AP_N2>
INLINE bool operator == (const af_range_ref<_AP_W2, _AP_I2, _AP_S2, _AP_Q2, _AP_O2, _AP_N2>& op2) {
ap_private<_AP_W,false> lhs=get();
ap_private<_AP_W2,false> rhs=op2.get();
return lhs==rhs;
}
template<int _AP_W2, int _AP_I2, bool _AP_S2, ap_q_mode _AP_Q2, ap_o_mode _AP_O2, int _AP_N2>
INLINE bool operator != (const af_range_ref<_AP_W2, _AP_I2, _AP_S2, _AP_Q2, _AP_O2, _AP_N2>& op2) {
ap_private<_AP_W,false> lhs=get();
ap_private<_AP_W2,false> rhs=op2.get();
return lhs!=rhs;
}
template<int _AP_W2, int _AP_I2, bool _AP_S2, ap_q_mode _AP_Q2, ap_o_mode _AP_O2, int _AP_N2>
INLINE bool operator > (const af_range_ref<_AP_W2, _AP_I2, _AP_S2, _AP_Q2, _AP_O2, _AP_N2>& op2) {
ap_private<_AP_W,false> lhs=get();
ap_private<_AP_W2,false> rhs=op2.get();
return lhs>rhs;
}
template<int _AP_W2, int _AP_I2, bool _AP_S2, ap_q_mode _AP_Q2, ap_o_mode _AP_O2, int _AP_N2>
INLINE bool operator >= (const af_range_ref<_AP_W2, _AP_I2, _AP_S2, _AP_Q2, _AP_O2, _AP_N2>& op2) {
ap_private<_AP_W,false> lhs=get();
ap_private<_AP_W2,false> rhs=op2.get();
return lhs>=rhs;
}
template<int _AP_W2, int _AP_I2, bool _AP_S2, ap_q_mode _AP_Q2, ap_o_mode _AP_O2, int _AP_N2>
INLINE bool operator < (const af_range_ref<_AP_W2, _AP_I2, _AP_S2, _AP_Q2, _AP_O2, _AP_N2>& op2) {
ap_private<_AP_W,false> lhs=get();
ap_private<_AP_W2,false> rhs=op2.get();
return lhs<rhs;
}
template<int _AP_W2, int _AP_I2, bool _AP_S2, ap_q_mode _AP_Q2, ap_o_mode _AP_O2, int _AP_N2>
INLINE bool operator <= (const af_range_ref<_AP_W2, _AP_I2, _AP_S2, _AP_Q2, _AP_O2, _AP_N2>& op2) {
ap_private<_AP_W,false> lhs=get();
ap_private<_AP_W2,false> rhs=op2.get();
return lhs<=rhs;
}
template<int _AP_W2>
INLINE void set(const ap_private<_AP_W2,false>& val) {
ap_private<_AP_W,_AP_S> vval=val;
if (l_index>h_index) {
for (int i=0, j=l_index;j>=0&&j>=h_index;j--,i++)
vval[i]? d_bv.V.set(j):d_bv.V.clear(j);
} else {
ap_private<_AP_W,_AP_S> mask(-1);
if (l_index>0) {
ap_private<_AP_W,false> mask1(-1);
mask1>>=_AP_W-l_index;
mask1.flip();
mask=mask1;
//vval&=mask1;
vval<<=l_index;
}
if (h_index<_AP_W-1) {
ap_private<_AP_W,false> mask2(-1);
mask2<<=h_index+1;
mask2.flip();
mask&=mask2;
vval&=mask2;
}
mask.flip();
d_bv&=mask;
d_bv|=vval;
}
}
INLINE ap_private<_AP_W,false> get() const {
if (h_index<l_index) {
ap_private<_AP_W, false> val(0);
for (int i=0, j=l_index;j>=0&&j>=h_index;j--,i++)
if ((d_bv.V)[j]) val.set(i);
return val;
} else {
ap_private<_AP_W, false> val = ap_private<_AP_W,false>(d_bv.V);
val>>= l_index;
if (h_index<_AP_W-1)
{
ap_private<_AP_W,false> mask(-1);
mask>>=_AP_W-(h_index-l_index+1);
val&=mask;
}
return val;
}
}
template<int _AP_W2, int _AP_S2>
INLINE ap_concat_ref<_AP_W, af_range_ref, _AP_W2, ap_private<_AP_W2, _AP_S2> >
operator, (ap_private<_AP_W2, _AP_S2>& op) {
return ap_concat_ref<_AP_W, af_range_ref, _AP_W2,
ap_private<_AP_W2, _AP_S2> >(*this, op);
}
template<int _AP_W2, int _AP_S2>
INLINE ap_concat_ref<_AP_W, af_range_ref, 1, ap_bit_ref<_AP_W2, _AP_S2> >
operator, (const ap_bit_ref<_AP_W2, _AP_S2> &op) {
return ap_concat_ref<_AP_W, af_range_ref, 1,
ap_bit_ref<_AP_W2, _AP_S2> >(*this,
const_cast<ap_bit_ref<_AP_W2, _AP_S2>& >(op));
}
template<int _AP_W2, int _AP_S2>
INLINE ap_concat_ref<_AP_W, af_range_ref, _AP_W2, ap_range_ref<_AP_W2, _AP_S2> >
operator, (const ap_range_ref<_AP_W2, _AP_S2> &op) {
return ap_concat_ref<_AP_W, af_range_ref, _AP_W2,
ap_range_ref<_AP_W2, _AP_S2> >(*this,
const_cast<ap_range_ref<_AP_W2, _AP_S2>& >(op));
}
template<int _AP_W2, typename _AP_T2, int _AP_W3, typename _AP_T3>
INLINE ap_concat_ref<_AP_W, af_range_ref, _AP_W2 + _AP_W3,
ap_concat_ref<_AP_W2, _AP_T2, _AP_W3, _AP_T3> >
operator, (const ap_concat_ref<_AP_W2, _AP_T2, _AP_W3, _AP_T3> &op) {
return ap_concat_ref<_AP_W, af_range_ref, _AP_W2 + _AP_W3,
ap_concat_ref<_AP_W2, _AP_T2, _AP_W3, _AP_T3> >(*this,
const_cast<ap_concat_ref<_AP_W2, _AP_T2, _AP_W3,
_AP_T3>& >(op));
}
template<int _AP_W2, int _AP_I2, bool _AP_S2,
ap_q_mode _AP_Q2, ap_o_mode _AP_O2, int _AP_N2>
INLINE ap_concat_ref<_AP_W, af_range_ref, _AP_W2,
af_range_ref<_AP_W2, _AP_I2, _AP_S2, _AP_Q2, _AP_O2, _AP_N2> >
operator, (const af_range_ref<_AP_W2, _AP_I2, _AP_S2, _AP_Q2,
_AP_O2, _AP_N2> &op) {
return ap_concat_ref<_AP_W, af_range_ref, _AP_W2,
af_range_ref<_AP_W2, _AP_I2, _AP_S2, _AP_Q2, _AP_O2, _AP_N2> >(*this,
const_cast<af_range_ref<_AP_W2, _AP_I2, _AP_S2, _AP_Q2, _AP_O2,
_AP_N2>& > (op));
}
template<int _AP_W2, int _AP_I2, bool _AP_S2,
ap_q_mode _AP_Q2, ap_o_mode _AP_O2, int _AP_N2>
INLINE ap_concat_ref<_AP_W, af_range_ref, 1,
af_bit_ref<_AP_W2, _AP_I2, _AP_S2, _AP_Q2, _AP_O2, _AP_N2> >
operator, (const af_bit_ref<_AP_W2, _AP_I2, _AP_S2, _AP_Q2,
_AP_O2, _AP_N2> &op) {
return ap_concat_ref<_AP_W, af_range_ref, 1,
af_bit_ref<_AP_W2, _AP_I2, _AP_S2, _AP_Q2, _AP_O2, _AP_N2> >(*this,
const_cast<af_bit_ref<_AP_W2, _AP_I2, _AP_S2, _AP_Q2,
_AP_O2, _AP_N2>& >(op));
}
INLINE int length() const {
return h_index>=l_index?h_index-l_index+1:l_index-h_index+1;
}
INLINE int to_int() const {
ap_private<_AP_W,false> val=get();
return val.to_int();
}
INLINE unsigned int to_uint() const {
ap_private<_AP_W,false> val=get();
return val.to_uint();
}
INLINE long to_long() const {
ap_private<_AP_W,false> val=get();
return val.to_long();
}
INLINE unsigned long to_ulong() const {
ap_private<_AP_W,false> val=get();
return val.to_ulong();
}
INLINE ap_slong to_int64() const {
ap_private<_AP_W,false> val=get();
return val.to_int64();
}
INLINE ap_ulong to_uint64() const {
ap_private<_AP_W,false> val=get();
return val.to_uint64();
}
INLINE std::string to_string(uint8_t radix) const {
return get().to_string(radix);
}
};
//-----------------------------------------------------------------------------
///ap_fixed_base: AutoPilot fixed point
//-----------------------------------------------------------------------------
template<int _AP_W, int _AP_I, bool _AP_S=true, ap_q_mode _AP_Q=AP_TRN,
ap_o_mode _AP_O=AP_WRAP, int _AP_N=0>
struct ap_fixed_base {
#ifdef _MSC_VER
#pragma warning(disable: 4521 4522)
#endif /* #ifdef _MSC_VER */
public:
template<int _AP_W2, int _AP_I2, bool _AP_S2, ap_q_mode _AP_Q2, ap_o_mode _AP_O2, int _AP_N2> friend struct
ap_fixed_base;
template<int _AP_W2, int _AP_I2, bool _AP_S2, ap_q_mode _AP_Q2, ap_o_mode _AP_O2, int _AP_N2> friend struct
af_bit_ref;
INLINE void overflow_adjust(bool underflow, bool overflow,
bool lD, bool sign) {
if (!overflow && !underflow) return;
switch (_AP_O) {
case AP_WRAP:
if (_AP_N == 0)
return;
if (_AP_S) {
//signed SC_WRAP
//n_bits == 1;
if (_AP_N > 1) {
ap_private<_AP_W, _AP_S> mask(-1);
if (_AP_N >= _AP_W) mask = 0;
else mask.lshr(_AP_N);
if (sign)
V &= mask;
else
V |= ~mask;
}
sign ? V.set(_AP_W - 1) : V.clear(_AP_W - 1);
} else {
//unsigned SC_WRAP
ap_private<_AP_W, _AP_S> mask(-1);
if (_AP_N >= _AP_W) mask = 0;
else mask.lshr(_AP_N);
mask.flip();
V |= mask;
}
break;
case AP_SAT_ZERO:
V.clear();
break;
case AP_WRAP_SM:
{
bool Ro = ap_private_ops::get<_AP_W, _AP_S, _AP_W -1>(V); // V[_AP_W -1];
if (_AP_N == 0) {
if (lD != Ro) {
V.flip();
lD ? ap_private_ops::set<_AP_W, _AP_S, _AP_W - 1>(V) :
ap_private_ops::clear<_AP_W, _AP_S, _AP_W - 1>(V);
}
} else {
if (_AP_N == 1 && sign != Ro) {
V.flip();
} else if (_AP_N > 1) {
bool lNo = ap_private_ops::get<_AP_W, _AP_S, _AP_W - _AP_N> (V); // V[_AP_W - _AP_N];
if (lNo == sign)
V.flip();
ap_private<_AP_W, false> mask(-1);
if (_AP_N >= _AP_W) mask = 0;
else mask.lshr(_AP_N);
if (sign)
V &= mask;
else
V |= mask.flip();
sign ? ap_private_ops::set<_AP_W, _AP_S, _AP_W - 1>(V) : ap_private_ops::clear<_AP_W, _AP_S, _AP_W - 1>(V);
}
}
}
break;
default:
if (_AP_S) {
if (overflow) {
V.set(); ap_private_ops::clear<_AP_W, _AP_S, _AP_W-1>(V);
} else if (underflow) {
V.clear();
ap_private_ops::set<_AP_W, _AP_S, _AP_W-1>(V);
if (_AP_O == AP_SAT_SYM)
ap_private_ops::set<_AP_W, _AP_S, 0>(V);
}
} else {
if (overflow)
V.set();
else if (underflow)
V.clear();
}
}
}
INLINE bool quantization_adjust(bool qb, bool r, bool s) {
bool carry=ap_private_ops::get<_AP_W, _AP_S, _AP_W-1>(V);
switch (_AP_Q) {
case AP_TRN:
return false;
case AP_RND_ZERO:
qb &= s || r;
break;
case AP_RND_MIN_INF:
qb &= r;
break;
case AP_RND_INF:
qb &= !s || r;
break;
case AP_RND_CONV:
qb &= ap_private_ops::get<_AP_W, _AP_S, 0>(V) || r;
break;
case AP_TRN_ZERO:
qb = s && ( qb || r );
break;
default:;
}
if (qb) ++V;
//only when old V[_AP_W-1]==1 && new V[_AP_W-1]==0
return carry && !(ap_private_ops::get<_AP_W, _AP_S, _AP_W-1>(V)); //(!V[_AP_W-1]);
}
template<int _AP_W2, int _AP_I2, bool _AP_S2>
struct RType {
enum {
_AP_F=_AP_W-_AP_I,
F2=_AP_W2-_AP_I2,
mult_w = _AP_W+_AP_W2,
mult_i = _AP_I+_AP_I2,
mult_s = _AP_S||_AP_S2,
plus_w = AP_MAX(_AP_I+(_AP_S2&&!_AP_S),_AP_I2+(_AP_S&&!_AP_S2))+1+AP_MAX(_AP_F,F2),
plus_i = AP_MAX(_AP_I+(_AP_S2&&!_AP_S),_AP_I2+(_AP_S&&!_AP_S2))+1,
plus_s = _AP_S||_AP_S2,
minus_w = AP_MAX(_AP_I+(_AP_S2&&!_AP_S),_AP_I2+(_AP_S&&!_AP_S2))+1+AP_MAX(_AP_F,F2),
minus_i = AP_MAX(_AP_I+(_AP_S2&&!_AP_S),_AP_I2+(_AP_S&&!_AP_S2))+1,
minus_s = true,
#ifndef __SC_COMPATIBLE__
div_w = _AP_W + AP_MAX(_AP_W2 - _AP_I2, 0) + _AP_S2,
#else
div_w = _AP_W + AP_MAX(_AP_W2 - _AP_I2, 0) + _AP_S2 + AP_MAX(_AP_I2, 0),
#endif /* #ifndef __SC_COMPATIBLE__ */
div_i = _AP_I + (_AP_W2-_AP_I2) + _AP_S2,
div_s = _AP_S||_AP_S2,
logic_w = AP_MAX(_AP_I+(_AP_S2&&!_AP_S),_AP_I2+(_AP_S&&!_AP_S2))+AP_MAX(_AP_F,F2),
logic_i = AP_MAX(_AP_I+(_AP_S2&&!_AP_S),_AP_I2+(_AP_S&&!_AP_S2)),
logic_s = _AP_S||_AP_S2
};
typedef ap_fixed_base<mult_w, mult_i, mult_s> mult;
typedef ap_fixed_base<plus_w, plus_i, plus_s> plus;
typedef ap_fixed_base<minus_w, minus_i, minus_s> minus;
typedef ap_fixed_base<logic_w, logic_i, logic_s> logic;
typedef ap_fixed_base<div_w, div_i, div_s> div;
typedef ap_fixed_base<_AP_W, _AP_I, _AP_S> arg1;
};
INLINE void report() {
#if 0
if (_AP_W > 1024 && _AP_W <= 4096) {
fprintf(stderr, "[W] W=%d is out of bound (1<=W<=1024):"
" for synthesis, please define macro AP_INT_TYPE_EXT(N) to"
" extend the valid range.\n", _AP_W);
} else
#endif /* #if 0 */
if (_AP_W > MAX_MODE(AP_INT_MAX_W) * 1024) {
fprintf(stderr, "[E] ap_%sfixed<%d, ...>: Bitwidth exceeds the "
"default max value %d. Please use macro "
"AP_INT_MAX_W to set a larger max value.\n",
_AP_S?"":"u", _AP_W,
MAX_MODE(AP_INT_MAX_W) * 1024);
exit(1);
}
}
/// Constructors.
// -------------------------------------------------------------------------
#if 0
#ifdef __SC_COMPATIBLE__
INLINE ap_fixed_base():V(uint32_t(_AP_W), uint64_t(0)) {}
#else
INLINE ap_fixed_base():V(uint32_t(_AP_W)) {}
#endif /* #ifdef __SC_COMPATIBLE__ */
#else
INLINE ap_fixed_base():V(0) {}
#endif /* #if 0 */
// INLINE ap_fixed_base():V() {}
// INLINE explicit ap_fixed_base(const ap_private<_AP_W+_AP_I, _AP_S>& _V):V(_V) {}
INLINE ap_fixed_base(const ap_fixed_base& op):V(op.V) {}
template<int _AP_W2, int _AP_I2, bool _AP_S2, ap_q_mode _AP_Q2, ap_o_mode _AP_O2, int _AP_N2>
INLINE ap_fixed_base(const ap_fixed_base<_AP_W2,_AP_I2,_AP_S2,_AP_Q2,_AP_O2, _AP_N2>& op):V(0) {
enum {N2=_AP_W2,_AP_F=_AP_W-_AP_I,F2=_AP_W2-_AP_I2,QUAN_INC=F2>_AP_F && !(_AP_Q==AP_TRN ||
(_AP_Q==AP_TRN_ZERO && !_AP_S2))};
if (!op) return;
bool carry=false;
//handle quantization
enum { sh_amt =(F2>_AP_F)?F2-_AP_F:_AP_F-F2};
const ap_private<_AP_W2, _AP_S2>& val = op.V;
bool neg_src=val.isNegative();
if (F2==_AP_F)
V=val;
else if (F2>_AP_F) {
if (sh_amt >= _AP_W2)
V = neg_src ? -1 : 0;
else
V = _AP_S2?val.ashr(sh_amt):val.lshr(sh_amt);
if (_AP_Q!=AP_TRN && !(_AP_Q==AP_TRN_ZERO && !_AP_S2)) {
bool qb = false;
if (F2-_AP_F>_AP_W2)
qb = neg_src;
else
qb = ap_private_ops::get<_AP_W2, _AP_S2, F2-_AP_F-1>(val);
bool r=false;
enum { pos3 = F2-_AP_F-2};
if (pos3>=_AP_W2-1)
r=val!=0;
else if (pos3>=0)
r = (val<<(_AP_W2-1-pos3))!=0;
carry = quantization_adjust(qb,r,neg_src);
}
} else { //no quantization
if (sh_amt < _AP_W) {
V=val;
V <<= sh_amt;
}
}
//hanle overflow/underflow
if ((_AP_O!=AP_WRAP || _AP_N != 0) &&
((!_AP_S && _AP_S2) || _AP_I-_AP_S <
_AP_I2 - _AP_S2 + (QUAN_INC|| (_AP_S2 &&
_AP_O==AP_SAT_SYM)))) {//saturation
bool deleted_zeros = _AP_S2?true:!carry,
deleted_ones = true;
bool lD=(_AP_I2>_AP_I && _AP_W2-_AP_I2+_AP_I>=0) &&
ap_private_ops::get<_AP_W2, _AP_S2, _AP_W2-_AP_I2+_AP_I>(val);
enum { pos1=F2-_AP_F+_AP_W, pos2=F2-_AP_F+_AP_W+1};
if (pos1 < _AP_W2) {
bool Range1_all_ones= true;
bool Range1_all_zeros= true;
if (pos1 >= 0) {
enum { __W = (_AP_W2-pos1) > 0 ? (_AP_W2-pos1) : 1 };
const ap_private<__W, _AP_S2> Range1=ap_private<__W, _AP_S2>(val.lshr(pos1));
Range1_all_ones=Range1.isAllOnesValue();
Range1_all_zeros=Range1.isMinValue();
} else {
Range1_all_ones=false;
Range1_all_zeros=val.isMinValue();
}
bool Range2_all_ones=true;
if (pos2<_AP_W2 && pos2>=0) {
enum { __W = (_AP_W2-pos2)>0 ? (_AP_W2-pos2) : 1};
ap_private<__W, true> Range2=ap_private<__W, true>(val.lshr(pos2));
Range2_all_ones=Range2.isAllOnesValue();
} else if (pos2<0)
Range2_all_ones=false;
deleted_zeros=deleted_zeros && (carry?Range1_all_ones:Range1_all_zeros);
deleted_ones=carry?Range2_all_ones&&(F2-_AP_F+_AP_W<0||!lD)
:Range1_all_ones;
neg_src= neg_src&&!(carry && Range1_all_ones);
} else
neg_src = neg_src && V[_AP_W-1];
bool neg_trg= V.isNegative();
bool overflow=(neg_trg||!deleted_zeros) && !val.isNegative();
bool underflow=(!neg_trg||!deleted_ones)&&neg_src;
//printf("neg_src = %d, neg_trg = %d, deleted_zeros = %d,
// deleted_ones = %d, overflow = %d, underflow = %d\n",
// neg_src, neg_trg, deleted_zeros, deleted_ones,
// overflow, underflow);
if (_AP_O==AP_SAT_SYM && _AP_S2 && _AP_S)
underflow |= neg_src && (_AP_W>1?V.isMinSignedValue():true);
overflow_adjust(underflow, overflow, lD, neg_src);
}
report();
}
template<int _AP_W2, int _AP_I2, bool _AP_S2, ap_q_mode _AP_Q2, ap_o_mode _AP_O2, int _AP_N2>
INLINE ap_fixed_base(const volatile ap_fixed_base<_AP_W2,_AP_I2,
_AP_S2,_AP_Q2,_AP_O2, _AP_N2> &op) : V(op.V) {
*this = const_cast<ap_fixed_base<_AP_W2,_AP_I2,
_AP_S2,_AP_Q2,_AP_O2, _AP_N2>&>(op);
}
template<int _AP_W2, bool _AP_S2>
INLINE ap_fixed_base(const ap_private<_AP_W2,_AP_S2>& op) {
ap_fixed_base<_AP_W2,_AP_W2,_AP_S2> f_op;
f_op.V=op;
*this = f_op;
}
INLINE ap_fixed_base(bool b) {
*this=(ap_private<1,false>)b;
report();
}
INLINE ap_fixed_base(char b) {
*this=(ap_private<8,false>)b;
report();
}
INLINE ap_fixed_base(signed char b) {
*this=(ap_private<8,true>)b;
report();
}
INLINE ap_fixed_base(unsigned char b) {
*this=(ap_private<8,false>)b;
report();
}
INLINE ap_fixed_base(signed short b) {
*this=(ap_private<16,true>)b;
report();
}
INLINE ap_fixed_base(unsigned short b) {
*this=(ap_private<16,false>)b;
report();
}
INLINE ap_fixed_base(signed int b) {
*this=(ap_private<32,true>)b;
report();
}
INLINE ap_fixed_base(unsigned int b) {
*this=(ap_private<32,false>)b;
report();
}
# if defined __x86_64__
INLINE ap_fixed_base(signed long b) {
*this=(ap_private<64,true>)b;
report();
}
INLINE ap_fixed_base(unsigned long b) {
*this=(ap_private<64,false>)b;
report();
}
# else
INLINE ap_fixed_base(signed long b) {
*this=(ap_private<32,true>)b;
report();
}
INLINE ap_fixed_base(unsigned long b) {
*this=(ap_private<32,false>)b;
report();
}
# endif
INLINE ap_fixed_base(ap_slong b) {
*this=(ap_private<64,true>)b;
report();
}
INLINE ap_fixed_base(ap_ulong b) {
*this=(ap_private<64,false>)b;
report();
}
#if 1
INLINE ap_fixed_base(const char* val):V(0) {
ap_private<_AP_W, _AP_S> Tmp(val);
V = Tmp;
}
INLINE ap_fixed_base(const char* val, signed char rd): V(0) {
ap_private<_AP_W, _AP_S> Tmp(val, rd);
V = Tmp;
}
#endif
INLINE ap_fixed_base(const std::string& val) {
ap_private<_AP_W, _AP_S> Tmp(val, 2);
V = Tmp;
report();
}
template<int _AP_W2, bool _AP_S2>
INLINE ap_fixed_base(const ap_bit_ref<_AP_W2, _AP_S2>& op) {
*this = ((bool)op);
report();
}
template<int _AP_W2, bool _AP_S2>
INLINE ap_fixed_base(const ap_range_ref<_AP_W2, _AP_S2>& op) {
*this = ap_private<_AP_W2, _AP_S2>(op);
report();
}
template<int _AP_W2, typename _AP_T2, int _AP_W3, typename _AP_T3>
INLINE ap_fixed_base(const ap_concat_ref<_AP_W2, _AP_T2, _AP_W3, _AP_T3>& op) {
*this = ((const ap_private<_AP_W2 + _AP_W3, false>&)(op));
report();
}
template<int _AP_W2, int _AP_I2, bool _AP_S2,
ap_q_mode _AP_Q2, ap_o_mode _AP_O2, int _AP_N2>
INLINE ap_fixed_base(const af_bit_ref<_AP_W2, _AP_I2, _AP_S2, _AP_Q2, _AP_O2, _AP_N2>& op) {
*this = (bool(op));
report();
}
template<int _AP_W2, int _AP_I2, bool _AP_S2,
ap_q_mode _AP_Q2, ap_o_mode _AP_O2, int _AP_N2>
INLINE ap_fixed_base(const af_range_ref<_AP_W2, _AP_I2, _AP_S2, _AP_Q2, _AP_O2, _AP_N2>& op) {
*this = (ap_private<_AP_W2, false>(op));
report();
}
//helper function
INLINE unsigned long long doubleToRawBits(double pf)const {
union {
unsigned long long __L;
double __D;
}LD;
LD.__D=pf;
return LD.__L;
}
INLINE double rawBitsToDouble(unsigned long long pi) const {
union {
unsigned long long __L;
double __D;
}LD;
LD.__L=pi;
return LD.__D;
}
INLINE float rawBitsToFloat(uint32_t pi) const {
union {
uint32_t __L;
float __D;
}LD;
LD.__L = pi;
return LD.__D;
}
INLINE ap_fixed_base(double d):V(0) {
if (!d) return;
const bool isneg=d<0;
const uint64_t ireg=doubleToRawBits(isneg?-d:d);
if ((ireg&0x7fffffffffffffffULL)!=0) {
const int32_t exp=(((ireg)>>DOUBLE_MAN)&0x07ff)-DOUBLE_BIAS;
ap_private<DOUBLE_MAN+2, true> man = ireg & DOUBLE_MAN_MASK;
man.clear(DOUBLE_MAN+1);
man.set(DOUBLE_MAN);
if (isneg) {
man.flip();
man++;
}
enum {_AP_S2=true, _AP_W2=DOUBLE_MAN+2,_AP_F=_AP_W -_AP_I };
const int _AP_I2=exp+2;
const int F2=_AP_W2-_AP_I2;
const bool QUAN_INC=F2>_AP_F && !(_AP_Q==AP_TRN || (_AP_Q==AP_TRN_ZERO &&
!_AP_S2));
bool carry=false;
//handle quantization
const unsigned sh_amt=abs(F2-_AP_F); // sh_amt = F2>_AP_F ? F2 -_AP_F : _AP_F-F2;
if (F2==_AP_F )
V=man;
else if (F2>_AP_F) {
if (sh_amt >= DOUBLE_MAN+2)
V=isneg?-1:0;
else
V=(man>>sh_amt) | ((man & 1ULL<<(DOUBLE_MAN+1)) ? (DOUBLE_MAN_MASK>>(DOUBLE_MAN+2-sh_amt) <<(DOUBLE_MAN+2-sh_amt)):0);
if (_AP_Q!=AP_TRN && !(_AP_Q==AP_TRN_ZERO && !_AP_S2)) {
const bool qb=((F2-_AP_F > DOUBLE_MAN+2) ? isneg : (man & (1ULL<<(F2-_AP_F-1))) != 0);
const int pos3=F2-_AP_F-2;
const bool r = (pos3>= 0) ? (man << AP_MAX(0, _AP_W2-pos3-1)& DOUBLE_MAN_MASK)!=0 : false;
carry = quantization_adjust(qb,r,isneg);
}
}
else { //no quantization
// V=man;
if (sh_amt < _AP_W) {
V = man;
V <<= sh_amt;
}
}
//handle overflow/underflow
if ((_AP_O != AP_WRAP || _AP_N != 0) &&
((!_AP_S && _AP_S2) || _AP_I-_AP_S <
_AP_I2-_AP_S2+(QUAN_INC|| (_AP_S2 &&
_AP_O==AP_SAT_SYM)) )) {// saturation
bool deleted_zeros = _AP_S2?true:!carry,
deleted_ones = true;
bool neg_src;
const bool lD=(_AP_I2>_AP_I) && (_AP_W2-_AP_I2+_AP_I>=0) && (man & (1ULL <<(DOUBLE_MAN+2-_AP_I2+_AP_I)));
int pos1=F2+_AP_W-_AP_F;
if (pos1 < _AP_W2) {
int pos2=pos1+1;
bool Range1_all_ones=true;
bool Range1_all_zeros=true;
if (pos1>=0) {
ap_private<_AP_W,_AP_S> Range1=
ap_private<_AP_W,_AP_S>((man >> pos1) | ((1ULL<<(DOUBLE_MAN+1)&man) ? (DOUBLE_MAN_MASK >> (DOUBLE_MAN+2-pos1) <<(DOUBLE_MAN+2-pos1)):0));
Range1_all_ones = Range1.isAllOnesValue(); // Range1.isAllOnesValue();
Range1_all_zeros = Range1.isMinValue(); // Range1.isMinValue();
} else {
Range1_all_ones=false;
Range1_all_zeros = man==0; // man.isMinValue();
}
bool Range2_all_ones=true;
if (pos2<_AP_W2 && pos2>=0) {
ap_private<_AP_W, _AP_S> Range2=
ap_private<_AP_W, _AP_S>((man >> pos2) | ((1ULL<<(DOUBLE_MAN+1)&man) ? (DOUBLE_MAN_MASK >> (DOUBLE_MAN+2-pos2) <<(DOUBLE_MAN+2-pos2)):0));
Range2_all_ones=Range2.isAllOnesValue(); // Range2.isAllOnesValue();
} else if (pos2<0)
Range2_all_ones=false;
deleted_zeros=deleted_zeros && (carry?Range1_all_ones:Range1_all_zeros);
deleted_ones=carry?Range2_all_ones&&(F2-_AP_F+_AP_W<0||!lD) : Range1_all_ones;
neg_src=isneg&&!(carry&Range1_all_ones);
} else
neg_src = isneg && V[_AP_W -1];
const bool neg_trg=V.isNegative();
const bool overflow=(neg_trg||!deleted_zeros) && !isneg;
bool underflow=(!neg_trg||!deleted_ones)&&neg_src;
//printf("neg_src = %d, neg_trg = %d, deleted_zeros = %d,
// deleted_ones = %d, overflow = %d, underflow = %d\n",
// neg_src, neg_trg, deleted_zeros, deleted_ones,
// overflow, underflow);
if (_AP_O==AP_SAT_SYM && _AP_S2 && _AP_S)
underflow |= neg_src && (_AP_W>1?V.isMinSignedValue():true);
overflow_adjust(underflow,overflow,lD, neg_src);
}
}
report();
}
///assign operators
//-------------------------------------------------------------------------
INLINE volatile ap_fixed_base& operator=(const ap_fixed_base<_AP_W, _AP_I, _AP_S,
_AP_Q, _AP_O, _AP_N>& op) volatile {
V = op.V;
return *this;
}
INLINE ap_fixed_base& operator=(const ap_fixed_base<_AP_W, _AP_I, _AP_S,
_AP_Q, _AP_O, _AP_N>& op) {
V = op.V;
return *this;
}
INLINE volatile ap_fixed_base& operator=(const volatile ap_fixed_base<_AP_W, _AP_I, _AP_S,
_AP_Q, _AP_O, _AP_N>& op) volatile {
V = op.V;
return *this;
}
INLINE ap_fixed_base& operator=(const volatile ap_fixed_base<_AP_W, _AP_I, _AP_S,
_AP_Q, _AP_O, _AP_N>& op) {
V = op.V;
return *this;
}
// Set this ap_fixed_base with a bits string. That means the ssdm_int::V
// inside this ap_fixed_base is assigned by bv.
// Note the input parameter should be a fixed-point formatted bit string.
INLINE ap_fixed_base& setBits(unsigned long long bv) {
V=bv;
return *this;
}
// Return a ap_fixed_base object whose ssdm_int::V is assigned by bv.
// Note the input parameter should be a fixed-point formatted bit string.
static INLINE ap_fixed_base bitsToFixed(unsigned long long bv) {
ap_fixed_base Tmp=bv;
return Tmp;
}
// Explicit conversion functions to ap_private that captures
// all integer bits (bits are truncated)
INLINE ap_private<AP_MAX(_AP_I,1),_AP_S>
to_ap_private(bool Cnative = true) const {
ap_private<AP_MAX(_AP_I,1),_AP_S> ret = ap_private<AP_MAX(_AP_I,1),_AP_S> ((_AP_I >= 1) ? (_AP_S==true ? V.ashr(AP_MAX(0,_AP_W - _AP_I)) : V.lshr(AP_MAX(0,_AP_W - _AP_I))) : ap_private<_AP_W, _AP_S>(0));
if (Cnative) {
bool r = false;
if (_AP_I < _AP_W) {
if (_AP_I > 0) r = !(V.getLoBits(_AP_W - _AP_I).isMinValue());
else r = !(V.isMinValue());
}
if (r && V.isNegative()) { // if this is negative integer
++ret;//ap_private<AP_MAX(_AP_I,1),_AP_S>(1,_AP_S);
}
} else {
//Follow OSCI library, conversion from sc_fixed to sc_int
}
return ret;
}
template<int _AP_W2, bool _AP_S2>
INLINE operator ap_private<_AP_W2,_AP_S2> () const {
return (ap_private<_AP_W2,_AP_S2>)to_ap_private();
}
template<int _AP_W2, bool _AP_S2, int _AP_N2>
INLINE operator ap_private<_AP_W2,_AP_S2,_AP_N2> () const {
return (ap_private<_AP_W2,_AP_S2,_AP_N2>)to_ap_private();
}
//Explict conversion function to C built-in integral type
INLINE int to_int() const {
return to_ap_private().to_int();
}
INLINE int to_uint() const {
return to_ap_private().to_uint();
}
INLINE ap_slong to_int64() const {
return to_ap_private().to_int64();
}
INLINE ap_ulong to_uint64() const {
return to_ap_private().to_uint64();
}
INLINE double to_double() const {
if (!V)
return 0;
if (_AP_W>64 || (_AP_W - _AP_I) > 0) {
bool isneg = _AP_S && V[_AP_W-1];
uint64_t res = isneg ? 0x8000000000000000ULL : 0;
ap_private<_AP_W, false> tmp = V;
if (isneg) tmp = -tmp;
int i = _AP_W -1 - tmp.countLeadingZeros();
int exp = _AP_I-(_AP_W-i);
res|=((uint64_t)(exp+DOUBLE_BIAS))<<DOUBLE_MAN;
if (i!=0) {
tmp.clear(i);
uint64_t man = ((i>DOUBLE_MAN)?tmp.lshr(i-DOUBLE_MAN):tmp).to_uint64() & DOUBLE_MAN_MASK;
res |= i<DOUBLE_MAN ? (man)<<(DOUBLE_MAN-i)& DOUBLE_MAN_MASK : man;
}
double dp=rawBitsToDouble(res);
return dp;
} else if (_AP_W - _AP_I > 0) {
/* This specialization is disabled. It is giving wrong results in some cases.
bool isneg=V.isNegative();
double dp = V.get();
dp /= (1<< (_AP_W - _AP_I));
return dp;*/
} else
return double(to_int64());
}
INLINE float to_float() const {
uint32_t res=0;
if (V==0)
return 0;
bool isneg=V.isNegative();
ap_private<_AP_W, _AP_S> tmp=V;
if (isneg) tmp = -tmp;
if (_AP_W-_AP_I>0||_AP_W>64) {
if (isneg)
res=0x80000000;
int i=_AP_W-1;
i-=tmp.countLeadingZeros();
int exp=_AP_I-(_AP_W-i);
res|=(exp+FLOAT_BIAS)<<FLOAT_MAN;
ap_private<_AP_W, _AP_S> man = 0;
if (i!=0) {
tmp.clear(i);
if (i>FLOAT_MAN)
man=tmp.lshr(i-FLOAT_MAN);
else
man=tmp;
res |= i < FLOAT_MAN?man.getZExtValue()<<(FLOAT_MAN-i):man.getZExtValue();
}
} else {
return float(to_int64());
}
float dp=rawBitsToFloat(res);
return dp;
}
INLINE operator double () const {
return to_double();
}
#ifndef __SC_COMPATIBLE__
INLINE operator float () const {
return to_float();
}
INLINE operator char () const {
return (char) to_int();
}
INLINE operator unsigned char () const {
return (unsigned char) to_uint();
}
INLINE operator short () const {
return (short) to_int();
}
INLINE operator unsigned short () const {
return (unsigned short) to_uint();
}
INLINE operator int () const {
return to_int();
}
INLINE operator unsigned int () const {
return to_uint();
}
#if 1
#ifdef __x86_64__
INLINE operator long () const {
return (long)to_int64();
}
INLINE operator unsigned long () const {
return (unsigned long) to_uint64();
}
#else
INLINE operator long () const {
return to_int64();
}
INLINE operator unsigned long () const {
return to_uint64();
}
#endif
#endif
INLINE operator unsigned long long () const {
return to_uint64();
}
INLINE operator long long () const {
return to_int64();
}
#endif
INLINE std::string to_string(uint8_t radix=2, bool sign=false) const;
INLINE ap_slong bits_to_int64() const {
ap_private<AP_MIN(_AP_W, 64), _AP_S> res(V);
return (ap_slong) res;
}
INLINE ap_ulong bits_to_uint64() const {
ap_private<AP_MIN(64,_AP_W), _AP_S> res(V);
return (ap_ulong) res;
}
INLINE int length() const {return _AP_W;}
// Count the number of zeros from the most significant bit
// to the first one bit. Note this is only for ap_fixed_base whose
// _AP_W <= 64, otherwise will incur assertion.
INLINE int countLeadingZeros() {
return V.countLeadingZeros();
}
///Arithmetic:Binary
//-------------------------------------------------------------------------
template<int _AP_W2, int _AP_I2, bool _AP_S2, ap_q_mode _AP_Q2, ap_o_mode _AP_O2, int _AP_N2>
INLINE typename RType<_AP_W2,_AP_I2,_AP_S2>::mult
operator * (const ap_fixed_base<_AP_W2,_AP_I2,_AP_S2,_AP_Q2,_AP_O2, _AP_N2>& op2) const {
typename RType<_AP_W2,_AP_I2,_AP_S2>::mult r;
r.V = V * op2.V;
return r;
}
template<int _AP_W1, int _AP_I1, bool _AP_S1, int _AP_W2, int _AP_I2, bool _AP_S2>
static INLINE ap_fixed_base multiply(const ap_fixed_base<_AP_W1,_AP_I1,_AP_S1>& op1, const
ap_fixed_base<_AP_W2,_AP_I2,_AP_S2>& op2) {
ap_private<_AP_W+_AP_W2, _AP_S> OP1=op1.V;
ap_private<_AP_W2,_AP_S2> OP2=op2.V;
return OP1*OP2;
}
template<int _AP_W2, int _AP_I2, bool _AP_S2, ap_q_mode _AP_Q2, ap_o_mode _AP_O2, int _AP_N2>
INLINE typename RType<_AP_W2,_AP_I2,_AP_S2>::div
operator / (const ap_fixed_base<_AP_W2,_AP_I2,_AP_S2,_AP_Q2,_AP_O2, _AP_N2>& op2) const {
enum {F2 = _AP_W2-_AP_I2, _W1=AP_MAX(_AP_W + AP_MAX(F2, 0), _AP_W2),
_W2=AP_MAX(_AP_W2,AP_MAX(_AP_W + AP_MAX(F2, 0), _AP_W2))};
ap_private<_W1, _AP_S> dividend = (ap_private<_W1, _AP_S>(V)) << ((_W1>_AP_W)?F2:0);
ap_private<_W1, _AP_S2> divisior = ap_private<_W2, _AP_S2>(op2.V);
ap_private<_W1, _AP_S> ret = ap_private<_W1,_AP_S> ((_AP_S||_AP_S2) ? dividend.sdiv(divisior): dividend.udiv(divisior));
typename RType<_AP_W2, _AP_I2, _AP_S2>::div r;
r.V = ret;
return r;
}
#define OP_BIN_AF(Sym, Rty, Width, Sign, Fun) \
template<int _AP_W2, int _AP_I2, bool _AP_S2, ap_q_mode _AP_Q2, ap_o_mode _AP_O2, int _AP_N2> \
INLINE typename RType<_AP_W2,_AP_I2,_AP_S2>::Rty \
operator Sym (const ap_fixed_base<_AP_W2,_AP_I2,_AP_S2,_AP_Q2,_AP_O2, _AP_N2>& op2) const \
{ \
enum {_AP_F=_AP_W-_AP_I, F2=_AP_W2-_AP_I2}; \
typename RType<_AP_W2,_AP_I2,_AP_S2>::Rty r, lhs(*this), rhs(op2); \
r.V = lhs.V.Fun(rhs.V); \
return r; \
} \
INLINE typename RType<_AP_W,_AP_I,_AP_S>::Rty \
operator Sym (const ap_fixed_base& op2) const \
{ \
typename RType<_AP_W,_AP_I,_AP_S>::Rty r; \
r.V = V Sym op2.V; \
return r; \
} \
OP_BIN_AF(+, plus, plus_w, plus_s, Add)
OP_BIN_AF(-, minus, minus_w, minus_s, Sub)
#define OP_LOGIC_BIN_AF(Sym, Rty, Width, Sign) \
template<int _AP_W2, int _AP_I2, bool _AP_S2, ap_q_mode _AP_Q2, ap_o_mode _AP_O2, int _AP_N2> \
INLINE typename RType<_AP_W2,_AP_I2,_AP_S2>::Rty \
operator Sym (const ap_fixed_base<_AP_W2,_AP_I2,_AP_S2,_AP_Q2,_AP_O2, _AP_N2>& op2) const \
{ \
typename RType<_AP_W2,_AP_I2,_AP_S2>::Rty r, lhs(*this), rhs(op2); \
r.V=lhs.V Sym rhs.V; \
return r; \
} \
INLINE typename RType<_AP_W,_AP_I,_AP_S>::Rty \
operator Sym (const ap_fixed_base& op2) const \
{ \
typename RType<_AP_W,_AP_I,_AP_S>::Rty r; \
r.V = V Sym op2.V; \
return r; \
} \
INLINE typename RType<_AP_W,_AP_I,_AP_S>::Rty operator Sym(int op2) const \
{ \
return V Sym (op2<<(_AP_W - _AP_I)); \
}
OP_LOGIC_BIN_AF(&, logic, logic_w, logic_s)
OP_LOGIC_BIN_AF(|, logic, logic_w, logic_s)
OP_LOGIC_BIN_AF(^, logic, logic_w, logic_s)
///Arithmic : assign
//-------------------------------------------------------------------------
#define OP_ASSIGN_AF(Sym) \
template<int _AP_W2, int _AP_I2, bool _AP_S2, ap_q_mode _AP_Q2, ap_o_mode _AP_O2, int _AP_N2> \
INLINE ap_fixed_base& operator Sym##= (const ap_fixed_base<_AP_W2,_AP_I2,_AP_S2,_AP_Q2,_AP_O2, _AP_N2>& op2) \
{ \
*this=operator Sym (op2) ; \
return *this; \
}
OP_ASSIGN_AF(+)
OP_ASSIGN_AF(-)
OP_ASSIGN_AF(&)
OP_ASSIGN_AF(|)
OP_ASSIGN_AF(^)
OP_ASSIGN_AF(*)
OP_ASSIGN_AF(/)
///Prefix increment, decrement
//-------------------------------------------------------------------------
INLINE ap_fixed_base& operator ++() {
operator+=(ap_fixed_base<1,1,false>(1)); //SystemC's semantics
return *this;
}
INLINE ap_fixed_base& operator --() {
operator-=(ap_fixed_base<1,1,false>(1)); //SystemC's semantics
return *this;
}
//Postfix increment, decrement
//-------------------------------------------------------------------------
INLINE const ap_fixed_base operator ++(int) {
ap_fixed_base t(*this);
operator++();
return t;
}
INLINE const ap_fixed_base operator --(int) {
ap_fixed_base t = *this;
operator--();
return t;
}
///Unary arithmetic
//-------------------------------------------------------------------------
INLINE ap_fixed_base operator +() {return *this;}
INLINE ap_fixed_base<_AP_W + 1, _AP_I + 1, true> operator -() const {
ap_fixed_base<_AP_W + 1, _AP_I + 1, true> Tmp(*this);
Tmp.V = - Tmp.V;
return Tmp;
}
INLINE ap_fixed_base<_AP_W,_AP_I,true,_AP_Q,_AP_O, _AP_N> getNeg() {
ap_fixed_base<_AP_W,_AP_I,true,_AP_Q,_AP_O, _AP_N> Tmp(*this);
Tmp.V=-Tmp.V;
return Tmp;
}
///Not (!)
//-------------------------------------------------------------------------
INLINE bool operator !() const {
return !V;
}
///Bitwise complement
//-------------------------------------------------------------------------
INLINE ap_fixed_base<_AP_W, _AP_I, _AP_S>
operator ~() const {
ap_fixed_base<_AP_W, _AP_I, _AP_S> res(*this);
res.V.flip();
return res;
}
///Shift
///template argument as shift value
template<int _AP_SHIFT>
INLINE ap_fixed_base<_AP_W, _AP_I + _AP_SHIFT, _AP_S> lshift () const {
ap_fixed_base<_AP_W, _AP_I + _AP_SHIFT, _AP_S> r;
r.V = V;
return r;
}
template<int _AP_SHIFT>
INLINE ap_fixed_base<_AP_W, _AP_I - _AP_SHIFT, _AP_S> rshift () const {
ap_fixed_base<_AP_W, _AP_I - _AP_SHIFT, _AP_S> r;
r.V = V;
return r;
}
//Because the return type is the type of the the first operand, shift assign
//operators do not carry out any quantization or overflow
//While systemc, shift assigns for sc_fixed/sc_ufixed will result in
//quantization or overflow (depending on the mode of the first operand)
//-------------------------------------------------------------------------
INLINE ap_fixed_base operator << (int sh) const {
ap_fixed_base r;
bool isNeg=(sh&0x80000000) != 0;
sh=isNeg?-sh:sh;
bool shiftoverflow = sh >= _AP_W;
bool NegSrc = V.isNegative();
if (isNeg) {
if (shiftoverflow)
NegSrc?r.V.set():r.V.clear();
else
r.V=_AP_S?V.ashr(sh):V.lshr(sh);
} else {
if (shiftoverflow)
r.V.clear();
else
r.V=V<<sh;
}
#ifdef __SC_COMPATIBLE__
if (sh == 0) return r;
if (isNeg == true && _AP_Q != AP_TRN) {
bool qb = false;
if (sh <= _AP_W) qb = V[sh - 1];
bool rb = false;
if (sh > 1 && sh <= _AP_W)
rb = (V << (_AP_W - sh + 1 )) != 0;
else if (sh > _AP_W)
rb = V != 0;
r.quantization_adjust(qb, rb, NegSrc);
} else if (isNeg == false && _AP_O != AP_WRAP) {
bool allones, allzeros;
if (sh < _AP_W ) {
ap_private<_AP_W, _AP_S > range1 = V.lshr(_AP_W - sh - 1);
allones = range1.isAllOnesValue();
allzeros = range1.isMinValue();
} else {
allones = false;
allzeros = V.isMinValue();
}
bool overflow = !allzeros && !NegSrc;
bool underflow = !allones && NegSrc;
if (_AP_O == AP_SAT_SYM && _AP_S)
underflow |= NegSrc && (_AP_W > 1 ? r.V.isMinSignedValue():true);
bool lD = false;
if ( sh < _AP_W ) lD = V[_AP_W - sh - 1];
r.overflow_adjust(underflow, overflow, lD, NegSrc);
}
#endif
return r;
}
template<int _AP_W2>
INLINE ap_fixed_base operator<<(const ap_private<_AP_W2,true>& op2) const {
int sh = op2.to_int();
return operator << (sh);
}
INLINE ap_fixed_base operator << (unsigned int sh ) const {
ap_fixed_base r;
bool shiftoverflow = sh >= _AP_W;
r.V = shiftoverflow ? ap_private<_AP_W, _AP_S >(0) : V << sh;
if (sh == 0) return r;
#ifdef __SC_COMPATIBLE__
bool NegSrc = V.isNegative();
if (_AP_O != AP_WRAP) {
bool allones, allzeros;
if (sh < _AP_W ) {
ap_private<_AP_W, _AP_S > range1 = V.lshr(_AP_W - sh -1);
allones = range1.isAllOnesValue();
allzeros = range1.isMinValue();
} else {
allones = false;
allzeros = V.isMinValue();
}
bool overflow = !allzeros && !NegSrc;
bool underflow = !allones && NegSrc;
if (_AP_O == AP_SAT_SYM && _AP_S)
underflow |= NegSrc && (_AP_W > 1 ? r.V.isMinSignedValue():true);
bool lD = false;
if ( sh < _AP_W ) lD = V[_AP_W - sh - 1];
r.overflow_adjust(underflow, overflow, lD, NegSrc);
}
#endif
return r;
}
template<int _AP_W2>
INLINE ap_fixed_base operator << (const ap_private<_AP_W2,false>& op2) const {
unsigned int sh = op2.to_uint();
return operator << (sh);
}
INLINE ap_fixed_base operator >> (int sh) const {
ap_fixed_base r;
bool isNeg=(sh&0x80000000) != 0;
bool NegSrc = V.isNegative();
sh=isNeg?-sh:sh;
bool shiftoverflow = sh >= _AP_W;
if (isNeg && !shiftoverflow) r.V=V<<sh;
else {
if (shiftoverflow)
NegSrc?r.V.set():r.V.clear();
else
r.V=_AP_S?V.ashr(sh):V.lshr(sh);
}
#ifdef __SC_COMPATIBLE__
if (sh == 0) return r;
if (isNeg == false && _AP_Q != AP_TRN) {
bool qb = false;
if (sh <= _AP_W) qb = V[sh - 1];
bool rb = false;
if (sh > 1 && sh <= _AP_W)
rb = (V << (_AP_W - sh + 1 )) != 0;
else if (sh > _AP_W)
rb = V != 0;
r.quantization_adjust(qb, rb, NegSrc);
} else if (isNeg == true && _AP_O != AP_WRAP) {
bool allones, allzeros;
if (sh < _AP_W ) {
ap_private<_AP_W, _AP_S > range1 = V.lshr(_AP_W - sh - 1);
allones = range1.isAllOnesValue();
allzeros = range1.isMinValue();
} else {
allones = false;
allzeros = V.isMinValue();
}
bool overflow = !allzeros && !NegSrc;
bool underflow = !allones && NegSrc;
if (_AP_O == AP_SAT_SYM && _AP_S)
underflow |= NegSrc && (_AP_W > 1 ? r.V.isMinSignedValue():true);
bool lD = false;
if ( sh < _AP_W ) lD = V[_AP_W - sh - 1];
r.overflow_adjust(underflow, overflow, lD, NegSrc);
}
#endif
return r;
}
template<int _AP_W2>
INLINE ap_fixed_base operator >> (const ap_private<_AP_W2,true>& op2) const {
int sh = op2.to_int();
return operator >> (sh);
}
INLINE ap_fixed_base operator >> (unsigned int sh) const {
ap_fixed_base r;
bool NegSrc = V.isNegative();
bool shiftoverflow = sh >= _AP_W;
if (shiftoverflow)
NegSrc?r.V.set():r.V.clear();
else
r.V=_AP_S?V.ashr(sh):V.lshr(sh);
#ifdef __SC_COMPATIBLE__
if (sh == 0) return r;
if (_AP_Q != AP_TRN) {
bool qb = false;
if (sh <= _AP_W) qb = V[sh - 1];
bool rb = false;
if (sh > 1 && sh <= _AP_W)
rb = (V << (_AP_W - sh + 1 )) != 0;
else if (sh > _AP_W)
rb = V != 0;
r.quantization_adjust(qb, rb, NegSrc);
}
#endif
return r;
}
template<int _AP_W2>
INLINE ap_fixed_base operator >> (const ap_private<_AP_W2,false>& op2) const {
unsigned int sh = op2.to_uint();
return operator >> (sh);
}
///shift assign
//-------------------------------------------------------------------------
#define OP_AP_SHIFT_AP_ASSIGN_AF(Sym) \
template<int _AP_W2, bool _AP_S2> \
INLINE ap_fixed_base& operator Sym##=(const ap_private<_AP_W2,_AP_S2>& op2) \
{ \
*this=operator Sym (op2); \
return *this; \
}
OP_AP_SHIFT_AP_ASSIGN_AF(<<)
OP_AP_SHIFT_AP_ASSIGN_AF(>>)
///Support shift(ap_fixed_base)
#define OP_AP_SHIFT_AF(Sym) \
template<int _AP_W2, int _AP_I2, bool _AP_S2, ap_q_mode _AP_Q2, ap_o_mode _AP_O2, int _AP_N2> \
INLINE ap_fixed_base operator Sym (const ap_fixed_base<_AP_W2,_AP_I2,_AP_S2,_AP_Q2,_AP_O2, _AP_N2>& op2) const \
{ \
return operator Sym (op2.to_ap_private()); \
} \
template<int _AP_W2, int _AP_I2, bool _AP_S2, ap_q_mode _AP_Q2, ap_o_mode _AP_O2, int _AP_N2> \
INLINE ap_fixed_base& operator Sym##= (const ap_fixed_base<_AP_W2,_AP_I2,_AP_S2,_AP_Q2,_AP_O2, _AP_N2>& op2) \
{ \
*this=operator Sym (op2); \
return *this; \
}
OP_AP_SHIFT_AF(<<)
OP_AP_SHIFT_AF(>>)
INLINE ap_fixed_base& operator >>= (unsigned int sh) {
*this = operator >> (sh);
return *this;
}
INLINE ap_fixed_base& operator <<= (unsigned int sh) {
*this = operator << (sh);
return *this;
}
INLINE ap_fixed_base& operator >>= (int sh) {
*this = operator >> (sh);
return *this;
}
INLINE ap_fixed_base& operator <<= (int sh) {
*this = operator << (sh);
return *this;
}
///Comparisons
//-------------------------------------------------------------------------
template<int _AP_W2, int _AP_I2, bool _AP_S2, ap_q_mode _AP_Q2, ap_o_mode _AP_O2, int _AP_N2>
INLINE bool operator == (const ap_fixed_base<_AP_W2,_AP_I2,_AP_S2,_AP_Q2,_AP_O2, _AP_N2>& op2) const {
enum {_AP_F=_AP_W-_AP_I,F2=_AP_W2-_AP_I2, shAmt1 = AP_MAX(F2-_AP_F, 0), shAmt2 = AP_MAX(_AP_F-F2,0), _AP_W3 = (_AP_F==F2) ? AP_MAX(_AP_W,_AP_W2) : AP_MAX(_AP_W+shAmt1, _AP_W2+shAmt2)};
ap_private<_AP_W3, _AP_S > OP1= ap_private<_AP_W3, _AP_S >(V)<<shAmt1;
ap_private<_AP_W3,_AP_S2 > OP2=ap_private<_AP_W3,_AP_S2 >(op2.V)<<shAmt2;
return OP1 == OP2;
}
template<int _AP_W2, int _AP_I2, bool _AP_S2, ap_q_mode _AP_Q2, ap_o_mode _AP_O2, int _AP_N2>
INLINE bool operator != (const ap_fixed_base<_AP_W2,_AP_I2,_AP_S2,_AP_Q2,_AP_O2, _AP_N2>& op2) const {
return !(*this==op2);
}
template<int _AP_W2, int _AP_I2, bool _AP_S2, ap_q_mode _AP_Q2, ap_o_mode _AP_O2, int _AP_N2>
INLINE bool operator > (const ap_fixed_base<_AP_W2,_AP_I2,_AP_S2,_AP_Q2,_AP_O2, _AP_N2>& op2) const {
enum {_AP_F=_AP_W-_AP_I,F2=_AP_W2-_AP_I2, shAmt1 = AP_MAX(F2-_AP_F, 0), shAmt2 = AP_MAX(_AP_F-F2,0), _AP_W3 = (_AP_F==F2) ? AP_MAX(_AP_W,_AP_W2) : AP_MAX(_AP_W+shAmt1, _AP_W2+shAmt2)};
ap_private<_AP_W3, _AP_S > OP1= ap_private<_AP_W3, _AP_S >(V)<<shAmt1;
ap_private<_AP_W3,_AP_S2 > OP2=ap_private<_AP_W3,_AP_S2 >(op2.V)<<shAmt2;
if (_AP_S||_AP_S2)
return OP1.sgt(OP2);
else
return OP1.ugt(OP2);
}
template<int _AP_W2, int _AP_I2, bool _AP_S2, ap_q_mode _AP_Q2, ap_o_mode _AP_O2, int _AP_N2>
INLINE bool operator <= (const ap_fixed_base<_AP_W2,_AP_I2,_AP_S2,_AP_Q2,_AP_O2, _AP_N2>& op2) const {
return !(*this>op2);
}
template<int _AP_W2, int _AP_I2, bool _AP_S2, ap_q_mode _AP_Q2, ap_o_mode _AP_O2, int _AP_N2>
INLINE bool operator < (const ap_fixed_base<_AP_W2,_AP_I2,_AP_S2,_AP_Q2,_AP_O2, _AP_N2>& op2) const {
enum {_AP_F=_AP_W-_AP_I,F2=_AP_W2-_AP_I2, shAmt1 = AP_MAX(F2-_AP_F, 0), shAmt2 = AP_MAX(_AP_F-F2,0), _AP_W3 = (_AP_F==F2) ? AP_MAX(_AP_W,_AP_W2) : AP_MAX(_AP_W+shAmt1, _AP_W2+shAmt2)};
ap_private<_AP_W3, _AP_S > OP1= ap_private<_AP_W3, _AP_S >(V)<<shAmt1;
ap_private<_AP_W3,_AP_S2 > OP2=ap_private<_AP_W3,_AP_S2 >(op2.V)<<shAmt2;
if (_AP_S||_AP_S2)
return OP1.slt(OP2);
else
return OP1.ult(OP2);
}
template<int _AP_W2, int _AP_I2, bool _AP_S2, ap_q_mode _AP_Q2, ap_o_mode _AP_O2, int _AP_N2>
INLINE bool operator >= (const ap_fixed_base<_AP_W2,_AP_I2,_AP_S2,_AP_Q2,_AP_O2, _AP_N2>& op2) const {
return !(*this<op2);
}
#define DOUBLE_CMP_AF(Sym) \
INLINE bool operator Sym (double d) const { \
return to_double() Sym d; \
}
DOUBLE_CMP_AF(==)
DOUBLE_CMP_AF(!=)
DOUBLE_CMP_AF(>)
DOUBLE_CMP_AF(>=)
DOUBLE_CMP_AF(<)
DOUBLE_CMP_AF(<=)
// Bit and Slice Select
INLINE af_bit_ref<_AP_W,_AP_I,_AP_S,_AP_Q,_AP_O, _AP_N> operator [] (unsigned int index) {
assert(index<_AP_W&&"Attemping to read bit beyond MSB");
return af_bit_ref<_AP_W,_AP_I,_AP_S,_AP_Q,_AP_O, _AP_N>(this, index);
}
INLINE af_bit_ref<_AP_W, _AP_I,_AP_S,_AP_Q,_AP_O, _AP_N> bit(unsigned int index) {
assert(index < _AP_W && "Attempting to read bit beyond MSB");
return af_bit_ref<_AP_W,_AP_I,_AP_S,_AP_Q,_AP_O, _AP_N>(this, index);
}
template<int _AP_W2, bool _AP_S2>
INLINE af_bit_ref<_AP_W,_AP_I,_AP_S,_AP_Q,_AP_O, _AP_N> bit (const ap_private<_AP_W2,_AP_S2>& index) {
assert(index >= 0 && "Attempting to read bit with negative index");
assert(index < _AP_W && "Attempting to read bit beyond MSB");
return af_bit_ref<_AP_W,_AP_I,_AP_S,_AP_Q,_AP_O, _AP_N>(this, index.to_int());
}
INLINE bool bit (unsigned int index) const {
assert(index < _AP_W && "Attempting to read bit beyond MSB");
return V[index];
}
INLINE bool operator [] (unsigned int index) const {
assert(index < _AP_W && "Attempting to read bit beyond MSB");
return V[index];
}
template<int _AP_W2, bool _AP_S2>
INLINE bool bit (const ap_private<_AP_W2, _AP_S2>& index) const {
assert(index < _AP_W && "Attempting to read bit beyond MSB");
return V[index.to_uint()];
}
template<int _AP_W2, bool _AP_S2>
INLINE bool operator [] (const ap_private<_AP_W2, _AP_S2>& index) const {
assert(index < _AP_W && "Attempting to read bit beyond MSB");
return V[index.to_uint()];
}
INLINE af_bit_ref<_AP_W, _AP_I,_AP_S,_AP_Q,_AP_O, _AP_N> get_bit(int index) {
assert(index < _AP_I && "Attempting to read bit beyond MSB");
assert(index >= _AP_I - _AP_W&& "Attempting to read bit beyond MSB");
return af_bit_ref<_AP_W,_AP_I,_AP_S,_AP_Q,_AP_O, _AP_N>(this, index + _AP_W - _AP_I);
}
template<int _AP_W2>
INLINE af_bit_ref<_AP_W,_AP_I,_AP_S,_AP_Q,_AP_O, _AP_N> get_bit (const ap_private<_AP_W2, true>& index) {
assert(index >= _AP_I - _AP_W && "Attempting to read bit with negative index");
assert(index < _AP_I && "Attempting to read bit beyond MSB");
return af_bit_ref<_AP_W,_AP_I,_AP_S,_AP_Q,_AP_O, _AP_N>(this, index.to_int() + _AP_W - _AP_I);
}
INLINE bool get_bit (int index) const {
assert(index >= _AP_I - _AP_W && "Attempting to read bit with negative index");
assert(index < _AP_I && "Attempting to read bit beyond MSB");
return V[index + _AP_W - _AP_I];
}
template<int _AP_W2>
INLINE bool get_bit (const ap_private<_AP_W2, true>& index) const {
assert(index >= _AP_I - _AP_W && "Attempting to read bit with negative index");
assert(index < _AP_I && "Attempting to read bit beyond MSB");
return V[index.to_int() + _AP_W - _AP_I];
}
INLINE af_range_ref<_AP_W,_AP_I,_AP_S, _AP_Q, _AP_O, _AP_N>
range(int Hi, int Lo) {
assert((Hi < _AP_W) && (Lo < _AP_W) && "Out of bounds in range()");
return af_range_ref<_AP_W, _AP_I, _AP_S, _AP_Q, _AP_O, _AP_N>(this, Hi, Lo);
}
INLINE af_range_ref<_AP_W, _AP_I, _AP_S, _AP_Q, _AP_O, _AP_N>
operator () (int Hi, int Lo) {
assert((Hi < _AP_W) && (Lo < _AP_W) && "Out of bounds in range()");
return af_range_ref<_AP_W, _AP_I, _AP_S, _AP_Q, _AP_O, _AP_N>(this, Hi, Lo);
}
INLINE af_range_ref<_AP_W, _AP_I, _AP_S, _AP_Q, _AP_O, _AP_N>
range(int Hi, int Lo) const {
assert((Hi < _AP_W) && (Lo < _AP_W) &&"Out of bounds in range()");
return af_range_ref<_AP_W, _AP_I, _AP_S, _AP_Q, _AP_O, _AP_N>(const_cast<ap_fixed_base*>(this), Hi, Lo);
}
INLINE af_range_ref<_AP_W, _AP_I, _AP_S, _AP_Q, _AP_O, _AP_N>
operator () (int Hi, int Lo) const {
return this->range(Hi, Lo);
}
template<int _AP_W2, bool _AP_S2, int _AP_W3, bool _AP_S3>
INLINE af_range_ref<_AP_W,_AP_I,_AP_S, _AP_Q, _AP_O, _AP_N>
range(const ap_private<_AP_W2, _AP_S2> &HiIdx,
const ap_private<_AP_W3, _AP_S3> &LoIdx) {
int Hi = HiIdx.to_int();
int Lo = LoIdx.to_int();
assert((Hi < _AP_W) && (Lo < _AP_W) && "Out of bounds in range()");
return af_range_ref<_AP_W, _AP_I, _AP_S, _AP_Q, _AP_O, _AP_N>(this, Hi, Lo);
}
template<int _AP_W2, bool _AP_S2, int _AP_W3, bool _AP_S3>
INLINE af_range_ref<_AP_W,_AP_I,_AP_S, _AP_Q, _AP_O, _AP_N>
operator () (const ap_private<_AP_W2, _AP_S2> &HiIdx,
const ap_private<_AP_W3, _AP_S3> &LoIdx) {
int Hi = HiIdx.to_int();
int Lo = LoIdx.to_int();
assert((Hi < _AP_W) && (Lo < _AP_W) && "Out of bounds in range()");
return af_range_ref<_AP_W, _AP_I, _AP_S, _AP_Q, _AP_O, _AP_N>(this, Hi, Lo);
}
template<int _AP_W2, bool _AP_S2, int _AP_W3, bool _AP_S3>
INLINE af_range_ref<_AP_W,_AP_I,_AP_S, _AP_Q, _AP_O, _AP_N>
range(const ap_private<_AP_W2, _AP_S2> &HiIdx,
const ap_private<_AP_W3, _AP_S3> &LoIdx) const {
int Hi = HiIdx.to_int();
int Lo = LoIdx.to_int();
assert((Hi < _AP_W) && (Lo < _AP_W) && "Out of bounds in range()");
return af_range_ref<_AP_W, _AP_I, _AP_S, _AP_Q, _AP_O, _AP_N>(const_cast<
ap_fixed_base<_AP_W, _AP_I, _AP_S, _AP_Q, _AP_O, _AP_N>*>(this),
Hi, Lo);
}
template<int _AP_W2, bool _AP_S2, int _AP_W3, bool _AP_S3>
INLINE af_range_ref<_AP_W,_AP_I,_AP_S, _AP_Q, _AP_O, _AP_N>
operator () (const ap_private<_AP_W2, _AP_S2> &HiIdx,
const ap_private<_AP_W3, _AP_S3> &LoIdx) const {
int Hi = HiIdx.to_int();
int Lo = LoIdx.to_int();
return this->range(Hi, Lo);
}
INLINE af_range_ref<_AP_W, _AP_I, _AP_S, _AP_Q, _AP_O, _AP_N>
range() {
return this->range(_AP_W - 1, 0);
}
INLINE af_range_ref<_AP_W, _AP_I, _AP_S, _AP_Q, _AP_O, _AP_N>
range() const {
return this->range(_AP_W - 1, 0);
}
INLINE bool is_zero () const {
return V.isMinValue();
}
INLINE bool is_neg () const {
if (V.isNegative())
return true;
return false;
}
INLINE int wl () const {
return _AP_W;
}
INLINE int iwl () const {
return _AP_I;
}
INLINE ap_q_mode q_mode () const {
return _AP_Q;
}
INLINE ap_o_mode o_mode () const {
return _AP_O;
}
INLINE int n_bits () const {
return 0;
}
//private:
public:
ap_private<_AP_W, _AP_S> V;
};
template<int _AP_W, int _AP_I, bool _AP_S, ap_q_mode _AP_Q, ap_o_mode _AP_O, int _AP_N>
std::string ap_fixed_base<_AP_W, _AP_I, _AP_S, _AP_Q, _AP_O, _AP_N>::to_string(
uint8_t radix, bool sign) const {
std::string str;
str.clear();
char step;
std::string prefix;
switch (radix) {
case 2 : prefix = "0b"; step = 1; break;
case 8 : prefix = "0o"; step = 3; break;
case 16 : prefix = "0x"; step = 4; break;
default : break;
}
if (_AP_W <= _AP_I)
str = this->to_ap_private().to_string(radix,
radix == 10 ? _AP_S : sign);
else {
if (radix == 10) {
bool isNeg = _AP_S && V.isNegative();
if (_AP_I > 0) {
ap_private<AP_MAX(_AP_I, 1), _AP_S> int_part(0);
int_part = this->to_ap_private();
str += int_part.to_string(radix, false);
} else {
if (isNeg) str += '-';
}
ap_fixed_base<_AP_W, _AP_I, _AP_S> tmp(*this);
if (isNeg && _AP_I <= 0) tmp = -tmp;
ap_fixed_base<_AP_W - AP_MIN(_AP_I, 0), 0, false> frac_part = tmp;
if (frac_part == 0) return str;
str += ".";
while (frac_part != 0) {
char digit = (frac_part * radix).to_ap_private();
str += static_cast<char>(digit + '0');
frac_part *= radix;
}
} else {
if (_AP_I > 0) {
for (signed i = _AP_W - _AP_I; i < _AP_W; i += step) {
char digit = (char)(this->range(AP_MIN(i + step - 1, _AP_W - 1), i));
str = (digit < 10 ? static_cast<char>(digit + '0') :
static_cast<char>(digit - 10 + 'a')) + str;
}
}
str += '.';
ap_fixed_base<AP_MAX(_AP_W - _AP_I, 1), 0, _AP_S> tmp(*this);
for (signed i = _AP_W - _AP_I - 1; i >= 0; i -= step) {
char digit = (char)(tmp.range(i, AP_MAX(0, i - step + 1)));
str += digit < 10 ? static_cast<char>(digit + '0') :
static_cast<char>(digit - 10 + 'a');
}
}
}
str = prefix + str;
return str;
}
template<int _AP_W, int _AP_I, bool _AP_S, ap_q_mode _AP_Q, ap_o_mode _AP_O, int _AP_N>
INLINE void b_not(ap_fixed_base<_AP_W, _AP_I, _AP_S, _AP_Q, _AP_O, _AP_N>& ret,
const ap_fixed_base<_AP_W, _AP_I, _AP_S, _AP_Q, _AP_O, _AP_N>& op) {
ret.V = op.V;
ret.V.flip();
}
template<int _AP_W, int _AP_I, bool _AP_S, ap_q_mode _AP_Q, ap_o_mode _AP_O, int _AP_N>
INLINE void b_and(ap_fixed_base<_AP_W, _AP_I, _AP_S, _AP_Q, _AP_O, _AP_N>& ret,
const ap_fixed_base<_AP_W, _AP_I, _AP_S, _AP_Q, _AP_O, _AP_N>& op1,
const ap_fixed_base<_AP_W, _AP_I, _AP_S, _AP_Q, _AP_O, _AP_N>& op2) {
ret.V = op1.V & op2.V;
}
template<int _AP_W, int _AP_I, bool _AP_S, ap_q_mode _AP_Q, ap_o_mode _AP_O, int _AP_N>
INLINE void b_or(ap_fixed_base<_AP_W, _AP_I, _AP_S, _AP_Q, _AP_O, _AP_N>& ret,
const ap_fixed_base<_AP_W, _AP_I, _AP_S, _AP_Q, _AP_O, _AP_N>& op1,
const ap_fixed_base<_AP_W, _AP_I, _AP_S, _AP_Q, _AP_O, _AP_N>& op2) {
ret.V = op1.V | op2.V;
}
template<int _AP_W, int _AP_I, bool _AP_S, ap_q_mode _AP_Q, ap_o_mode _AP_O, int _AP_N>
INLINE void b_xor(ap_fixed_base<_AP_W, _AP_I, _AP_S, _AP_Q, _AP_O, _AP_N>& ret,
const ap_fixed_base<_AP_W, _AP_I, _AP_S, _AP_Q, _AP_O, _AP_N>& op1,
const ap_fixed_base<_AP_W, _AP_I, _AP_S, _AP_Q, _AP_O, _AP_N>& op2) {
ret.V = op1.V ^ op2.V;
}
template<int _AP_W, int _AP_I, bool _AP_S, ap_q_mode _AP_Q, ap_o_mode _AP_O,
int _AP_N, int _AP_W2, int _AP_I2, bool _AP_S2, ap_q_mode _AP_Q2,
ap_o_mode _AP_O2, int _AP_N2>
INLINE void neg(ap_fixed_base<_AP_W, _AP_I, _AP_S, _AP_Q, _AP_O, _AP_N>& ret,
const ap_fixed_base<_AP_W2, _AP_I2, _AP_S2, _AP_Q2, _AP_O2, _AP_N2>& op) {
ap_fixed_base<_AP_W2+!_AP_S2, _AP_I2+!_AP_S2, true, _AP_Q2, _AP_O2, _AP_N2> Tmp;
Tmp.V = - op.V;
ret = Tmp;
}
template<int _AP_W, int _AP_I, bool _AP_S, ap_q_mode _AP_Q, ap_o_mode _AP_O,
int _AP_N>
INLINE void neg(ap_fixed_base<_AP_W, _AP_I, _AP_S, _AP_Q, _AP_O, _AP_N>& ret,
const ap_fixed_base<_AP_W, _AP_I, _AP_S, _AP_Q, _AP_O, _AP_N>& op) {
ret.V = -op.V;
}
template<int _AP_W, int _AP_I, bool _AP_S, ap_q_mode _AP_Q, ap_o_mode _AP_O,
int _AP_N, int _AP_W2, int _AP_I2, bool _AP_S2, ap_q_mode _AP_Q2,
ap_o_mode _AP_O2, int _AP_N2>
INLINE void lshift(ap_fixed_base<_AP_W, _AP_I, _AP_S, _AP_Q, _AP_O, _AP_N>& ret,
const ap_fixed_base<_AP_W2, _AP_I2, _AP_S2, _AP_Q2, _AP_O2, _AP_N2>& op,
int i) {
ap_fixed_base<_AP_W2 - _AP_I2 + AP_MAX(_AP_I, _AP_I2), AP_MAX(_AP_I, _AP_I2), _AP_S2, _AP_Q2, _AP_O2, _AP_N2> Tmp;
Tmp = op;
Tmp.V <<= i;
ret = Tmp;
}
template<int _AP_W, int _AP_I, bool _AP_S, ap_q_mode _AP_Q, ap_o_mode _AP_O,
int _AP_N>
INLINE void lshift(ap_fixed_base<_AP_W, _AP_I, _AP_S, _AP_Q, _AP_O, _AP_N>& ret,
const ap_fixed_base<_AP_W, _AP_I, _AP_S, _AP_Q, _AP_O, _AP_N>& op,
int i) {
ret.V = op.V << i;
}
template<int _AP_W, int _AP_I, bool _AP_S, ap_q_mode _AP_Q, ap_o_mode _AP_O,
int _AP_N, int _AP_W2, int _AP_I2, bool _AP_S2, ap_q_mode _AP_Q2,
ap_o_mode _AP_O2, int _AP_N2>
INLINE void rshift(ap_fixed_base<_AP_W, _AP_I, _AP_S, _AP_Q, _AP_O, _AP_N>& ret,
const ap_fixed_base<_AP_W2, _AP_I2, _AP_S2, _AP_Q2, _AP_O2, _AP_N2>& op,
int i) {
ap_fixed_base<_AP_I2 + AP_MAX(_AP_W - _AP_I, _AP_W2 - _AP_I2), _AP_I2, _AP_S2, _AP_Q2, _AP_O2, _AP_N2> Tmp;
Tmp = op;
Tmp.V = _AP_S2 ? Tmp.V.ashr(i): Tmp.V.lshr(i);
ret = Tmp;
}
template<int _AP_W, int _AP_I, bool _AP_S, ap_q_mode _AP_Q, ap_o_mode _AP_O,
int _AP_N>
INLINE void rshift(ap_fixed_base<_AP_W, _AP_I, _AP_S, _AP_Q, _AP_O, _AP_N>& ret,
const ap_fixed_base<_AP_W, _AP_I, _AP_S, _AP_Q, _AP_O, _AP_N>& op,
int i) {
ret.V = _AP_S ? op.V.ashr(i): op.V.lshr(i);
}
#define AF_CTOR_SPEC_BASE(_AP_W,_AP_S,C_TYPE) \
template<> INLINE ap_fixed_base<_AP_W,_AP_W,_AP_S,AP_TRN,AP_WRAP>::ap_fixed_base(C_TYPE i_op):V(i_op) \
{ \
}
#define AF_CTOR_SPEC(__W,C_TYPE) \
AF_CTOR_SPEC_BASE(__W,true,C_TYPE) \
AF_CTOR_SPEC_BASE(__W,false,C_TYPE)
AF_CTOR_SPEC(1,bool)
AF_CTOR_SPEC(8, signed char)
AF_CTOR_SPEC(8, unsigned char)
AF_CTOR_SPEC(16, signed short)
AF_CTOR_SPEC(16, unsigned short)
AF_CTOR_SPEC(32, signed int)
AF_CTOR_SPEC(32, unsigned int)
AF_CTOR_SPEC(64, ap_slong)
AF_CTOR_SPEC(64, ap_ulong)
///Output streaming
//-----------------------------------------------------------------------------
template<int _AP_W, int _AP_I, bool _AP_S, ap_q_mode _AP_Q, ap_o_mode _AP_O, int _AP_N>
INLINE std::ostream&
operator <<(std::ostream& os, const ap_fixed_base<_AP_W,_AP_I,_AP_S,_AP_Q,_AP_O, _AP_N>& x) {
os << x.to_double();
return os;
}
///Input streaming
//-----------------------------------------------------------------------------
template<int _AP_W, int _AP_I, bool _AP_S, ap_q_mode _AP_Q, ap_o_mode _AP_O, int _AP_N>
INLINE std::istream&
operator >> (std::istream& os, ap_fixed_base<_AP_W,_AP_I,_AP_S,_AP_Q,_AP_O, _AP_N>& x) {
double d;
os >> d;
x = ap_fixed_base<_AP_W,_AP_I,_AP_S,_AP_Q,_AP_O, _AP_N>(x);
return os;
}
template<int _AP_W, int _AP_I, bool _AP_S, ap_q_mode _AP_Q, ap_o_mode _AP_O, int _AP_N>
INLINE void print(const ap_fixed_base<_AP_W,_AP_I,_AP_S,_AP_Q,_AP_O, _AP_N>& x) {
ap_private<_AP_W,_AP_S> data=x.V;
if (_AP_I>0) {
const ap_private<_AP_I,_AP_S> p1=data>>(_AP_W-_AP_I);
print(p1);
} else
printf("0");
printf(".");
if (_AP_I<_AP_W) {
const ap_private<_AP_W-_AP_I,false> p2=data;
print(p2,false);
}
}
///Operators mixing Integers with ap_fixed_base
//-----------------------------------------------------------------------------
#if 1
#define AF_BIN_OP_WITH_INT_SF(BIN_OP,C_TYPE,_AP_W2,_AP_S2,RTYPE) \
template<int _AP_W, int _AP_I, bool _AP_S, ap_q_mode _AP_Q, ap_o_mode _AP_O, int _AP_N> \
INLINE typename ap_fixed_base<_AP_W,_AP_I,_AP_S>::template RType<_AP_W2,_AP_W2,_AP_S2>::RTYPE \
operator BIN_OP (const ap_fixed_base<_AP_W,_AP_I,_AP_S,_AP_Q,_AP_O, _AP_N>& op, C_TYPE i_op) \
{ \
return op.operator BIN_OP(ap_private<_AP_W2,_AP_S2>(i_op)); \
}
#define AF_BIN_OP_WITH_INT(BIN_OP, C_TYPE, _AP_W2,_AP_S2,RTYPE) \
template<int _AP_W, int _AP_I, bool _AP_S, ap_q_mode _AP_Q, ap_o_mode _AP_O, int _AP_N> \
INLINE typename ap_fixed_base<_AP_W,_AP_I,_AP_S>::template RType<_AP_W2,_AP_W2,_AP_S2>::RTYPE \
operator BIN_OP (const ap_fixed_base<_AP_W,_AP_I,_AP_S,_AP_Q,_AP_O, _AP_N>& op, C_TYPE i_op) \
{ \
return op.operator BIN_OP (ap_fixed_base<_AP_W2,_AP_W2,_AP_S2>(i_op)); \
} \
\
template<int _AP_W, int _AP_I, bool _AP_S,ap_q_mode _AP_Q, ap_o_mode _AP_O, int _AP_N > \
INLINE typename ap_fixed_base<_AP_W,_AP_I,_AP_S>::template RType<_AP_W2,_AP_W2,_AP_S2>::RTYPE \
operator BIN_OP (C_TYPE i_op, const ap_fixed_base<_AP_W,_AP_I,_AP_S,_AP_Q,_AP_O, _AP_N>& op) \
{ \
return ap_fixed_base<_AP_W2,_AP_W2,_AP_S2>(i_op).operator BIN_OP (op); \
}
#else
#define AF_BIN_OP_WITH_INT_SF(BIN_OP,C_TYPE,_AP_W2,_AP_S2,RTYPE) \
template<int _AP_W, int _AP_I, bool _AP_S, ap_q_mode _AP_Q, ap_o_mode _AP_O, int _AP_N> \
INLINE typename ap_fixed_base<_AP_W,_AP_I,_AP_S>::template RType<_AP_W2,_AP_W2,_AP_S2>::RTYPE \
operator BIN_OP (const ap_fixed_base<_AP_W,_AP_I,_AP_S,_AP_Q,_AP_O, _AP_N>& op, C_TYPE i_op) \
{ \
return op BIN_OP (i_op); \
}
#define AF_BIN_OP_WITH_INT(BIN_OP, C_TYPE, _AP_W2,_AP_S2,RTYPE) \
template<int _AP_W, int _AP_I, bool _AP_S, ap_q_mode _AP_Q, ap_o_mode _AP_O, int _AP_N> \
INLINE typename ap_fixed_base<_AP_W,_AP_I,_AP_S>::template RType<_AP_W2,_AP_W2,_AP_S2>::RTYPE \
operator BIN_OP (const ap_fixed_base<_AP_W,_AP_I,_AP_S,_AP_Q,_AP_O, _AP_N>& op, C_TYPE i_op) \
{ \
return op.V BIN_OP (i_op<<(_AP_W-_AP_I)); \
} \
\
\
template<int _AP_W, int _AP_I, bool _AP_S,ap_q_mode _AP_Q, ap_o_mode _AP_O, int _AP_N > \
INLINE typename ap_fixed_base<_AP_W,_AP_I,_AP_S>::template RType<_AP_W2,_AP_W2,_AP_S2>::RTYPE \
operator BIN_OP (C_TYPE i_op, const ap_fixed_base<_AP_W,_AP_I,_AP_S,_AP_Q,_AP_O, _AP_N>& op) \
{ \
return ap_fixed_base<_AP_W2,_AP_W2,_AP_S2>(i_op).operator BIN_OP (op); \
}
#endif
#if 1
#define AF_REL_OP_WITH_INT(REL_OP, C_TYPE, _AP_W2,_AP_S2) \
template<int _AP_W, int _AP_I, bool _AP_S, ap_q_mode _AP_Q, ap_o_mode _AP_O, int _AP_N> \
INLINE bool operator REL_OP (const ap_fixed_base<_AP_W,_AP_I,_AP_S,_AP_Q,_AP_O, _AP_N>& op, C_TYPE i_op) \
{ \
return op.operator REL_OP (ap_fixed_base<_AP_W2,_AP_W2,_AP_S2>(i_op)); \
} \
\
\
template<int _AP_W, int _AP_I, bool _AP_S, ap_q_mode _AP_Q, ap_o_mode _AP_O, int _AP_N> \
INLINE bool operator REL_OP (C_TYPE i_op, const ap_fixed_base<_AP_W,_AP_I,_AP_S,_AP_Q,_AP_O, _AP_N>& op) \
{ \
return ap_fixed_base<_AP_W2,_AP_W2,_AP_S2>(i_op).operator REL_OP (op); \
}
#else
#define AF_REL_OP_WITH_INT(REL_OP, C_TYPE, _AP_W2,_AP_S2) \
template<int _AP_W, int _AP_I, bool _AP_S, ap_q_mode _AP_Q, ap_o_mode _AP_O, int _AP_N> \
INLINE bool operator REL_OP (const ap_fixed_base<_AP_W,_AP_I,_AP_S,_AP_Q,_AP_O, _AP_N>& op, C_TYPE i_op) \
{ \
return op.V.operator REL_OP (i_op<<(_AP_W-_AP_I)); \
} \
\
\
template<int _AP_W, int _AP_I, bool _AP_S, ap_q_mode _AP_Q, ap_o_mode _AP_O, int _AP_N> \
INLINE bool operator REL_OP (C_TYPE i_op, const ap_fixed_base<_AP_W,_AP_I,_AP_S,_AP_Q,_AP_O, _AP_N>& op) \
{ \
return (i_op<<(_AP_W-_AP_I)) REL_OP (op.V.VAL); \
}
#endif
#if 1
#define AF_ASSIGN_OP_WITH_INT(ASSIGN_OP, C_TYPE, _AP_W2, _AP_S2) \
template<int _AP_W, int _AP_I, bool _AP_S, ap_q_mode _AP_Q, ap_o_mode _AP_O, int _AP_N> \
INLINE ap_fixed_base<_AP_W,_AP_I,_AP_S,_AP_Q,_AP_O, _AP_N>& operator ASSIGN_OP ( ap_fixed_base<_AP_W,_AP_I,_AP_S,_AP_Q,_AP_O, _AP_N>& op, C_TYPE i_op) { \
return op.operator ASSIGN_OP (ap_fixed_base<_AP_W2,_AP_W2,_AP_S2>(i_op)); \
}
#define AF_ASSIGN_OP_WITH_INT_SF(ASSIGN_OP, C_TYPE, _AP_W2, _AP_S2) \
template<int _AP_W, int _AP_I, bool _AP_S, ap_q_mode _AP_Q, ap_o_mode _AP_O, int _AP_N> \
INLINE ap_fixed_base<_AP_W,_AP_I,_AP_S,_AP_Q,_AP_O, _AP_N>& operator ASSIGN_OP ( ap_fixed_base<_AP_W,_AP_I,_AP_S,_AP_Q,_AP_O, _AP_N>& op, C_TYPE i_op) { \
return op.operator ASSIGN_OP (ap_private<_AP_W2,_AP_S2>(i_op)); \
}
#else
#define AF_ASSIGN_OP_WITH_INT(ASSIGN_OP, C_TYPE, _AP_W2, _AP_S2) \
template<int _AP_W, int _AP_I, bool _AP_S, ap_q_mode _AP_Q, ap_o_mode _AP_O, int _AP_N> \
INLINE ap_fixed_base<_AP_W,_AP_I,_AP_S,_AP_Q,_AP_O, _AP_N>& operator ASSIGN_OP ( ap_fixed_base<_AP_W,_AP_I,_AP_S,_AP_Q,_AP_O, _AP_N>& op, C_TYPE i_op) { \
return op.V.operator ASSIGN_OP (i_op); \
}
#define AF_ASSIGN_OP_WITH_INT_SF(ASSIGN_OP, C_TYPE, _AP_W2, _AP_S2) \
template<int _AP_W, int _AP_I, bool _AP_S, ap_q_mode _AP_Q, ap_o_mode _AP_O, int _AP_N> \
INLINE ap_fixed_base<_AP_W,_AP_I,_AP_S,_AP_Q,_AP_O, _AP_N>& operator ASSIGN_OP ( ap_fixed_base<_AP_W,_AP_I,_AP_S,_AP_Q,_AP_O, _AP_N>& op, C_TYPE i_op) { \
return op.V.operator ASSIGN_OP (i_op); \
}
#endif
#define AF_OPS_WITH_INT(C_TYPE, WI, SI) \
AF_BIN_OP_WITH_INT(+, C_TYPE, WI, SI, plus) \
AF_BIN_OP_WITH_INT(-, C_TYPE, WI, SI, minus) \
AF_BIN_OP_WITH_INT(*, C_TYPE, WI, SI, mult) \
AF_BIN_OP_WITH_INT(/, C_TYPE, WI, SI, div) \
AF_BIN_OP_WITH_INT_SF(>>, C_TYPE, WI, SI, arg1) \
AF_BIN_OP_WITH_INT_SF(<<, C_TYPE, WI, SI, arg1) \
AF_BIN_OP_WITH_INT(&, C_TYPE, WI, SI, logic) \
AF_BIN_OP_WITH_INT(|, C_TYPE, WI, SI, logic) \
AF_BIN_OP_WITH_INT(^, C_TYPE, WI, SI, logic) \
\
AF_REL_OP_WITH_INT(==, C_TYPE, WI, SI) \
AF_REL_OP_WITH_INT(!=, C_TYPE, WI, SI) \
AF_REL_OP_WITH_INT(>, C_TYPE, WI, SI) \
AF_REL_OP_WITH_INT(>=, C_TYPE, WI, SI) \
AF_REL_OP_WITH_INT(<, C_TYPE, WI, SI) \
AF_REL_OP_WITH_INT(<=, C_TYPE, WI, SI) \
\
AF_ASSIGN_OP_WITH_INT(+=, C_TYPE, WI, SI) \
AF_ASSIGN_OP_WITH_INT(-=, C_TYPE, WI, SI) \
AF_ASSIGN_OP_WITH_INT(*=, C_TYPE, WI, SI) \
AF_ASSIGN_OP_WITH_INT(/=, C_TYPE, WI, SI) \
AF_ASSIGN_OP_WITH_INT_SF(>>=, C_TYPE, WI, SI) \
AF_ASSIGN_OP_WITH_INT_SF(<<=, C_TYPE, WI, SI) \
AF_ASSIGN_OP_WITH_INT(&=, C_TYPE, WI, SI) \
AF_ASSIGN_OP_WITH_INT(|=, C_TYPE, WI, SI) \
AF_ASSIGN_OP_WITH_INT(^=, C_TYPE, WI, SI)
AF_OPS_WITH_INT(bool, 1, false)
AF_OPS_WITH_INT(char, 8, true)
AF_OPS_WITH_INT(signed char, 8, true)
AF_OPS_WITH_INT(unsigned char, 8, false)
AF_OPS_WITH_INT(short, 16, true)
AF_OPS_WITH_INT(unsigned short, 16, false)
AF_OPS_WITH_INT(int, 32, true)
AF_OPS_WITH_INT(unsigned int, 32, false)
# if defined __x86_64__
AF_OPS_WITH_INT(long, 64, true)
AF_OPS_WITH_INT(unsigned long, 64, false)
# else
AF_OPS_WITH_INT(long, 32, true)
AF_OPS_WITH_INT(unsigned long, 32, false)
# endif
AF_OPS_WITH_INT(ap_slong, 64, true)
AF_OPS_WITH_INT(ap_ulong, 64, false)
#define AF_BIN_OP_WITH_AP_INT(BIN_OP, RTYPE) \
template<int _AP_W, int _AP_I, bool _AP_S, ap_q_mode _AP_Q, ap_o_mode _AP_O,int _AP_N, int _AP_W2, bool _AP_S2> \
INLINE typename ap_fixed_base<_AP_W2,_AP_W2,_AP_S2>::template RType<_AP_W,_AP_I,_AP_S>::RTYPE \
operator BIN_OP ( const ap_private<_AP_W2,_AP_S2>& i_op, const ap_fixed_base<_AP_W,_AP_I,_AP_S,_AP_Q,_AP_O, _AP_N>& op) { \
return ap_fixed_base<_AP_W2,_AP_W2,_AP_S2>(i_op).operator BIN_OP (op); \
} \
template<int _AP_W, int _AP_I, bool _AP_S, ap_q_mode _AP_Q, ap_o_mode _AP_O,int _AP_N, int _AP_W2, bool _AP_S2> \
INLINE typename ap_fixed_base<_AP_W,_AP_I,_AP_S>::template RType<_AP_W2,_AP_W2,_AP_S2>::RTYPE \
operator BIN_OP ( const ap_fixed_base<_AP_W,_AP_I,_AP_S,_AP_Q,_AP_O, _AP_N>& op, const ap_private<_AP_W2,_AP_S2>& i_op) { \
return op.operator BIN_OP (ap_fixed_base<_AP_W2,_AP_W2,_AP_S2>(i_op)); \
}
#define AF_REL_OP_WITH_AP_INT(REL_OP) \
template<int _AP_W, int _AP_I, bool _AP_S, ap_q_mode _AP_Q, ap_o_mode _AP_O,int _AP_N, int _AP_W2, bool _AP_S2> \
INLINE bool operator REL_OP ( const ap_fixed_base<_AP_W,_AP_I,_AP_S,_AP_Q,_AP_O, _AP_N>& op, const ap_private<_AP_W2,_AP_S2>& i_op) { \
return op.operator REL_OP ( ap_fixed_base<_AP_W2,_AP_W2,_AP_S2>(i_op)); \
} \
template<int _AP_W, int _AP_I, bool _AP_S, ap_q_mode _AP_Q, ap_o_mode _AP_O,int _AP_N, int _AP_W2, bool _AP_S2> \
INLINE bool operator REL_OP ( const ap_private<_AP_W2,_AP_S2>& i_op, const ap_fixed_base<_AP_W,_AP_I,_AP_S,_AP_Q,_AP_O, _AP_N>& op) { \
return ap_fixed_base<_AP_W2,_AP_W2,_AP_S2>(i_op).operator REL_OP (op); \
}
#define AF_ASSIGN_OP_WITH_AP_INT(ASSIGN_OP) \
template<int _AP_W, int _AP_I, bool _AP_S, ap_q_mode _AP_Q, ap_o_mode _AP_O,int _AP_N, int _AP_W2, bool _AP_S2> \
INLINE ap_fixed_base<_AP_W,_AP_I,_AP_S,_AP_Q,_AP_O, _AP_N>& operator ASSIGN_OP ( ap_fixed_base<_AP_W,_AP_I,_AP_S,_AP_Q,_AP_O, _AP_N>& op, const ap_private<_AP_W2,_AP_S2>& i_op) { \
return op.operator ASSIGN_OP (ap_fixed_base<_AP_W2,_AP_W2,_AP_S2>(i_op)); \
} \
template<int _AP_W, int _AP_I, bool _AP_S, ap_q_mode _AP_Q, ap_o_mode _AP_O,int _AP_N, int _AP_W2, bool _AP_S2> \
INLINE ap_private<_AP_W2,_AP_S2>& operator ASSIGN_OP ( ap_private<_AP_W2,_AP_S2>& i_op, const ap_fixed_base<_AP_W,_AP_I,_AP_S,_AP_Q,_AP_O, _AP_N>& op) { \
return i_op.operator ASSIGN_OP (op.to_ap_private()); \
}
AF_BIN_OP_WITH_AP_INT(+, plus)
AF_BIN_OP_WITH_AP_INT(-, minus)
AF_BIN_OP_WITH_AP_INT(*, mult)
AF_BIN_OP_WITH_AP_INT(/, div)
AF_BIN_OP_WITH_AP_INT(&, logic)
AF_BIN_OP_WITH_AP_INT(|, logic)
AF_BIN_OP_WITH_AP_INT(^, logic)
AF_REL_OP_WITH_AP_INT(==)
AF_REL_OP_WITH_AP_INT(!=)
AF_REL_OP_WITH_AP_INT(>)
AF_REL_OP_WITH_AP_INT(>=)
AF_REL_OP_WITH_AP_INT(<)
AF_REL_OP_WITH_AP_INT(<=)
AF_ASSIGN_OP_WITH_AP_INT(+=)
AF_ASSIGN_OP_WITH_AP_INT(-=)
AF_ASSIGN_OP_WITH_AP_INT(*=)
AF_ASSIGN_OP_WITH_AP_INT(/=)
AF_ASSIGN_OP_WITH_AP_INT(&=)
AF_ASSIGN_OP_WITH_AP_INT(|=)
AF_ASSIGN_OP_WITH_AP_INT(^=)
#define AF_REF_REL_OP_MIX_INT(REL_OP, C_TYPE, _AP_W2, _AP_S2) \
template<int _AP_W, int _AP_I, bool _AP_S, \
ap_q_mode _AP_Q, ap_o_mode _AP_O, int _AP_N> \
INLINE bool operator REL_OP ( const af_range_ref<_AP_W,_AP_I, _AP_S, _AP_Q, _AP_O, _AP_N> &op, C_TYPE op2) { \
return (ap_private<_AP_W, false>(op)).operator REL_OP (ap_private<_AP_W2,_AP_S2>(op2)); \
} \
template<int _AP_W, int _AP_I, bool _AP_S, \
ap_q_mode _AP_Q, ap_o_mode _AP_O, int _AP_N> \
INLINE bool operator REL_OP ( C_TYPE op2, const af_range_ref<_AP_W, _AP_I, _AP_S, _AP_Q, _AP_O, _AP_N> &op) { \
return ap_private<_AP_W2,_AP_S2>(op2).operator REL_OP (ap_private<_AP_W, false>(op)); \
} \
template<int _AP_W, int _AP_I, bool _AP_S, \
ap_q_mode _AP_Q, ap_o_mode _AP_O, int _AP_N> \
INLINE bool operator REL_OP ( const af_bit_ref<_AP_W, _AP_I, _AP_S, _AP_Q, _AP_O, _AP_N> &op, C_TYPE op2) { \
return (bool(op)) REL_OP op2; \
} \
template<int _AP_W, int _AP_I, bool _AP_S, \
ap_q_mode _AP_Q, ap_o_mode _AP_O, int _AP_N> \
INLINE bool operator REL_OP ( C_TYPE op2, const af_bit_ref<_AP_W, _AP_I, _AP_S, _AP_Q, _AP_O, _AP_N> &op) { \
return op2 REL_OP (bool(op)); \
}
#define AF_REF_REL_MIX_INT(C_TYPE, _AP_WI, _AP_SI) \
AF_REF_REL_OP_MIX_INT(>, C_TYPE, _AP_WI, _AP_SI) \
AF_REF_REL_OP_MIX_INT(<, C_TYPE, _AP_WI, _AP_SI) \
AF_REF_REL_OP_MIX_INT(>=, C_TYPE, _AP_WI, _AP_SI) \
AF_REF_REL_OP_MIX_INT(<=, C_TYPE, _AP_WI, _AP_SI) \
AF_REF_REL_OP_MIX_INT(==, C_TYPE, _AP_WI, _AP_SI) \
AF_REF_REL_OP_MIX_INT(!=, C_TYPE, _AP_WI, _AP_SI)
AF_REF_REL_MIX_INT(bool, 1, false)
AF_REF_REL_MIX_INT(char, 8, true)
AF_REF_REL_MIX_INT(signed char, 8, true)
AF_REF_REL_MIX_INT(unsigned char, 8, false)
AF_REF_REL_MIX_INT(short, 16, true)
AF_REF_REL_MIX_INT(unsigned short, 16, false)
AF_REF_REL_MIX_INT(int, 32, true)
AF_REF_REL_MIX_INT(unsigned int, 32, false)
# if defined __x86_64__
AF_REF_REL_MIX_INT(long, 64, true)
AF_REF_REL_MIX_INT(unsigned long, 64, false)
# else
AF_REF_REL_MIX_INT(long, 32, true)
AF_REF_REL_MIX_INT(unsigned long, 32, false)
# endif
AF_REF_REL_MIX_INT(ap_slong, 64, true)
AF_REF_REL_MIX_INT(ap_ulong, 64, false)
#define AF_REF_REL_OP_AP_INT(REL_OP) \
template<int _AP_W, int _AP_I, bool _AP_S, \
ap_q_mode _AP_Q, ap_o_mode _AP_O, int _AP_N, int _AP_W2, bool _AP_S2> \
INLINE bool operator REL_OP ( const af_range_ref<_AP_W,_AP_I, _AP_S, _AP_Q, _AP_O, _AP_N> &op, const ap_private<_AP_W2, _AP_S> &op2) { \
return (ap_private<_AP_W, false>(op)).operator REL_OP (op2); \
} \
template<int _AP_W, int _AP_I, bool _AP_S, \
ap_q_mode _AP_Q, ap_o_mode _AP_O, int _AP_N, int _AP_W2, bool _AP_S2> \
INLINE bool operator REL_OP (const ap_private<_AP_W2, _AP_S2> &op2, const af_range_ref<_AP_W, _AP_I, _AP_S, _AP_Q, _AP_O, _AP_N> &op) { \
return op2.operator REL_OP (ap_private<_AP_W, false>(op)); \
} \
template<int _AP_W, int _AP_I, bool _AP_S, \
ap_q_mode _AP_Q, ap_o_mode _AP_O, int _AP_N, int _AP_W2, bool _AP_S2> \
INLINE bool operator REL_OP ( const af_bit_ref<_AP_W, _AP_I, _AP_S, _AP_Q, _AP_O, _AP_N> &op, const ap_private<_AP_W2, _AP_S2> &op2) { \
return (ap_private<1, false>(op)).operator REL_OP (op2); \
} \
template<int _AP_W, int _AP_I, bool _AP_S, \
ap_q_mode _AP_Q, ap_o_mode _AP_O, int _AP_N, int _AP_W2, bool _AP_S2> \
INLINE bool operator REL_OP ( const ap_private<_AP_W2, _AP_S2> &op2, const af_bit_ref<_AP_W, _AP_I, _AP_S, _AP_Q, _AP_O, _AP_N> &op) { \
return op2.operator REL_OP (ap_private<1,false>(op)); \
}
AF_REF_REL_OP_AP_INT(>)
AF_REF_REL_OP_AP_INT(<)
AF_REF_REL_OP_AP_INT(>=)
AF_REF_REL_OP_AP_INT(<=)
AF_REF_REL_OP_AP_INT(==)
AF_REF_REL_OP_AP_INT(!=)
// Relational Operators with double
template<int _AP_W, int _AP_I, bool _AP_S, ap_q_mode _AP_Q, ap_o_mode _AP_O, int _AP_N>
INLINE bool operator == ( double op1, const ap_fixed_base<_AP_W,_AP_I,_AP_S,_AP_Q,_AP_O, _AP_N>& op2) {
return op2.operator == (op1);
}
template<int _AP_W, int _AP_I, bool _AP_S, ap_q_mode _AP_Q, ap_o_mode _AP_O, int _AP_N>
INLINE bool operator != ( double op1, const ap_fixed_base<_AP_W,_AP_I,_AP_S,_AP_Q,_AP_O, _AP_N>& op2) {
return op2.operator != (op1);
}
template<int _AP_W, int _AP_I, bool _AP_S, ap_q_mode _AP_Q, ap_o_mode _AP_O, int _AP_N>
INLINE bool operator > ( double op1, const ap_fixed_base<_AP_W,_AP_I,_AP_S,_AP_Q,_AP_O, _AP_N>& op2) {
return op2.operator < (op1);
}
template<int _AP_W, int _AP_I, bool _AP_S, ap_q_mode _AP_Q, ap_o_mode _AP_O, int _AP_N>
INLINE bool operator >= ( double op1, const ap_fixed_base<_AP_W,_AP_I,_AP_S,_AP_Q,_AP_O, _AP_N>& op2) {
return op2.operator <= (op1);
}
template<int _AP_W, int _AP_I, bool _AP_S, ap_q_mode _AP_Q, ap_o_mode _AP_O, int _AP_N>
INLINE bool operator < ( double op1, const ap_fixed_base<_AP_W,_AP_I,_AP_S,_AP_Q,_AP_O, _AP_N>& op2) {
return op2.operator > (op1);
}
template<int _AP_W, int _AP_I, bool _AP_S, ap_q_mode _AP_Q, ap_o_mode _AP_O, int _AP_N>
INLINE bool operator <= ( double op1, const ap_fixed_base<_AP_W,_AP_I,_AP_S,_AP_Q,_AP_O, _AP_N>& op2) {
return op2.operator >= (op1);
}
#endif /* #ifndef __AESL_GCC_AP_FIXED_H__ */
\ No newline at end of file
/*
* Copyright 2012 Xilinx, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef __AESL_GCC_AP_INT_H__
#define __AESL_GCC_AP_INT_H__
#ifndef __cplusplus
#error C++ is required to include this header file
#endif /* #ifndef __cplusplus */
#undef _AP_DEBUG_
#include <stdio.h>
#include <iostream>
// for safety
#if (defined(_AP_N)|| defined(_AP_C))
#error One or more of the following is defined: _AP_N, _AP_C. Definition conflicts with their usage as template parameters.
#endif /* #if (defined(_AP_N)|| defined(_AP_C)) */
// for safety
#if (defined(_AP_W) || defined(_AP_I) || defined(_AP_S) || defined(_AP_Q) || defined(_AP_O) || defined(_AP_W2) || defined(_AP_I2) || defined(_AP_S2) || defined(_AP_Q2) || defined(_AP_O2))
#error One or more of the following is defined: _AP_W, _AP_I, _AP_S, _AP_Q, _AP_O, _AP_W2, _AP_I2, _AP_S2, _AP_Q2, _AP_O2. Definition conflicts with their usage as template parameters.
#endif /* #if (defined(_AP_W) || defined(_AP_I) || defined(_AP_S) || defined(_AP_Q) || defined(_AP_O) || defined(_AP_W2) || defined(_AP_I2) || defined(_AP_S2) || defined(_AP_Q2) || defined(_AP_O2)) */
//for safety
#if (defined(_AP_W3) || defined(_AP_S3) || defined(_AP_W4) || defined(_AP_S4))
#error One or more of the following is defined: _AP_W3, _AP_S3, _AP_W4,_AP_S4. Definition conflicts with their usage as template parameters.
#endif /* #if (defined(_AP_W3) || defined(_AP_S3) || defined(_AP_W4) || defined(_AP_S4)) */
//for safety
#if (defined(_AP_W1) || defined(_AP_S1) || defined(_AP_I1) || defined(_AP_T) || defined(_AP_T1) || defined(_AP_T2) || defined(_AP_T3) || defined(_AP_T4))
#error One or more of the following is defined: _AP_W1, _AP_S1, _AP_I1, _AP_T, _AP_T1, _AP_T2, _AP_T3, _AP_T4. Definition conflicts with their usage as template parameters.
#endif /* #if (defined(_AP_W1) || defined(_AP_S1) || defined(_AP_I1) || defined(_AP_T) || defined(_AP_T1) || defined(_AP_T2) || defined(_AP_T3) || defined(_AP_T4)) */
#define __AESL_APDT_IN_SCFLOW__
#ifndef __AESL_APDT_IN_SCFLOW__
#include "etc/ap_private.h"
#else
#include "../etc/ap_private.h"
#endif /* #ifndef __AESL_APDT_IN_SCFLOW__ */
#ifdef _AP_DEBUG_
#define AP_DEBUG(s) s
#else
#define AP_DEBUG(s)
#endif /* #ifdef _AP_DEBUG_ */
#ifndef __SIMULATION__
#define __SIMULATION__
#endif /* #ifndef __SIMULATION__ */
#if !(defined SYSTEMC_H) && !(defined SYSTEMC_INCLUDED)
#ifndef SC_TRN
#define SC_TRN AP_TRN
#endif /* #ifndef SC_TRN */
#ifndef SC_RND
#define SC_RND AP_RND
#endif /* #ifndef SC_RND */
#ifndef SC_TRN_ZERO
#define SC_TRN_ZERO AP_TRN_ZERO
#endif /* #ifndef SC_TRN_ZERO */
#ifndef SC_RND_ZERO
#define SC_RND_ZERO AP_RND_ZERO
#endif /* #ifndef SC_RND_ZERO */
#ifndef SC_RND_INF
#define SC_RND_INF AP_RND_INF
#endif /* #ifndef SC_RND_INF */
#ifndef SC_RND_MIN_INF
#define SC_RND_MIN_INF AP_RND_MIN_INF
#endif /* #ifndef SC_RND_MIN_INF */
#ifndef SC_RND_CONV
#define SC_RND_CONV AP_RND_CONV
#endif /* #ifndef SC_RND_CONV */
#ifndef SC_WRAP
#define SC_WRAP AP_WRAP
#endif /* #ifndef SC_WRAP */
#ifndef SC_SAT
#define SC_SAT AP_SAT
#endif /* #ifndef SC_SAT */
#ifndef SC_SAT_ZERO
#define SC_SAT_ZERO AP_SAT_ZERO
#endif /* #ifndef SC_SAT_ZERO */
#ifndef SC_SAT_SYM
#define SC_SAT_SYM AP_SAT_SYM
#endif /* #ifndef SC_SAT_SYM */
#ifndef SC_WRAP_SM
#define SC_WRAP_SM AP_WRAP_SM
#endif /* #ifndef SC_WRAP_SM */
#ifndef SC_BIN
#define SC_BIN AP_BIN
#endif /* #ifndef SC_BIN */
#ifndef SC_OCT
#define SC_OCT AP_OCT
#endif /* #ifndef SC_OCT */
#ifndef SC_DEC
#define SC_DEC AP_DEC
#endif /* #ifndef SC_DEC */
#ifndef SC_HEX
#define SC_HEX AP_HEX
#endif /* #ifndef SC_HEX */
#endif /* #if !(defined SYSTEMC_H) && !(defined SYSTEMC_INCLUDED) */
#ifndef AP_INT_MAX_W
#define AP_INT_MAX_W 1024
#endif
#define BIT_WIDTH_UPPER_LIMIT (1 << 15)
#if AP_INT_MAX_W > BIT_WIDTH_UPPER_LIMIT
#error "Bitwidth exceeds 32768 (1 << 15), the maximum allowed value"
#endif
#define MAX_MODE(BITS) ((BITS + 1023) / 1024)
///Forward declaration
template<int _AP_W, bool _AP_S> struct ap_range_ref;
template<int _AP_W, bool _AP_S> struct ap_bit_ref;
template<int _AP_W, int _AP_I, bool _AP_S, ap_q_mode _AP_Q,
ap_o_mode _AP_O, int _AP_N> struct ap_fixed_base;
template<int _AP_W, int _AP_I, bool _AP_S,
ap_q_mode _AP_Q, ap_o_mode _AP_O, int _AP_N> struct af_range_ref;
template<int _AP_W, int _AP_I, bool _AP_S,
ap_q_mode _AP_Q, ap_o_mode _AP_O, int _AP_N> struct af_bit_ref;
template<int _AP_W> class ap_uint;
enum {
AP_BIN = 2,
AP_OCT = 8,
AP_DEC = 10,
AP_HEX = 16
};
///Why to use reference?
///Because we will operate the original object indirectly by operating the
///result object directly after concating or part selecting
///Proxy class which allows concatination to be used as rvalue(for reading) and
//lvalue(for writing)
/// Concatination reference.
// ----------------------------------------------------------------
template<int _AP_W1, typename _AP_T1, int _AP_W2, typename _AP_T2>
struct ap_concat_ref {
#ifdef _MSC_VER
#pragma warning(disable: 4521 4522)
#endif /* #ifdef _MSC_VER */
enum {_AP_WR=_AP_W1+_AP_W2,};
_AP_T1& mbv1;
_AP_T2& mbv2;
INLINE ap_concat_ref(const ap_concat_ref<_AP_W1, _AP_T1,
_AP_W2, _AP_T2>& ref):
mbv1(ref.mbv1), mbv2(ref.mbv2) {}
INLINE ap_concat_ref(_AP_T1& bv1, _AP_T2& bv2):mbv1(bv1),mbv2(bv2) {}
template <int _AP_W3, bool _AP_S3>
INLINE ap_concat_ref& operator = (const ap_private<_AP_W3,_AP_S3>& val) {
ap_private<_AP_W1+_AP_W2, false> vval(val);
int W_ref1=mbv1.length();
int W_ref2=mbv2.length();
ap_private<_AP_W1,false> mask1(-1);
mask1>>=_AP_W1-W_ref1;
ap_private<_AP_W2,false> mask2(-1);
mask2>>=_AP_W2-W_ref2;
mbv1.set(ap_private<_AP_W1,false>((vval>>W_ref2)&mask1));
mbv2.set(ap_private<_AP_W2,false>(vval&mask2));
return *this;
}
INLINE ap_concat_ref& operator = (unsigned long long val) {
ap_private<_AP_W1+_AP_W2, false> tmpVal(val);
return operator = (tmpVal);
}
template<int _AP_W3, typename _AP_T3, int _AP_W4, typename _AP_T4>
INLINE ap_concat_ref& operator =
(const ap_concat_ref <_AP_W3, _AP_T3, _AP_W4, _AP_T4>& val) {
ap_private<_AP_W1+_AP_W2, false> tmpVal(val);
return operator = (tmpVal);
}
INLINE ap_concat_ref& operator =
(const ap_concat_ref <_AP_W1, _AP_T1, _AP_W2, _AP_T2>& val) {
ap_private<_AP_W1+_AP_W2, false> tmpVal(val);
return operator = (tmpVal);
}
template <int _AP_W3, bool _AP_S3>
INLINE ap_concat_ref& operator =(const ap_bit_ref<_AP_W3, _AP_S3>& val) {
ap_private<_AP_W1+_AP_W2, false> tmpVal(val);
return operator = (tmpVal);
}
template <int _AP_W3, bool _AP_S3>
INLINE ap_concat_ref& operator =(const ap_range_ref<_AP_W3,_AP_S3>& val) {
ap_private<_AP_W1+_AP_W2, false> tmpVal(val);
return operator =(tmpVal);
}
template<int _AP_W3, int _AP_I3, bool _AP_S3,
ap_q_mode _AP_Q3, ap_o_mode _AP_O3, int _AP_N3>
INLINE ap_concat_ref& operator= (const af_range_ref<_AP_W3, _AP_I3, _AP_S3,
_AP_Q3, _AP_O3, _AP_N3>& val) {
return operator = ((const ap_private<_AP_W3, false>)(val));
}
template<int _AP_W3, int _AP_I3, bool _AP_S3,
ap_q_mode _AP_Q3, ap_o_mode _AP_O3, int _AP_N3>
INLINE ap_concat_ref& operator= (const ap_fixed_base<_AP_W3, _AP_I3, _AP_S3,
_AP_Q3, _AP_O3, _AP_N3>& val) {
return operator = (val.to_ap_private());
}
template<int _AP_W3, int _AP_I3, bool _AP_S3,
ap_q_mode _AP_Q3, ap_o_mode _AP_O3, int _AP_N3>
INLINE ap_concat_ref& operator= (const af_bit_ref<_AP_W3, _AP_I3, _AP_S3,
_AP_Q3, _AP_O3, _AP_N3>& val) {
return operator=((unsigned long long)(bool)(val));
}
INLINE operator ap_private<_AP_WR, false> () const {
return get();
}
INLINE operator unsigned long long () const {
return get().to_uint64();
}
template<int _AP_W3, bool _AP_S3>
INLINE ap_concat_ref<_AP_WR, ap_concat_ref, _AP_W3, ap_range_ref<_AP_W3, _AP_S3> >
operator, (const ap_range_ref<_AP_W3, _AP_S3> &a2) {
return ap_concat_ref<_AP_WR, ap_concat_ref,
_AP_W3, ap_range_ref<_AP_W3, _AP_S3> >(*this,
const_cast<ap_range_ref<_AP_W3, _AP_S3> &>(a2));
}
template<int _AP_W3, bool _AP_S3>
INLINE ap_concat_ref<_AP_WR, ap_concat_ref, _AP_W3, ap_private<_AP_W3, _AP_S3> >
operator, (ap_private<_AP_W3, _AP_S3> &a2) {
return ap_concat_ref<_AP_WR, ap_concat_ref,
_AP_W3, ap_private<_AP_W3, _AP_S3> >(*this, a2);
}
template<int _AP_W3, bool _AP_S3>
INLINE ap_concat_ref<_AP_WR, ap_concat_ref, _AP_W3, ap_private<_AP_W3, _AP_S3> >
operator, (const ap_private<_AP_W3, _AP_S3> &a2) {
return ap_concat_ref<_AP_WR, ap_concat_ref,
_AP_W3, ap_private<_AP_W3, _AP_S3> >(*this,
const_cast<ap_private<_AP_W3, _AP_S3>&>(a2));
}
template<int _AP_W3, bool _AP_S3>
INLINE ap_concat_ref<_AP_WR, ap_concat_ref, 1, ap_bit_ref<_AP_W3, _AP_S3> >
operator, (const ap_bit_ref<_AP_W3, _AP_S3> &a2) {
return ap_concat_ref<_AP_WR, ap_concat_ref,
1, ap_bit_ref<_AP_W3, _AP_S3> >(*this,
const_cast<ap_bit_ref<_AP_W3, _AP_S3> &>(a2));
}
template<int _AP_W3, typename _AP_T3, int _AP_W4, typename _AP_T4>
INLINE ap_concat_ref<_AP_WR, ap_concat_ref, _AP_W3+_AP_W4, ap_concat_ref<_AP_W3,_AP_T3,_AP_W4,_AP_T4> >
operator, (const ap_concat_ref<_AP_W3,_AP_T3,_AP_W4,_AP_T4> &a2)
{
return ap_concat_ref<_AP_WR, ap_concat_ref,
_AP_W3+_AP_W4, ap_concat_ref<_AP_W3,_AP_T3,_AP_W4,
_AP_T4> >(*this, const_cast<ap_concat_ref<_AP_W3,
_AP_T3,_AP_W4, _AP_T4>& >(a2));
}
template <int _AP_W3, int _AP_I3, bool _AP_S3, ap_q_mode _AP_Q3, ap_o_mode _AP_O3, int _AP_N3>
INLINE
ap_concat_ref<_AP_WR, ap_concat_ref, _AP_W3, af_range_ref<_AP_W3, _AP_I3, _AP_S3, _AP_Q3, _AP_O3, _AP_N3> >
operator, (const af_range_ref<_AP_W3, _AP_I3, _AP_S3, _AP_Q3,
_AP_O3, _AP_N3> &a2) {
return ap_concat_ref<_AP_WR, ap_concat_ref, _AP_W3, af_range_ref<_AP_W3,
_AP_I3, _AP_S3, _AP_Q3, _AP_O3, _AP_N3> >(*this,
const_cast<af_range_ref<_AP_W3,_AP_I3, _AP_S3, _AP_Q3,
_AP_O3, _AP_N3>& >(a2));
}
template <int _AP_W3, int _AP_I3, bool _AP_S3, ap_q_mode _AP_Q3, ap_o_mode _AP_O3, int _AP_N3>
INLINE
ap_concat_ref<_AP_WR, ap_concat_ref, 1, af_bit_ref<_AP_W3, _AP_I3, _AP_S3, _AP_Q3, _AP_O3, _AP_N3> >
operator, (const af_bit_ref<_AP_W3, _AP_I3, _AP_S3, _AP_Q3,
_AP_O3, _AP_N3> &a2) {
return ap_concat_ref<_AP_WR, ap_concat_ref, 1, af_bit_ref<_AP_W3,
_AP_I3, _AP_S3, _AP_Q3, _AP_O3, _AP_N3> >(*this,
const_cast<af_bit_ref<_AP_W3,_AP_I3, _AP_S3,
_AP_Q3, _AP_O3, _AP_N3>& >(a2));
}
template<int _AP_W3, bool _AP_S3>
INLINE ap_private<AP_MAX(_AP_WR,_AP_W3), _AP_S3>
operator & (const ap_private<_AP_W3,_AP_S3>& a2) {
return get() & a2;
}
template<int _AP_W3, bool _AP_S3>
INLINE ap_private<AP_MAX(_AP_WR,_AP_W3), _AP_S3>
operator | (const ap_private<_AP_W3,_AP_S3>& a2) {
return get() | a2;
}
template<int _AP_W3, bool _AP_S3>
INLINE ap_private<AP_MAX(_AP_WR,_AP_W3), _AP_S3>
operator ^ (const ap_private<_AP_W3,_AP_S3>& a2) {
return ap_private<AP_MAX(_AP_WR,_AP_W3), _AP_S3>(get() ^ a2);
}
INLINE const ap_private<_AP_WR, false> get() const
{
ap_private<_AP_W1+_AP_W2, false> tmpVal = ap_private<_AP_W1+_AP_W2, false> (mbv1.get());
ap_private<_AP_W1+_AP_W2, false> tmpVal2 = ap_private<_AP_W1+_AP_W2, false> (mbv2.get());
int W_ref2 = mbv2.length();
tmpVal <<= W_ref2;
tmpVal |= tmpVal2;
return tmpVal;
}
INLINE const ap_private<_AP_WR, false> get() {
ap_private<_AP_W1+_AP_W2, false> tmpVal = ap_private<_AP_W1+_AP_W2, false> ( mbv1.get());
ap_private<_AP_W1+_AP_W2, false> tmpVal2 = ap_private<_AP_W1+_AP_W2, false> (mbv2.get());
int W_ref2 = mbv2.length();
tmpVal <<= W_ref2;
tmpVal |= tmpVal2;
return tmpVal;
}
template <int _AP_W3>
INLINE void set(const ap_private<_AP_W3,false> & val) {
ap_private<_AP_W1+_AP_W2, false> vval(val);
int W_ref1=mbv1.length();
int W_ref2=mbv2.length();
ap_private<_AP_W1,false> mask1(-1);
mask1>>=_AP_W1-W_ref1;
ap_private<_AP_W2,false> mask2(-1);
mask2>>=_AP_W2-W_ref2;
mbv1.set(ap_private<_AP_W1,false>((vval>>W_ref2)&mask1));
mbv2.set(ap_private<_AP_W2,false>(vval&mask2));
}
INLINE int length() const {
return mbv1.length()+mbv2.length();
}
INLINE std::string to_string(uint8_t radix=2) const {
return get().to_string(radix);
}
};
///Proxy class, which allows part selection to be used as rvalue(for reading) and
//lvalue(for writing)
///Range(slice) reference
//------------------------------------------------------------
template<int _AP_W, bool _AP_S>
struct ap_range_ref {
#ifdef _MSC_VER
#pragma warning( disable : 4521 4522 )
#endif /* #ifdef _MSC_VER */
ap_private<_AP_W,_AP_S> &d_bv;
int l_index;
int h_index;
public:
INLINE ap_range_ref(const ap_range_ref<_AP_W, _AP_S>& ref):
d_bv(ref.d_bv), l_index(ref.l_index), h_index(ref.h_index) {}
INLINE ap_range_ref(ap_private<_AP_W,_AP_S>* bv, int h, int l):
d_bv(*bv),l_index(l),h_index(h) {
//if (h < l)
//fprintf(stderr, "Warning! The bits selected will be returned in reverse order\n");
}
INLINE operator ap_private<_AP_W, false> () const {
ap_private<_AP_W, false> val(0);
if(h_index>=l_index) {
if (_AP_W > 64) {
val=d_bv;
ap_private<_AP_W,false> mask(-1);
mask>>=_AP_W-(h_index-l_index+1);
val>>=l_index;
val&=mask;
} else {
const static uint64_t mask = (~0ULL>> (64>_AP_W ? (64-_AP_W):0));
val = (d_bv >> l_index) & (mask >>(_AP_W-(h_index-l_index+1)));
}
} else {
for(int i=0, j=l_index;j>=0&&j>=h_index;j--,i++)
if((d_bv)[j]) val.set(i);
}
return val;
}
INLINE operator unsigned long long () const {
return to_uint64();
}
template<int _AP_W2,bool _AP_S2>
INLINE ap_range_ref& operator =(const ap_private<_AP_W2,_AP_S2>& val) {
ap_private<_AP_W,false> vval=ap_private<_AP_W,false>(val);
if (l_index>h_index) {
for (int i=0, j=l_index;j>=0&&j>=h_index;j--,i++)
(vval)[i]? d_bv.set(j):d_bv.clear(j);
} else {
if (_AP_W > 64) {
ap_private<_AP_W,false> mask(-1);
if (l_index>0) {
mask<<=l_index;
vval<<=l_index;
}
if(h_index<_AP_W-1)
{
ap_private<_AP_W,false> mask2(-1);
mask2>>=_AP_W-h_index-1;
mask&=mask2;
vval&=mask2;
}
mask.flip();
d_bv&=mask;
d_bv|=vval;
} else {
unsigned shift = 64-_AP_W;
uint64_t mask = ~0ULL>>(shift);
if(l_index>0)
{
vval = mask & vval << l_index;
mask = mask & mask << l_index;
}
if(h_index<_AP_W-1)
{
uint64_t mask2 = mask;
mask2 >>= (_AP_W-h_index-1);
mask&=mask2;
vval&=mask2;
}
mask = ~mask;
d_bv&=mask;
d_bv|=vval;
}
}
return *this;
}
INLINE ap_range_ref& operator = (unsigned long long val)
{
const ap_private<_AP_W,_AP_S> vval=val;
return operator = (vval);
}
INLINE ap_range_ref& operator =(const ap_range_ref<_AP_W, _AP_S>& val)
{
const ap_private<_AP_W, false> tmpVal(val);
return operator =(tmpVal);
}
template<int _AP_W3, typename _AP_T3, int _AP_W4, typename _AP_T4>
INLINE ap_range_ref& operator =
(const ap_concat_ref <_AP_W3, _AP_T3, _AP_W4, _AP_T4>& val)
{
const ap_private<_AP_W, false> tmpVal(val);
return operator = (tmpVal);
}
template <int _AP_W3, bool _AP_S3>
INLINE ap_range_ref& operator =(const ap_range_ref<_AP_W3,_AP_S3>& val)
{
const ap_private<_AP_W, false> tmpVal(val);
return operator =(tmpVal);
}
template<int _AP_W2, int _AP_I2, bool _AP_S2,
ap_q_mode _AP_Q2, ap_o_mode _AP_O2, int _AP_N2>
INLINE ap_range_ref& operator= (const af_range_ref<_AP_W2, _AP_I2, _AP_S2,
_AP_Q2, _AP_O2, _AP_N2>& val) {
return operator=((const ap_private<_AP_W2, _AP_S2>)(val));
}
template<int _AP_W2, int _AP_I2, bool _AP_S2,
ap_q_mode _AP_Q2, ap_o_mode _AP_O2, int _AP_N2>
INLINE ap_range_ref& operator= (const ap_fixed_base<_AP_W2, _AP_I2, _AP_S2,
_AP_Q2, _AP_O2, _AP_N2>& val) {
return operator=(val.to_ap_private());
}
template<int _AP_W2, int _AP_I2, bool _AP_S2,
ap_q_mode _AP_Q2, ap_o_mode _AP_O2, int _AP_N2>
INLINE ap_range_ref& operator= (const af_bit_ref<_AP_W2, _AP_I2, _AP_S2,
_AP_Q2, _AP_O2, _AP_N2>& val) {
return operator=((unsigned long long)(bool)(val));
}
template<int _AP_W2, bool _AP_S2>
INLINE ap_range_ref& operator= (const ap_bit_ref<_AP_W2, _AP_S2>& val) {
return operator=((unsigned long long)(bool)(val));
}
template <int _AP_W2, bool _AP_S2>
INLINE
ap_concat_ref<_AP_W,ap_range_ref,_AP_W2,ap_range_ref<_AP_W2,_AP_S2> >
operator, (const ap_range_ref<_AP_W2,_AP_S2> &a2)
{
return
ap_concat_ref<_AP_W, ap_range_ref, _AP_W2,
ap_range_ref<_AP_W2,_AP_S2> >(*this,
const_cast<ap_range_ref<_AP_W2,_AP_S2>& >(a2));
}
template <int _AP_W2, bool _AP_S2>
INLINE ap_concat_ref<_AP_W,ap_range_ref,_AP_W2,ap_private<_AP_W2,_AP_S2> >
operator , (ap_private<_AP_W2,_AP_S2>& a2)
{
return
ap_concat_ref<_AP_W, ap_range_ref, _AP_W2, ap_private<_AP_W2,_AP_S2> >(*this, a2);
}
INLINE ap_concat_ref<_AP_W,ap_range_ref,_AP_W,ap_private<_AP_W,_AP_S> >
operator , (ap_private<_AP_W, _AP_S>& a2)
{
return
ap_concat_ref<_AP_W, ap_range_ref, _AP_W,
ap_private<_AP_W,_AP_S> >(*this, a2);
}
template <int _AP_W2, bool _AP_S2>
INLINE
ap_concat_ref<_AP_W,ap_range_ref,1,ap_bit_ref<_AP_W2,_AP_S2> >
operator, (const ap_bit_ref<_AP_W2,_AP_S2> &a2)
{
return
ap_concat_ref<_AP_W, ap_range_ref, 1,
ap_bit_ref<_AP_W2,_AP_S2> >(*this, const_cast<ap_bit_ref<
_AP_W2,_AP_S2>& >(a2));
}
template <int _AP_W2, typename _AP_T2, int _AP_W3, typename _AP_T3>
INLINE
ap_concat_ref<_AP_W, ap_range_ref, _AP_W2+_AP_W3, ap_concat_ref<_AP_W2, _AP_T2, _AP_W3, _AP_T3> >
operator, (const ap_concat_ref<_AP_W2, _AP_T2, _AP_W3, _AP_T3> &a2)
{
return ap_concat_ref<_AP_W, ap_range_ref, _AP_W2+_AP_W3,
ap_concat_ref<_AP_W2, _AP_T2, _AP_W3, _AP_T3> >(*this,
const_cast<ap_concat_ref<_AP_W2, _AP_T2, _AP_W3,
_AP_T3>& >(a2));
}
template <int _AP_W2, int _AP_I2, bool _AP_S2, ap_q_mode _AP_Q2, ap_o_mode _AP_O2, int _AP_N2>
INLINE
ap_concat_ref<_AP_W, ap_range_ref, _AP_W2, af_range_ref<_AP_W2, _AP_I2, _AP_S2, _AP_Q2, _AP_O2, _AP_N2> >
operator, (const af_range_ref<_AP_W2, _AP_I2, _AP_S2, _AP_Q2,
_AP_O2, _AP_N2> &a2) {
return ap_concat_ref<_AP_W, ap_range_ref, _AP_W2, af_range_ref<_AP_W2,
_AP_I2, _AP_S2, _AP_Q2, _AP_O2, _AP_N2> >(*this,
const_cast<af_range_ref<_AP_W2,_AP_I2, _AP_S2,
_AP_Q2, _AP_O2, _AP_N2>& >(a2));
}
template <int _AP_W2, int _AP_I2, bool _AP_S2, ap_q_mode _AP_Q2, ap_o_mode _AP_O2, int _AP_N2>
INLINE
ap_concat_ref<_AP_W, ap_range_ref, 1, af_bit_ref<_AP_W2, _AP_I2, _AP_S2, _AP_Q2, _AP_O2, _AP_N2> >
operator, (const af_bit_ref<_AP_W2, _AP_I2, _AP_S2, _AP_Q2,
_AP_O2, _AP_N2> &a2) {
return ap_concat_ref<_AP_W, ap_range_ref, 1, af_bit_ref<_AP_W2,
_AP_I2, _AP_S2, _AP_Q2, _AP_O2, _AP_N2> >(*this,
const_cast<af_bit_ref<_AP_W2,_AP_I2, _AP_S2,
_AP_Q2, _AP_O2, _AP_N2>& >(a2));
}
template<int _AP_W2, bool _AP_S2>
INLINE bool operator == (const ap_range_ref<_AP_W2, _AP_S2>& op2)
{
ap_private<_AP_W,false> lhs=get();
ap_private<_AP_W2,false> rhs=op2.get();
return lhs==rhs;
}
template<int _AP_W2, bool _AP_S2>
INLINE bool operator != (const ap_range_ref<_AP_W2, _AP_S2>& op2)
{
ap_private<_AP_W,false> lhs=get();
ap_private<_AP_W2,false> rhs=op2.get();
return lhs!=rhs;
}
template<int _AP_W2, bool _AP_S2>
INLINE bool operator > (const ap_range_ref<_AP_W2, _AP_S2>& op2)
{
ap_private<_AP_W,false> lhs=get();
ap_private<_AP_W2,false> rhs=op2.get();
return lhs>rhs;
}
template<int _AP_W2, bool _AP_S2>
INLINE bool operator >= (const ap_range_ref<_AP_W2, _AP_S2>& op2)
{
ap_private<_AP_W,false> lhs=get();
ap_private<_AP_W2,false> rhs=op2.get();
return lhs>=rhs;
}
template<int _AP_W2, bool _AP_S2>
INLINE bool operator < (const ap_range_ref<_AP_W2, _AP_S2>& op2)
{
ap_private<_AP_W,false> lhs=get();
ap_private<_AP_W2,false> rhs=op2.get();
return lhs<rhs;
}
template<int _AP_W2, bool _AP_S2>
INLINE bool operator <= (const ap_range_ref<_AP_W2, _AP_S2>& op2)
{
ap_private<_AP_W,false> lhs=get();
ap_private<_AP_W2,false> rhs=op2.get();
return lhs<=rhs;
}
template<int _AP_W2>
INLINE void set(const ap_private<_AP_W2,false>& val)
{
ap_private<_AP_W,_AP_S> vval=val;
if(l_index>h_index)
{
for(int i=0, j=l_index;j>=0&&j>=h_index;j--,i++)
(vval)[i]? d_bv.set(j):d_bv.clear(j);
} else {
if (_AP_W>64 ) {
ap_private<_AP_W,_AP_S> mask(-1);
if(l_index>0)
{
ap_private<_AP_W,false> mask1(-1);
mask1>>=_AP_W-l_index;
mask1.flip();
mask=mask1;
//vval&=mask1;
vval<<=l_index;
}
if(h_index<_AP_W-1)
{
ap_private<_AP_W,false> mask2(-1);
mask2<<=h_index+1;
mask2.flip();
mask&=mask2;
vval&=mask2;
}
mask.flip();
d_bv&=mask;
d_bv|=vval;
} else {
uint64_t mask = ~0ULL >> (64-_AP_W);
if(l_index>0)
{
uint64_t mask1 = mask;
mask1=mask & (mask1>>(_AP_W-l_index));
vval =mask&( vval <<l_index);
mask=~mask1&mask;
//vval&=mask1;
}
if(h_index<_AP_W-1) {
uint64_t mask2 = ~0ULL >> (64-_AP_W);
mask2 = mask &(mask2<<(h_index+1));
mask&=~mask2;
vval&=~mask2;
}
d_bv&=(~mask&(~0ULL >> (64-_AP_W)));
d_bv|=vval;
}
}
}
INLINE ap_private<_AP_W,false> get() const
{
ap_private<_AP_W,false> val(0);
if(h_index<l_index) {
for(int i=0, j=l_index;j>=0&&j>=h_index;j--,i++)
if((d_bv)[j]) val.set(i);
} else {
val=d_bv;
val>>=l_index;
if(h_index<_AP_W-1)
{
if (_AP_W <= 64) {
const static uint64_t mask = (~0ULL>> (64>_AP_W ? (64-_AP_W):0));
val &= (mask>> (_AP_W-(h_index-l_index+1)));
} else {
ap_private<_AP_W,false> mask(-1);
mask>>=_AP_W-(h_index-l_index+1);
val&=mask;
}
}
}
return val;
}
INLINE ap_private<_AP_W,false> get()
{
ap_private<_AP_W,false> val(0);
if(h_index<l_index) {
for(int i=0, j=l_index;j>=0&&j>=h_index;j--,i++)
if((d_bv)[j]) val.set(i);
} else {
val=d_bv;
val>>=l_index;
if(h_index<_AP_W-1)
{
if (_AP_W <= 64 ) {
static const uint64_t mask = ~0ULL>> (64>_AP_W ? (64-_AP_W):0);
return val &= ( (mask) >> (_AP_W - (h_index-l_index+1)));
} else {
ap_private<_AP_W,false> mask(-1);
mask>>=_AP_W-(h_index-l_index+1);
val&=mask;
}
}
}
return val;
}
INLINE int length() const
{
return h_index>=l_index?h_index-l_index+1:l_index-h_index+1;
}
INLINE int to_int() const
{
ap_private<_AP_W,false> val=get();
return val.to_int();
}
INLINE unsigned int to_uint() const
{
ap_private<_AP_W,false> val=get();
return val.to_uint();
}
INLINE long to_long() const
{
ap_private<_AP_W,false> val=get();
return val.to_long();
}
INLINE unsigned long to_ulong() const
{
ap_private<_AP_W,false> val=get();
return val.to_ulong();
}
INLINE ap_slong to_int64() const
{
ap_private<_AP_W,false> val=get();
return val.to_int64();
}
INLINE ap_ulong to_uint64() const
{
ap_private<_AP_W,false> val=get();
return val.to_uint64();
}
INLINE std::string to_string(uint8_t radix=2) const {
return get().to_string(radix);
}
};
///Proxy class, which allows bit selection to be used as rvalue(for reading) and
//lvalue(for writing)
///Bit reference
//--------------------------------------------------------------
template <int _AP_W, bool _AP_S>
struct ap_bit_ref {
#ifdef _MSC_VER
#pragma warning( disable : 4521 4522 )
#endif
ap_private<_AP_W,_AP_S>& d_bv;
int d_index;
public:
INLINE ap_bit_ref(const ap_bit_ref<_AP_W, _AP_S>& ref):
d_bv(ref.d_bv), d_index(ref.d_index) {}
INLINE ap_bit_ref(ap_private<_AP_W,_AP_S>& bv, int index=0):
d_bv(bv),d_index(index)
{
#ifdef _AP_DEBUG_
assert(d_index<_AP_W&&"index out of bound");
#endif
}
INLINE operator bool () const
{
return d_bv.get_bit(d_index);
}
INLINE bool to_bool() const
{
return operator bool ();
}
INLINE ap_bit_ref& operator = (unsigned long long val)
{
if(val)
d_bv.set(d_index);
else
d_bv.clear(d_index);
return *this;
}
#if 0
INLINE ap_bit_ref& operator = (bool val)
{
if(val)
d_bv.set(d_index);
else
d_bv.clear(d_index);
return *this;
}
#endif
template<int _AP_W2, bool _AP_S2>
INLINE ap_bit_ref& operator =(const ap_private<_AP_W2,_AP_S2>& val)
{
return operator =((unsigned long long)(val != 0));
}
template<int _AP_W2, bool _AP_S2>
INLINE ap_bit_ref& operator =(const ap_bit_ref<_AP_W2,_AP_S2>& val)
{
return operator =((unsigned long long)(bool)val);
}
INLINE ap_bit_ref& operator =(const ap_bit_ref<_AP_W,_AP_S>& val)
{
return operator =((unsigned long long)(bool)val);
}
template<int _AP_W2, bool _AP_S2>
INLINE ap_bit_ref& operator =(const ap_range_ref<_AP_W2,_AP_S2>& val)
{
return operator =((unsigned long long)(bool) val);
}
template<int _AP_W2, int _AP_I2, bool _AP_S2,
ap_q_mode _AP_Q2, ap_o_mode _AP_O2, int _AP_N2>
INLINE ap_bit_ref& operator= (const af_range_ref<_AP_W2, _AP_I2, _AP_S2,
_AP_Q2, _AP_O2, _AP_N2>& val) {
return operator=((const ap_private<_AP_W2, false>)(val));
}
template<int _AP_W2, int _AP_I2, bool _AP_S2,
ap_q_mode _AP_Q2, ap_o_mode _AP_O2, int _AP_N2>
INLINE ap_bit_ref& operator= (const af_bit_ref<_AP_W2, _AP_I2, _AP_S2,
_AP_Q2, _AP_O2, _AP_N2>& val) {
return operator=((unsigned long long)(bool)(val));
}
template<int _AP_W2, typename _AP_T2, int _AP_W3, typename _AP_T3>
INLINE ap_bit_ref& operator= (const ap_concat_ref<_AP_W2, _AP_T3, _AP_W3, _AP_T3>& val) {
return operator=((const ap_private<_AP_W2 + _AP_W3, false>)(val));
}
template<int _AP_W2, bool _AP_S2>
INLINE ap_concat_ref<1, ap_bit_ref, _AP_W2, ap_private<_AP_W2,_AP_S2> >
operator , (ap_private<_AP_W2, _AP_S2>& a2)
{
return ap_concat_ref<1, ap_bit_ref, _AP_W2, ap_private<_AP_W2,_AP_S2> >(*this, a2);
}
template<int _AP_W2, bool _AP_S2>
INLINE ap_concat_ref<1, ap_bit_ref, _AP_W2, ap_range_ref<_AP_W2,_AP_S2> >
operator, (const ap_range_ref<_AP_W2, _AP_S2> &a2)
{
return
ap_concat_ref<1, ap_bit_ref, _AP_W2, ap_range_ref<_AP_W2,_AP_S2> >(*this,
const_cast<ap_range_ref<_AP_W2, _AP_S2> &>(a2));
}
template<int _AP_W2, bool _AP_S2>
INLINE ap_concat_ref<1, ap_bit_ref, 1, ap_bit_ref<_AP_W2,_AP_S2> >
operator, (const ap_bit_ref<_AP_W2, _AP_S2> &a2)
{
return
ap_concat_ref<1, ap_bit_ref, 1, ap_bit_ref<_AP_W2,_AP_S2> >(*this,
const_cast<ap_bit_ref<_AP_W2, _AP_S2> &>(a2));
}
INLINE ap_concat_ref<1, ap_bit_ref, 1, ap_bit_ref >
operator, (const ap_bit_ref &a2)
{
return
ap_concat_ref<1, ap_bit_ref, 1, ap_bit_ref >(*this,
const_cast<ap_bit_ref&>(a2));
}
template<int _AP_W2, typename _AP_T2, int _AP_W3, typename _AP_T3>
INLINE ap_concat_ref<1, ap_bit_ref, _AP_W2+_AP_W3, ap_concat_ref<_AP_W2,_AP_T2,_AP_W3,_AP_T3> >
operator, (const ap_concat_ref<_AP_W2,_AP_T2,_AP_W3,_AP_T3> &a2)
{
return
ap_concat_ref<1,ap_bit_ref,_AP_W2+_AP_W3,
ap_concat_ref<_AP_W2,_AP_T2,_AP_W3,_AP_T3> >(*this,
const_cast<ap_concat_ref<_AP_W2,_AP_T2,_AP_W3,_AP_T3>& >(a2));
}
template <int _AP_W2, int _AP_I2, bool _AP_S2, ap_q_mode _AP_Q2, ap_o_mode _AP_O2, int _AP_N2>
INLINE
ap_concat_ref<1, ap_bit_ref, _AP_W2, af_range_ref<_AP_W2, _AP_I2, _AP_S2, _AP_Q2, _AP_O2, _AP_N2> >
operator, (const af_range_ref<_AP_W2, _AP_I2, _AP_S2, _AP_Q2,
_AP_O2, _AP_N2> &a2) {
return ap_concat_ref<1, ap_bit_ref, _AP_W2, af_range_ref<_AP_W2,
_AP_I2, _AP_S2, _AP_Q2, _AP_O2, _AP_N2> >(*this,
const_cast<af_range_ref<_AP_W2,_AP_I2, _AP_S2, _AP_Q2,
_AP_O2, _AP_N2>& >(a2));
}
template <int _AP_W2, int _AP_I2, bool _AP_S2, ap_q_mode _AP_Q2, ap_o_mode _AP_O2, int _AP_N2>
INLINE
ap_concat_ref<1, ap_bit_ref, 1, af_bit_ref<_AP_W2, _AP_I2, _AP_S2, _AP_Q2, _AP_O2, _AP_N2> >
operator, (const af_bit_ref<_AP_W2, _AP_I2, _AP_S2, _AP_Q2,
_AP_O2, _AP_N2> &a2) {
return ap_concat_ref<1, ap_bit_ref, 1, af_bit_ref<_AP_W2,
_AP_I2, _AP_S2, _AP_Q2, _AP_O2, _AP_N2> >(*this,
const_cast<af_bit_ref<_AP_W2, _AP_I2, _AP_S2,
_AP_Q2, _AP_O2, _AP_N2>& >(a2));
}
template<int _AP_W2, bool _AP_S2>
INLINE bool operator == (const ap_bit_ref<_AP_W2, _AP_S2>& op) {
return get() == op.get();
}
template<int _AP_W2, bool _AP_S2>
INLINE bool operator != (const ap_bit_ref<_AP_W2, _AP_S2>& op) {
return get() != op.get();
}
INLINE bool get() const
{
return operator bool ();
}
INLINE bool get()
{
return operator bool ();
}
template <int _AP_W3>
INLINE void set(const ap_private<_AP_W3, false>& val)
{
operator = (val);
}
INLINE bool operator ~ () const {
bool bit = (d_bv)[d_index];
return bit ? false : true;
}
INLINE int length() const { return 1; }
INLINE std::string to_string() const {
bool val = get();
return val ? "1" : "0";
}
};
/// Operators mixing Integers with AP_Int
// ----------------------------------------------------------------
#if 1
#define OP_BIN_MIX_INT(BIN_OP, C_TYPE, _AP_WI, _AP_SI, RTYPE) \
template<int _AP_W, bool _AP_S> \
INLINE typename ap_private<_AP_WI,_AP_SI>::template RType<_AP_W,_AP_S>::RTYPE \
operator BIN_OP ( C_TYPE i_op, const ap_private<_AP_W,_AP_S> &op) { \
return ap_private<_AP_WI,_AP_SI>(i_op).operator BIN_OP (op); \
} \
template<int _AP_W, bool _AP_S> \
INLINE typename ap_private<_AP_W,_AP_S>::template RType<_AP_WI,_AP_SI>::RTYPE \
operator BIN_OP ( const ap_private<_AP_W,_AP_S> &op, C_TYPE i_op) { \
return op.operator BIN_OP (ap_private<_AP_WI,_AP_SI>(i_op)); \
}
#else
#define OP_BIN_MIX_INT(BIN_OP, C_TYPE, _AP_WI, _AP_SI, RTYPE) \
template<int _AP_W, bool _AP_S> \
INLINE typename ap_private<_AP_WI,_AP_SI>::template RType<_AP_W,_AP_S>::RTYPE \
operator BIN_OP ( C_TYPE i_op, const ap_private<_AP_W,_AP_S> &op) { \
return ap_private<_AP_WI,_AP_SI>(i_op).operator BIN_OP (op); \
} \
template<int _AP_W, bool _AP_S> \
INLINE typename ap_private<_AP_W,_AP_S>::template RType<_AP_WI,_AP_SI>::RTYPE \
operator BIN_OP ( const ap_private<_AP_W,_AP_S> &op, C_TYPE i_op) { \
return op.operator BIN_OP (ap_private<_AP_WI,_AP_SI>(i_op)); \
}
#endif
#define OP_REL_MIX_INT(REL_OP, C_TYPE, _AP_W2, _AP_S2) \
template<int _AP_W, bool _AP_S> \
INLINE bool operator REL_OP ( const ap_private<_AP_W,_AP_S> &op, C_TYPE op2) { \
return op.operator REL_OP (ap_private<_AP_W2, _AP_S2>(op2)); \
} \
template<int _AP_W, bool _AP_S> \
INLINE bool operator REL_OP ( C_TYPE op2, const ap_private<_AP_W,_AP_S> &op) { \
return ap_private<_AP_W2,_AP_S2>(op2).operator REL_OP (op); \
}
#define OP_ASSIGN_MIX_INT(ASSIGN_OP, C_TYPE, _AP_W2, _AP_S2) \
template<int _AP_W, bool _AP_S> \
INLINE ap_private<_AP_W,_AP_S> &operator ASSIGN_OP ( ap_private<_AP_W,_AP_S> &op, C_TYPE op2) { \
return op.operator ASSIGN_OP (ap_private<_AP_W2,_AP_S2>(op2)); \
}
#define OP_BIN_SHIFT_INT(BIN_OP, C_TYPE, _AP_WI, _AP_SI, RTYPE) \
template<int _AP_W, bool _AP_S> \
C_TYPE operator BIN_OP ( C_TYPE i_op, const ap_private<_AP_W,_AP_S> &op) { \
return i_op BIN_OP (op.getVal()); \
} \
template<int _AP_W, bool _AP_S> \
INLINE typename ap_private<_AP_W,_AP_S>::template RType<_AP_WI,_AP_SI>::RTYPE \
operator BIN_OP ( const ap_private<_AP_W,_AP_S> &op, C_TYPE i_op) { \
return op.operator BIN_OP (i_op); \
}
#define OP_ASSIGN_RSHIFT_INT(ASSIGN_OP, C_TYPE, _AP_W2, _AP_S2) \
template<int _AP_W, bool _AP_S> \
INLINE ap_private<_AP_W,_AP_S> &operator ASSIGN_OP ( ap_private<_AP_W,_AP_S> &op, C_TYPE op2) { \
op = op.operator >> (op2); \
return op; \
}
#define OP_ASSIGN_LSHIFT_INT(ASSIGN_OP, C_TYPE, _AP_W2, _AP_S2) \
template<int _AP_W, bool _AP_S> \
INLINE ap_private<_AP_W,_AP_S> &operator ASSIGN_OP ( ap_private<_AP_W,_AP_S> &op, C_TYPE op2) { \
op = op.operator << (op2); \
return op; \
}
#define OPS_MIX_INT(C_TYPE, WI, SI) \
OP_BIN_MIX_INT(*, C_TYPE, WI, SI, mult) \
OP_BIN_MIX_INT(+, C_TYPE, WI, SI, plus) \
OP_BIN_MIX_INT(-, C_TYPE, WI, SI, minus) \
OP_BIN_MIX_INT(/, C_TYPE, WI, SI, div) \
OP_BIN_MIX_INT(%, C_TYPE, WI, SI, mod) \
OP_BIN_MIX_INT(&, C_TYPE, WI, SI, logic) \
OP_BIN_MIX_INT(|, C_TYPE, WI, SI, logic) \
OP_BIN_MIX_INT(^, C_TYPE, WI, SI, logic) \
OP_BIN_SHIFT_INT(>>, C_TYPE, WI, SI, arg1) \
OP_BIN_SHIFT_INT(<<, C_TYPE, WI, SI, arg1) \
\
OP_REL_MIX_INT(==, C_TYPE, WI, SI) \
OP_REL_MIX_INT(!=, C_TYPE, WI, SI) \
OP_REL_MIX_INT(>, C_TYPE, WI, SI) \
OP_REL_MIX_INT(>=, C_TYPE, WI, SI) \
OP_REL_MIX_INT(<, C_TYPE, WI, SI) \
OP_REL_MIX_INT(<=, C_TYPE, WI, SI) \
\
OP_ASSIGN_MIX_INT(+=, C_TYPE, WI, SI) \
OP_ASSIGN_MIX_INT(-=, C_TYPE, WI, SI) \
OP_ASSIGN_MIX_INT(*=, C_TYPE, WI, SI) \
OP_ASSIGN_MIX_INT(/=, C_TYPE, WI, SI) \
OP_ASSIGN_MIX_INT(%=, C_TYPE, WI, SI) \
OP_ASSIGN_MIX_INT(&=, C_TYPE, WI, SI) \
OP_ASSIGN_MIX_INT(|=, C_TYPE, WI, SI) \
OP_ASSIGN_MIX_INT(^=, C_TYPE, WI, SI) \
OP_ASSIGN_RSHIFT_INT(>>=, C_TYPE, WI, SI) \
OP_ASSIGN_LSHIFT_INT(<<=, C_TYPE, WI, SI)
OPS_MIX_INT(bool, 1, false)
OPS_MIX_INT(char, 8, true)
OPS_MIX_INT(signed char, 8, true)
OPS_MIX_INT(unsigned char, 8, false)
OPS_MIX_INT(short, 16, true)
OPS_MIX_INT(unsigned short, 16, false)
OPS_MIX_INT(int, 32, true)
OPS_MIX_INT(unsigned int, 32, false)
# if defined __x86_64__
OPS_MIX_INT(long, 64, true)
OPS_MIX_INT(unsigned long, 64, false)
# else
OPS_MIX_INT(long, 32, true)
OPS_MIX_INT(unsigned long, 32, false)
# endif
OPS_MIX_INT(ap_slong, 64, true)
OPS_MIX_INT(ap_ulong, 64, false)
#define OP_BIN_MIX_RANGE(BIN_OP, RTYPE) \
template<int _AP_W1, bool _AP_S1, int _AP_W2, bool _AP_S2> \
INLINE typename ap_private<_AP_W1,_AP_S1>::template RType<_AP_W2,_AP_S2>::RTYPE \
operator BIN_OP ( const ap_range_ref<_AP_W1,_AP_S1>& op1, const ap_private<_AP_W2,_AP_S2>& op2) { \
return ap_private<_AP_W1, false>(op1).operator BIN_OP (op2); \
} \
template<int _AP_W1, bool _AP_S1, int _AP_W2, bool _AP_S2> \
INLINE typename ap_private<_AP_W1,_AP_S1>::template RType<_AP_W2,_AP_S2>::RTYPE \
operator BIN_OP ( const ap_private<_AP_W1,_AP_S1>& op1, const ap_range_ref<_AP_W2,_AP_S2>& op2) { \
return op1.operator BIN_OP (ap_private<_AP_W2, false>(op2)); \
}
#define OP_REL_MIX_RANGE(REL_OP) \
template<int _AP_W1, bool _AP_S1, int _AP_W2, bool _AP_S2> \
INLINE bool operator REL_OP ( const ap_range_ref<_AP_W1,_AP_S1>& op1, const ap_private<_AP_W2,_AP_S2>& op2) { \
return ap_private<_AP_W1,false>(op1).operator REL_OP (op2); \
} \
template<int _AP_W1, bool _AP_S1, int _AP_W2, bool _AP_S2> \
INLINE bool operator REL_OP ( const ap_private<_AP_W1,_AP_S1>& op1, const ap_range_ref<_AP_W2,_AP_S2>& op2) { \
return op1.operator REL_OP (op2.operator ap_private<_AP_W2, false>()); \
}
#define OP_ASSIGN_MIX_RANGE(ASSIGN_OP) \
template<int _AP_W1, bool _AP_S1, int _AP_W2, bool _AP_S2> \
INLINE ap_private<_AP_W1,_AP_S1>& operator ASSIGN_OP ( ap_private<_AP_W1,_AP_S1>& op1, const ap_range_ref<_AP_W2,_AP_S2>& op2) { \
return op1.operator ASSIGN_OP (ap_private<_AP_W2, false>(op2)); \
} \
template<int _AP_W1, bool _AP_S1, int _AP_W2, bool _AP_S2> \
INLINE ap_range_ref<_AP_W1,_AP_S1>& operator ASSIGN_OP (ap_range_ref<_AP_W1,_AP_S1>& op1, ap_private<_AP_W2,_AP_S2>& op2) { \
ap_private<_AP_W1, false> tmp(op1); \
tmp.operator ASSIGN_OP (op2); \
op1 = tmp; \
return op1; \
}
OP_ASSIGN_MIX_RANGE(+=)
OP_ASSIGN_MIX_RANGE(-=)
OP_ASSIGN_MIX_RANGE(*=)
OP_ASSIGN_MIX_RANGE(/=)
OP_ASSIGN_MIX_RANGE(%=)
OP_ASSIGN_MIX_RANGE(>>=)
OP_ASSIGN_MIX_RANGE(<<=)
OP_ASSIGN_MIX_RANGE(&=)
OP_ASSIGN_MIX_RANGE(|=)
OP_ASSIGN_MIX_RANGE(^=)
OP_REL_MIX_RANGE(==)
OP_REL_MIX_RANGE(!=)
OP_REL_MIX_RANGE(>)
OP_REL_MIX_RANGE(>=)
OP_REL_MIX_RANGE(<)
OP_REL_MIX_RANGE(<=)
OP_BIN_MIX_RANGE(+, plus)
OP_BIN_MIX_RANGE(-, minus)
OP_BIN_MIX_RANGE(*, mult)
OP_BIN_MIX_RANGE(/, div)
OP_BIN_MIX_RANGE(%, mod)
OP_BIN_MIX_RANGE(>>, arg1)
OP_BIN_MIX_RANGE(<<, arg1)
OP_BIN_MIX_RANGE(&, logic)
OP_BIN_MIX_RANGE(|, logic)
OP_BIN_MIX_RANGE(^, logic)
#define OP_BIN_MIX_BIT(BIN_OP, RTYPE) \
template<int _AP_W1, bool _AP_S1, int _AP_W2, bool _AP_S2> \
INLINE typename ap_private<1, false>::template RType<_AP_W2,_AP_S2>::RTYPE \
operator BIN_OP ( const ap_bit_ref<_AP_W1,_AP_S1>& op1, const ap_private<_AP_W2,_AP_S2>& op2) { \
return ap_private<1, false>(op1).operator BIN_OP (op2); \
} \
template<int _AP_W1, bool _AP_S1, int _AP_W2, bool _AP_S2> \
INLINE typename ap_private<_AP_W1,_AP_S1>::template RType<1,false>::RTYPE \
operator BIN_OP ( const ap_private<_AP_W1,_AP_S1>& op1, const ap_bit_ref<_AP_W2,_AP_S2>& op2) { \
return op1.operator BIN_OP (ap_private<1, false>(op2)); \
}
#define OP_REL_MIX_BIT(REL_OP) \
template<int _AP_W1, bool _AP_S1, int _AP_W2, bool _AP_S2> \
INLINE bool operator REL_OP ( const ap_bit_ref<_AP_W1,_AP_S1>& op1, const ap_private<_AP_W2,_AP_S2>& op2) { \
return ap_private<_AP_W1,false>(op1).operator REL_OP (op2); \
} \
template<int _AP_W1, bool _AP_S1, int _AP_W2, bool _AP_S2> \
INLINE bool operator REL_OP ( const ap_private<_AP_W1,_AP_S1>& op1, const ap_bit_ref<_AP_W2,_AP_S2>& op2) { \
return op1.operator REL_OP (ap_private<1, false>(op2)); \
}
#define OP_ASSIGN_MIX_BIT(ASSIGN_OP) \
template<int _AP_W1, bool _AP_S1, int _AP_W2, bool _AP_S2> \
INLINE ap_private<_AP_W1,_AP_S1>& operator ASSIGN_OP ( ap_private<_AP_W1,_AP_S1>& op1, ap_bit_ref<_AP_W2,_AP_S2>& op2) { \
return op1.operator ASSIGN_OP (ap_private<1, false>(op2)); \
} \
template<int _AP_W1, bool _AP_S1, int _AP_W2, bool _AP_S2> \
INLINE ap_bit_ref<_AP_W1,_AP_S1>& operator ASSIGN_OP ( ap_bit_ref<_AP_W1,_AP_S1>& op1, ap_private<_AP_W2,_AP_S2>& op2) { \
ap_private<1, false> tmp(op1); \
tmp.operator ASSIGN_OP (op2); \
op1 = tmp; \
return op1; \
}
OP_ASSIGN_MIX_BIT(+=)
OP_ASSIGN_MIX_BIT(-=)
OP_ASSIGN_MIX_BIT(*=)
OP_ASSIGN_MIX_BIT(/=)
OP_ASSIGN_MIX_BIT(%=)
OP_ASSIGN_MIX_BIT(>>=)
OP_ASSIGN_MIX_BIT(<<=)
OP_ASSIGN_MIX_BIT(&=)
OP_ASSIGN_MIX_BIT(|=)
OP_ASSIGN_MIX_BIT(^=)
OP_REL_MIX_BIT(==)
OP_REL_MIX_BIT(!=)
OP_REL_MIX_BIT(>)
OP_REL_MIX_BIT(>=)
OP_REL_MIX_BIT(<)
OP_REL_MIX_BIT(<=)
OP_BIN_MIX_BIT(+, plus)
OP_BIN_MIX_BIT(-, minus)
OP_BIN_MIX_BIT(*, mult)
OP_BIN_MIX_BIT(/, div)
OP_BIN_MIX_BIT(%, mod)
OP_BIN_MIX_BIT(>>, arg1)
OP_BIN_MIX_BIT(<<, arg1)
OP_BIN_MIX_BIT(&, logic)
OP_BIN_MIX_BIT(|, logic)
OP_BIN_MIX_BIT(^, logic)
#define REF_REL_OP_MIX_INT(REL_OP, C_TYPE, _AP_W2, _AP_S2) \
template<int _AP_W, bool _AP_S> \
INLINE bool operator REL_OP ( const ap_range_ref<_AP_W,_AP_S> &op, C_TYPE op2) { \
return (ap_private<_AP_W, false>(op)).operator REL_OP (ap_private<_AP_W2,_AP_S2>(op2)); \
} \
template<int _AP_W, bool _AP_S> \
INLINE bool operator REL_OP ( C_TYPE op2, const ap_range_ref<_AP_W,_AP_S> &op) { \
return ap_private<_AP_W2,_AP_S2>(op2).operator REL_OP (ap_private<_AP_W, false>(op)); \
} \
template<int _AP_W, bool _AP_S> \
INLINE bool operator REL_OP ( const ap_bit_ref<_AP_W,_AP_S> &op, C_TYPE op2) { \
return (bool(op)) REL_OP op2; \
} \
template<int _AP_W, bool _AP_S> \
INLINE bool operator REL_OP ( C_TYPE op2, const ap_bit_ref<_AP_W,_AP_S> &op) { \
return op2 REL_OP (bool(op)); \
} \
template<int _AP_W, typename _AP_T, int _AP_W1, typename _AP_T1> \
INLINE bool operator REL_OP ( const ap_concat_ref<_AP_W,_AP_T, _AP_W1, _AP_T1> &op, C_TYPE op2) { \
return (ap_private<_AP_W + _AP_W1, false>(op)).operator REL_OP (ap_private<_AP_W2,_AP_S2>(op2)); \
} \
template<int _AP_W, typename _AP_T, int _AP_W1, typename _AP_T1> \
INLINE bool operator REL_OP ( C_TYPE op2, const ap_concat_ref<_AP_W,_AP_T, _AP_W1, _AP_T1> &op) { \
return ap_private<_AP_W2,_AP_S2>(op2).operator REL_OP (ap_private<_AP_W + _AP_W1, false>(op)); \
}
#define REF_REL_MIX_INT(C_TYPE, _AP_WI, _AP_SI) \
REF_REL_OP_MIX_INT(>, C_TYPE, _AP_WI, _AP_SI) \
REF_REL_OP_MIX_INT(<, C_TYPE, _AP_WI, _AP_SI) \
REF_REL_OP_MIX_INT(>=, C_TYPE, _AP_WI, _AP_SI) \
REF_REL_OP_MIX_INT(<=, C_TYPE, _AP_WI, _AP_SI) \
REF_REL_OP_MIX_INT(==, C_TYPE, _AP_WI, _AP_SI) \
REF_REL_OP_MIX_INT(!=, C_TYPE, _AP_WI, _AP_SI)
REF_REL_MIX_INT(bool, 1, false)
REF_REL_MIX_INT(char, 8, true)
REF_REL_MIX_INT(signed char, 8, true)
REF_REL_MIX_INT(unsigned char, 8, false)
REF_REL_MIX_INT(short, 16, true)
REF_REL_MIX_INT(unsigned short, 16, false)
REF_REL_MIX_INT(int, 32, true)
REF_REL_MIX_INT(unsigned int, 32, false)
# if defined __x86_64__
REF_REL_MIX_INT(long, 64, true)
REF_REL_MIX_INT(unsigned long, 64, false)
# else
REF_REL_MIX_INT(long, 32, true)
REF_REL_MIX_INT(unsigned long, 32, false)
# endif
REF_REL_MIX_INT(ap_slong, 64, true)
REF_REL_MIX_INT(ap_ulong, 64, false)
#define REF_BIN_OP_MIX_INT(BIN_OP, RTYPE, C_TYPE, _AP_W2, _AP_S2) \
template<int _AP_W, bool _AP_S> \
INLINE typename ap_private<_AP_W, false>::template RType<_AP_W2,_AP_S2>::RTYPE \
operator BIN_OP ( const ap_range_ref<_AP_W,_AP_S> &op, C_TYPE op2) { \
return (ap_private<_AP_W, false>(op)).operator BIN_OP (ap_private<_AP_W2,_AP_S2>(op2)); \
} \
template<int _AP_W, bool _AP_S> \
INLINE typename ap_private<_AP_W2, _AP_S2>::template RType<_AP_W,false>::RTYPE \
operator BIN_OP ( C_TYPE op2, const ap_range_ref<_AP_W,_AP_S> &op) { \
return ap_private<_AP_W2,_AP_S2>(op2).operator BIN_OP (ap_private<_AP_W, false>(op)); \
}
#define REF_BIN_MIX_INT(C_TYPE, _AP_WI, _AP_SI) \
REF_BIN_OP_MIX_INT(+, plus, C_TYPE, _AP_WI, _AP_SI) \
REF_BIN_OP_MIX_INT(-, minus, C_TYPE, _AP_WI, _AP_SI) \
REF_BIN_OP_MIX_INT(*, mult, C_TYPE, _AP_WI, _AP_SI) \
REF_BIN_OP_MIX_INT(/, div, C_TYPE, _AP_WI, _AP_SI) \
REF_BIN_OP_MIX_INT(%, mod, C_TYPE, _AP_WI, _AP_SI) \
REF_BIN_OP_MIX_INT(>>, arg1, C_TYPE, _AP_WI, _AP_SI) \
REF_BIN_OP_MIX_INT(<<, arg1, C_TYPE, _AP_WI, _AP_SI) \
REF_BIN_OP_MIX_INT(&, logic, C_TYPE, _AP_WI, _AP_SI) \
REF_BIN_OP_MIX_INT(|, logic, C_TYPE, _AP_WI, _AP_SI) \
REF_BIN_OP_MIX_INT(^, logic, C_TYPE, _AP_WI, _AP_SI)
REF_BIN_MIX_INT(bool, 1, false)
REF_BIN_MIX_INT(char, 8, true)
REF_BIN_MIX_INT(signed char, 8, true)
REF_BIN_MIX_INT(unsigned char, 8, false)
REF_BIN_MIX_INT(short, 16, true)
REF_BIN_MIX_INT(unsigned short, 16, false)
REF_BIN_MIX_INT(int, 32, true)
REF_BIN_MIX_INT(unsigned int, 32, false)
# if defined __x86_64__
REF_BIN_MIX_INT(long, 64, true)
REF_BIN_MIX_INT(unsigned long, 64, false)
#else
REF_BIN_MIX_INT(long, 32, true)
REF_BIN_MIX_INT(unsigned long, 32, false)
#endif
REF_BIN_MIX_INT(ap_slong, 64, true)
REF_BIN_MIX_INT(ap_ulong, 64, false)
#define REF_BIN_OP(BIN_OP, RTYPE) \
template<int _AP_W, bool _AP_S, int _AP_W2, bool _AP_S2> \
INLINE typename ap_private<_AP_W, false>::template RType<_AP_W2, false>::RTYPE \
operator BIN_OP (const ap_range_ref<_AP_W,_AP_S> &lhs, const ap_range_ref<_AP_W2,_AP_S2> &rhs) { \
return ap_private<_AP_W,false>(lhs).operator BIN_OP (ap_private<_AP_W2, false>(rhs)); \
}
REF_BIN_OP(+, plus)
REF_BIN_OP(-, minus)
REF_BIN_OP(*, mult)
REF_BIN_OP(/, div)
REF_BIN_OP(%, mod)
REF_BIN_OP(>>, arg1)
REF_BIN_OP(<<, arg1)
REF_BIN_OP(&, logic)
REF_BIN_OP(|, logic)
REF_BIN_OP(^, logic)
#if 1
#define CONCAT_OP_MIX_INT(C_TYPE, _AP_WI, _AP_SI) \
template<int _AP_W, bool _AP_S> \
INLINE \
ap_private< _AP_W + _AP_WI, false > \
operator, (const ap_private<_AP_W, _AP_S> &op1, C_TYPE op2) { \
ap_private<_AP_WI + _AP_W, false> val(op2); \
ap_private<_AP_WI + _AP_W, false> ret(op1); \
ret <<= _AP_WI; \
if (_AP_SI) { \
val <<= _AP_W; val >>= _AP_W; \
}\
ret |= val; \
return ret;\
} \
template<int _AP_W, bool _AP_S> \
INLINE \
ap_private< _AP_W + _AP_WI, false > \
operator, (C_TYPE op1, const ap_private<_AP_W, _AP_S>& op2) { \
ap_private<_AP_WI + _AP_W, false> val(op1); \
ap_private<_AP_WI + _AP_W, false> ret(op2); \
if (_AP_S) { \
ret <<= _AP_WI; ret >>= _AP_WI; \
} \
ret |= val << _AP_W; \
return ret; \
} \
template<int _AP_W, bool _AP_S> \
INLINE \
ap_private< _AP_W + _AP_WI, false > \
operator, (const ap_range_ref<_AP_W, _AP_S> &op1, C_TYPE op2) { \
ap_private<_AP_WI + _AP_W, false> val(op2); \
ap_private<_AP_WI + _AP_W, false> ret(op1); \
ret <<= _AP_WI; \
if (_AP_SI) { \
val <<= _AP_W; val >>= _AP_W; \
} \
ret |= val; \
return ret; \
} \
template<int _AP_W, bool _AP_S> \
INLINE \
ap_private< _AP_W + _AP_WI, false > \
operator, (C_TYPE op1, const ap_range_ref<_AP_W, _AP_S> &op2) { \
ap_private<_AP_WI + _AP_W, false> val(op1); \
ap_private<_AP_WI + _AP_W, false> ret(op2); \
int len = op2.length(); \
val <<= len; \
ret |= val; \
return ret; \
} \
template<int _AP_W, bool _AP_S> \
INLINE \
ap_private<_AP_WI + 1, false > \
operator, (const ap_bit_ref<_AP_W, _AP_S> &op1, C_TYPE op2) { \
ap_private<_AP_WI + 1, false> val(op2); \
val[_AP_WI] = op1; \
return val; \
} \
template<int _AP_W, bool _AP_S> \
INLINE \
ap_private<_AP_WI + 1, false > \
operator, (C_TYPE op1, const ap_bit_ref<_AP_W, _AP_S> &op2) { \
ap_private<_AP_WI + 1, false> val(op1); \
val <<= 1; \
val[0] = op2; \
return val; \
} \
template<int _AP_W, typename _AP_T, int _AP_W2, typename _AP_T2> \
INLINE \
ap_private<_AP_W + _AP_W2 + _AP_WI, false > \
operator, (const ap_concat_ref<_AP_W, _AP_T, _AP_W2, _AP_T2> &op1, C_TYPE op2) {\
ap_private<_AP_WI + _AP_W + _AP_W2, _AP_SI> val(op2);\
ap_private<_AP_WI + _AP_W + _AP_W2, _AP_SI> ret(op1);\
if (_AP_SI) { \
val <<= _AP_W + _AP_W2; val >>= _AP_W + _AP_W2; \
} \
ret <<= _AP_WI; \
ret |= val; \
return ret; \
}\
template<int _AP_W, typename _AP_T, int _AP_W2, typename _AP_T2> \
INLINE \
ap_private<_AP_W + _AP_W2 + _AP_WI, false > \
operator, (C_TYPE op1, const ap_concat_ref<_AP_W, _AP_T, _AP_W2, _AP_T2> &op2) {\
ap_private<_AP_WI + _AP_W + _AP_W2, _AP_SI> val(op1);\
ap_private<_AP_WI + _AP_W + _AP_W2, _AP_SI> ret(op2);\
int len = op2.length(); \
val <<= len; \
ret |= val;\
return ret; \
}\
template<int _AP_W, int _AP_I, bool _AP_S, ap_q_mode _AP_Q, ap_o_mode _AP_O, \
int _AP_N > \
INLINE \
ap_private< _AP_W + _AP_WI, false > \
operator, (const af_range_ref<_AP_W, _AP_I, _AP_S, _AP_Q, _AP_O, _AP_N> &op1, C_TYPE op2) { \
ap_private<_AP_WI + _AP_W, false> val(op2); \
ap_private<_AP_WI + _AP_W, false> ret(op1); \
if (_AP_SI) { \
val <<= _AP_W; val >>= _AP_W; \
}\
ret <<= _AP_WI; \
ret |= val; \
return ret; \
} \
template<int _AP_W, int _AP_I, bool _AP_S, ap_q_mode _AP_Q, ap_o_mode _AP_O, \
int _AP_N > \
INLINE \
ap_private< _AP_W + _AP_WI, false > \
operator, (C_TYPE op1, const af_range_ref<_AP_W, _AP_I, _AP_S, \
_AP_Q, _AP_O, _AP_N> &op2) { \
ap_private<_AP_WI + _AP_W, false> val(op1); \
ap_private<_AP_WI + _AP_W, false> ret(op2); \
int len = op2.length(); \
val <<= len; \
ret |= val; \
return ret; \
} \
template<int _AP_W, int _AP_I, bool _AP_S, ap_q_mode _AP_Q, ap_o_mode _AP_O, \
int _AP_N > \
INLINE \
ap_private< 1 + _AP_WI, false> \
operator, (const af_bit_ref<_AP_W, _AP_I, _AP_S, _AP_Q, _AP_O, \
_AP_N> &op1, C_TYPE op2) { \
ap_private<_AP_WI + 1, _AP_SI> val(op2); \
val[_AP_WI] = op1; \
return val; \
} \
template<int _AP_W, int _AP_I, bool _AP_S, ap_q_mode _AP_Q, ap_o_mode _AP_O, \
int _AP_N > \
INLINE \
ap_private< 1 + _AP_WI, false> \
operator, (C_TYPE op1, const af_bit_ref<_AP_W, _AP_I, _AP_S, _AP_Q,\
_AP_O, _AP_N> &op2) { \
ap_private<_AP_WI + 1, _AP_SI> val(op1); \
val <<= 1; \
val[0] = op2; \
return val; \
}
CONCAT_OP_MIX_INT(bool, 1, false)
CONCAT_OP_MIX_INT(char, 8, true)
CONCAT_OP_MIX_INT(signed char, 8, true)
CONCAT_OP_MIX_INT(unsigned char, 8, false)
CONCAT_OP_MIX_INT(short, 16, true)
CONCAT_OP_MIX_INT(unsigned short, 16, false)
CONCAT_OP_MIX_INT(int, 32, true)
CONCAT_OP_MIX_INT(unsigned int, 32, false)
# if defined __x86_64__
CONCAT_OP_MIX_INT(long, 64, true)
CONCAT_OP_MIX_INT(unsigned long, 64, false)
# else
CONCAT_OP_MIX_INT(long, 32, true)
CONCAT_OP_MIX_INT(unsigned long, 32, false)
# endif
CONCAT_OP_MIX_INT(ap_slong, 64, true)
CONCAT_OP_MIX_INT(ap_ulong, 64, false)
#endif
#if 1
#define CONCAT_SHIFT_MIX_INT(C_TYPE, op) \
template<int _AP_W, typename _AP_T, int _AP_W1, typename _AP_T1> \
INLINE ap_uint<_AP_W+_AP_W1> operator op (const ap_concat_ref<_AP_W, _AP_T, _AP_W1, _AP_T1> lhs, C_TYPE rhs) { \
return ((ap_uint<_AP_W+_AP_W1>)lhs.get()) op ((int)rhs); \
}
CONCAT_SHIFT_MIX_INT(long, <<)
CONCAT_SHIFT_MIX_INT(unsigned long, <<)
CONCAT_SHIFT_MIX_INT(unsigned int, <<)
CONCAT_SHIFT_MIX_INT(ap_ulong, <<)
CONCAT_SHIFT_MIX_INT(ap_slong, <<)
CONCAT_SHIFT_MIX_INT(long, >>)
CONCAT_SHIFT_MIX_INT(unsigned long, >>)
CONCAT_SHIFT_MIX_INT(unsigned int, >>)
CONCAT_SHIFT_MIX_INT(ap_ulong, >>)
CONCAT_SHIFT_MIX_INT(ap_slong, >>)
#endif
#if defined(SYSTEMC_H) || defined(SYSTEMC_INCLUDED)
template<int _AP_W, bool _AP_S>
INLINE void sc_trace(sc_core::sc_trace_file *tf, const ap_private<_AP_W, _AP_S> &op,
const std::string &name) {
if (tf)
tf->trace(sc_dt::sc_lv<_AP_W>(op.to_string(2).c_str()), name);
}
#endif
template<int _AP_W, bool _AP_S>
INLINE std::ostream& operator<<(std::ostream& out, const ap_private<_AP_W,_AP_S> &op)
{
ap_private<_AP_W, _AP_S> v=op;
const std::ios_base::fmtflags basefield = out.flags() & std::ios_base::basefield;
unsigned radix = (basefield == std::ios_base::hex) ? 16 :
((basefield == std::ios_base::oct) ? 8 : 10);
std::string str=v.toString(radix,_AP_S);
out<<str;
return out;
}
template<int _AP_W, bool _AP_S>
INLINE std::istream& operator >> (std::istream& in, ap_private<_AP_W,_AP_S> &op)
{
std::string str;
in >> str;
op = ap_private<_AP_W, _AP_S>(str.c_str());
return in;
}
template<int _AP_W, bool _AP_S>
INLINE std::ostream& operator<<(std::ostream& out, const ap_range_ref<_AP_W,_AP_S> &op)
{
return operator<<(out, ap_private<_AP_W, _AP_S>(op));
}
template<int _AP_W, bool _AP_S>
INLINE std::istream& operator >> (std::istream& in, ap_range_ref<_AP_W,_AP_S> &op)
{
return operator>>(in, ap_private<_AP_W, _AP_S>(op));;
}
template<int _AP_W, bool _AP_S>
INLINE void print(const ap_private<_AP_W,_AP_S> &op, bool fill=true )
{
ap_private<_AP_W, _AP_S> v=op;
uint32_t ws=v.getNumWords();
const uint64_t *ptr=v.getRawData();
int i=ws-1;
#if 0
if(fill)
printf("%016llx",*(ptr+i));
else
printf("%llx",*(ptr+i));
#else
//match SystemC output
if(_AP_W%64 != 0) {
uint32_t offset=_AP_W%64;
uint32_t count=(offset+3)/4;
int64_t data=*(ptr+i);
if(_AP_S)
data=(data<<(64-offset))>>(64-offset);
else
count=(offset+4)/4;
while(count-->0)
printf("%llx",(data>>(count*4))&0xf);
} else {
if(_AP_S==false)
printf("0");
printf("%016llx",*(ptr+i));
}
#endif
for(--i;i>=0;i--)
printf("%016llx",*(ptr+i));
printf("\n");
}
#endif /* #ifndef __AESL_GCC_AP_INT_H__ */
\ No newline at end of file
This source diff could not be displayed because it is too large. You can view the blob instead.
/**
* main.cpp
*
* for Vivado HLS
*/
#ifdef SOFTWARE
#include "ap_int.h"
#else
#include <ap_int.h>
#endif
#ifdef CALCTIME
#include <stdio.h>
#include <time.h>
#endif
#include "router.hpp"
int main(int argc, char *argv[]) {
using namespace std;
// テストデータ (文字列形式)
// NL_Q00.txt
//char boardstr[BOARDSTR_SIZE] = "X10Y05Z3L0000107041L0004107002L0102102021L0900100003";
// NL_Q06.txt
char boardstr[BOARDSTR_SIZE] = "X10Y18Z2L0900109002L0901105012L0902103052L0903103062L0904100102L0905106012L0906109022L0717109102L0808109112L0017209172L0401200072L0912208152L0009201092L0709209092L0901206052L0309204092L0701209072L0101201022L0011202152L0016202162";
// NL_Q08.txt
//char boardstr[BOARDSTR_SIZE] = "X17Y20Z2L0000103022L1603115052L0916107032L0302108012L1104111042L1002100002L0919116162L1616113182L1001115012L0500201182L1603213152L0600210022";
// 指定されてればコマンドラインから問題文字列を読み込む
if (1 < argc) {
//先頭がXではないならば標準入力から読み込む
if(argv[1][0]!='X')
{
char* c_p=fgets(boardstr, BOARDSTR_SIZE, stdin);
int length=strlen(c_p);
boardstr[length-1]=0;
}
else
{
strcpy(boardstr, argv[1]);
}
}
// 指定されてればシード値を読み込む
int seed = 12345;
if (2 < argc) {
seed = atoi(argv[2]);
}
int size_x = (boardstr[1] - '0') * 10 + (boardstr[2] - '0');
int size_y = (boardstr[4] - '0') * 10 + (boardstr[5] - '0');
int size_z = (boardstr[7] - '0');
// ソルバ実行
ap_int<32> status;
clock_t clock_start, clock_done;
clock_start = clock();
bool result = pynqrouter(boardstr, seed, &status);
clock_done = clock();
if (result) {
cout << endl << "Test Passed!" << endl;
} else {
cout << endl << "Test Failed!" << endl;
}
cout << "status = " << (int)status << endl;
cout << "elapsed = " << ((double)(clock_done - clock_start) / CLOCKS_PER_SEC) << endl << endl;
// 解表示
cout << "SOLUTION" << endl;
cout << "========" << endl;
cout << "SIZE " << size_x << "X" << size_y << "X" << size_z << endl;
for (int z = 0; z < size_z; z++) {
cout << "LAYER " << (z + 1) << endl;
for (int y = 0; y < size_y; y++) {
for (int x = 0; x < size_x; x++) {
if (x != 0) {
cout << ",";
}
int i = ((x * MAX_WIDTH + y) << BITWIDTH_Z) | z;
//cout << setfill('0') << setw(2) << right << (unsigned int)(unsigned char)(boardstr[i]);
cout << (unsigned int)(unsigned char)(boardstr[i]);
}
cout << endl;
}
}
return 0;
}
/**
* router.cpp
*
* for Vivado HLS
*/
#ifdef SOFTWARE
#include "ap_int.h"
#else
#include <ap_int.h>
#endif
#include "./router.hpp"
// ================================ //
// LFST
// ================================ //
// 参考 https://highlevel-synthesis.com/2017/02/10/lfsr-in-hls/
static ap_uint<32> lfsr;
void lfsr_random_init(ap_uint<32> seed) {
#pragma HLS INLINE
lfsr = seed;
}
ap_uint<32> lfsr_random() {
#pragma HLS INLINE
bool b_32 = lfsr.get_bit(32-32);
bool b_22 = lfsr.get_bit(32-22);
bool b_2 = lfsr.get_bit(32-2);
bool b_1 = lfsr.get_bit(32-1);
bool new_bit = b_32 ^ b_22 ^ b_2 ^ b_1;
lfsr = lfsr >> 1;
lfsr.set_bit(31, new_bit);
return lfsr.to_uint();
}
// AからBの範囲 (AとBを含む) の整数の乱数が欲しいとき
// 参考 http://www.sat.t.u-tokyo.ac.jp/~omi/random_variables_generation.html
/*ap_uint<32> lfsr_random_uint32(ap_uint<32> a, ap_uint<32> b) {
#pragma HLS INLINE
return lfsr_random() % (b - a + 1) + a;
}*/
// 0からBの範囲 (AとBを含む) の整数の乱数が欲しいとき
// 参考 http://www.sat.t.u-tokyo.ac.jp/~omi/random_variables_generation.html
/*ap_uint<32> lfsr_random_uint32_0(ap_uint<32> b) {
#pragma HLS INLINE
return lfsr_random() % (b + 1);
}*/
// ================================ //
// メインモジュール
// ================================ //
// 重みの更新
// TODO 調整
// min_uint8(r, MAX_WEIGHT) と同じ
ap_uint<8> new_weight(ap_uint<16> x) {
#pragma HLS INLINE
#if 1
// 下位8ビット (最大 255) を抜き出して、1/8 をかけて最大 31 (32) にする
ap_uint<8> y = x & 255;
return (ap_uint<8>)(y / 8 + 1);
#endif
#if 0
// 下位10ビット (最大 1023) を抜き出して、1/32 をかけて最大 31 (32) にする
ap_uint<10> y = x & 1023;
return (ap_uint<8>)(y / 32 + 1);
#endif
#if 0
ap_uint<8> y = x / 8;
if (y < (ap_uint<16>)MAX_WEIGHT) { return y; }
else { return MAX_WEIGHT; }
#endif
}
// ボードに関する変数
static ap_uint<7> size_x; // ボードの X サイズ
static ap_uint<7> size_y; // ボードの Y サイズ
static ap_uint<4> size_z; // ボードの Z サイズ
static ap_uint<7> line_num = 0; // ラインの総数
// グローバル変数で定義する
#ifdef GLOBALVARS
ap_uint<16> starts[MAX_LINES]; // ラインのスタートリスト
ap_uint<16> goals[MAX_LINES]; // ラインのゴールリスト
ap_uint<8> weights[MAX_CELLS]; // セルの重み
ap_uint<8> paths_size[MAX_LINES]; // ラインが対応するセルIDのサイズ
ap_uint<16> paths[MAX_LINES][MAX_PATH]; // ラインが対応するセルIDの集合 (スタートとゴールは除く)
bool adjacents[MAX_LINES]; // スタートとゴールが隣接しているライン
#endif
bool pynqrouter(char boardstr[BOARDSTR_SIZE], ap_uint<32> seed, ap_int<32> *status) {
#pragma HLS INTERFACE s_axilite port=boardstr bundle=AXI4LS
#pragma HLS INTERFACE s_axilite port=seed bundle=AXI4LS
#pragma HLS INTERFACE s_axilite port=status bundle=AXI4LS
#pragma HLS INTERFACE s_axilite port=return bundle=AXI4LS
*status = -1;
// グローバル変数では定義しない
#ifndef GLOBALVARS
ap_uint<16> starts[MAX_LINES]; // ラインのスタートリスト
#pragma HLS ARRAY_PARTITION variable=starts complete dim=1
ap_uint<16> goals[MAX_LINES]; // ラインのゴールリスト
#pragma HLS ARRAY_PARTITION variable=goals complete dim=1
ap_uint<8> weights[MAX_CELLS]; // セルの重み
//#pragma HLS ARRAY_PARTITION variable=weights cyclic factor=8 dim=1 partition
// Note: weights は様々な順番でアクセスされるからパーティションしても全然効果ない
ap_uint<8> paths_size[MAX_LINES]; // ラインが対応するセルIDのサイズ
//#pragma HLS ARRAY_PARTITION variable=paths_size complete dim=1
ap_uint<16> paths[MAX_LINES][MAX_PATH]; // ラインが対応するセルIDの集合 (スタートとゴールは除く)
//#pragma HLS ARRAY_PARTITION variable=paths cyclic factor=16 dim=2 partition
bool adjacents[MAX_LINES]; // スタートとゴールが隣接しているライン
//#pragma HLS ARRAY_PARTITION variable=adjacents complete dim=1
#endif
// ================================
// 初期化 BEGIN
// ================================
// ループカウンタは1ビット余分に用意しないと終了判定できない
INIT_ADJACENTS:
for (ap_uint<8> i = 0; i < (ap_uint<8>)(MAX_LINES); i++) {
adjacents[i] = false;
}
INIT_WEIGHTS:
for (ap_uint<16> i = 0; i < (ap_uint<16>)(MAX_CELLS); i++) {
//#pragma HLS PIPELINE
//#pragma HLS UNROLL factor=8
weights[i] = 1;
}
// ボードストリングの解釈
size_x = (boardstr[1] - '0') * 10 + (boardstr[2] - '0');
size_y = (boardstr[4] - '0') * 10 + (boardstr[5] - '0');
size_z = (boardstr[7] - '0');
INIT_BOARDS:
for (ap_uint<16> idx = 8; idx < BOARDSTR_SIZE; idx+=11) {
//#pragma HLS LOOP_TRIPCOUNT min=100 max=32768 avg=1000
// 終端 (null) 文字
if (boardstr[idx] == 0) {
break;
}
ap_uint<7> s_x = (boardstr[idx+1] - '0') * 10 + (boardstr[idx+2] - '0');
ap_uint<7> s_y = (boardstr[idx+3] - '0') * 10 + (boardstr[idx+4] - '0');
ap_uint<3> s_z = (boardstr[idx+5] - '0') - 1;
ap_uint<7> g_x = (boardstr[idx+6] - '0') * 10 + (boardstr[idx+7] - '0');
ap_uint<7> g_y = (boardstr[idx+8] - '0') * 10 + (boardstr[idx+9] - '0');
ap_uint<3> g_z = (boardstr[idx+10] - '0') - 1;
//cout << "L " << line_num << ": (" << s_x << ", " << s_y << ", " << s_z << ") "
// "(" << g_x << ", " << g_y << ", " << g_z << ")" << endl;
// スタートとゴール
ap_uint<16> start_id = (((ap_uint<16>)s_x * MAX_WIDTH + (ap_uint<16>)s_y) << BITWIDTH_Z) | (ap_uint<16>)s_z;
ap_uint<16> goal_id = (((ap_uint<16>)g_x * MAX_WIDTH + (ap_uint<16>)g_y) << BITWIDTH_Z) | (ap_uint<16>)g_z;
starts[line_num] = start_id;
goals[line_num] = goal_id;
// 初期状態で数字が隣接しているか判断
ap_int<8> dx = (ap_int<8>)g_x - (ap_int<8>)s_x; // 最小-71 最大71 (-> 符号付き8ビット)
ap_int<8> dy = (ap_int<8>)g_y - (ap_int<8>)s_y; // 最小-71 最大71 (-> 符号付き8ビット)
ap_int<4> dz = (ap_int<4>)g_z - (ap_int<4>)s_z; // 最小-7 最大7 (-> 符号付き4ビット)
if ((dx == 0 && dy == 0 && (dz == 1 || dz == -1)) || (dx == 0 && (dy == 1 || dy == -1) && dz == 0) || ((dx == 1 || dx == -1) && dy == 0 && dz == 0)) {
adjacents[line_num] = true;
paths_size[line_num] = 0;
} else {
adjacents[line_num] = false;
}
paths_size[line_num] = 0;
weights[start_id] = MAX_WEIGHT;
weights[goal_id] = MAX_WEIGHT;
line_num++;
}
//cout << size_x << " " << size_y << " " << size_z << endl;
// 乱数の初期化
lfsr_random_init(seed);
// TODO
// すべてのラインが隣接してたらソルバを終わりにする
// ================================
// 初期化 END
// ================================
// ================================
// ルーティング BEGIN
// ================================
// [Step 1] 初期ルーティング
cout << "Initial Routing" << endl;
FIRST_ROUTING:
for (ap_uint<8> i = 0; i < (ap_uint<8>)(line_num); i++) {
#pragma HLS LOOP_TRIPCOUNT min=2 max=127 avg=50
//#pragma HLS PIPELINE
//#pragma HLS UNROLL factor=2
// 数字が隣接する場合スキップ、そうでない場合は実行
if (adjacents[i] == false) {
// 経路探索
#ifdef DEBUG_PRINT
cout << "LINE #" << (int)(i + 1) << endl;
#endif
search(&(paths_size[i]), paths[i], starts[i], goals[i], weights);
}
}
ap_uint<1> overlap_checks[MAX_CELLS];
#pragma HLS ARRAY_PARTITION variable=overlap_checks cyclic factor=16 dim=1 partition
bool has_overlap = false;
#ifndef USE_MOD_CALC
// line_num_2: line_num 以上で最小の2のべき乗数
ap_uint<8> line_num_2;
CALC_LINE_NUM_2:
for (line_num_2 = 1; line_num_2 < (ap_uint<8>)line_num; line_num_2 *= 2) {
#pragma HLS LOOP_TRIPCOUNT min=1 max=8 avg=4
;
}
//cout << "line_num: " << line_num << endl;
//cout << "line_num_2: " << line_num_2 << endl;
#endif
ap_uint<8> last_target = 255;
// [Step 2] Rip-up 再ルーティング
ROUTING:
for (ap_uint<16> round = 1; round <= 32768 /* = (2048 * 16) */; round++) {
#pragma HLS LOOP_TRIPCOUNT min=1 max=4000 avg=50
//#ifdef DEBUG_PRINT
//cout << "ITERATION " << round;
//#endif
// 対象ラインを選択
#ifdef USE_MOD_CALC
// (1) 剰余演算を用いる方法
ap_uint<8> target = lfsr_random() % line_num; // i.e., lfsr_random_uint32(0, line_num - 1);
#else
// (2) 剰余演算を用いない方法
ap_uint<8> target = lfsr_random() & (line_num_2 - 1);
if (line_num <= target) {
//cout << endl;
continue;
}
#endif
// 数字が隣接する場合スキップ、そうでない場合は実行
if (adjacents[target] == true) {
//cout << endl;
continue;
}
// 直前のイテレーション (ラウンド) と同じ対象ラインだったらルーティングスキップする
if (target == last_target) {
//cout << endl;
continue;
}
last_target = target;
// (1) 引きはがすラインの重みをリセット
ROUTING_RESET:
for (ap_uint<9> j = 0; j < (ap_uint<9>)(paths_size[target]); j++) {
#pragma HLS LOOP_TRIPCOUNT min=1 max=255 avg=50
weights[paths[target][j]] = 1;
}
// 対象ラインのスタートの重みも一旦リセット あとで (*) で戻す
weights[starts[target]] = 1;
// (2) 重みを更新
ap_uint<8> current_round_weight = new_weight(round);
//cout << " weight " << current_round_weight << endl;
ROUTING_UPDATE:
for (ap_uint<8> i = 0; i < (ap_uint<8>)(line_num); i++) {
#pragma HLS LOOP_TRIPCOUNT min=2 max=127 avg=50
// 数字が隣接する場合スキップ、そうでない場合は実行
if (adjacents[i] == false && i != target) {
ROUTING_UPDATE_PATH:
for (ap_uint<9> j = 0; j < (ap_uint<9>)(paths_size[i]); j++) {
#pragma HLS LOOP_TRIPCOUNT min=1 max=255 avg=50
weights[paths[i][j]] = current_round_weight;
}
}
}
// 経路探索
#ifdef DEBUG_PRINT
cout << "LINE #" << (int)(target + 1) << endl;
#endif
search(&(paths_size[target]), paths[target], starts[target], goals[target], weights);
// (*) 対象ラインのスタートの重みを戻す
weights[starts[target]] = MAX_WEIGHT;
// ルーティング後
// オーバーラップのチェック
has_overlap = false;
OVERLAP_RESET:
for (ap_uint<16> i = 0; i < (ap_uint<16>)(MAX_CELLS); i++) {
#pragma HLS UNROLL factor=16
overlap_checks[i] = 0;
}
OVERLAP_CHECK:
for (ap_uint<8> i = 0; i < (ap_uint<8>)(line_num); i++) {
#pragma HLS LOOP_FLATTEN off
#pragma HLS LOOP_TRIPCOUNT min=2 max=127 avg=50
overlap_checks[starts[i]] = 1;
overlap_checks[goals[i]] = 1;
// 数字が隣接する場合スキップ、そうでない場合は実行
//if (adjacents[i] == false) {
OVERLAP_CHECK_PATH:
for (ap_uint<9> j = 0; j < (ap_uint<9>)(paths_size[i]); j++) {
#pragma HLS LOOP_TRIPCOUNT min=1 max=255 avg=50
//#pragma HLS PIPELINE rewind II=33
#pragma HLS PIPELINE II=17
#pragma HLS UNROLL factor=8
ap_uint<16> cell_id = paths[i][j];
if (overlap_checks[cell_id] == 1) {
has_overlap = true;
break;
}
overlap_checks[cell_id] = 1;
}
//}
}
// オーバーラップなければ探索終了
if (has_overlap == false) {
break;
}
}
// 解導出できなかった場合
if (has_overlap == true) {
*status = 1;
return false;
}
// ================================
// ルーティング END
// ================================
// ================================
// 解生成 BEGIN
// ================================
// 空白
OUTPUT_INIT:
for (ap_uint<16> i = 0; i < (ap_uint<16>)(MAX_CELLS); i++) {
boardstr[i] = 0;
}
// ライン
// このソルバでのラインIDを+1して表示する
// なぜなら空白を 0 で表すことにするからラインIDは 1 以上にしたい
OUTPUT_LINE:
for (ap_uint<8> i = 0; i < (ap_uint<8>)(line_num); i++) {
#pragma HLS LOOP_TRIPCOUNT min=2 max=127 avg=50
boardstr[starts[i]] = (i + 1);
boardstr[goals[i]] = (i + 1);
OUTPUT_LINE_PATH:
for (ap_uint<9> j = 0; j < (ap_uint<9>)(paths_size[i]); j++) {
#pragma HLS LOOP_TRIPCOUNT min=1 max=255 avg=50
boardstr[paths[i][j]] = (i + 1);
}
}
// ================================
// 解生成 END
// ================================
*status = 0;
return true;
}
// ================================ //
// 探索
// ================================ //
#ifdef USE_ASTAR
// A* ヒューリスティック用
// 最大71 最小0
ap_uint<7> abs_uint7(ap_uint<7> a, ap_uint<7> b) {
#pragma HLS INLINE
if (a < b) { return b - a; }
else { return a - b; }
}
// 最大7 最小0
ap_uint<3> abs_uint3(ap_uint<3> a, ap_uint<3> b) {
#pragma HLS INLINE
if (a < b) { return b - a; }
else { return a - b; }
}
#endif
// * Pythonでダイクストラアルゴリズムを実装した - フツーって言うなぁ!
// http://lethe2211.hatenablog.com/entry/2014/12/30/011030
// * Implementation of A*
// http://www.redblobgames.com/pathfinding/a-star/implementation.html
// をベース
void search(ap_uint<8> *path_size, ap_uint<16> path[MAX_PATH], ap_uint<16> start, ap_uint<16> goal, ap_uint<8> w[MAX_CELLS]) {
//#pragma HLS INLINE // search関数はインラインすると遅くなるしBRAM足りなくなる
//#pragma HLS FUNCTION_INSTANTIATE variable=start
//#pragma HLS FUNCTION_INSTANTIATE variable=goal
ap_uint<16> dist[MAX_CELLS]; // 始点から各頂点までの最短距離を格納する
#pragma HLS ARRAY_PARTITION variable=dist cyclic factor=64 dim=1 partition
// Note: dist のパーティションの factor は 128 にするとBRAMが足りなくなる
ap_uint<16> prev[MAX_CELLS]; // 最短経路における,その頂点の前の頂点のIDを格納する
SEARCH_INIT_DIST:
for (ap_uint<16> i = 0; i < MAX_CELLS; i++) {
#pragma HLS UNROLL factor=64
dist[i] = 65535; // = (2^16 - 1)
}
// プライオリティ・キュー
ap_uint<12> pq_len = 0;
ap_uint<32> pq_nodes[MAX_PQ];
//#pragma HLS ARRAY_PARTITION variable=pq_nodes complete dim=1
//#pragma HLS ARRAY_PARTITION variable=pq_nodes cyclic factor=2 dim=1 partition
#ifdef DEBUG_PRINT
// キューの最大長さチェック用
ap_uint<12> max_pq_len = 0;
#endif
#ifdef USE_ASTAR
// ゴールの座標
ap_uint<13> goal_xy = (ap_uint<13>)(goal >> BITWIDTH_Z);
ap_uint<7> goal_x = (ap_uint<7>)(goal_xy / MAX_WIDTH);
ap_uint<7> goal_y = (ap_uint<7>)(goal_xy - goal_x * MAX_WIDTH);
ap_uint<3> goal_z = (ap_uint<3>)(goal & BITMASK_Z);
#endif
dist[start] = 0;
pq_push(0, start, &pq_len, pq_nodes); // 始点をpush
#ifdef DEBUG_PRINT
if (max_pq_len < pq_len) { max_pq_len = pq_len; }
#endif
SEARCH_PQ:
while (0 < pq_len) {
#pragma HLS LOOP_FLATTEN off
#pragma HLS LOOP_TRIPCOUNT min=1 max=1000 avg=100
ap_uint<16> prov_cost;
ap_uint<16> src;
pq_pop(&prov_cost, &src, &pq_len, pq_nodes);
#ifdef DEBUG_PRINT
//ap_uint<13> _src_xy = (ap_uint<13>)(src >> BITWIDTH_Z);
//ap_uint<7> _src_x = (ap_uint<7>)(_src_xy / MAX_WIDTH);
//ap_uint<7> _src_y = (ap_uint<7>)(_src_xy % MAX_WIDTH);
//ap_uint<3> _src_z = (ap_uint<3>)(src & BITMASK_Z);
//cout << "Picked up " << (int)src << " (" << (int)_src_x << "," << (int)_src_y << "," << (int)_src_z << ") prov_cost=" << (int)prov_cost << " this_cost=" << w[src] << endl;
#endif
ap_uint<16> dist_src = dist[src];
#ifndef USE_ASTAR
// プライオリティキューに格納されている最短距離が,現在計算できている最短距離より大きければ,distの更新をする必要はない
if (dist_src < prov_cost) {
continue;
}
#endif
// PQの先頭がゴールの場合にPQがまだ空じゃなくても探索終わらせしまう
if (src == goal) {
break;
}
// 隣接する他の頂点の探索
// (0) コスト
ap_uint<8> cost = w[src];
// (1) ノードIDから3次元座標をマスクして抜き出す
ap_uint<13> src_xy = (ap_uint<13>)(src >> BITWIDTH_Z);
ap_uint<7> src_x = (ap_uint<7>)(src_xy / MAX_WIDTH);
ap_uint<7> src_y = (ap_uint<7>)(src_xy - src_x * MAX_WIDTH);
ap_uint<3> src_z = (ap_uint<3>)(src & BITMASK_Z);
//cout << src << " " << src_x << " " << src_y << " " << src_z << endl;
// (2) 3次元座標で隣接するノード (6個) を調べる // 手動ループ展開
/***********************************************************
if (src_x > 0) { // x-を調査
ap_uint<16> dest = (((ap_uint<16>)(src_x-1) * MAX_WIDTH + (ap_uint<16>)(src_y)) << BITWIDTH_Z) | (ap_uint<16>)(src_z);
ap_uint<16> dist_new = dist_src + cost;
if (dist[dest] > dist_new) {
dist[dest] = dist_new; // distの更新
prev[dest] = src; // 前の頂点を記録
dist_new += abs_uint7(src_x-1, goal_x) + abs_uint7(src_y, goal_y) + abs_uint3(src_z, goal_z); // A* ヒューリスティック
pq_push(dist_new, dest, &pq_len, pq_nodes); // キューに新たな仮の距離の情報をpush
}
}
if (src_x < (size_x-1)) { // x+を調査
ap_uint<16> dest = (((ap_uint<16>)(src_x+1) * MAX_WIDTH + (ap_uint<16>)(src_y)) << BITWIDTH_Z) | (ap_uint<16>)(src_z);
ap_uint<16> dist_new = dist_src + cost;
if (dist[dest] > dist_new) {
dist[dest] = dist_new; // distの更新
prev[dest] = src; // 前の頂点を記録
dist_new += abs_uint7(src_x+1, goal_x) + abs_uint7(src_y, goal_y) + abs_uint3(src_z, goal_z); // A* ヒューリスティック
pq_push(dist_new, dest, &pq_len, pq_nodes); // キューに新たな仮の距離の情報をpush
}
}
if (src_y > 0) { // y-を調査
ap_uint<16> dest = (((ap_uint<16>)(src_x) * MAX_WIDTH + (ap_uint<16>)(src_y-1)) << BITWIDTH_Z) | (ap_uint<16>)(src_z);
ap_uint<16> dist_new = dist_src + cost;
if (dist[dest] > dist_new) {
dist[dest] = dist_new; // distの更新
prev[dest] = src; // 前の頂点を記録
dist_new += abs_uint7(src_x, goal_x) + abs_uint7(src_y-1, goal_y) + abs_uint3(src_z, goal_z); // A* ヒューリスティック
pq_push(dist_new, dest, &pq_len, pq_nodes); // キューに新たな仮の距離の情報をpush
}
}
if (src_y < (size_y-1)) { // y+を調査
ap_uint<16> dest = (((ap_uint<16>)(src_x) * MAX_WIDTH + (ap_uint<16>)(src_y+1)) << BITWIDTH_Z) | (ap_uint<16>)(src_z);
ap_uint<16> dist_new = dist_src + cost;
if (dist[dest] > dist_new) {
dist[dest] = dist_new; // distの更新
prev[dest] = src; // 前の頂点を記録
dist_new += abs_uint7(src_x, goal_x) + abs_uint7(src_y+1, goal_y) + abs_uint3(src_z, goal_z); // A* ヒューリスティック
pq_push(dist_new, dest, &pq_len, pq_nodes); // キューに新たな仮の距離の情報をpush
}
}
if (src_z > 0) { // z-を調査
ap_uint<16> dest = (((ap_uint<16>)(src_x) * MAX_WIDTH + (ap_uint<16>)(src_y)) << BITWIDTH_Z) | (ap_uint<16>)(src_z-1);
ap_uint<16> dist_new = dist_src + cost;
if (dist[dest] > dist_new) {
dist[dest] = dist_new; // distの更新
prev[dest] = src; // 前の頂点を記録
dist_new += abs_uint7(src_x, goal_x) + abs_uint7(src_y, goal_y) + abs_uint3(src_z-1, goal_z); // A* ヒューリスティック
pq_push(dist_new, dest, &pq_len, pq_nodes); // キューに新たな仮の距離の情報をpush
}
}
if (src_z < (size_z-1)) { // y+を調査
ap_uint<16> dest = (((ap_uint<16>)(src_x) * MAX_WIDTH + (ap_uint<16>)(src_y)) << BITWIDTH_Z) | (ap_uint<16>)(src_z+1);
ap_uint<16> dist_new = dist_src + cost;
if (dist[dest] > dist_new) {
dist[dest] = dist_new; // distの更新
prev[dest] = src; // 前の頂点を記録
dist_new += abs_uint7(src_x, goal_x) + abs_uint7(src_y, goal_y) + abs_uint3(src_z+1, goal_z); // A* ヒューリスティック
pq_push(dist_new, dest, &pq_len, pq_nodes); // キューに新たな仮の距離の情報をpush
}
}
***********************************************************/
SEARCH_ADJACENTS:
for (ap_uint<3> a = 0; a < 6; a++) {
//#pragma HLS PIPELINE
//#pragma HLS UNROLL factor=2
ap_int<8> dest_x = (ap_int<8>)src_x; // 最小-1 最大72 (->符号付き8ビット)
ap_int<8> dest_y = (ap_int<8>)src_y; // 最小-1 最大72 (->符号付き8ビット)
ap_int<5> dest_z = (ap_int<5>)src_z; // 最小-1 最大8 (->符号付き5ビット)
if (a == 0) { dest_x -= 1; }
if (a == 1) { dest_x += 1; }
if (a == 2) { dest_y -= 1; }
if (a == 3) { dest_y += 1; }
if (a == 4) { dest_z -= 1; }
if (a == 5) { dest_z += 1; }
if (0 <= dest_x && dest_x < (ap_int<8>)size_x && 0 <= dest_y && dest_y < (ap_int<8>)size_y && 0 <= dest_z && dest_z < (ap_int<5>)size_z) {
ap_uint<16> dest = (((ap_uint<16>)dest_x * MAX_WIDTH + (ap_uint<16>)dest_y) << BITWIDTH_Z) | (ap_uint<16>)dest_z;
ap_uint<16> dist_new = dist_src + cost;
#ifdef DEBUG_PRINT
//cout << " adjacent " << (int)dest << " (" << (int)dest_x << "," << (int)dest_y << "," << (int)dest_z << ") dist_new=" << (int)dist_new;
#endif
if (dist[dest] > dist_new) {
dist[dest] = dist_new; // distの更新
prev[dest] = src; // 前の頂点を記録
#ifdef USE_ASTAR
dist_new += abs_uint7(dest_x, goal_x) + abs_uint7(dest_y, goal_y) + abs_uint3(dest_z, goal_z); // A* ヒューリスティック
#endif
pq_push(dist_new, dest, &pq_len, pq_nodes); // キューに新たな仮の距離の情報をpush
#ifdef DEBUG_PRINT
//cout << " h=" << (int)(abs_uint7(dest_x, goal_x) + abs_uint7(dest_y, goal_y) + abs_uint3(dest_z, goal_z)) << endl;
//cout << (int)dest_x << " " << (int)goal_x << " " << (int)abs_uint7(dest_x, goal_x) << endl;
//cout << (int)dest_y << " " << (int)goal_y << " " << (int)abs_uint7(dest_y, goal_y) << endl;
//cout << (int)dest_z << " " << (int)goal_z << " " << (int)abs_uint7(dest_z, goal_z) << endl;
if (max_pq_len < pq_len) { max_pq_len = pq_len; }
#endif
}
#ifdef DEBUG_PRINT
//else { cout << " -> skip pushing" << endl; }
#endif
}
}
}
// 経路を出力
// ゴールからスタートへの順番で表示される (ゴールとスタートは含まれない)
ap_uint<16> t = prev[goal];
#ifdef DEBUG_PRINT
int dbg_start_xy = start >> BITWIDTH_Z;
int dbg_start_x = dbg_start_xy / MAX_WIDTH;
int dbg_start_y = dbg_start_xy % MAX_WIDTH;
int dbg_start_z = start & BITMASK_Z;
int dbg_goal_xy = goal >> BITWIDTH_Z;
int dbg_goal_x = dbg_goal_xy / MAX_WIDTH;
int dbg_goal_y = dbg_goal_xy % MAX_WIDTH;
int dbg_goal_z = goal & BITMASK_Z;
cout << "(" << dbg_start_x << ", " << dbg_start_y << ", " << dbg_start_z << ") #" << start << " -> "
<< "(" << dbg_goal_x << ", " << dbg_goal_y << ", " << dbg_goal_z << ") #" << goal << endl;
#endif
// バックトラック
ap_uint<8> p = 0;
SEARCH_BACKTRACK:
while (t != start) {
#pragma HLS LOOP_TRIPCOUNT min=1 max=255 avg=50
#pragma HLS PIPELINE II=2
#ifdef DEBUG_PRINT
int t_xy = prev[t] >> BITWIDTH_Z;
int t_x = t_xy / MAX_WIDTH;
int t_y = t_xy % MAX_WIDTH;
int t_z = prev[t] & BITMASK_Z;
cout << " via " << "(" << t_x << ", " << t_y << ", " << t_z << ") #" << prev[t] << " dist=" << dist[t] << endl;
#endif
path[p] = t; // 記録
p++;
t = prev[t]; // 次に移動
}
*path_size = p;
#ifdef DEBUG_PRINT
cout << "max_path_len = " << p << endl;
cout << "max_pq_len = " << max_pq_len << endl;
#endif
}
// プライオリティ・キュー (ヒープで実装)
// 優先度の最小値がヒープのルートに来る
// 参考
// * ヒープ - Wikipedia https://ja.wikipedia.org/wiki/%E3%83%92%E3%83%BC%E3%83%97
// * 二分ヒープ - Wikipedia https://ja.wikipedia.org/wiki/%E4%BA%8C%E5%88%86%E3%83%92%E3%83%BC%E3%83%97
// * ヒープの正体 - http://www.maroontress.com/Heap/
// * Priority queue - Rosetta Code https://rosettacode.org/wiki/Priority_queue#C
// Note
// インデックスが0から始まるとき (0-origin index)
// --> 親: (n-1)/2, 左の子: 2n+1, 右の子: 2n+2
// インデックスが1から始まるとき (1-origin index)
// --> 親: n/2, 左の子: 2n, 右の子: 2n+1
// FPGA的にはどちらも遅延は同じだけど 1-origin の方がLUTリソース少なくて済む (ただし配列の0要素が無駄になる)
// ノードの挿入は,末尾に追加してから優先度が正しい高さの位置までノードを上げていく
// 探索の都合上,同じ優先度では後から入れた方を先に出したいから,
// ループの終了条件は挿入ノードの優先度が比較対象の優先度よりも小さくなったとき
void pq_push(ap_uint<16> priority, ap_uint<16> data, ap_uint<12> *pq_len, ap_uint<32> pq_nodes[MAX_PQ]) {
#pragma HLS INLINE
(*pq_len)++;
ap_uint<12> i = (*pq_len); // target
ap_uint<12> p = (*pq_len) >> 1; // i.e., (*pq_len) / 2; // 親
PQ_PUSH_LOOP:
while (i > 1 && (ap_uint<16>)(pq_nodes[p] & PQ_PRIORITY_MASK) >= priority) {
#pragma HLS LOOP_TRIPCOUNT min=1 max=8 avg=4
//#pragma HLS PIPELINE
//#pragma HLS UNROLL factor=2
pq_nodes[i] = pq_nodes[p];
i = p;
p = p >> 1; // i.e., p / 2; // 親
}
pq_nodes[i] = ((ap_uint<32>)data << 16) | (ap_uint<32>)priority;
}
// ノードの取り出しは,ルートを取ってくるだけ
// 次に最小の優先度をもつノードをルートに移動させるために,
// まず,末尾のノードをルートに移動する
// 両方の子で優先度が小さい方を上にもっていく (ルートを適切な高さまで下げる)
// これを再帰的に繰り返す
void pq_pop(ap_uint<16> *ret_priority, ap_uint<16> *ret_data, ap_uint<12> *pq_len, ap_uint<32> pq_nodes[MAX_PQ]) {
#pragma HLS INLINE
*ret_priority = (ap_uint<16>)(pq_nodes[1] & PQ_PRIORITY_MASK);
*ret_data = (ap_uint<16>)(pq_nodes[1] >> PQ_PRIORITY_WIDTH);
//pq_nodes[1] = pq_nodes[*pq_len];
ap_uint<12> i = 1; // 親ノード
//ap_uint<12> t = 1; // 交換対象ノード
ap_uint<16> last_priority = (ap_uint<16>)(pq_nodes[*pq_len] & PQ_PRIORITY_MASK); // 末尾ノードの優先度
PQ_POP_LOOP:
while (1) {
#pragma HLS LOOP_TRIPCOUNT min=1 max=8 avg=4
//#pragma HLS PIPELINE
//#pragma HLS UNROLL factor=2
ap_uint<12> c1 = i << 1; // i.e., 2 * i; // 左の子
ap_uint<12> c2 = c1 + 1; // i.e., 2 * i + 1; // 右の子
if (c1 < *pq_len && (ap_uint<16>)(pq_nodes[c1] & PQ_PRIORITY_MASK) <= last_priority) {
if (c2 < *pq_len && (ap_uint<16>)(pq_nodes[c2] & PQ_PRIORITY_MASK) <= (ap_uint<16>)(pq_nodes[c1] & PQ_PRIORITY_MASK)) {
pq_nodes[i] = pq_nodes[c2];
i = c2;
}
else {
pq_nodes[i] = pq_nodes[c1];
i = c1;
}
}
else {
if (c2 < *pq_len && (ap_uint<16>)(pq_nodes[c2] & PQ_PRIORITY_MASK) <= last_priority) {
pq_nodes[i] = pq_nodes[c2];
i = c2;
}
else {
break;
}
}
}
pq_nodes[i] = pq_nodes[*pq_len];
(*pq_len)--;
// For verification
//for (int k = 1;k<(*pq_len);k++){
// cout << (ap_uint<16>)(pq_nodes[k] & PQ_PRIORITY_MASK) << " ";
//}
//cout << endl;
}
/**
* router.hpp
*
* for Vivado HLS
*/
#ifndef __ROUTER_HPP__
#define __ROUTER_HPP__
#ifdef SOFTWARE
#include "ap_int.h"
#else
#include <ap_int.h>
#endif
//#define DEBUG_PRINT // いろいろ表示する
#define USE_ASTAR // A* 探索を使う
#define USE_MOD_CALC // ターゲットラインの選択に剰余演算を用いる
using namespace std;
// 各種設定値
#define MAX_WIDTH 72 // X, Y の最大値 (7ビットで収まる)
#define BITWIDTH_XY 13
#define BITMASK_XY 65528 // 1111 1111 1111 1000
#define MAX_LAYER 8 // Z の最大値 (3ビット)
#define BITWIDTH_Z 3
#define BITMASK_Z 7 // 0000 0000 0000 0111
#define MAX_CELLS 41472 // セルの総数 =72*72*8 (16ビットで収まる)
#define MAX_LINES 128 // ライン数の最大値 (7ビット)
#define MAX_PATH 256 // 1つのラインが対応するセル数の最大値 (8ビット)
#define MAX_PQ 4096 // 探索時のプライオリティ・キューの最大サイズ (12ビット) 足りないかも?
#define PQ_PRIORITY_WIDTH 16
#define PQ_PRIORITY_MASK 65535 // 0000 0000 0000 0000 1111 1111 1111 1111
#define PQ_DATA_WIDTH 16
#define PQ_DATA_MASK 4294901760 // 1111 1111 1111 1111 0000 0000 0000 0000
#define MAX_WEIGHT 255 // 重みの最大値 (8ビットで収まる)
#define BOARDSTR_SIZE 41472 // ボードストリングの最大文字数 (セル数 72*72*8 あれば良い)
void lfsr_random_init(ap_uint<32> seed);
ap_uint<32> lfsr_random();
//ap_uint<32> lfsr_random_uint32(ap_uint<32> a, ap_uint<32> b);
//ap_uint<32> lfsr_random_uint32_0(ap_uint<32> b);
ap_uint<8> new_weight(ap_uint<16> x);
bool pynqrouter(char boardstr[BOARDSTR_SIZE], ap_uint<32> seed, ap_int<32> *status);
#ifdef USE_ASTAR
ap_uint<7> abs_uint7(ap_uint<7> a, ap_uint<7> b);
ap_uint<3> abs_uint3(ap_uint<3> a, ap_uint<3> b);
#endif
void search(ap_uint<8> *path_size, ap_uint<16> path[MAX_PATH], ap_uint<16> start, ap_uint<16> goal, ap_uint<8> w[MAX_WEIGHT]);
void pq_push(ap_uint<16> priority, ap_uint<16> data, ap_uint<12> *pq_len, ap_uint<32> pq_nodes[MAX_PQ]);
void pq_pop(ap_uint<16> *ret_priority, ap_uint<16> *ret_data, ap_uint<12> *pq_len, ap_uint<32> pq_nodes[MAX_PQ]);
#endif /* __ROUTER_HPP__ */
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment