<commit_before>#include "search.h"

#include <assert.h>
#include <inttypes.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <unistd.h>

#include "bitops.h"
#include "config.h"
#include "evaluate.h"
#include "history.h"
#include "move.h"
#include "moveiter.h"
#include "timer.h"
#include "tt.h"

#define max_quiescent_depth 50
#define aspiration_window 30

#define DISALLOW_NULL_MOVE 0
#define ALLOW_NULL_MOVE 1

#if ENABLE_FUTILITY_DEPTH > 0
static const int futility_margins[] = {0, 75, 500};
#endif

#if ENABLE_REVERSE_FUTILITY_DEPTH > 0
static const int reverse_futility_margins[] = {0, 300, 500};
#endif

static int timeup;
static uint64_t nodes_searched;

// main search workhorse
static int search_alpha_beta(Bitboard* board,
                             int alpha,
                             int beta,
                             int8_t depth,
                             int8_t ply,
                             Move* pv,
                             uint8_t allow_null);

static void search_print_pv(Move* pv, int8_t depth, FILE* f);

Move search_find_move(Bitboard* board, const SearchDebug* debug) {
  FILE* f;
  if (debug && debug->out)
    f = debug->out;
  else
    f = stdout;

  Move best_move = 0;
  nodes_searched = 0;
  history_clear();

  Move pv[max_possible_depth + 1];

  time_t start_cs = timer_get_centiseconds();

  timeup = 0;
  timer_begin();

  int alpha = -INFINITY;
  int beta = INFINITY;

  // for each depth, call the main workhorse, search_alpha_beta
  uint8_t max_depth = debug && debug->maxDepth > 0
                          ? min(debug->maxDepth, max_possible_depth)
                          : max_possible_depth;
  for (int8_t depth = 1; depth <= max_depth; depth++) {
    // here we go...
    int val =
        search_alpha_beta(board, alpha, beta, depth, 1, pv, ALLOW_NULL_MOVE);

    time_t centiseconds_taken = timer_get_centiseconds() - start_cs;

    if (((val <= alpha) || (val >= beta)) && !timeup) {
      // aspiration window failure
      fprintf(f, "%i\t%i\t%lu\t%" PRIu64 "\taspiration failure\n", depth,
              board->to_move == WHITE ? val : -val, centiseconds_taken,
              nodes_searched);
      alpha = -INFINITY;
      beta = INFINITY;
      depth--;
      continue;
    }

    if (!timeup) {
      best_move = pv[0];
      if (debug && debug->score)
        *debug->score = val;

      fprintf(f, "%i\t%i\t%lu\t%" PRIu64 "\t", depth,
              board->to_move == WHITE ? val : -val, centiseconds_taken,
              nodes_searched);
      search_print_pv(pv, depth, f);

      alpha = val - aspiration_window;
      beta = val + aspiration_window;

      if ((val >= MATE) || (val <= -MATE)) {
        fprintf(f, "-> mate");

        if (!(debug && debug->continueOnMate)) {
          fprintf(f, "\n");
          break;
        }
      }

      fprintf(f, "\n");

      if (debug && debug->stopMove) {<commit_msg>Print xboard eval relative to engine<commit_after>#include "search.h"

#include <assert.h>
#include <inttypes.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <unistd.h>

#include "bitops.h"
#include "config.h"
#include "evaluate.h"
#include "history.h"
#include "move.h"
#include "moveiter.h"
#include "timer.h"
#include "tt.h"

#define max_quiescent_depth 50
#define aspiration_window 30

#define DISALLOW_NULL_MOVE 0
#define ALLOW_NULL_MOVE 1

#if ENABLE_FUTILITY_DEPTH > 0
static const int futility_margins[] = {0, 75, 500};
#endif

#if ENABLE_REVERSE_FUTILITY_DEPTH > 0
static const int reverse_futility_margins[] = {0, 300, 500};
#endif

static int timeup;
static uint64_t nodes_searched;

// main search workhorse
static int search_alpha_beta(Bitboard* board,
                             int alpha,
                             int beta,
                             int8_t depth,
                             int8_t ply,
                             Move* pv,
                             uint8_t allow_null);

static void search_print_pv(Move* pv, int8_t depth, FILE* f);

Move search_find_move(Bitboard* board, const SearchDebug* debug) {
  FILE* f;
  if (debug && debug->out)
    f = debug->out;
  else
    f = stdout;

  Move best_move = 0;
  nodes_searched = 0;
  history_clear();

  Move pv[max_possible_depth + 1];

  time_t start_cs = timer_get_centiseconds();

  timeup = 0;
  timer_begin();

  int alpha = -INFINITY;
  int beta = INFINITY;

  // for each depth, call the main workhorse, search_alpha_beta
  uint8_t max_depth = debug && debug->maxDepth > 0
                          ? min(debug->maxDepth, max_possible_depth)
                          : max_possible_depth;
  for (int8_t depth = 1; depth <= max_depth; depth++) {
    // here we go...
    int val =
        search_alpha_beta(board, alpha, beta, depth, 1, pv, ALLOW_NULL_MOVE);

    time_t centiseconds_taken = timer_get_centiseconds() - start_cs;

    if (((val <= alpha) || (val >= beta)) && !timeup) {
      // aspiration window failure
      fprintf(f, "%i\t%i\t%lu\t%" PRIu64 "\taspiration failure\n", depth, val,
              centiseconds_taken, nodes_searched);
      alpha = -INFINITY;
      beta = INFINITY;
      depth--;
      continue;
    }

    if (!timeup) {
      best_move = pv[0];
      if (debug && debug->score)
        *debug->score = val;

      fprintf(f, "%i\t%i\t%lu\t%" PRIu64 "\t", depth, val, centiseconds_taken,
              nodes_searched);
      search_print_pv(pv, depth, f);

      alpha = val - aspiration_window;
      beta = val + aspiration_window;

      if ((val >= MATE) || (val <= -MATE)) {
        fprintf(f, "-> mate");

        if (!(debug && debug->continueOnMate)) {
          fprintf(f, "\n");
          break;
        }
      }

      fprintf(f, "\n");

      if (debug && debug->stopMove) {
{'fraction_non_alphanumeric': 0.12865697177074423, 'fraction_numerical': 0.014029084687767323, 'mean_word_length': 2.3291571753986333, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 49, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '707991', 'n_tokens_mistral': 2103, 'n_tokens_neox': 2000, 'n_words': 501}