11 set_search_limit_time( int turn )
15 sec_limit time limit of a side for an entire game in seconds
16 sec_limit_up time limit for a move when the ordinary time has run out.
17 the time is known as 'byo-yomi'.
18 sec_b_total seconds spent by black
19 sec_w_total seconds spent by white
21 ( game_status & flag_puzzling ) time-control for puzzling-search
22 ( game_status & flag_pondering ) time-control for pondering-search
23 ( game_status & flag_time_extendable ) bonanza isn't punctual to the time
30 time_limit tentative deadline for searching in millisecond
31 time_max_limit maximum allowed time to search in millisecond
35 sec_elapsed second spent by the side to move
36 sec_left time left for the side in seconds
37 u0 tentative deadline for searching in second
38 u1 maximum allowed time to search in second
43 extern char xboard_mode;
44 if(xboard_mode) return;
47 if ( sec_limit_up == UINT_MAX || ( game_status & flag_pondering ) )
49 time_max_limit = time_limit = UINT_MAX;
53 /* not punctual to the time */
54 if ( ! sec_limit && ( game_status & flag_time_extendable ) )
57 u1 = sec_limit_up * 5U;
60 else if ( sec_limit_up )
62 unsigned int umax, umin, sec_elapsed, sec_left;
64 sec_elapsed = turn ? sec_w_total : sec_b_total;
65 sec_left = ( sec_elapsed <= sec_limit ) ? sec_limit - sec_elapsed : 0;
66 u0 = ( sec_left + ( TC_NMOVE / 2U ) ) / TC_NMOVE;
69 t = 2s is not beneficial since 2.8s are almost the same as 1.8s.
70 So that, we rather want to use up the ordinary time.
72 if ( u0 == 2U ) { u0 = 3U; }
74 /* 'byo-yomi' is so long that the ordinary time-limit is negligible. */
75 if ( u0 < sec_limit_up * 3U / 2U ) { u0 = sec_limit_up * 3U / 2U; }
79 umax = sec_left + sec_limit_up;
82 if ( umax < u0 ) { u0 = umax; }
83 if ( umax < u1 ) { u1 = umax; }
84 if ( umin > u0 ) { u0 = umin; }
85 if ( umin > u1 ) { u1 = umin; }
89 unsigned int sec_elapsed, sec_left;
91 sec_elapsed = turn ? sec_w_total : sec_b_total;
93 /* We have some seconds left to think. */
94 if ( sec_elapsed + SEC_MARGIN < sec_limit )
96 sec_left = sec_limit - sec_elapsed;
97 u0 = ( sec_left + ( TC_NMOVE / 2U ) ) / TC_NMOVE;
99 /* t = 2s is not beneficial since 2.8s is almost the same as 1.8s. */
100 /* So that, we rather want to save the time. */
101 if ( u0 == 2U ) { u0 = 1U; }
104 /* We are running out of time... */
105 else { u0 = u1 = 1U; }
108 time_limit = u0 * 1000U + 1000U - time_response;
109 time_max_limit = u1 * 1000U + 1000U - time_response;
111 if ( game_status & flag_puzzling )
113 time_max_limit = time_max_limit / 5U;
114 time_limit = time_max_limit / 5U;
117 Out( "- time ctrl: %u -- %u\n", time_limit, time_max_limit );
122 renovate_time( int turn )
124 unsigned int te, time_elapsed;
127 iret = get_elapsed( &te );
128 if ( iret < 0 ) { return iret; }
130 time_elapsed = te - time_turn_start;
131 sec_elapsed = time_elapsed / 1000U;
132 if ( ! sec_elapsed ) { sec_elapsed = 1U; }
134 if ( turn ) { sec_w_total += sec_elapsed; }
135 else { sec_b_total += sec_elapsed; }
137 time_turn_start = te;
144 adjust_time( unsigned int elapsed_new, int turn )
146 const char *str = "TIME SKEW DETECTED";
150 if ( sec_w_total + elapsed_new < sec_elapsed )
155 else { sec_w_total = sec_w_total + elapsed_new - sec_elapsed; };
158 if ( sec_b_total + elapsed_new < sec_elapsed )
163 else { sec_b_total = sec_b_total + elapsed_new - sec_elapsed; };
169 reset_time( unsigned int b_remain, unsigned int w_remain )
171 if ( sec_limit_up == UINT_MAX )
173 str_error = "no time limit is set";
177 if ( b_remain > sec_limit || w_remain > sec_limit )
179 snprintf( str_message, SIZE_MESSAGE,
180 "time remaining can't be larger than %u", sec_limit );
181 str_error = str_message;
185 sec_b_total = sec_limit - b_remain;
186 sec_w_total = sec_limit - w_remain;
187 Out( " elapsed: b%u, w%u\n", sec_b_total, sec_w_total );
194 str_time( unsigned int time )
197 unsigned int time_min = time / 60000;
198 unsigned int time_sec = time / 1000;
199 unsigned int time_mil = time % 1000;
203 snprintf( str, 32, "%02u:%02u.%02u",
204 time_min, time_sec%60, time_mil/10 );
207 snprintf( str, 32, "%02u:%02u:%02u.%02u",
208 time_min / 60, time_min % 60, time_sec%60, time_mil/10 );
215 str_time_symple( unsigned int time )
218 unsigned int time_min = time / 60000;
219 unsigned int time_sec = time / 1000;
220 unsigned int time_mil = time % 1000;
224 snprintf( str, 32, "%5.2f", (float)(time_sec*1000+time_mil)/1000.0 );
226 else { snprintf( str, 32, "%2u:%02u", time_min, time_sec % 60 ); }
233 get_cputime( unsigned int *ptime )
237 FILETIME CreationTime, ExitTime, KernelTime, UserTime;
238 ULARGE_INTEGER uli_temp;
240 hProcess = GetCurrentProcess();
241 if ( GetProcessTimes( hProcess, &CreationTime, &ExitTime,
242 &KernelTime, &UserTime ) )
244 uli_temp.LowPart = UserTime.dwLowDateTime;
245 uli_temp.HighPart = UserTime.dwHighDateTime;
246 *ptime = (unsigned int)( uli_temp.QuadPart / 10000 );
250 str_error = "GetProcessTimes() failed.";
260 if ( times( &t ) == -1 )
262 str_error = "times() faild.";
265 clock_temp = t.tms_utime + t.tms_stime + t.tms_cutime + t.tms_cstime;
266 *ptime = (unsigned int)( clock_temp / clk_tck ) * 1000;
267 clock_temp %= clk_tck;
268 *ptime += (unsigned int)( (clock_temp * 1000) / clk_tck );
269 #endif /* no _WIN32 */
276 get_elapsed( unsigned int *ptime )
280 try timeBeginPeriod() and timeGetTime(),
281 or QueryPerformanceFrequency and QueryPerformanceCounter()
284 ULARGE_INTEGER uli_temp;
286 GetSystemTimeAsFileTime( &FileTime );
287 uli_temp.LowPart = FileTime.dwLowDateTime;
288 uli_temp.HighPart = FileTime.dwHighDateTime;
289 *ptime = (unsigned int)( uli_temp.QuadPart / 10000U );
291 struct timeval timeval;
293 if ( gettimeofday( &timeval, NULL ) == -1 )
295 str_error = "gettimeofday() faild.";
298 *ptime = (unsigned int)timeval.tv_sec * 1000;
299 *ptime += (unsigned int)timeval.tv_usec / 1000;
300 #endif /* no _WIN32 */