source: golgotha/src/i4/time/unixtime.cc

Last change on this file was 80, checked in by Sam Hocevar, 11 years ago
  • Adding the Golgotha source code. Not sure what's going to be interesting in there, but since it's all public domain, there's certainly stuff to pick up.
File size: 3.3 KB
RevLine 
[80]1/********************************************************************** <BR>
2  This file is part of Crack dot Com's free source code release of
3  Golgotha. <a href="http://www.crack.com/golgotha_release"> <BR> for
4  information about compiling & licensing issues visit this URL</a>
5  <PRE> If that doesn't help, contact Jonathan Clark at
6  golgotha_source@usa.net (Subject should have "GOLG" in it)
7***********************************************************************/
8
9#include "time/time.hh"
10#include <stdio.h>
11#include <stdlib.h>
12#include <sys/time.h>
13#include <unistd.h>
14
15void i4_time_class::get()
16{
17  struct timezone tz={0,0};
18  struct timeval tv;
19
20  gettimeofday( (struct timeval *)&tv,&tz);
21             
22  time.unix_time.sec=tv.tv_sec;
23  time.unix_time.usec=tv.tv_usec;
24}
25
26
27void i4_time_class::add_milli(sw32 milli_sec)
28{
29
30  sw32 new_usec;
31
32  if (milli_sec>0)
33  {
34    if (milli_sec>1000)
35    {
36      int sec_add=milli_sec/1000;
37      time.unix_time.sec+=sec_add;
38      milli_sec-=sec_add*1000;
39    }
40
41    new_usec=time.unix_time.usec+milli_sec*1000;
42    if (new_usec>1000*1000)
43    {
44      time.unix_time.sec++;
45      new_usec-=1000*1000;
46    }
47  } else
48  {
49    if (milli_sec<-1000)
50    {
51      int sec_add=milli_sec/1000;
52      time.unix_time.sec+=sec_add;
53      milli_sec-=sec_add*1000;
54    }
55
56    new_usec=time.unix_time.usec-milli_sec*1000;
57    if (new_usec<0)
58    {
59      new_usec+=1000*1000;
60      time.unix_time.sec--;
61    }
62  }
63  time.unix_time.usec=new_usec;
64}
65
66
67i4_bool i4_time_class::operator >(const i4_time_class &other) const
68{
69  return (other.time.unix_time.sec<time.unix_time.sec ||
70          (other.time.unix_time.sec==time.unix_time.sec &&
71           other.time.unix_time.usec<time.unix_time.usec));
72}
73
74
75i4_bool i4_time_class::operator <(const i4_time_class &other) const
76{
77  return (other.time.unix_time.sec>time.unix_time.sec ||
78          (other.time.unix_time.sec==time.unix_time.sec &&
79           other.time.unix_time.usec>time.unix_time.usec));
80}
81
82
83
84sw32 i4_time_class::milli_diff(const i4_time_class &past_time) const
85{
86  return (time.unix_time.usec-past_time.time.unix_time.usec)/1000 +
87    (time.unix_time.sec-past_time.time.unix_time.sec)*1000;
88}
89
90i4_time_class::i4_time_class(sw32 milli_sec)
91{
92  sw32 sec=milli_sec/1000;
93  time.unix_time.sec=sec;
94  milli_sec-=sec*1000;
95  time.unix_time.usec=milli_sec*1000;
96}
97
98
99w64 i4_get_system_clock()
100{
101  w32 low, high;
102
103  asm volatile ("      .byte 0x0f
104                       .byte 0x31
105                           movl %%eax, %0
106                           movl %%edx, %1"
107                : /* no outputs */
108                : /* two "m" (memory) inputs */ "m" (low), "m" (high)
109                : /* two trashed registers */ "eax", "edx");
110
111
112  return (((w64)high)<<32) | low;
113}
114
115
116
117int i4_win_clocks_per_sec = -1;
118
119int i4_get_clocks_per_second()
120{
121  if (i4_win_clocks_per_sec==-1)
122  {
123    w64 _start = i4_get_system_clock();
124 
125    i4_time_class now,start;
126    while (now.milli_diff(start) < 1000) now.get();
127 
128    w64 end = i4_get_system_clock();
129
130    i4_win_clocks_per_sec = end - _start;
131  }
132 
133  return i4_win_clocks_per_sec;
134}
135
136void i4_sleep(int seconds) { sleep(seconds); }
137void i4_milli_sleep(int milli_seconds) { usleep(milli_seconds*1000); }
138
Note: See TracBrowser for help on using the repository browser.