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;
54 if ( usi_mode != usi_off && usi_byoyomi )
56 time_max_limit = time_limit = usi_byoyomi;
57 Out( "- time ctrl: %u -- %u\n", time_limit, time_max_limit );
62 /* not punctual to the time */
63 if ( ! sec_limit && ( game_status & flag_time_extendable ) )
66 u1 = sec_limit_up * 5U;
69 else if ( sec_limit_up )
71 unsigned int umax, umin, sec_elapsed, sec_left;
73 sec_elapsed = turn ? sec_w_total : sec_b_total;
74 sec_left = ( sec_elapsed <= sec_limit ) ? sec_limit - sec_elapsed : 0;
75 u0 = ( sec_left + ( TC_NMOVE / 2U ) ) / TC_NMOVE;
78 t = 2s is not beneficial since 2.8s are almost the same as 1.8s.
79 So that, we rather want to use up the ordinary time.
81 if ( u0 == 2U ) { u0 = 3U; }
83 /* 'byo-yomi' is so long that the ordinary time-limit is negligible. */
84 if ( u0 < sec_limit_up * 5U ) { u0 = sec_limit_up * 5U; }
88 umax = sec_left + sec_limit_up;
91 if ( umax < u0 ) { u0 = umax; }
92 if ( umax < u1 ) { u1 = umax; }
93 if ( umin > u0 ) { u0 = umin; }
94 if ( umin > u1 ) { u1 = umin; }
98 unsigned int sec_elapsed, sec_left;
100 sec_elapsed = turn ? sec_w_total : sec_b_total;
102 /* We have some seconds left to think. */
103 if ( sec_elapsed + SEC_MARGIN < sec_limit )
105 sec_left = sec_limit - sec_elapsed;
106 u0 = ( sec_left + ( TC_NMOVE / 2U ) ) / TC_NMOVE;
108 /* t = 2s is not beneficial since 2.8s is almost the same as 1.8s. */
109 /* So that, we rather want to save the time. */
110 if ( u0 == 2U ) { u0 = 1U; }
113 /* We are running out of time... */
114 else { u0 = u1 = 1U; }
117 time_limit = u0 * 1000U + 1000U - time_response;
118 time_max_limit = u1 * 1000U + 1000U - time_response;
120 if ( game_status & flag_puzzling )
122 time_max_limit = time_max_limit / 5U;
123 time_limit = time_max_limit / 5U;
126 Out( "- time ctrl: %u -- %u\n", time_limit, time_max_limit );
131 update_time( int turn )
133 unsigned int te, time_elapsed;
136 iret = get_elapsed( &te );
137 if ( iret < 0 ) { return iret; }
139 time_elapsed = te - time_turn_start;
140 sec_elapsed = time_elapsed / 1000U;
141 if ( ! sec_elapsed ) { sec_elapsed = 1U; }
143 if ( turn ) { sec_w_total += sec_elapsed; }
144 else { sec_b_total += sec_elapsed; }
146 time_turn_start = te;
153 adjust_time( unsigned int elapsed_new, int turn )
155 const char *str = "TIME SKEW DETECTED";
159 if ( sec_w_total + elapsed_new < sec_elapsed )
164 else { sec_w_total = sec_w_total + elapsed_new - sec_elapsed; };
167 if ( sec_b_total + elapsed_new < sec_elapsed )
172 else { sec_b_total = sec_b_total + elapsed_new - sec_elapsed; };
178 reset_time( unsigned int b_remain, unsigned int w_remain )
180 if ( sec_limit_up == UINT_MAX )
182 str_error = "no time limit is set";
186 if ( b_remain > sec_limit || w_remain > sec_limit )
188 snprintf( str_message, SIZE_MESSAGE,
189 "time remaining can't be larger than %u", sec_limit );
190 str_error = str_message;
194 sec_b_total = sec_limit - b_remain;
195 sec_w_total = sec_limit - w_remain;
196 Out( " elapsed: b%u, w%u\n", sec_b_total, sec_w_total );
203 str_time( unsigned int time )
206 unsigned int time_min = time / 60000;
207 unsigned int time_sec = time / 1000;
208 unsigned int time_mil = time % 1000;
212 snprintf( str, 32, "%02u:%02u.%02u",
213 time_min, time_sec%60, time_mil/10 );
216 snprintf( str, 32, "%02u:%02u:%02u.%02u",
217 time_min / 60, time_min % 60, time_sec%60, time_mil/10 );
224 str_time_symple( unsigned int time )
227 unsigned int time_min = time / 60000;
228 unsigned int time_sec = time / 1000;
229 unsigned int time_mil = time % 1000;
233 snprintf( str, 32, "%5.2f", (float)(time_sec*1000+time_mil)/1000.0 );
235 else { snprintf( str, 32, "%2u:%02u", time_min, time_sec % 60 ); }
242 get_cputime( unsigned int *ptime )
246 FILETIME CreationTime, ExitTime, KernelTime, UserTime;
247 ULARGE_INTEGER uli_temp;
249 hProcess = GetCurrentProcess();
250 if ( GetProcessTimes( hProcess, &CreationTime, &ExitTime,
251 &KernelTime, &UserTime ) )
253 uli_temp.LowPart = UserTime.dwLowDateTime;
254 uli_temp.HighPart = UserTime.dwHighDateTime;
255 *ptime = (unsigned int)( uli_temp.QuadPart / 10000 );
259 str_error = "GetProcessTimes() failed.";
269 if ( times( &t ) == -1 )
271 str_error = "times() faild.";
274 clock_temp = t.tms_utime + t.tms_stime + t.tms_cutime + t.tms_cstime;
275 *ptime = (unsigned int)( clock_temp / clk_tck ) * 1000;
276 clock_temp %= clk_tck;
277 *ptime += (unsigned int)( (clock_temp * 1000) / clk_tck );
278 #endif /* no _WIN32 */
285 get_elapsed( unsigned int *ptime )
289 try timeBeginPeriod() and timeGetTime(),
290 or QueryPerformanceFrequency and QueryPerformanceCounter()
293 ULARGE_INTEGER uli_temp;
295 GetSystemTimeAsFileTime( &FileTime );
296 uli_temp.LowPart = FileTime.dwLowDateTime;
297 uli_temp.HighPart = FileTime.dwHighDateTime;
298 *ptime = (unsigned int)( uli_temp.QuadPart / 10000U );
300 struct timeval timeval;
302 if ( gettimeofday( &timeval, NULL ) == -1 )
304 str_error = "gettimeofday() faild.";
307 *ptime = (unsigned int)timeval.tv_sec * 1000;
308 *ptime += (unsigned int)timeval.tv_usec / 1000;
309 #endif /* no _WIN32 */