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
93
94
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
//! C's narrowing `double -> T` conversions — the single owner of each.
//!
//! `dbConvert.c` converts a `DBF_DOUBLE` value into an integer field with a
//! bare C cast — the PUT/GET macros are literally `*pdst = (typeb) *psrc`
//! (`dbConvert.c:96-113` and the GET twin at `:63-80`), instantiated per
//! destination width:
//!
//! ```c
//! static long putDoubleChar PUT(epicsFloat64, char)
//! static long putDoubleShort PUT(epicsFloat64, epicsInt16)
//! static long putDoubleLong PUT(epicsFloat64, epicsInt32)
//! static long putDoubleUlong PUT(epicsFloat64, epicsUInt32)
//! ... /* dbConvert.c:1631-1638 */
//! ```
//!
//! # Why this does not reproduce a compiled C IOC
//!
//! An out-of-range `double -> integer` cast is **undefined behaviour**
//! (C17 6.3.1.4p1), and the compiled IOC is therefore *not single-valued*.
//! Compiling that exact macro body for the two targets EPICS actually ships on
//! gives two different stored values:
//!
//! ```text
//! x86_64 cvttsd2si (%rdi), %eax out of range -> INT_MIN; NaN -> INT_MIN
//! aarch64 fcvtzs w0, d0 out of range -> SATURATES; NaN -> 0
//! ```
//!
//! So `3.0e9 -> i32` is `-2147483648` on an x86-64 IOC and `2147483647` on an
//! ARM one (Raspberry Pi and Zynq IOCs are ordinary in EPICS). There is no
//! "what C does" here to be bug-for-bug faithful *to*: the behaviour is UB, and
//! UB is definitionally not a contract. An earlier revision of this module
//! reproduced the x86-64 `INT_MIN`, which did not close a divergence so much as
//! trade agreement-with-ARM for agreement-with-x86 while deliberately adopting
//! the undefined value.
//!
//! *C's bugs are not the contract; clean is the goal* — the port
//! **saturates**: an out-of-range value clamps to the destination's range and
//! NaN converts to 0. That is Rust's native `as`, and it is byte-identical to
//! a compiled C IOC on aarch64. This is a Tier 2 (semantics) decision, signed
//! off 2026-07-14; Tier 1 is untouched, because a `DBF_LONG` field carries 32
//! bits on the wire regardless of which 32 bits we chose.
//!
//! No alarm is raised: `dbConvert`'s routines run on the put path, outside the
//! record's own process cycle, so an alarm raised here would be erased by the
//! next `recGblResetAlarms` unless routed through `nsta`/`nsev` — and even
//! routed correctly it would flag a record whose stored value is now perfectly
//! valid and in range.
//!
//! This module remains the SINGLE OWNER of the cast. Every `double -> integer`
//! conversion that models a `dbConvert` cast calls it; no such site may use a
//! bare `as`, so that the policy above is changeable in exactly one place.
//!
//! # `double -> float` is the opposite case
//!
//! [`f64_to_f32`] is not part of the story above. C narrows a `double` to a
//! `DBF_FLOAT` through a named helper, `epicsConvertDoubleToFloat`, so the
//! behaviour is specified rather than undefined and the port reproduces it
//! exactly — Tier 1, because the clamped magnitude is what goes on the wire in
//! a `DBR_FLOAT` reply. It shares this module only because the ownership rule
//! is the same: a field, link, or wire conversion may not narrow with a bare
//! `as f32`.
/// C `(epicsInt32) d`, saturated: clamps to `i32`'s range, NaN -> 0.
/// C `(epicsInt64) d`, saturated: clamps to `i64`'s range, NaN -> 0.
/// C `(char) d` / `(epicsInt8) d`, saturated: clamps to `i8`'s range, NaN -> 0.
/// C `(epicsUInt8) d`, saturated: clamps to `u8`'s range, negatives and NaN -> 0.
/// C `(epicsInt16) d`, saturated: clamps to `i16`'s range, NaN -> 0.
/// C `(epicsUInt16) d` / `(epicsEnum16) d`, saturated: clamps to `u16`'s range,
/// negatives and NaN -> 0.
/// C `(epicsUInt32) d`, saturated: clamps to `u32`'s range, negatives and
/// NaN -> 0.
/// C `(epicsUInt64) d`, saturated: clamps to `u64`'s range, negatives and
/// NaN -> 0.
/// C `epicsConvertDoubleToFloat` (`epicsConvert.c:19-34`), verbatim.
///
/// ```c
/// if (value == 0 || !finite(value)) return (float) value;
/// abs = fabs(value);
/// if (abs >= FLT_MAX) return (value > 0) ? FLT_MAX : -FLT_MAX;
/// if (abs <= FLT_MIN) return (value > 0) ? FLT_MIN : -FLT_MIN;
/// return (float) value;
/// ```
///
/// Not saturation: zero and non-finite pass straight through, so a NaN stays a
/// NaN and a genuine infinity stays an infinity — only a finite magnitude is
/// clamped. Both bounds are inclusive, and the lower one clamps to `FLT_MIN`,
/// the smallest NORMAL float, keeping the sign: a tiny non-zero double
/// (`1e-300`) reaches a `DBF_FLOAT` field as `1.17549e-38`, never as `0.0`.
///
/// Every C `double -> float` conversion goes through this helper —
/// `getDoubleFloat`/`putDoubleFloat` (`dbConvert.c:815,822,1646,1651`),
/// `cvt_d_f` (`dbFastLinkConv.c:1426`), and the `DBR_GR_FLOAT`/`DBR_CTRL_FLOAT`
/// limit fill (`db_access.c:480-485,629-636`) — which is why the port has one
/// owner here rather than a clamp at each site.