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
44 if ( sec_limit_up == UINT_MAX || ( game_status & flag_pondering ) )
46 time_max_limit = time_limit = UINT_MAX;
50 /* not punctual to the time */
51 if ( ! sec_limit && ( game_status & flag_time_extendable ) )
54 u1 = sec_limit_up * 5U;
57 else if ( sec_limit_up )
59 unsigned int umax, umin, sec_elapsed, sec_left;
61 sec_elapsed = turn ? sec_w_total : sec_b_total;
62 sec_left = ( sec_elapsed <= sec_limit ) ? sec_limit - sec_elapsed : 0;
63 u0 = ( sec_left + ( TC_NMOVE / 2U ) ) / TC_NMOVE;
66 t = 2s is not beneficial since 2.8s are almost the same as 1.8s.
67 So that, we rather want to use up the ordinary time.
69 if ( u0 == 2U ) { u0 = 3U; }
71 /* 'byo-yomi' is so long that the ordinary time-limit is negligible. */
72 if ( u0 < sec_limit_up * 3U / 2U ) { u0 = sec_limit_up * 3U / 2U; }
76 umax = sec_left + sec_limit_up;
79 if ( umax < u0 ) { u0 = umax; }
80 if ( umax < u1 ) { u1 = umax; }
81 if ( umin > u0 ) { u0 = umin; }
82 if ( umin > u1 ) { u1 = umin; }
86 unsigned int sec_elapsed, sec_left;
88 sec_elapsed = turn ? sec_w_total : sec_b_total;
90 /* We have some seconds left to think. */
91 if ( sec_elapsed + SEC_MARGIN < sec_limit )
93 sec_left = sec_limit - sec_elapsed;
94 u0 = ( sec_left + ( TC_NMOVE / 2U ) ) / TC_NMOVE;
96 /* t = 2s is not beneficial since 2.8s is almost the same as 1.8s. */
97 /* So that, we rather want to save the time. */
98 if ( u0 == 2U ) { u0 = 1U; }
101 /* We are running out of time... */
102 else { u0 = u1 = 1U; }
105 time_limit = u0 * 1000U + 1000U - time_response;
106 time_max_limit = u1 * 1000U + 1000U - time_response;
108 if ( game_status & flag_puzzling )
110 time_max_limit = time_max_limit / 5U;
111 time_limit = time_max_limit / 5U;
114 Out( "- time ctrl: %u -- %u\n", time_limit, time_max_limit );
119 renovate_time( int turn )
121 unsigned int te, time_elapsed;
124 iret = get_elapsed( &te );
125 if ( iret < 0 ) { return iret; }
127 time_elapsed = te - time_turn_start;
128 sec_elapsed = time_elapsed / 1000U;
129 if ( ! sec_elapsed ) { sec_elapsed = 1U; }
131 if ( turn ) { sec_w_total += sec_elapsed; }
132 else { sec_b_total += sec_elapsed; }
134 time_turn_start = te;
141 adjust_time( unsigned int elapsed_new, int turn )
143 const char *str = "TIME SKEW DETECTED";
147 if ( sec_w_total + elapsed_new < sec_elapsed )
152 else { sec_w_total = sec_w_total + elapsed_new - sec_elapsed; };
155 if ( sec_b_total + elapsed_new < sec_elapsed )
160 else { sec_b_total = sec_b_total + elapsed_new - sec_elapsed; };
166 reset_time( unsigned int b_remain, unsigned int w_remain )
168 if ( sec_limit_up == UINT_MAX )
170 str_error = "no time limit is set";
174 if ( b_remain > sec_limit || w_remain > sec_limit )
176 snprintf( str_message, SIZE_MESSAGE,
177 "time remaining can't be larger than %u", sec_limit );
178 str_error = str_message;
182 sec_b_total = sec_limit - b_remain;
183 sec_w_total = sec_limit - w_remain;
184 Out( " elapsed: b%u, w%u\n", sec_b_total, sec_w_total );
191 str_time( unsigned int time )
194 unsigned int time_min = time / 60000;
195 unsigned int time_sec = time / 1000;
196 unsigned int time_mil = time % 1000;
200 snprintf( str, 32, "%02u:%02u.%02u",
201 time_min, time_sec%60, time_mil/10 );
204 snprintf( str, 32, "%02u:%02u:%02u.%02u",
205 time_min / 60, time_min % 60, time_sec%60, time_mil/10 );
212 str_time_symple( unsigned int time )
215 unsigned int time_min = time / 60000;
216 unsigned int time_sec = time / 1000;
217 unsigned int time_mil = time % 1000;
221 snprintf( str, 32, "%5.2f", (float)(time_sec*1000+time_mil)/1000.0 );
223 else { snprintf( str, 32, "%2u:%02u", time_min, time_sec % 60 ); }
230 get_cputime( unsigned int *ptime )
234 FILETIME CreationTime, ExitTime, KernelTime, UserTime;
235 ULARGE_INTEGER uli_temp;
237 hProcess = GetCurrentProcess();
238 if ( GetProcessTimes( hProcess, &CreationTime, &ExitTime,
239 &KernelTime, &UserTime ) )
241 uli_temp.LowPart = UserTime.dwLowDateTime;
242 uli_temp.HighPart = UserTime.dwHighDateTime;
243 *ptime = (unsigned int)( uli_temp.QuadPart / 10000 );
247 str_error = "GetProcessTimes() failed.";
257 if ( times( &t ) == -1 )
259 str_error = "times() faild.";
262 clock_temp = t.tms_utime + t.tms_stime + t.tms_cutime + t.tms_cstime;
263 *ptime = (unsigned int)( clock_temp / clk_tck ) * 1000;
264 clock_temp %= clk_tck;
265 *ptime += (unsigned int)( (clock_temp * 1000) / clk_tck );
266 #endif /* no _WIN32 */
273 get_elapsed( unsigned int *ptime )
277 try timeBeginPeriod() and timeGetTime(),
278 or QueryPerformanceFrequency and QueryPerformanceCounter()
281 ULARGE_INTEGER uli_temp;
283 GetSystemTimeAsFileTime( &FileTime );
284 uli_temp.LowPart = FileTime.dwLowDateTime;
285 uli_temp.HighPart = FileTime.dwHighDateTime;
286 *ptime = (unsigned int)( uli_temp.QuadPart / 10000U );
288 struct timeval timeval;
290 if ( gettimeofday( &timeval, NULL ) == -1 )
292 str_error = "gettimeofday() faild.";
295 *ptime = (unsigned int)timeval.tv_sec * 1000;
296 *ptime += (unsigned int)timeval.tv_usec / 1000;
297 #endif /* no _WIN32 */