]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/safe_numerics/example/example91.cpp
import quincy beta 17.1.0
[ceph.git] / ceph / src / boost / libs / safe_numerics / example / example91.cpp
1 //////////////////////////////////////////////////////////////////
2 // example91.cpp
3 //
4 // Copyright (c) 2015 Robert Ramey
5 //
6 // Distributed under the Boost Software License, Version 1.0. (See
7 // accompanying file LICENSE_1_0.txt or copy at
8 // http://www.boost.org/LICENSE_1_0.txt)
9
10 #include <iostream>
11 #include <limits>
12
13 #include <boost/safe_numerics/cpp.hpp>
14 #include <boost/safe_numerics/safe_integer.hpp>
15 #include <boost/safe_numerics//safe_integer_range.hpp>
16
17 // use same type promotion as used by the pic compiler
18 // see the following comment in motor.c
19 // Types: int8,int16,int32=8,16,32bit integers
20
21 using pic16_promotion = boost::safe_numerics::cpp<
22 8, // char
23 8, // short
24 8, // int
25 16, // long
26 32 // long long
27 >;
28
29 // define safe types used desktop version of the program. In conjunction
30 // with the promotion policy above, this will permit us to guarantee that
31 // the resulting program will be free of arithmetic errors introduced by
32 // C expression syntax and type promotion with no runtime penalty
33 template <typename T> // T is char, int, etc data type
34 using safe_t = boost::safe_numerics::safe<
35 T,
36 pic16_promotion,
37 boost::safe_numerics::default_exception_policy // use for compiling and running tests
38 >;
39 using safe_bool_t = boost::safe_numerics::safe_unsigned_range<
40 0,
41 1,
42 pic16_promotion,
43 boost::safe_numerics::default_exception_policy // use for compiling and running tests
44 >;
45 // alias original program's integer types to corresponding PIC safe types
46 // In conjunction with the promotion policy above, this will permit us to
47 // guarantee that the resulting program will be free of arithmetic errors
48 // introduced by C expression syntax and type promotion with no runtime penalty
49
50 typedef safe_t<int8_t> int8;
51 typedef safe_t<int16_t> int16;
52 typedef safe_t<int32_t> int32;
53 typedef safe_t<uint8_t> uint8;
54 typedef safe_t<uint16_t> uint16;
55 typedef safe_t<uint32_t> uint32;
56
57 // ***************************
58 // 4. emulate PIC features on the desktop
59
60 // filter out special keyword used only by XC8 compiler
61 #define __interrupt
62 // filter out XC8 enable/disable global interrupts
63 #define ei()
64 #define di()
65
66 #define DESKTOP
67 #include "motor1.c"
68
69 #include <chrono>
70 #include <thread>
71
72 void sleep(int16){
73 std::this_thread::sleep_for(std::chrono::microseconds(ccpr));
74 }
75
76 int main(){
77 std::cout << "start test\n";
78 try{
79 initialize();
80 motor_run(100);
81 do{
82 isr_motor_step();
83 }while (run_flg);
84
85 // move motor to position 1000
86 motor_run(1000);
87 do{
88 sleep(ccpr);
89 isr_motor_step();
90 }while (run_flg);
91
92 // move back to position 0
93 motor_run(0);
94 do{
95 sleep(ccpr);
96 isr_motor_step();
97 }while (run_flg);
98 }
99 catch(std::exception & e){
100 std::cout << e.what() << '\n';
101 // we expect to trap an exception
102 return 0;
103 }
104 catch(...){
105 std::cout << "test interrupted\n";
106 return 1;
107 }
108 std::cout << "end test\n";
109 return 1;
110 }