+ENABLE_OPERATORS_ON(Value)
+ENABLE_OPERATORS_ON(PieceType)
+ENABLE_OPERATORS_ON(Piece)
+ENABLE_OPERATORS_ON(Color)
+ENABLE_OPERATORS_ON(Depth)
+ENABLE_OPERATORS_ON(Square)
+ENABLE_OPERATORS_ON(File)
+ENABLE_OPERATORS_ON(Rank)
+
+#undef ENABLE_OPERATORS_ON
+
+// Extra operators for adding integers to a Value
+inline Value operator+ (Value v, int i) { return Value(int(v) + i); }
+inline Value operator- (Value v, int i) { return Value(int(v) - i); }
+
+// Extracting the _signed_ lower and upper 16 bits it not so trivial
+// because according to the standard a simple cast to short is
+// implementation defined and so is a right shift of a signed integer.
+inline Value mg_value(Score s) { return Value(((s + 32768) & ~0xffff) / 0x10000); }
+
+// Unfortunatly on Intel 64 bit we have a small speed regression, so use a faster code in
+// this case, although not 100% standard compliant it seems to work for Intel and MSVC.
+#if defined(IS_64BIT) && (!defined(__GNUC__) || defined(__INTEL_COMPILER))
+inline Value eg_value(Score s) { return Value(int16_t(s & 0xffff)); }
+#else
+inline Value eg_value(Score s) { return Value((int)(unsigned(s) & 0x7fffu) - (int)(unsigned(s) & 0x8000u)); }
+#endif
+
+inline Score make_score(int mg, int eg) { return Score((mg << 16) + eg); }
+
+// Division must be handled separately for each term
+inline Score operator/(Score s, int i) { return make_score(mg_value(s) / i, eg_value(s) / i); }
+
+// Only declared but not defined. We don't want to multiply two scores due to
+// a very high risk of overflow. So user should explicitly convert to integer.
+inline Score operator*(Score s1, Score s2);
+
+// Remaining Score operators are standard
+inline Score operator+ (const Score d1, const Score d2) { return Score(int(d1) + int(d2)); }
+inline Score operator- (const Score d1, const Score d2) { return Score(int(d1) - int(d2)); }
+inline Score operator* (int i, const Score d) { return Score(i * int(d)); }
+inline Score operator* (const Score d, int i) { return Score(int(d) * i); }
+inline Score operator- (const Score d) { return Score(-int(d)); }
+inline void operator+= (Score& d1, const Score d2) { d1 = d1 + d2; }
+inline void operator-= (Score& d1, const Score d2) { d1 = d1 - d2; }
+inline void operator*= (Score& d, int i) { d = Score(int(d) * i); }
+inline void operator/= (Score& d, int i) { d = Score(int(d) / i); }
+
+const Value PawnValueMidgame = Value(0x0C6);
+const Value PawnValueEndgame = Value(0x102);
+const Value KnightValueMidgame = Value(0x331);
+const Value KnightValueEndgame = Value(0x34E);
+const Value BishopValueMidgame = Value(0x344);
+const Value BishopValueEndgame = Value(0x359);
+const Value RookValueMidgame = Value(0x4F6);
+const Value RookValueEndgame = Value(0x4FE);
+const Value QueenValueMidgame = Value(0x9D9);
+const Value QueenValueEndgame = Value(0x9FE);
+
+extern const Value PieceValueMidgame[17];
+extern const Value PieceValueEndgame[17];
+
+inline Value piece_value_midgame(Piece p) {
+ return PieceValueMidgame[p];
+}
+
+inline Value piece_value_endgame(Piece p) {
+ return PieceValueEndgame[p];
+}
+
+inline Value value_mate_in(int ply) {
+ return VALUE_MATE - ply;
+}
+
+inline Value value_mated_in(int ply) {
+ return -VALUE_MATE + ply;
+}
+
+inline Piece make_piece(Color c, PieceType pt) {
+ return Piece((c << 3) | pt);
+}
+
+inline PieceType type_of_piece(Piece p) {
+ return PieceType(p & 7);
+}
+
+inline Color color_of_piece(Piece p) {
+ return Color(p >> 3);
+}
+
+inline Color opposite_color(Color c) {
+ return Color(c ^ 1);
+}
+
+inline char piece_type_to_char(PieceType pt) {
+ static const char ch[] = " PNBRQK";
+ return ch[pt];
+}
+
+inline Square make_square(File f, Rank r) {
+ return Square((r << 3) | f);
+}
+
+inline bool square_is_ok(Square s) {
+ return s >= SQ_A1 && s <= SQ_H8;
+}
+
+inline File square_file(Square s) {
+ return File(s & 7);
+}
+
+inline Rank square_rank(Square s) {
+ return Rank(s >> 3);
+}
+
+inline Square flip_square(Square s) {
+ return Square(s ^ 56);
+}
+
+inline Square flop_square(Square s) {
+ return Square(s ^ 7);
+}
+
+inline Square relative_square(Color c, Square s) {
+ return Square(s ^ (c * 56));
+}
+
+inline Rank relative_rank(Color c, Rank r) {
+ return Rank(r ^ (c * 7));
+}
+
+inline Rank relative_rank(Color c, Square s) {
+ return relative_rank(c, square_rank(s));
+}
+
+inline SquareColor square_color(Square s) {
+ return SquareColor(int(square_rank(s) + s) & 1);
+}
+
+inline bool opposite_color_squares(Square s1, Square s2) {
+ int s = s1 ^ s2;
+ return ((s >> 3) ^ s) & 1;
+}
+
+inline int file_distance(Square s1, Square s2) {
+ return abs(square_file(s1) - square_file(s2));
+}
+
+inline int rank_distance(Square s1, Square s2) {
+ return abs(square_rank(s1) - square_rank(s2));
+}
+
+inline int square_distance(Square s1, Square s2) {
+ return Max(file_distance(s1, s2), rank_distance(s1, s2));
+}
+
+inline char file_to_char(File f) {
+ return char(f - FILE_A + int('a'));
+}
+
+inline char rank_to_char(Rank r) {
+ return char(r - RANK_1 + int('1'));
+}
+
+inline const std::string square_to_string(Square s) {
+ char ch[] = { file_to_char(square_file(s)), rank_to_char(square_rank(s)), 0 };
+ return std::string(ch);
+}
+
+inline Square pawn_push(Color c) {
+ return c == WHITE ? DELTA_N : DELTA_S;
+}
+