xref: /netbsd-src/external/gpl3/gcc.old/dist/libiberty/timeval-utils.c (revision 8feb0f0b7eaff0608f8350bbfa3098827b4bb91b)
11debfc3dSmrg /* Basic struct timeval utilities.
2*8feb0f0bSmrg    Copyright (C) 2011-2020 Free Software Foundation, Inc.
31debfc3dSmrg 
41debfc3dSmrg This file is part of the libiberty library.
51debfc3dSmrg Libiberty is free software; you can redistribute it and/or
61debfc3dSmrg modify it under the terms of the GNU Library General Public
71debfc3dSmrg License as published by the Free Software Foundation; either
81debfc3dSmrg version 2 of the License, or (at your option) any later version.
91debfc3dSmrg 
101debfc3dSmrg Libiberty is distributed in the hope that it will be useful,
111debfc3dSmrg but WITHOUT ANY WARRANTY; without even the implied warranty of
121debfc3dSmrg MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
131debfc3dSmrg Library General Public License for more details.
141debfc3dSmrg 
151debfc3dSmrg You should have received a copy of the GNU Library General Public
161debfc3dSmrg License along with libiberty; see the file COPYING.LIB.  If not,
171debfc3dSmrg write to the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
181debfc3dSmrg Boston, MA 02110-1301, USA.  */
191debfc3dSmrg 
201debfc3dSmrg #include "config.h"
211debfc3dSmrg 
221debfc3dSmrg /* On some systems (such as WindISS), you must include <sys/types.h>
231debfc3dSmrg    to get the definition of "time_t" before you include <time.h>.  */
241debfc3dSmrg #include <sys/types.h>
251debfc3dSmrg 
261debfc3dSmrg #ifdef TIME_WITH_SYS_TIME
271debfc3dSmrg # include <sys/time.h>
281debfc3dSmrg # include <time.h>
291debfc3dSmrg #else
301debfc3dSmrg # if HAVE_SYS_TIME_H
311debfc3dSmrg #  include <sys/time.h>
321debfc3dSmrg # else
331debfc3dSmrg #  ifdef HAVE_TIME_H
341debfc3dSmrg #   include <time.h>
351debfc3dSmrg #  endif
361debfc3dSmrg # endif
371debfc3dSmrg #endif
381debfc3dSmrg 
391debfc3dSmrg #include "timeval-utils.h"
401debfc3dSmrg 
411debfc3dSmrg /*
421debfc3dSmrg 
431debfc3dSmrg @deftypefn Extension void timeval_add (struct timeval *@var{a}, @
441debfc3dSmrg   struct timeval *@var{b}, struct timeval *@var{result})
451debfc3dSmrg 
461debfc3dSmrg Adds @var{a} to @var{b} and stores the result in @var{result}.
471debfc3dSmrg 
481debfc3dSmrg @end deftypefn
491debfc3dSmrg 
501debfc3dSmrg */
511debfc3dSmrg 
521debfc3dSmrg void
timeval_add(struct timeval * result,const struct timeval * a,const struct timeval * b)531debfc3dSmrg timeval_add (struct timeval *result,
541debfc3dSmrg 	     const struct timeval *a, const struct timeval *b)
551debfc3dSmrg {
561debfc3dSmrg   result->tv_sec = a->tv_sec + b->tv_sec;
571debfc3dSmrg   result->tv_usec = a->tv_usec + b->tv_usec;
581debfc3dSmrg   if (result->tv_usec >= 1000000)
591debfc3dSmrg     {
601debfc3dSmrg       ++result->tv_sec;
611debfc3dSmrg       result->tv_usec -= 1000000;
621debfc3dSmrg     }
631debfc3dSmrg }
641debfc3dSmrg 
651debfc3dSmrg /*
661debfc3dSmrg 
671debfc3dSmrg @deftypefn Extension void timeval_sub (struct timeval *@var{a}, @
681debfc3dSmrg   struct timeval *@var{b}, struct timeval *@var{result})
691debfc3dSmrg 
701debfc3dSmrg Subtracts @var{b} from @var{a} and stores the result in @var{result}.
711debfc3dSmrg 
721debfc3dSmrg @end deftypefn
731debfc3dSmrg 
741debfc3dSmrg */
751debfc3dSmrg 
761debfc3dSmrg void
timeval_sub(struct timeval * result,const struct timeval * a,const struct timeval * b)771debfc3dSmrg timeval_sub (struct timeval *result,
781debfc3dSmrg 	     const struct timeval *a, const struct timeval *b)
791debfc3dSmrg {
801debfc3dSmrg   result->tv_sec = a->tv_sec - b->tv_sec;
811debfc3dSmrg   result->tv_usec = a->tv_usec - b->tv_usec;
821debfc3dSmrg   if (result->tv_usec < 0)
831debfc3dSmrg     {
841debfc3dSmrg       --result->tv_sec;
851debfc3dSmrg       result->tv_usec += 1000000;
861debfc3dSmrg     }
871debfc3dSmrg }
88