comparison base/RealTimeSV.cpp @ 1427:622d193a00dc

Rework canonicalisation so as to avoid theoretical possibility of integer overflow
author Chris Cannam
date Mon, 11 Dec 2017 09:28:40 +0000
parents a1af054d8f75
children 48e9f538e6e9
comparison
equal deleted inserted replaced
1426:9ae40c7aecdf 1427:622d193a00dc
17 Rosegarden MIDI and audio sequencer and notation editor. 17 Rosegarden MIDI and audio sequencer and notation editor.
18 This file copyright 2000-2006 Chris Cannam. 18 This file copyright 2000-2006 Chris Cannam.
19 */ 19 */
20 20
21 #include <iostream> 21 #include <iostream>
22 #include <limits.h>
22 23
23 #include <cstdlib> 24 #include <cstdlib>
24 #include <sstream> 25 #include <sstream>
25 26
26 #include "RealTime.h" 27 #include "RealTime.h"
41 #define ONE_BILLION 1000000000 42 #define ONE_BILLION 1000000000
42 43
43 RealTime::RealTime(int s, int n) : 44 RealTime::RealTime(int s, int n) :
44 sec(s), nsec(n) 45 sec(s), nsec(n)
45 { 46 {
46 if (sec == 0) { 47 while (nsec <= -ONE_BILLION && sec > INT_MIN) { nsec += ONE_BILLION; --sec; }
47 while (nsec <= -ONE_BILLION) { nsec += ONE_BILLION; --sec; } 48 while (nsec >= ONE_BILLION && sec < INT_MAX) { nsec -= ONE_BILLION; ++sec; }
48 while (nsec >= ONE_BILLION) { nsec -= ONE_BILLION; ++sec; } 49 while (nsec > 0 && sec < 0) { nsec -= ONE_BILLION; ++sec; }
49 } else if (sec < 0) { 50 while (nsec < 0 && sec > 0) { nsec += ONE_BILLION; --sec; }
50 while (nsec <= -ONE_BILLION) { nsec += ONE_BILLION; --sec; }
51 while (nsec > 0 && sec < 0) { nsec -= ONE_BILLION; ++sec; }
52 } else {
53 while (nsec >= ONE_BILLION) { nsec -= ONE_BILLION; ++sec; }
54 while (nsec < 0 && sec > 0) { nsec += ONE_BILLION; --sec; }
55 }
56 } 51 }
57 52
58 RealTime 53 RealTime
59 RealTime::fromSeconds(double sec) 54 RealTime::fromSeconds(double sec)
60 { 55 {