Currently job artifacts in CI/CD pipelines on LRZ GitLab never expire. Starting from Wed 26.1.2022 the default expiration time will be 30 days (GitLab default). Currently existing artifacts in already completed jobs will not be affected by the change. The latest artifacts for all jobs in the latest successful pipelines will be kept. More information: https://gitlab.lrz.de/help/user/admin_area/settings/continuous_integration.html#default-artifacts-expiration

sensoroperations.cpp 3.85 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
//
//  sensoroperations.cpp
//  dcdb
//
//  Created by Tafani, Daniele on 13.04.18.
//  Copyright © 2018 LRZ. All rights reserved.
//

#include <limits>
#include <cmath>
#include "dcdb/sensoroperations.h"

namespace DCDB {
    
void doubleToFraction(int64_t* num, int64_t* den, double number) {
    
    *den = 1;
    while((std::abs(number) - std::abs(floor(number)))) {
        number *= 10;
        *den *= 10;
    }
    *num = number;
}

/* Safe implementation of addition between 64-bit integers */
DCDB_OP_RESULT safeAdd(int64_t lh, int64_t rh, int64_t* result) {
    
    if ( (lh < 0.0) == (rh < 0.0)
        && std::abs(lh) > std::numeric_limits<int64_t>::max() - std::abs(rh) )
        return DCDB_OP_OVERFLOW;
    
    *result = (int64_t) lh + rh;
    
    return DCDB_OP_SUCCESS;
}

/* Safe multiplication for 64-bit integers */
DCDB_OP_RESULT safeMult(int64_t lh, int64_t rh, int64_t* result) {
    
    if (lh > 0 && rh > 0 && lh > std::numeric_limits<int64_t>::max() / rh) {
        *result = std::numeric_limits<int64_t>::max();
        return DCDB_OP_OVERFLOW;
    }
    if (lh < 0 && rh > 0 && lh < std::numeric_limits<int64_t>::min() / rh) {
        *result = std::numeric_limits<int64_t>::min();
        return DCDB_OP_OVERFLOW;
    }
    if (lh > 0 && rh < 0 && rh < std::numeric_limits<int64_t>::min() / lh) {
        *result = std::numeric_limits<int64_t>::min();
        return DCDB_OP_OVERFLOW;
    }
    if (lh < 0 && rh < 0 && (lh <= std::numeric_limits<int64_t>::min()
                             || rh <= std::numeric_limits<int64_t>::min()
                             || -lh > std::numeric_limits<int64_t>::max() / -rh)) {
        *result = std::numeric_limits<int64_t>::max();
        return DCDB_OP_OVERFLOW;
    }
    *result = lh*rh;
    
    return DCDB_OP_SUCCESS;
}
    
/* Scale function for double  */
DCDB_OP_RESULT scale(int64_t* result, double scalingFactor, double baseScalingFactor) {
    
    double factor = scalingFactor * baseScalingFactor;
    int64_t product = *result;
    
    if(std::abs(factor) - std::abs(floor(factor))) {
        
        int64_t num = 0;
        int64_t denom = 0;
        
        doubleToFraction(&num, &denom, factor);
        
        DCDB_OP_RESULT multResult = safeMult(num, *result, &product);
        
        if(multResult != DCDB_OP_SUCCESS) {
            *result = product;
            return multResult;
        }
        
        *result = product / denom;
        return DCDB_OP_SUCCESS;
    }
        
    return safeMult((int64_t) factor, product, result);
};

/* Safe delta function */
DCDB_OP_RESULT delta(int64_t lh, int64_t rh, int64_t* result) {
    
Daniele Tafani's avatar
Daniele Tafani committed
93
94
    //TODO: Need to address overflow for monotonic sensor data, e.g., energy reaching max value.
    //Maybe need to add another field to sensorconfig ("monotonic")?
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
    //Or need to pass the maximum value detectable by the sensor?
    
    *result = lh - rh;
    
    return DCDB_OP_SUCCESS;
};

/* Safe implementation of a derivative */
DCDB_OP_RESULT derivative(int64_t lhx, int64_t rhx, uint64_t lht, uint64_t rht, int64_t* result) {
    
    int64_t dx = 0;
    uint64_t dt = 0;
    
    DCDB_OP_RESULT deltaResult = delta(lhx, rhx, &dx);
    
    if(deltaResult != DCDB_OP_SUCCESS) {
        *result = dx;
        return deltaResult;
    }
    
    dt = (int64_t) lht - rht;
    
    if(dt == 0)
        return DCDB_OP_DIVISION_BY_ZERO; //Presumably it's always != 0...
        
    *result = dx / dt;
    
    return DCDB_OP_SUCCESS;
};

/* Safe implementation of an integral */
DCDB_OP_RESULT integral(int64_t lhx, int64_t rhx, uint64_t lht, uint64_t rht, int64_t* result) {
    
    int64_t dx = 0;
    int64_t dt = 0;
    
    DCDB_OP_RESULT deltaResult = delta(lhx, rhx, &dx);
    if(deltaResult != DCDB_OP_SUCCESS) {
        *result = dx;
        return deltaResult;
    }
    
    dt = (int64_t) lht - rht;
    
    return safeMult(dx, dt, result);
    
};

} /* end DCDB namespace */