rogue
Loading...
Searching...
No Matches
Block.cpp
Go to the documentation of this file.
1
18#include "rogue/Directives.h"
19
20#ifndef NO_PYTHON
21
22#define NO_IMPORT_ARRAY
23#include "rogue/numpy.h"
24#include <boost/python.hpp>
25
26namespace bp = boost::python;
27
28#endif
29
31
32#include <inttypes.h>
33#include <sys/time.h>
34
35#include <cmath>
36#include <cstdio>
37#include <cstring>
38#include <exception>
39#include <iomanip>
40#include <memory>
41#include <mutex>
42#include <sstream>
43#include <string>
44#include <vector>
45
46#include "rogue/GeneralError.h"
47#include "rogue/GilRelease.h"
48#include "rogue/ScopedGil.h"
53
55
56
57// Class factory which returns a pointer to a Block (BlockPtr)
58rim::BlockPtr rim::Block::create(uint64_t offset, uint32_t size) {
59 rim::BlockPtr b = std::make_shared<rim::Block>(offset, size);
60 return (b);
61}
62
63#ifndef NO_PYTHON
64// Setup class for use in python
65void rim::Block::setup_python() {
66 bp::class_<rim::Block, rim::BlockPtr, bp::bases<rim::Master>, boost::noncopyable>("Block",
67 bp::init<uint64_t, uint32_t>())
68 .add_property("path", &rim::Block::path)
69 .add_property("mode", &rim::Block::mode)
70 .add_property("bulkOpEn", &rim::Block::bulkOpEn)
71 .add_property("offset", &rim::Block::offset)
72 .add_property("address", &rim::Block::address)
73 .add_property("size", &rim::Block::size)
74 .def("setEnable", &rim::Block::setEnable)
75 .def("_startTransaction", &rim::Block::startTransactionPy)
76 .def("_checkTransaction", &rim::Block::checkTransactionPy)
77 .def("addVariables", &rim::Block::addVariablesPy)
78 .def("_rateTest", &rim::Block::rateTest)
79 .add_property("variables", &rim::Block::variablesPy);
80
81 bp::implicitly_convertible<rim::BlockPtr, rim::MasterPtr>();
82}
83#endif
84
85// Create a Hub device with a given offset
86rim::Block::Block(uint64_t offset, uint32_t size) {
87 path_ = "Undefined";
88 mode_ = "RW";
89 bulkOpEn_ = false;
90 updateEn_ = false;
91 offset_ = offset;
92 size_ = size;
93 verifyEn_ = false;
94 verifyReq_ = false;
95 verifyInp_ = false;
96 doUpdate_ = false;
97 blockPyTrans_ = false;
98 enable_ = false;
99 stale_ = false;
100 retryCount_ = 0;
101
102 verifyBase_ = 0; // Verify Range
103 verifySize_ = 0; // Verify Range
104
105 blockData_ = reinterpret_cast<uint8_t*>(malloc(size_));
106 memset(blockData_, 0, size_);
107
108 verifyData_ = reinterpret_cast<uint8_t*>(malloc(size_));
109 memset(verifyData_, 0, size_);
110
111 verifyMask_ = reinterpret_cast<uint8_t*>(malloc(size_));
112 memset(verifyMask_, 0, size_);
113
114 verifyBlock_ = reinterpret_cast<uint8_t*>(malloc(size_));
115 memset(verifyBlock_, 0, size_);
116
117 expectedData_ = reinterpret_cast<uint8_t*>(malloc(size_));
118 memset(expectedData_, 0, size_);
119}
120
121// Destroy the Hub
122rim::Block::~Block() {
123 // Custom clean
124 customClean();
125
126 free(blockData_);
127 free(verifyData_);
128 free(verifyMask_);
129 free(verifyBlock_);
130 free(expectedData_);
131}
132
133// Return the path of the block
134std::string rim::Block::path() {
135 return path_;
136}
137
138// Return the mode of the block
139std::string rim::Block::mode() {
140 return mode_;
141}
142
143// Return bulk enable flag
144bool rim::Block::bulkOpEn() {
145 return bulkOpEn_;
146}
147
148// Set enable state
149void rim::Block::setEnable(bool newState) {
150 rogue::GilRelease noGil;
151 std::lock_guard<std::mutex> lock(mtx_);
152 enable_ = newState;
153}
154
155// Get offset of this Block
156uint64_t rim::Block::offset() {
157 return offset_;
158}
159
160// Get full address of this Block
161uint64_t rim::Block::address() {
162 return (reqAddress() + offset_);
163}
164
165// Get size of this block in bytes.
166uint32_t rim::Block::size() {
167 return size_;
168}
169
170// Block transactions
171bool rim::Block::blockPyTrans() {
172 return blockPyTrans_;
173}
174
175// Start a transaction for this block
176void rim::Block::intStartTransaction(uint32_t type, bool forceWr, rim::Variable* var, int32_t index) {
177 uint32_t x;
178 uint32_t tOff;
179 uint32_t tSize;
180 uint8_t* tData;
181 uint32_t highByte;
182 uint32_t lowByte;
183 uint32_t minAccess = getSlave()->doMinAccess();
184
185 std::vector<rim::VariablePtr>::iterator vit;
186
187 // Check for valid combinations
188 if ((type == rim::Write && ((mode_ == "RO") || (!stale_ && !forceWr))) || (type == rim::Post && (mode_ == "RO")) ||
189 (type == rim::Read && ((mode_ == "WO") || stale_)) ||
190 (type == rim::Verify && ((mode_ == "WO") || (mode_ == "RO") || stale_ || !verifyReq_)))
191 return;
192 {
193 rogue::GilRelease noGil;
194 std::lock_guard<std::mutex> lock(mtx_);
195 waitTransaction(0);
196 clearError();
197
198 // Determine transaction range
199 if (var == NULL) {
200 lowByte = 0;
201 highByte = size_ - 1;
202 if (type == rim::Write || type == rim::Post) {
203 stale_ = false;
204 for (vit = variables_.begin(); vit != variables_.end(); ++vit) {
205 (*vit)->stale_ = false;
206 }
207 }
208 } else {
209 if (type == rim::Read || type == rim::Verify) {
210 if (index < 0 || index >= var->numValues_) {
211 lowByte = var->lowTranByte_[0];
212
213 if (var->numValues_ == 0) {
214 highByte = var->highTranByte_[0];
215 } else {
216 highByte = var->highTranByte_[var->numValues_ - 1];
217 }
218 } else {
219 lowByte = var->lowTranByte_[index];
220 highByte = var->highTranByte_[index];
221 }
222 } else {
223 lowByte = var->staleLowByte_;
224 highByte = var->staleHighByte_;
225
226 // Catch case where fewer stale bytes than min access or non-aligned
227 if (lowByte % minAccess != 0) lowByte -= lowByte % minAccess;
228 if ((highByte + 1) % minAccess != 0) highByte += minAccess - ((highByte + 1) % minAccess);
229 stale_ = false;
230 for (vit = variables_.begin(); vit != variables_.end(); ++vit) {
231 if ((*vit)->stale_) {
232 if ((*vit)->staleLowByte_ < lowByte) lowByte = (*vit)->staleLowByte_;
233 if ((*vit)->staleHighByte_ > highByte) highByte = (*vit)->staleHighByte_;
234 (*vit)->stale_ = false;
235 }
236 }
237 }
238 }
239
240 // Device is disabled, check after clearing stale states
241 if (!enable_) return;
242
243 // Setup verify data, clear verify write flag if verify transaction
244 if (type == rim::Verify) {
245 tOff = verifyBase_;
246 tSize = verifySize_;
247 tData = verifyData_ + verifyBase_;
248 verifyInp_ = true;
249
250 // Not a verify transaction
251 } else {
252 // Derive offset and size based upon min transaction size
253 tOff = lowByte;
254 tSize = (highByte - lowByte) + 1;
255
256 // Set transaction pointer
257 tData = blockData_ + tOff;
258
259 // Track verify after writes.
260 // Only verify blocks that have been written since last verify
261 if (type == rim::Write) {
262 verifyBase_ = tOff;
263 verifySize_ = tSize;
264 verifyReq_ = verifyEn_;
265
266 // Snapshot expected data at write time so that concurrent
267 // modifications to blockData_ do not cause false verify failures
268 memcpy(expectedData_ + tOff, blockData_ + tOff, tSize);
269 }
270 }
271 doUpdate_ = updateEn_;
272
273 bLog_->debug("Start transaction type = %" PRIu32 ", Offset=0x%" PRIx64 ", lByte=%" PRIu32 ", hByte=%" PRIu32
274 ", tOff=0x%" PRIx32 ", tSize=%" PRIu32,
275 type,
276 offset_,
277 lowByte,
278 highByte,
279 tOff,
280 tSize);
281
282 // Start transaction
283 reqTransaction(offset_ + tOff, tSize, tData, type);
284 }
285}
286
287// Start a transaction for this block, cpp version
288void rim::Block::startTransaction(uint32_t type, bool forceWr, bool check, rim::Variable* var, int32_t index) {
289 uint32_t count;
290 bool fWr;
291
292 count = 0;
293 fWr = forceWr;
294
295 do {
296 intStartTransaction(type, fWr, var, index);
297
298 try {
299 if (check || retryCount_ > 0) checkTransaction();
300 count = retryCount_; // Success
301 } catch (rogue::GeneralError err) {
302 fWr = true; // Stale state is now lost
303
304 // Rethrow the error if this is the final retry, else log warning
305 if ((count + 1) > retryCount_) {
306 bLog_->error("Error on try %" PRIu32 " out of %" PRIu32 ": %s",
307 (count + 1),
308 (retryCount_ + 1),
309 err.what());
310 throw err;
311 } else {
312 bLog_->warning("Error on try %" PRIu32 " out of %" PRIu32 ": %s",
313 (count + 1),
314 (retryCount_ + 1),
315 err.what());
316 }
317 }
318 } while (count++ < retryCount_);
319}
320
321#ifndef NO_PYTHON
322
323// Start a transaction for this block, python version
324void rim::Block::startTransactionPy(uint32_t type, bool forceWr, bool check, rim::VariablePtr var, int32_t index) {
325 uint32_t count;
326 bool upd;
327 bool fWr;
328
329 if (blockPyTrans_) return;
330
331 count = 0;
332 upd = false;
333 fWr = forceWr;
334
335 do {
336 intStartTransaction(type, fWr, var.get(), index);
337
338 try {
339 if (check || retryCount_ > 0) upd = checkTransaction();
340 count = retryCount_; // Success
341 } catch (rogue::GeneralError err) {
342 fWr = true; // Stale state is now lost
343
344 // Rethrow the error if this is the final retry, else log warning
345 if ((count + 1) > retryCount_) {
346 bLog_->error("Error on try %" PRIu32 " out of %" PRIu32 ": %s",
347 (count + 1),
348 (retryCount_ + 1),
349 err.what());
350 throw err;
351 } else {
352 bLog_->warning("Error on try %" PRIu32 " out of %" PRIu32 ": %s",
353 (count + 1),
354 (retryCount_ + 1),
355 err.what());
356 }
357 }
358 } while (count++ < retryCount_);
359
360 if (upd) varUpdate();
361}
362
363#endif
364
365// Wait for pending transaction completion and check the result
366bool rim::Block::checkTransaction() {
367 std::string err;
368 bool locUpdate;
369 uint32_t x;
370
371 {
372 rogue::GilRelease noGil;
373 std::lock_guard<std::mutex> lock(mtx_);
374 waitTransaction(0);
375
376 err = getError();
377 clearError();
378
379 if (err != "") {
380 throw(rogue::GeneralError::create("Block::checkTransaction",
381 "Transaction error for block %s with address 0x%.8x. Error %s",
382 path_.c_str(),
383 address(),
384 err.c_str()));
385 }
386
387 // Device is disabled
388 if (!enable_) return false;
389
390 // Check verify data if verifyInp is set
391 if (verifyInp_) {
392 bLog_->debug("Verfying data. Base=0x%" PRIx32 ", size=%" PRIu32, verifyBase_, verifySize_);
393 verifyReq_ = false;
394 verifyInp_ = false;
395
396 for (x = verifyBase_; x < verifyBase_ + verifySize_; x++) {
397 if ((verifyData_[x] & verifyMask_[x]) != (expectedData_[x] & verifyMask_[x])) {
398 throw(rogue::GeneralError::create("Block::checkTransaction",
399 "Verify error for block %s with address 0x%.8" PRIx64
400 ". Byte: %" PRIu32 ". Got: 0x%.2" PRIx8 ", Exp: 0x%.2" PRIx8
401 ", Mask: 0x%.2" PRIx8,
402 path_.c_str(),
403 address(),
404 x,
405 verifyData_[x],
406 expectedData_[x],
407 verifyMask_[x]));
408 }
409 }
410 }
411 bLog_->debug("Transaction complete");
412
413 locUpdate = doUpdate_;
414 doUpdate_ = false;
415 }
416 return locUpdate;
417}
418
419#ifndef NO_PYTHON
420
421// Wait for pending transaction completion, check the result, and update variables
422void rim::Block::checkTransactionPy() {
423 if (blockPyTrans_) return;
424
425 if (checkTransaction()) varUpdate();
426}
427
428#endif
429
430// Write/verify/wait-and-check sequence
431void rim::Block::write(rim::Variable* var, int32_t index) {
432 startTransaction(rim::Write, true, false, var, index);
433 startTransaction(rim::Verify, false, false, var, index);
434 checkTransaction();
435}
436
437// Read/wait-and-check sequence
438void rim::Block::read(rim::Variable* var, int32_t index) {
439 startTransaction(rim::Read, false, false, var, index);
440 checkTransaction();
441}
442
443#ifndef NO_PYTHON
444
445// Call variable update for all variables
446void rim::Block::varUpdate() {
447 std::vector<rim::VariablePtr>::iterator vit;
448
450
451 for (vit = variables_.begin(); vit != variables_.end(); ++vit) {
452 if ((*vit)->updateNotify_) (*vit)->queueUpdate();
453 }
454}
455
456#endif
457
458// Add variables to block
459void rim::Block::addVariables(std::vector<rim::VariablePtr> variables) {
460 std::vector<rim::VariablePtr>::iterator vit;
461
462 uint32_t x;
463
464 uint8_t excMask[size_];
465 uint8_t oleMask[size_];
466
467 memset(excMask, 0, size_);
468 memset(oleMask, 0, size_);
469
470 variables_ = variables;
471
472 for (vit = variables_.begin(); vit != variables_.end(); ++vit) {
473 (*vit)->block_ = this;
474
475 if (vit == variables_.begin()) {
476 path_ = (*vit)->path_;
477 std::string logName = "memory.block." + path_;
478 bLog_ = rogue::Logging::create(logName.c_str());
479 mode_ = (*vit)->mode_;
480 }
481
482 if ((*vit)->bulkOpEn_) bulkOpEn_ = true;
483 if ((*vit)->updateNotify_) updateEn_ = true;
484
485 // Retry count
486 if ((*vit)->retryCount_ > retryCount_) retryCount_ = (*vit)->retryCount_;
487
488 // If variable modes mismatch, set block to read/write
489 if (mode_ != (*vit)->mode_) mode_ = "RW";
490
491 // Update variable masks for standard variable
492 if ((*vit)->numValues_ == 0) {
493 for (x = 0; x < (*vit)->bitOffset_.size(); x++) {
494 // Variable allows overlaps, add to overlap enable mask
495 if ((*vit)->overlapEn_) {
496 setBits(oleMask, (*vit)->bitOffset_[x], (*vit)->bitSize_[x]);
497
498 // Otherwise add to exclusive mask and check for existing mapping
499 } else {
500 if (anyBits(excMask, (*vit)->bitOffset_[x], (*vit)->bitSize_[x]))
502 "Block::addVariables",
503 "Variable bit overlap detected for block %s with address 0x%.8x and variable %s",
504 path_.c_str(),
505 address(),
506 (*vit)->name_.c_str()));
507
508 setBits(excMask, (*vit)->bitOffset_[x], (*vit)->bitSize_[x]);
509 }
510
511 // update verify mask
512 if ((*vit)->mode_ == "RW" && (*vit)->verifyEn_) {
513 verifyEn_ = true;
514 setBits(verifyMask_, (*vit)->bitOffset_[x], (*vit)->bitSize_[x]);
515 }
516
517 // update verify block
518 if ((*vit)->mode_ == "RO" || (*vit)->mode_ == "WO" || !(*vit)->verifyEn_) {
519 setBits(verifyBlock_, (*vit)->bitOffset_[x], (*vit)->bitSize_[x]);
520 }
521
522 bLog_->debug(
523 "Adding variable %s to block %s at offset 0x%.8x, bitIdx=%i, bitOffset %i, bitSize %i, mode %s, "
524 "verifyEn "
525 "%d " PRIx64,
526 (*vit)->name_.c_str(),
527 path_.c_str(),
528 offset_,
529 x,
530 (*vit)->bitOffset_[x],
531 (*vit)->bitSize_[x],
532 (*vit)->mode_.c_str(),
533 (*vit)->verifyEn_);
534 }
535
536 // List variables
537 } else {
538 for (x = 0; x < (*vit)->numValues_; x++) {
539 // Variable allows overlaps, add to overlap enable mask
540 if ((*vit)->overlapEn_) {
541 setBits(oleMask, x * (*vit)->valueStride_ + (*vit)->bitOffset_[0], (*vit)->valueBits_);
542
543 // Otherwise add to exclusive mask and check for existing mapping
544 } else {
545 if (anyBits(excMask, x * (*vit)->valueStride_ + (*vit)->bitOffset_[0], (*vit)->valueBits_))
547 "Block::addVariables",
548 "Variable bit overlap detected for block %s with address 0x%.8x and variable %s",
549 path_.c_str(),
550 address(),
551 (*vit)->name_.c_str()));
552
553 setBits(excMask, x * (*vit)->valueStride_ + (*vit)->bitOffset_[0], (*vit)->valueBits_);
554 }
555
556 // update verify mask
557 if ((*vit)->mode_ == "RW" && (*vit)->verifyEn_) {
558 verifyEn_ = true;
559 setBits(verifyMask_, x * (*vit)->valueStride_ + (*vit)->bitOffset_[0], (*vit)->valueBits_);
560 }
561
562 // update verify block
563 if ((*vit)->mode_ == "RO" || (*vit)->mode_ == "WO" || !(*vit)->verifyEn_) {
564 setBits(verifyBlock_, x * (*vit)->valueStride_ + (*vit)->bitOffset_[0], (*vit)->valueBits_);
565 }
566
567 bLog_->debug(
568 "Adding variable %s to block %s at offset 0x%.8x, index=%i, valueOffset=%i, valueBits %i, mode %s, "
569 "verifyEn %d",
570 (*vit)->name_.c_str(),
571 path_.c_str(),
572 offset_,
573 x,
574 x * (*vit)->valueStride_ + (*vit)->bitOffset_[0],
575 (*vit)->valueBits_,
576 (*vit)->mode_.c_str(),
577 (*vit)->verifyEn_);
578 }
579 }
580 }
581
582 // Check for overlaps by anding exclusive and overlap bit vectors
583 for (x = 0; x < size_; x++) {
584 if (oleMask[x] & excMask[x])
585 throw(rogue::GeneralError::create("Block::addVariables",
586 "Variable bit mask overlap detected for block %s with address 0x%.8x",
587 path_.c_str(),
588 address()));
589
590 // Update very mask using verify block
591 verifyMask_[x] &= (verifyBlock_[x] ^ 0xFF);
592 }
593
594 // Execute custom init
595 customInit();
596
597 // Debug the results of the build
598 std::stringstream ss;
599 uint32_t rem = size_;
600 uint32_t idx;
601 idx = 0;
602 x = 0;
603
604 while (rem > 0) {
605 ss << "0x" << std::setfill('0') << std::hex << std::setw(2) << static_cast<uint32_t>(verifyMask_[x]) << " ";
606 x++;
607 rem--;
608 if (rem == 0 || x % 10 == 0) {
609 bLog_->debug("Done adding variables. Verify Mask %.3i - %.3i: %s", idx, x - 1, ss.str().c_str());
610 ss.str("");
611 idx = x;
612 }
613 }
614}
615
616#ifndef NO_PYTHON
617
618// Add variables to block
619void rim::Block::addVariablesPy(bp::object variables) {
620 std::vector<rim::VariablePtr> vars = py_list_to_std_vector<rim::VariablePtr>(variables);
621 addVariables(vars);
622}
623
624#endif
625
627std::vector<rim::VariablePtr> rim::Block::variables() {
628 return variables_;
629}
630
631#ifndef NO_PYTHON
632
634bp::object rim::Block::variablesPy() {
635 return std_vector_to_py_list<rim::VariablePtr>(variables_);
636}
637
638#endif
639
640// byte reverse
641void rim::Block::reverseBytes(uint8_t* data, uint32_t byteSize) {
642 uint32_t x;
643 uint8_t tmp;
644
645 for (x = 0; x < byteSize / 2; x++) {
646 tmp = data[x];
647 data[x] = data[byteSize - x - 1];
648 data[byteSize - x - 1] = tmp;
649 }
650}
651
652// Set data from pointer to internal staged memory
653void rim::Block::setBytes(const uint8_t* data, rim::Variable* var, uint32_t index) {
654 uint32_t srcBit;
655 uint32_t x;
656 uint8_t tmp;
657 uint8_t* buff;
658
659 // Fast path: take mtx_ without dropping the GIL. The body below is pure
660 // in-memory work (memcpy/copyBits/byte-reverse) with no Python calls, so
661 // holding the GIL across it is correct and avoids the release/re-acquire
662 // thrash that dominates bulk update-queue drains (SLAC rogue #1262). Only
663 // when mtx_ is actually contended -- held by a memory-transaction worker
664 // that may need the GIL for a Python callback -- do we drop the GIL while
665 // blocking, preserving the deadlock-avoidance contract.
666 std::unique_lock<std::mutex> lock(mtx_, std::try_to_lock);
667 if (!lock.owns_lock()) {
668 rogue::GilRelease noGil;
669 lock.lock();
670 }
671
672 // Set stale flag
673 if (var->mode_ != "RO") stale_ = true;
674
675 // Change byte order, need to make a copy
676 if (var->byteReverse_) {
677 buff = reinterpret_cast<uint8_t*>(malloc(var->valueBytes_));
678 if (buff == NULL)
679 throw(rogue::GeneralError::create("Block::setBytes",
680 "Failed to allocate %" PRIu32 " bytes for byte-reversed copy of %s",
681 var->valueBytes_,
682 var->name_.c_str()));
683 memcpy(buff, data, var->valueBytes_);
684 reverseBytes(buff, var->valueBytes_);
685 } else {
686 buff = const_cast<uint8_t*>(reinterpret_cast<const uint8_t*>(data));
687 }
688
689 // List variable
690 if (var->numValues_ != 0) {
691 if (index >= var->numValues_) {
692 if (var->byteReverse_) free(buff);
693 throw(rogue::GeneralError::create("Block::setBytes",
694 "Index %" PRIu32 " is out of range for %s",
695 index,
696 var->name_.c_str()));
697 }
698
699 // Fast copy.
700 // Intentionally writes valueBytes_ (not valueStride_/8): pyrogue
701 // RemoteVariable.add() rejects valueStride < valueBits before any
702 // C++ caller is reachable, so a stride-cap here would silently
703 // truncate writes for misconfigured direct-C++ callers and hide
704 // the bug rather than surface it.
705 if (var->fastByte_ != NULL)
706 memcpy(blockData_ + var->fastByte_[index], buff, var->valueBytes_);
707
708 else
709 copyBits(blockData_, var->bitOffset_[0] + (index * var->valueStride_), buff, 0, var->valueBits_);
710
711 if (var->mode_ != "RO") {
712 if (var->stale_) {
713 if (var->lowTranByte_[index] < var->staleLowByte_) var->staleLowByte_ = var->lowTranByte_[index];
714
715 if (var->highTranByte_[index] > var->staleHighByte_) var->staleHighByte_ = var->highTranByte_[index];
716 } else {
717 var->staleLowByte_ = var->lowTranByte_[index];
718 var->staleHighByte_ = var->highTranByte_[index];
719 }
720 }
721
722 // Standard variable
723 } else {
724 if (var->mode_ != "RO") {
725 var->staleLowByte_ = var->lowTranByte_[0];
726 var->staleHighByte_ = var->highTranByte_[0];
727 }
728
729 // Fast copy
730 if (var->fastByte_ != NULL) {
731 memcpy(blockData_ + var->fastByte_[0], buff, var->valueBytes_);
732
733 } else if (var->bitOffset_.size() == 1) {
734 copyBits(blockData_, var->bitOffset_[0], buff, 0, var->bitSize_[0]);
735
736 } else {
737 srcBit = 0;
738 for (x = 0; x < var->bitOffset_.size(); x++) {
739 copyBits(blockData_, var->bitOffset_[x], buff, srcBit, var->bitSize_[x]);
740 srcBit += var->bitSize_[x];
741 }
742 }
743 }
744 if ( var->mode_ != "RO" ) var->stale_ = true;
745 if (var->byteReverse_) free(buff);
746}
747
748// Get data to pointer from internal block or staged memory
749void rim::Block::getBytes(uint8_t* data, rim::Variable* var, uint32_t index) {
750 uint32_t dstBit;
751 uint32_t x;
752
753 // Fast path: take mtx_ without dropping the GIL. See setBytes() for the full
754 // rationale -- the body is pure in-memory work, so we only release the GIL
755 // on the contended-wait path to preserve deadlock avoidance (SLAC rogue
756 // #1262).
757 std::unique_lock<std::mutex> lock(mtx_, std::try_to_lock);
758 if (!lock.owns_lock()) {
759 rogue::GilRelease noGil;
760 lock.lock();
761 }
762
763 // List variable
764 if (var->numValues_ != 0) {
765 if (index >= var->numValues_)
766 throw(rogue::GeneralError::create("Block::getBytes",
767 "Index %" PRIu32 " is out of range for %s",
768 index,
769 var->name_.c_str()));
770
771 // Fast copy. See setBytes() above for why this reads valueBytes_
772 // and not valueStride_/8 -- the read-side mirrors the write-side
773 // because pyrogue rejects valueStride < valueBits upstream.
774 if (var->fastByte_ != NULL)
775 memcpy(data, blockData_ + var->fastByte_[index], var->valueBytes_);
776
777 else
778 copyBits(data, 0, blockData_, var->bitOffset_[0] + (index * var->valueStride_), var->valueBits_);
779
780 } else {
781 // Fast copy
782 if (var->fastByte_ != NULL) {
783 memcpy(data, blockData_ + var->fastByte_[0], var->valueBytes_);
784
785 } else if (var->bitOffset_.size() == 1) {
786 copyBits(data, 0, blockData_, var->bitOffset_[0], var->bitSize_[0]);
787
788 } else {
789 dstBit = 0;
790 for (x = 0; x < var->bitOffset_.size(); x++) {
791 copyBits(data, dstBit, blockData_, var->bitOffset_[x], var->bitSize_[x]);
792 dstBit += var->bitSize_[x];
793 }
794 }
795 }
796
797 // Change byte order
798 if (var->byteReverse_) {
799 reverseBytes(data, var->valueBytes_);
800 }
801}
802
804// Python functions
806
807#ifndef NO_PYTHON
808
809// Set data using python function
810void rim::Block::setPyFunc(bp::object& value, rim::Variable* var, int32_t index) {
811 uint32_t x;
812 Py_buffer valueBuf;
813 bp::object tmp;
814
815 if (index == -1) index = 0;
816
817 // Passed value is a numpy value
818 if (PyArray_Check(value.ptr())) {
819 throw(rogue::GeneralError::create("Block::setPyFunc",
820 "Passing ndarray not supported for %s",
821 var->name_.c_str()));
822
823 // Is passed value a list
824 } else if (PyList_Check(value.ptr())) {
825 bp::list vl = bp::extract<bp::list>(value);
826 uint32_t vlen = len(vl);
827
828 if ((index + vlen) > var->numValues_)
829 throw(rogue::GeneralError::create("Block::setPyFunc",
830 "Overflow error for passed array with length %" PRIu32
831 " at index %" PRIu32 ". Variable length = %" PRIu32 " for %s",
832 vlen,
833 index,
834 var->numValues_,
835 var->name_.c_str()));
836
837 for (x = 0; x < vlen; x++) {
838 tmp = vl[x];
839 bp::object ret = ((rim::VariableWrap*)var)->toBytes(tmp);
840
841 if (PyObject_GetBuffer(ret.ptr(), &(valueBuf), PyBUF_SIMPLE) < 0)
842 throw(rogue::GeneralError::create("Block::setPyFunc",
843 "Failed to extract byte array for %s",
844 var->name_.c_str()));
845
846 setBytes(reinterpret_cast<uint8_t*>(valueBuf.buf), var, index + x);
847 PyBuffer_Release(&valueBuf);
848 }
849
850 // Single value
851 } else {
852 // Call python function
853 bp::object ret = ((rim::VariableWrap*)var)->toBytes(value);
854
855 if (PyObject_GetBuffer(ret.ptr(), &(valueBuf), PyBUF_SIMPLE) < 0)
856 throw(rogue::GeneralError::create("Block::setPyFunc",
857 "Failed to extract byte array from pyFunc return value for %s",
858 var->name_.c_str()));
859
860 setBytes(reinterpret_cast<uint8_t*>(valueBuf.buf), var, index);
861 PyBuffer_Release(&valueBuf);
862 }
863}
864
865// Get data using python function
866bp::object rim::Block::getPyFunc(rim::Variable* var, int32_t index) {
867 uint8_t getBuffer[var->valueBytes_];
868
869 // Unindexed with a list variable not supported
870 if (index < 0 && var->numValues_ > 0) {
871 throw(rogue::GeneralError::create("Block::getPyFunc",
872 "Accessing unindexed value not support for %s",
873 var->name_.c_str()));
874
875 // Single value
876 } else {
877 memset(getBuffer, 0, var->valueBytes_);
878
879 getBytes(getBuffer, var, index);
880 PyObject* val = Py_BuildValue("y#", getBuffer, var->valueBytes_);
881
882 if (val == NULL) throw(rogue::GeneralError::create("Block::getPyFunc", "Failed to generate bytearray"));
883
884 bp::handle<> handle(val);
885 bp::object pass = bp::object(handle);
886
887 return ((rim::VariableWrap*)var)->fromBytes(pass);
888 }
889}
890
891#endif
892
894// Byte Array
896
897#ifndef NO_PYTHON
898
899// Set data using byte array
900void rim::Block::setByteArrayPy(bp::object& value, rim::Variable* var, int32_t index) {
901 Py_buffer valueBuf;
902
903 // Unindexed with a list variable
904 if (index < 0 && var->numValues_ > 0)
905 throw(rogue::GeneralError::create("Block::setByteArrayPy",
906 "Accessing unindexed value not supported for %s",
907 var->name_.c_str()));
908
909 if (PyObject_GetBuffer(value.ptr(), &(valueBuf), PyBUF_SIMPLE) < 0)
910 throw(rogue::GeneralError::create("Block::setByteArray",
911 "Failed to extract byte array for %s",
912 var->name_.c_str()));
913
914 setBytes(reinterpret_cast<uint8_t*>(valueBuf.buf), var, index);
915 PyBuffer_Release(&valueBuf);
916}
917
918// Get data using byte array
919bp::object rim::Block::getByteArrayPy(rim::Variable* var, int32_t index) {
920 uint8_t getBuffer[var->valueBytes_];
921
922 // Unindexed with a list variable
923 if (index < 0 && var->numValues_ > 0)
924 throw(rogue::GeneralError::create("Block::setByteArrayPy",
925 "Accessing unindexed value not supported for %s",
926 var->name_.c_str()));
927
928 memset(getBuffer, 0, var->valueBytes_);
929
930 getBytes(getBuffer, var, index);
931 PyObject* val = Py_BuildValue("y#", getBuffer, var->valueBytes_);
932
933 if (val == NULL) throw(rogue::GeneralError::create("Block::setByteArrayPy", "Failed to generate bytearray"));
934
935 bp::handle<> handle(val);
936 return bp::object(handle);
937}
938
939#endif
940
941// Set data using byte array
942void rim::Block::setByteArray(const uint8_t* value, rim::Variable* var, int32_t index) {
943 setBytes(value, var, index);
944}
945
946// Get data using byte array
947void rim::Block::getByteArray(uint8_t* value, rim::Variable* var, int32_t index) {
948 getBytes(value, var, index);
949}
950
952// Unsigned Int
954
955#ifndef NO_PYTHON
956
957// Set data using unsigned int
958void rim::Block::setUIntPy(bp::object& value, rim::Variable* var, int32_t index) {
959 if (index == -1) index = 0;
960
961 // Lambda to process an array of unsigned values.
962 auto process_uint_array = [&](auto* src, npy_intp stride, npy_intp length) {
963 for (npy_intp i = 0; i < length; ++i) {
964 setUInt(src[i * stride], var, index + i);
965 }
966 };
967
968 // Passed value is a numpy value
969 if (PyArray_Check(value.ptr())) {
970 // Cast to an array object and check that the numpy array
971 PyArrayObject* arr = reinterpret_cast<PyArrayObject*>(value.ptr());
972 npy_intp ndims = PyArray_NDIM(arr);
973 npy_intp* dims = PyArray_SHAPE(arr);
974 npy_intp* strides = PyArray_STRIDES(arr);
975 if (ndims != 1)
976 throw(rogue::GeneralError::create("Block::setUIntPy",
977 "Invalid number of dimensions (%" PRIu32 ") for passed ndarray for %s",
978 ndims,
979 var->name_.c_str()));
980
981 if ((index + dims[0]) > var->numValues_)
982 throw(rogue::GeneralError::create("Block::setUIntPy",
983 "Overflow error for passed array with length %" PRIu32
984 " at index %" PRIu32 ". Variable length = %" PRIu32 " for %s",
985 dims[0],
986 index,
987 var->numValues_,
988 var->name_.c_str()));
989
990 int type = PyArray_TYPE(arr);
991 switch (type) {
992 case NPY_UINT64: {
993 uint64_t* src = reinterpret_cast<uint64_t*>(PyArray_DATA(arr));
994 npy_intp stride = strides[0] / sizeof(uint64_t);
995 process_uint_array(src, stride, dims[0]);
996 break;
997 }
998 case NPY_UINT32: {
999 uint32_t* src = reinterpret_cast<uint32_t*>(PyArray_DATA(arr));
1000 npy_intp stride = strides[0] / sizeof(uint32_t);
1001 process_uint_array(src, stride, dims[0]);
1002 break;
1003 }
1004 case NPY_UINT16: {
1005 uint16_t* src = reinterpret_cast<uint16_t*>(PyArray_DATA(arr));
1006 npy_intp stride = strides[0] / sizeof(uint16_t);
1007 process_uint_array(src, stride, dims[0]);
1008 break;
1009 }
1010 case NPY_UINT8: {
1011 uint8_t* src = reinterpret_cast<uint8_t*>(PyArray_DATA(arr));
1012 npy_intp stride = strides[0] / sizeof(uint8_t);
1013 process_uint_array(src, stride, dims[0]);
1014 break;
1015 }
1016 default:
1017 throw(rogue::GeneralError::create("Block::setUIntPy",
1018 "Passed nparray is not of an accepted unsigned int type (uint64, uint32, uint16, uint8) for %s",
1019 var->name_.c_str()));
1020 }
1021
1022 // Is passed value a list
1023 } else if (PyList_Check(value.ptr())) {
1024 bp::list vl = bp::extract<bp::list>(value);
1025 uint32_t vlen = len(vl);
1026 if ((index + vlen) > var->numValues_)
1027 throw(rogue::GeneralError::create("Block::setUIntPy",
1028 "Overflow error for passed list with length %" PRIu32
1029 " at index %" PRIi32 ". Variable length = %" PRIu32 " for %s",
1030 vlen,
1031 index,
1032 var->numValues_,
1033 var->name_.c_str()));
1034 for (uint32_t i = 0; i < vlen; i++) {
1035 bp::extract<uint64_t> tmp(vl[i]);
1036 if (!tmp.check())
1037 throw(rogue::GeneralError::create("Block::setUIntPy",
1038 "Failed to extract value for %s.",
1039 var->name_.c_str()));
1040 setUInt(tmp, var, index + i);
1041 }
1042
1043 // Passed scalar numpy value
1044 } else if (PyArray_CheckScalar(value.ptr())) {
1045 int type_num = PyArray_DescrFromScalar(value.ptr())->type_num;
1046 switch (type_num) {
1047 case NPY_UINT64: {
1048 uint64_t val;
1049 PyArray_ScalarAsCtype(value.ptr(), &val);
1050 setUInt(val, var, index);
1051 break;
1052 }
1053 case NPY_UINT32: {
1054 uint32_t val;
1055 PyArray_ScalarAsCtype(value.ptr(), &val);
1056 setUInt(val, var, index);
1057 break;
1058 }
1059 case NPY_UINT16: {
1060 uint16_t val;
1061 PyArray_ScalarAsCtype(value.ptr(), &val);
1062 setUInt(val, var, index);
1063 break;
1064 }
1065 case NPY_UINT8: {
1066 uint8_t val;
1067 PyArray_ScalarAsCtype(value.ptr(), &val);
1068 setUInt(val, var, index);
1069 break;
1070 }
1071 default:
1072 throw(rogue::GeneralError::create("Block::setUIntPy",
1073 "Failed to extract scalar unsigned int value for %s.",
1074 var->name_.c_str()));
1075 }
1076 } else {
1077 bp::extract<uint64_t> tmp(value);
1078 if (!tmp.check())
1079 throw(rogue::GeneralError::create("Block::setUIntPy", "Failed to extract value for %s.", var->name_.c_str()));
1080 setUInt(tmp, var, index);
1081 }
1082}
1083
1084// Get data using unsigned int
1085bp::object rim::Block::getUIntPy(rim::Variable* var, int32_t index) {
1086 bp::object ret;
1087
1088 // Unindexed with a list variable
1089 if (index < 0 && var->numValues_ > 0) {
1090 // Create a numpy array to receive it and locate the destination data buffer
1091 npy_intp dims[1] = {var->numValues_};
1092 int npType;
1093 // Choose numpy type based on the variable's valueBits.
1094 if (var->valueBits_ <= 8) {
1095 npType = NPY_UINT8;
1096 } else if (var->valueBits_ <= 16) {
1097 npType = NPY_UINT16;
1098 } else if (var->valueBits_ <= 32) {
1099 npType = NPY_UINT32;
1100 } else {
1101 npType = NPY_UINT64;
1102 }
1103
1104 PyObject* obj = PyArray_SimpleNew(1, dims, npType);
1105 PyArrayObject* arr = reinterpret_cast<PyArrayObject*>(obj);
1106 uint32_t x;
1107 switch (npType) {
1108 case NPY_UINT8: {
1109 uint8_t* dst = reinterpret_cast<uint8_t*>(PyArray_DATA(arr));
1110 for (x = 0; x < var->numValues_; x++) {
1111 dst[x] = static_cast<uint8_t>(getUInt(var, x));
1112 }
1113 break;
1114 }
1115 case NPY_UINT16: {
1116 uint16_t* dst = reinterpret_cast<uint16_t*>(PyArray_DATA(arr));
1117 for (x = 0; x < var->numValues_; x++) {
1118 dst[x] = static_cast<uint16_t>(getUInt(var, x));
1119 }
1120 break;
1121 }
1122 case NPY_UINT32: {
1123 uint32_t* dst = reinterpret_cast<uint32_t*>(PyArray_DATA(arr));
1124 for (x = 0; x < var->numValues_; x++) {
1125 dst[x] = static_cast<uint32_t>(getUInt(var, x));
1126 }
1127 break;
1128 }
1129 case NPY_UINT64: {
1130 uint64_t* dst = reinterpret_cast<uint64_t*>(PyArray_DATA(arr));
1131 for (x = 0; x < var->numValues_; x++) {
1132 dst[x] = getUInt(var, x);
1133 }
1134 break;
1135 }
1136 }
1137 boost::python::handle<> handle(obj);
1138 ret = bp::object(handle);
1139 } else {
1140 PyObject* val = Py_BuildValue("K", getUInt(var, index));
1141 if (val == NULL)
1142 throw(rogue::GeneralError::create("Block::getUIntPy", "Failed to generate UInt"));
1143 bp::handle<> handle(val);
1144 ret = bp::object(handle);
1145 }
1146 return ret;
1147}
1148
1149#endif
1150
1151// Set data using unsigned int
1152void rim::Block::setUInt(const uint64_t& val, rim::Variable* var, int32_t index) {
1153 // Check range
1154 if ((var->minValue_ != 0 || var->maxValue_ != 0) && (val > var->maxValue_ || val < var->minValue_))
1155 throw(rogue::GeneralError::create("Block::setUInt",
1156 "Value range error for %s. Value=%" PRIu64 ", Min=%f, Max=%f",
1157 var->name_.c_str(),
1158 val,
1159 var->minValue_,
1160 var->maxValue_));
1161
1162 setBytes(reinterpret_cast<uint8_t*>(const_cast<uint64_t*>(&val)), var, index);
1163}
1164
1165// Get data using unsigned int
1166uint64_t rim::Block::getUInt(rim::Variable* var, int32_t index) {
1167 uint64_t tmp = 0;
1168
1169 getBytes(reinterpret_cast<uint8_t*>(&tmp), var, index);
1170
1171 return tmp;
1172}
1173
1175// Signed Int
1177
1178#ifndef NO_PYTHON
1179
1180// Set data using int
1181void rim::Block::setIntPy(bp::object& value, rim::Variable* var, int32_t index) {
1182 if (index == -1) index = 0;
1183
1184 // Lambda to process an array of signed values.
1185 auto process_int_array = [&](auto* src, npy_intp stride, npy_intp length) {
1186 for (npy_intp i = 0; i < length; ++i) {
1187 setInt(src[i * stride], var, index + i);
1188 }
1189 };
1190
1191 // Passed value is a numpy value
1192 if (PyArray_Check(value.ptr())) {
1193 // Cast to an array object and check that the numpy array
1194 PyArrayObject* arr = reinterpret_cast<PyArrayObject*>(value.ptr());
1195 npy_intp ndims = PyArray_NDIM(arr);
1196 npy_intp* dims = PyArray_SHAPE(arr);
1197 npy_intp* strides = PyArray_STRIDES(arr);
1198 if (ndims != 1)
1199 throw(rogue::GeneralError::create("Block::setIntPy",
1200 "Invalid number of dimensions (%" PRIu32 ") for passed ndarray for %s",
1201 ndims,
1202 var->name_.c_str()));
1203
1204 if ((index + dims[0]) > var->numValues_)
1205 throw(rogue::GeneralError::create("Block::setIntPy",
1206 "Overflow error for passed array with length %" PRIu32
1207 " at index %" PRIi32 ". Variable length = %" PRIu32 " for %s",
1208 dims[0],
1209 index,
1210 var->numValues_,
1211 var->name_.c_str()));
1212
1213 int type = PyArray_TYPE(arr);
1214 switch (type) {
1215 case NPY_INT64: {
1216 int64_t* src = reinterpret_cast<int64_t*>(PyArray_DATA(arr));
1217 npy_intp stride = strides[0] / sizeof(int64_t);
1218 process_int_array(src, stride, dims[0]);
1219 break;
1220 }
1221 case NPY_INT32: {
1222 int32_t* src = reinterpret_cast<int32_t*>(PyArray_DATA(arr));
1223 npy_intp stride = strides[0] / sizeof(int32_t);
1224 process_int_array(src, stride, dims[0]);
1225 break;
1226 }
1227 case NPY_INT16: {
1228 int16_t* src = reinterpret_cast<int16_t*>(PyArray_DATA(arr));
1229 npy_intp stride = strides[0] / sizeof(int16_t);
1230 process_int_array(src, stride, dims[0]);
1231 break;
1232 }
1233 case NPY_INT8: {
1234 int8_t* src = reinterpret_cast<int8_t*>(PyArray_DATA(arr));
1235 npy_intp stride = strides[0] / sizeof(int8_t);
1236 process_int_array(src, stride, dims[0]);
1237 break;
1238 }
1239 default:
1240 throw(rogue::GeneralError::create("Block::setIntPy",
1241 "Passed nparray is not of an accepted signed int type (int64, int32, int16, int8) for %s",
1242 var->name_.c_str()));
1243 }
1244
1245 // Is passed value a list
1246 } else if (PyList_Check(value.ptr())) {
1247 bp::list vl = bp::extract<bp::list>(value);
1248 uint32_t vlen = len(vl);
1249
1250 if ((index + vlen) > var->numValues_)
1251 throw(rogue::GeneralError::create("Block::setIntPy",
1252 "Overflow error for passed list with length %" PRIu32
1253 " at index %" PRIi32 ". Variable length = %" PRIu32 " for %s",
1254 vlen,
1255 index,
1256 var->numValues_,
1257 var->name_.c_str()));
1258 for (uint32_t i = 0; i < vlen; i++) {
1259 bp::extract<int64_t> tmp(vl[i]);
1260 if (!tmp.check())
1261 throw(rogue::GeneralError::create("Block::setIntPy",
1262 "Failed to extract value for %s.",
1263 var->name_.c_str()));
1264 setInt(tmp, var, index + i);
1265 }
1266
1267 // Passed scalar numpy value
1268 } else if (PyArray_CheckScalar(value.ptr())) {
1269 int type_num = PyArray_DescrFromScalar(value.ptr())->type_num;
1270 switch (type_num) {
1271 case NPY_INT64: {
1272 int64_t val;
1273 PyArray_ScalarAsCtype(value.ptr(), &val);
1274 setInt(val, var, index);
1275 break;
1276 }
1277 case NPY_INT32: {
1278 int32_t val;
1279 PyArray_ScalarAsCtype(value.ptr(), &val);
1280 setInt(val, var, index);
1281 break;
1282 }
1283 case NPY_INT16: {
1284 int16_t val;
1285 PyArray_ScalarAsCtype(value.ptr(), &val);
1286 setInt(val, var, index);
1287 break;
1288 }
1289 case NPY_INT8: {
1290 int8_t val;
1291 PyArray_ScalarAsCtype(value.ptr(), &val);
1292 setInt(val, var, index);
1293 break;
1294 }
1295 default:
1296 throw(rogue::GeneralError::create("Block::setIntPy",
1297 "Failed to extract scalar signed int value for %s.",
1298 var->name_.c_str()));
1299 }
1300 } else {
1301 bp::extract<int64_t> tmp(value);
1302
1303 if (!tmp.check())
1304 throw(rogue::GeneralError::create("Block::setIntPy", "Failed to extract value for %s.", var->name_.c_str()));
1305 setInt(tmp, var, index);
1306 }
1307}
1308
1309// Get data using int
1310bp::object rim::Block::getIntPy(rim::Variable* var, int32_t index) {
1311 bp::object ret;
1312
1313 // Unindexed with a list variable
1314 if (index < 0 && var->numValues_ > 0) {
1315 // Create a numpy array to receive it and locate the destination data buffer
1316 npy_intp dims[1] = {var->numValues_};
1317 int npType;
1318 if (var->valueBits_ <= 8) {
1319 npType = NPY_INT8;
1320 } else if (var->valueBits_ <= 16) {
1321 npType = NPY_INT16;
1322 } else if (var->valueBits_ <= 32) {
1323 npType = NPY_INT32;
1324 } else {
1325 npType = NPY_INT64;
1326 }
1327 PyObject* obj = PyArray_SimpleNew(1, dims, npType);
1328 PyArrayObject* arr = reinterpret_cast<PyArrayObject*>(obj);
1329 uint32_t x;
1330 switch (npType) {
1331 case NPY_INT8: {
1332 int8_t* dst = reinterpret_cast<int8_t*>(PyArray_DATA(arr));
1333 for (x = 0; x < var->numValues_; x++) {
1334 dst[x] = static_cast<int8_t>(getInt(var, x));
1335 }
1336 break;
1337 }
1338 case NPY_INT16: {
1339 int16_t* dst = reinterpret_cast<int16_t*>(PyArray_DATA(arr));
1340 for (x = 0; x < var->numValues_; x++) {
1341 dst[x] = static_cast<int16_t>(getInt(var, x));
1342 }
1343 break;
1344 }
1345 case NPY_INT32: {
1346 int32_t* dst = reinterpret_cast<int32_t*>(PyArray_DATA(arr));
1347 for (x = 0; x < var->numValues_; x++) {
1348 dst[x] = static_cast<int32_t>(getInt(var, x));
1349 }
1350 break;
1351 }
1352 case NPY_INT64: {
1353 int64_t* dst = reinterpret_cast<int64_t*>(PyArray_DATA(arr));
1354 for (x = 0; x < var->numValues_; x++) {
1355 dst[x] = getInt(var, x);
1356 }
1357 break;
1358 }
1359 }
1360 boost::python::handle<> handle(obj);
1361 ret = bp::object(handle);
1362 } else {
1363 PyObject* val = Py_BuildValue("L", getInt(var, index));
1364 if (val == NULL)
1365 throw(rogue::GeneralError::create("Block::getIntPy", "Failed to generate Int"));
1366 bp::handle<> handle(val);
1367 ret = bp::object(handle);
1368 }
1369 return ret;
1370}
1371
1372#endif
1373
1374// Set data using int
1375void rim::Block::setInt(const int64_t& val, rim::Variable* var, int32_t index) {
1376 // Check range
1377 if ((var->minValue_ != 0 || var->maxValue_ != 0) && (val > var->maxValue_ || val < var->minValue_))
1378 throw(rogue::GeneralError::create("Block::setInt",
1379 "Value range error for %s. Value=%" PRId64 ", Min=%f, Max=%f",
1380 var->name_.c_str(),
1381 val,
1382 var->minValue_,
1383 var->maxValue_));
1384
1385 // This works because all bits between the msb and bit 64 are set to '1' for a negative value
1386 setBytes(reinterpret_cast<uint8_t*>(const_cast<int64_t*>(&val)), var, index);
1387}
1388
1389// Get data using int
1390int64_t rim::Block::getInt(rim::Variable* var, int32_t index) {
1391 int64_t tmp = 0;
1392
1393 getBytes(reinterpret_cast<uint8_t*>(&tmp), var, index);
1394
1395 if (var->valueBits_ != 64) {
1396 if (tmp >= static_cast<uint64_t>(pow(2, var->valueBits_ - 1))) {
1397 tmp -= static_cast<uint64_t>(pow(2, var->valueBits_));
1398 }
1399 }
1400 return tmp;
1401}
1402
1404// Bool
1406
1407#ifndef NO_PYTHON
1408
1409// Set data using bool
1410void rim::Block::setBoolPy(bp::object& value, rim::Variable* var, int32_t index) {
1411 uint32_t x;
1412
1413 if (index == -1) index = 0;
1414
1415 // Passed value is a numpy value
1416 if (PyArray_Check(value.ptr())) {
1417 // Cast to an array object and check that the numpy array
1418 PyArrayObject* arr = reinterpret_cast<decltype(arr)>(value.ptr());
1419 npy_intp ndims = PyArray_NDIM(arr);
1420 npy_intp* dims = PyArray_SHAPE(arr);
1421 npy_intp* strides = PyArray_STRIDES(arr);
1422
1423 if (ndims != 1)
1424 throw(rogue::GeneralError::create("Block::setBoolPy",
1425 "Invalid number of dimensions (%" PRIu32 ") for passed ndarray for %s",
1426 ndims,
1427 var->name_.c_str()));
1428
1429 if ((index + dims[0]) > var->numValues_)
1430 throw(rogue::GeneralError::create("Block::setBoolPy",
1431 "Overflow error for passed array with length %" PRIu32
1432 " at index %" PRIi32 ". Variable length = %" PRIu32 " for %s",
1433 dims[0],
1434 index,
1435 var->numValues_,
1436 var->name_.c_str()));
1437
1438 if (PyArray_TYPE(arr) == NPY_BOOL) {
1439 bool* src = reinterpret_cast<bool*>(PyArray_DATA(arr));
1440 npy_intp stride = strides[0] / sizeof(bool);
1441 for (x = 0; x < dims[0]; x++) {
1442 setBool(src[x * stride], var, index + x);
1443 }
1444 } else {
1445 throw(rogue::GeneralError::create("Block::setBoolPy",
1446 "Passed nparray is not of type (bool) for %s",
1447 var->name_.c_str()));
1448 }
1449
1450 // Is passed value a list
1451 } else if (PyList_Check(value.ptr())) {
1452 bp::list vl = bp::extract<bp::list>(value);
1453 uint32_t vlen = len(vl);
1454
1455 if ((index + vlen) > var->numValues_)
1456 throw(rogue::GeneralError::create("Block::setBoolPy",
1457 "Overflow error for passed array with length %" PRIu32
1458 " at index %" PRIi32 ". Variable length = %" PRIu32 " for %s",
1459 vlen,
1460 index,
1461 var->numValues_,
1462 var->name_.c_str()));
1463
1464 for (x = 0; x < vlen; x++) {
1465 bp::extract<bool> tmp(vl[x]);
1466
1467 if (!tmp.check())
1468 throw(rogue::GeneralError::create("Block::setBoolPy",
1469 "Failed to extract value for %s.",
1470 var->name_.c_str()));
1471
1472 setBool(tmp, var, index + x);
1473 }
1474
1475 // Passed scalar numpy value
1476 } else if (PyArray_CheckScalar(value.ptr())) {
1477 if (PyArray_DescrFromScalar(value.ptr())->type_num == NPY_BOOL) {
1478 bool val;
1479 PyArray_ScalarAsCtype(value.ptr(), &val);
1480 setBool(val, var, index);
1481 } else {
1482 throw(
1483 rogue::GeneralError::create("Block::setBoolPy", "Failed to extract value for %s.", var->name_.c_str()));
1484 }
1485 } else {
1486 bp::extract<bool> tmp(value);
1487
1488 if (!tmp.check())
1489 throw(
1490 rogue::GeneralError::create("Block::setBoolPy", "Failed to extract value for %s.", var->name_.c_str()));
1491
1492 setBool(tmp, var, index);
1493 }
1494}
1495
1496// Get data using bool
1497bp::object rim::Block::getBoolPy(rim::Variable* var, int32_t index) {
1498 bp::object ret;
1499 uint32_t x;
1500
1501 // Unindexed with a list variable
1502 if (index < 0 && var->numValues_ > 0) {
1503 // Create a numpy array to receive it and locate the destination data buffer
1504 npy_intp dims[1] = {var->numValues_};
1505 PyObject* obj = PyArray_SimpleNew(1, dims, NPY_BOOL);
1506 PyArrayObject* arr = reinterpret_cast<PyArrayObject*>(obj);
1507 bool* dst = reinterpret_cast<bool*>(PyArray_DATA(arr));
1508
1509 for (x = 0; x < var->numValues_; x++) dst[x] = getBool(var, x);
1510
1511 boost::python::handle<> handle(obj);
1512 ret = bp::object(handle);
1513
1514 } else {
1515 bp::handle<> handle(bp::borrowed(getBool(var, index) ? Py_True : Py_False));
1516 ret = bp::object(handle);
1517 }
1518 return ret;
1519}
1520
1521#endif
1522
1523// Set data using bool
1524void rim::Block::setBool(const bool& value, rim::Variable* var, int32_t index) {
1525 uint8_t val = (uint8_t)value;
1526 setBytes(reinterpret_cast<uint8_t*>(&val), var, index);
1527}
1528
1529// Get data using bool
1530bool rim::Block::getBool(rim::Variable* var, int32_t index) {
1531 uint8_t tmp = 0;
1532
1533 getBytes(reinterpret_cast<uint8_t*>(&tmp), var, index);
1534
1535 return tmp ? true : false;
1536}
1537
1539// String
1541
1542#ifndef NO_PYTHON
1543
1544// Set data using string
1545void rim::Block::setStringPy(bp::object& value, rim::Variable* var, int32_t index) {
1546 uint32_t x;
1547 std::string strVal;
1548
1549 // Unindexed with a list variable
1550 if (index < 0 && var->numValues_ > 0)
1551 throw(rogue::GeneralError::create("Block::setStringPy",
1552 "Using nparray not supported for %s",
1553 var->name_.c_str()));
1554
1555 bp::extract<char*> tmp(value);
1556
1557 if (!tmp.check())
1558 throw(rogue::GeneralError::create("Block::setStringPy", "Failed to extract value for %s.", var->name_.c_str()));
1559
1560 strVal = tmp;
1561 setString(strVal, var, index);
1562}
1563
1564// Get data using string
1565bp::object rim::Block::getStringPy(rim::Variable* var, int32_t index) {
1566 bp::object ret;
1567 std::string strVal;
1568 uint32_t x;
1569
1570 // Unindexed with a list variable
1571 if (index < 0 && var->numValues_ > 0)
1572 throw(
1573 rogue::GeneralError::create("Block::getStringPy", "Using ndarry not supported for %s", var->name_.c_str()));
1574
1575 getString(var, strVal, index);
1576 PyObject* val = Py_BuildValue("s", strVal.c_str());
1577
1578 if (val == NULL) throw(rogue::GeneralError::create("Block::getStringPy", "Failed to generate String"));
1579
1580 bp::handle<> handle(val);
1581 return bp::object(handle);
1582}
1583
1584#endif
1585
1586// Set data using string
1587void rim::Block::setString(const std::string& value, rim::Variable* var, int32_t index) {
1588 uint8_t getBuffer[var->valueBytes_];
1589
1590 memset(getBuffer, 0, var->valueBytes_);
1591
1592 strncpy(reinterpret_cast<char*>(getBuffer), value.c_str(), var->valueBytes_ - 1);
1593
1594 setBytes(getBuffer, var, index);
1595}
1596
1597// Get data using string
1598std::string rim::Block::getString(rim::Variable* var, int32_t index) {
1599 std::string ret;
1600 getString(var, ret, index);
1601 return ret;
1602}
1603
1604// Get data into string
1605void rim::Block::getString(rim::Variable* var, std::string& retString, int32_t index) {
1606 char getBuffer[var->valueBytes_ + 1];
1607
1608 memset(getBuffer, 0, var->valueBytes_ + 1);
1609
1610 getBytes(reinterpret_cast<uint8_t*>(getBuffer), var, index);
1611
1612 retString = getBuffer;
1613}
1614
1616// Float
1618
1619#ifndef NO_PYTHON
1620
1621// Set data using float
1622void rim::Block::setFloatPy(bp::object& value, rim::Variable* var, int32_t index) {
1623 uint32_t x;
1624
1625 if (index == -1) index = 0;
1626
1627 // Passed value is a numpy value
1628 if (PyArray_Check(value.ptr())) {
1629 // Cast to an array object and check that the numpy array
1630 PyArrayObject* arr = reinterpret_cast<decltype(arr)>(value.ptr());
1631 npy_intp ndims = PyArray_NDIM(arr);
1632 npy_intp* dims = PyArray_SHAPE(arr);
1633 npy_intp* strides = PyArray_STRIDES(arr);
1634
1635 if (ndims != 1)
1636 throw(rogue::GeneralError::create("Block::setFloatPy",
1637 "Invalid number of dimensions (%" PRIu32 ") for passed ndarray for %s",
1638 ndims,
1639 var->name_.c_str()));
1640
1641 if ((index + dims[0]) > var->numValues_)
1642 throw(rogue::GeneralError::create("Block::setFloatPy",
1643 "Overflow error for passed array with length %" PRIu32
1644 " at index %" PRIi32 ". Variable length = %" PRIu32 " for %s",
1645 dims[0],
1646 index,
1647 var->numValues_,
1648 var->name_.c_str()));
1649
1650 if (PyArray_TYPE(arr) == NPY_FLOAT32) {
1651 float* src = reinterpret_cast<float*>(PyArray_DATA(arr));
1652 npy_intp stride = strides[0] / sizeof(float);
1653 for (x = 0; x < dims[0]; x++) {
1654 setFloat(src[x * stride], var, index + x);
1655 }
1656 } else {
1657 throw(rogue::GeneralError::create("Block::setFLoatPy",
1658 "Passed nparray is not of type (float32) for %s",
1659 var->name_.c_str()));
1660 }
1661
1662 // Is passed value a list
1663 } else if (PyList_Check(value.ptr())) {
1664 bp::list vl = bp::extract<bp::list>(value);
1665 uint32_t vlen = len(vl);
1666
1667 if ((index + vlen) > var->numValues_)
1668 throw(rogue::GeneralError::create("Block::setFloatPy",
1669 "Overflow error for passed array with length %" PRIu32
1670 " at index %" PRIi32 ". Variable length = %" PRIu32 " for %s",
1671 vlen,
1672 index,
1673 var->numValues_,
1674 var->name_.c_str()));
1675
1676 for (x = 0; x < vlen; x++) {
1677 bp::extract<float> tmp(vl[x]);
1678
1679 if (!tmp.check())
1680 throw(rogue::GeneralError::create("Block::setFloatPy",
1681 "Failed to extract value for %s.",
1682 var->name_.c_str()));
1683
1684 setFloat(tmp, var, index + x);
1685 }
1686
1687 // Passed scalar numpy value
1688 } else if (PyArray_CheckScalar(value.ptr())) {
1689 if (PyArray_DescrFromScalar(value.ptr())->type_num == NPY_FLOAT32) {
1690 float val;
1691 PyArray_ScalarAsCtype(value.ptr(), &val);
1692 setFloat(val, var, index);
1693 } else {
1694 throw(rogue::GeneralError::create("Block::setFloatPy",
1695 "Failed to extract value for %s.",
1696 var->name_.c_str()));
1697 }
1698 } else {
1699 bp::extract<float> tmp(value);
1700
1701 if (!tmp.check())
1702 throw(rogue::GeneralError::create("Block::setFloatPy",
1703 "Failed to extract value for %s.",
1704 var->name_.c_str()));
1705
1706 setFloat(tmp, var, index);
1707 }
1708}
1709
1710// Get data using float
1711bp::object rim::Block::getFloatPy(rim::Variable* var, int32_t index) {
1712 bp::object ret;
1713 uint32_t x;
1714
1715 // Unindexed with a list variable
1716 if (index < 0 && var->numValues_ > 0) {
1717 // Create a numpy array to receive it and locate the destination data buffer
1718 npy_intp dims[1] = {var->numValues_};
1719 PyObject* obj = PyArray_SimpleNew(1, dims, NPY_FLOAT32);
1720 PyArrayObject* arr = reinterpret_cast<PyArrayObject*>(obj);
1721 float* dst = reinterpret_cast<float*>(PyArray_DATA(arr));
1722
1723 for (x = 0; x < var->numValues_; x++) dst[x] = getFloat(var, x);
1724
1725 boost::python::handle<> handle(obj);
1726 ret = bp::object(handle);
1727
1728 } else {
1729 PyObject* val = Py_BuildValue("f", getFloat(var, index));
1730
1731 if (val == NULL) throw(rogue::GeneralError::create("Block::getFloatPy", "Failed to generate Float"));
1732
1733 bp::handle<> handle(val);
1734 ret = bp::object(handle);
1735 }
1736 return ret;
1737}
1738
1739#endif
1740
1741// Set data using float
1742void rim::Block::setFloat(const float& val, rim::Variable* var, int32_t index) {
1743 // Check range
1744 if ((var->minValue_ != 0 || var->maxValue_ != 0) && (val > var->maxValue_ || val < var->minValue_))
1745 throw(rogue::GeneralError::create("Block::setFloat",
1746 "Value range error for %s. Value=%f, Min=%f, Max=%f",
1747 var->name_.c_str(),
1748 val,
1749 var->minValue_,
1750 var->maxValue_));
1751
1752 setBytes(reinterpret_cast<uint8_t*>(const_cast<float*>(&val)), var, index);
1753}
1754
1755// Get data using float
1756float rim::Block::getFloat(rim::Variable* var, int32_t index) {
1757 float tmp = 0;
1758
1759 getBytes(reinterpret_cast<uint8_t*>(&tmp), var, index);
1760
1761 return tmp;
1762}
1763
1764// IEEE 754 half-precision conversion helpers
1765namespace {
1766
1767uint16_t floatToHalf(float value) {
1768 uint32_t f;
1769 std::memcpy(&f, &value, sizeof(f));
1770
1771 uint16_t sign = (f >> 16) & 0x8000;
1772 int32_t exponent = ((f >> 23) & 0xFF) - 127 + 15;
1773 uint32_t mantissa = f & 0x7FFFFF;
1774
1775 if (exponent <= 0) {
1776 if (exponent < -10) return sign;
1777 mantissa |= 0x800000;
1778 uint32_t shift = 14 - exponent;
1779 uint32_t halfMant = mantissa >> shift;
1780 return sign | static_cast<uint16_t>(halfMant);
1781 } else if (exponent == 0xFF - 127 + 15) {
1782 if (mantissa) {
1783 uint16_t halfMantissa = static_cast<uint16_t>(mantissa >> 13);
1784 if (halfMantissa == 0) halfMantissa = 0x0001;
1785 return sign | 0x7C00 | halfMantissa;
1786 }
1787 return sign | 0x7C00;
1788 } else if (exponent > 30) {
1789 return sign | 0x7C00;
1790 }
1791 return sign | (exponent << 10) | (mantissa >> 13);
1792}
1793
1794float halfToFloat(uint16_t h) {
1795 uint32_t sign = (h & 0x8000) << 16;
1796 uint32_t exponent = (h >> 10) & 0x1F;
1797 uint32_t mantissa = h & 0x3FF;
1798
1799 uint32_t f;
1800 if (exponent == 0) {
1801 if (mantissa == 0) {
1802 f = sign;
1803 } else {
1804 int32_t exponentWork = 1;
1805 while (!(mantissa & 0x400)) {
1806 mantissa <<= 1;
1807 exponentWork--;
1808 }
1809 mantissa &= 0x3FF;
1810 f = sign | (static_cast<uint32_t>(exponentWork + 127 - 15) << 23) | (mantissa << 13);
1811 }
1812 } else if (exponent == 31) {
1813 f = sign | 0x7F800000 | (mantissa << 13);
1814 } else {
1815 f = sign | ((exponent + 127 - 15) << 23) | (mantissa << 13);
1816 }
1817
1818 float result;
1819 std::memcpy(&result, &f, sizeof(result));
1820 return result;
1821}
1822
1823uint8_t floatToFloat8(float value) {
1824 uint32_t f;
1825 std::memcpy(&f, &value, sizeof(f));
1826
1827 uint8_t sign = (f >> 24) & 0x80;
1828 int32_t exponent = ((f >> 23) & 0xFF) - 127 + 7; // rebias float32 to E4M3 bias=7
1829 uint32_t mantissa = f & 0x7FFFFF;
1830
1831 // NaN input -> NaN output (0x7F)
1832 if (((f >> 23) & 0xFF) == 0xFF && mantissa != 0) return 0x7F;
1833
1834 // Infinity or overflow -> max finite (0x7E positive, 0xFE negative)
1835 if (((f >> 23) & 0xFF) == 0xFF || exponent > 15) return sign | 0x7E;
1836
1837 if (exponent <= 0) {
1838 // Subnormal path
1839 if (exponent < -3) return sign; // too small, flush to zero
1840 mantissa |= 0x800000;
1841 uint32_t shift = 1 - exponent;
1842 mantissa >>= shift;
1843 return sign | ((mantissa >> 20) & 0x07);
1844 }
1845 uint8_t mag = static_cast<uint8_t>((exponent << 3) | ((mantissa >> 20) & 0x07));
1846 // 0x7F is NaN in E4M3; values in [480, 512) land there, clamp to max finite
1847 if (mag == 0x7F) mag = 0x7E;
1848 return sign | mag;
1849}
1850
1851float float8ToFloat(uint8_t f8) {
1852 // Both 0x7F and 0xFF are NaN (sign bit can be 0 or 1, exp=1111, mant=111)
1853 if ((f8 & 0x7F) == 0x7F) {
1854 uint32_t nan = 0x7FC00000;
1855 float result;
1856 std::memcpy(&result, &nan, sizeof(result));
1857 return result;
1858 }
1859
1860 uint32_t sign = (static_cast<uint32_t>(f8) & 0x80) << 24;
1861 uint32_t exponent = (f8 >> 3) & 0x0F;
1862 uint32_t mantissa = f8 & 0x07;
1863
1864 uint32_t f;
1865 if (exponent == 0) {
1866 if (mantissa == 0) {
1867 f = sign; // zero
1868 } else {
1869 // Subnormal: shift left until the implicit leading 1 reaches bit 3,
1870 // then strip it. Use int32_t so exponentWork can go negative safely.
1871 int32_t exponentWork = 1;
1872 while (!(mantissa & 0x08)) {
1873 mantissa <<= 1;
1874 exponentWork--;
1875 }
1876 mantissa &= 0x07;
1877 f = sign | (static_cast<uint32_t>(exponentWork + 127 - 7) << 23) | (mantissa << 20);
1878 }
1879 } else {
1880 f = sign | (static_cast<uint32_t>(exponent + 127 - 7) << 23) | (mantissa << 20);
1881 }
1882
1883 float result;
1884 std::memcpy(&result, &f, sizeof(result));
1885 return result;
1886}
1887
1888uint16_t floatToBFloat16(float value) {
1889 uint32_t f;
1890 std::memcpy(&f, &value, sizeof(f));
1891 // BFloat16 is simply the upper 16 bits of the float32 bit pattern.
1892 // All special values (NaN, infinity, zero, subnormals) are preserved.
1893 uint16_t bf16 = static_cast<uint16_t>(f >> 16);
1894 // Preserve NaN: truncation can clear payload bits, turning NaN into Inf.
1895 uint32_t exponent = (f >> 23) & 0xFF;
1896 uint32_t mantissa = f & 0x7FFFFF;
1897 if (exponent == 0xFF && mantissa != 0 && (bf16 & 0x007F) == 0) {
1898 bf16 |= 0x0001;
1899 }
1900 return bf16;
1901}
1902
1903float bfloat16ToFloat(uint16_t bf16) {
1904 uint32_t f = static_cast<uint32_t>(bf16) << 16;
1905 float result;
1906 std::memcpy(&result, &f, sizeof(result));
1907 return result;
1908}
1909
1910// TensorFloat32: 1s / 8e / 10m, bias=127, same exponent as float32
1911// Stored in 4 bytes; lower 13 mantissa bits are zeroed.
1912
1913uint32_t floatToTensorFloat32(float value) {
1914 uint32_t f;
1915 std::memcpy(&f, &value, sizeof(f));
1916 uint32_t tf32 = f & 0xFFFFE000U;
1917 // Preserve NaN: masking can clear payload bits, turning NaN into Inf.
1918 if ((f & 0x7F800000U) == 0x7F800000U && (f & 0x007FFFFFU) != 0 &&
1919 (tf32 & 0x007FFFFFU) == 0) {
1920 tf32 |= 0x00002000U;
1921 }
1922 return tf32;
1923}
1924
1925float tensorFloat32ToFloat(uint32_t tf32) {
1926 float result;
1927 std::memcpy(&result, &tf32, sizeof(result));
1928 return result;
1929}
1930
1931// Float6 E3M2: 1s / 3e / 2m, bias=3, no Inf/NaN
1932// Stored in lower 6 bits of uint8_t
1933
1934uint8_t floatToFloat6(float value) {
1935 uint32_t f;
1936 std::memcpy(&f, &value, sizeof(f));
1937
1938 uint8_t sign = (f >> 26) & 0x20; // sign bit -> bit 5 of result
1939 int32_t exponent = ((f >> 23) & 0xFF) - 127 + 3; // rebias to E3M2 bias=3
1940 uint32_t mantissa = f & 0x7FFFFF;
1941
1942 // NaN or Infinity input -> clamp to max finite (no NaN/Inf in E3M2)
1943 if (((f >> 23) & 0xFF) == 0xFF) {
1944 // NaN has no meaningful sign: always clamp to positive max.
1945 // Infinity preserves sign.
1946 uint8_t nan_sign = (f & 0x7FFFFF) ? 0x00 : static_cast<uint8_t>(sign);
1947 return nan_sign | 0x1F;
1948 }
1949
1950 // Overflow -> clamp to max finite
1951 if (exponent > 7) return sign | 0x1F;
1952
1953 if (exponent <= 0) {
1954 // Subnormal path
1955 if (exponent < -2) return sign; // too small, flush to zero
1956 mantissa |= 0x800000;
1957 uint32_t shift = 1 - exponent;
1958 mantissa >>= shift;
1959 return sign | ((mantissa >> 21) & 0x03);
1960 }
1961 return sign | (exponent << 2) | ((mantissa >> 21) & 0x03);
1962}
1963
1964float float6ToFloat(uint8_t f6) {
1965 // Mask to 6 bits (upper 2 bits of byte are unused)
1966 f6 &= 0x3F;
1967
1968 float sign = (f6 & 0x20) ? -1.0f : 1.0f;
1969 uint32_t exponent = (f6 >> 2) & 0x07;
1970 uint32_t mantissa = f6 & 0x03;
1971
1972 if (exponent == 0) {
1973 if (mantissa == 0) {
1974 return sign * 0.0f; // +/- zero
1975 }
1976 // Subnormal: value = sign * mantissa/4 * 2^(1-3) = sign * mantissa * 2^(-4)
1977 return sign * static_cast<float>(mantissa) * 0.0625f; // 0.0625 = 2^(-4)
1978 }
1979 // Normal: value = sign * (1 + mantissa/4) * 2^(exponent-3)
1980 float frac = 1.0f + static_cast<float>(mantissa) / 4.0f;
1981 return sign * std::ldexp(frac, static_cast<int>(exponent) - 3);
1982}
1983
1984// Float4 E2M1: 1s / 2e / 1m, bias=1, no Inf/NaN
1985// Stored in lower 4 bits of uint8_t
1986uint8_t floatToFloat4(float value) {
1987 uint32_t f;
1988 std::memcpy(&f, &value, sizeof(f));
1989
1990 uint8_t sign = (f >> 28) & 0x08; // sign bit -> bit 3 of result
1991 int32_t exponent = ((f >> 23) & 0xFF) - 127 + 1; // rebias to E2M1 bias=1
1992 uint32_t mantissa = f & 0x7FFFFF;
1993
1994 // NaN or Infinity input -> clamp to max finite (no NaN/Inf in E2M1)
1995 if (((f >> 23) & 0xFF) == 0xFF) {
1996 // NaN has no meaningful sign: always clamp to positive max.
1997 // Infinity preserves sign.
1998 uint8_t nan_sign = (f & 0x7FFFFF) ? 0x00 : static_cast<uint8_t>(sign);
1999 return nan_sign | 0x07;
2000 }
2001
2002 // Overflow -> clamp to max finite
2003 if (exponent > 3) return sign | 0x07;
2004
2005 if (exponent <= 0) {
2006 // Subnormal path
2007 if (exponent < -1) return sign; // too small, flush to zero
2008 mantissa |= 0x800000;
2009 uint32_t shift = 1 - exponent;
2010 mantissa >>= shift;
2011 return sign | ((mantissa >> 22) & 0x01);
2012 }
2013 return sign | (exponent << 1) | ((mantissa >> 22) & 0x01);
2014}
2015
2016float float4ToFloat(uint8_t f4) {
2017 // Mask to 4 bits (upper 4 bits of byte are unused)
2018 f4 &= 0x0F;
2019
2020 float sign = (f4 & 0x08) ? -1.0f : 1.0f;
2021 uint32_t exponent = (f4 >> 1) & 0x03;
2022 uint32_t mantissa = f4 & 0x01;
2023
2024 if (exponent == 0) {
2025 if (mantissa == 0) {
2026 return sign * 0.0f; // +/- zero
2027 }
2028 // Subnormal: value = sign * mantissa * 0.5 (only one subnormal: +/-0.5)
2029 return sign * static_cast<float>(mantissa) * 0.5f;
2030 }
2031 // Normal: value = sign * (1 + mantissa/2) * 2^(exponent-1)
2032 float frac = 1.0f + static_cast<float>(mantissa) / 2.0f;
2033 return sign * std::ldexp(frac, static_cast<int>(exponent) - 1);
2034}
2035
2036} // anonymous namespace
2037
2039// Float16 (half-precision)
2041
2042#ifndef NO_PYTHON
2043
2044// Set data using float16
2045void rim::Block::setFloat16Py(bp::object& value, rim::Variable* var, int32_t index) {
2046 uint32_t x;
2047
2048 if (index == -1) index = 0;
2049
2050 // Passed value is a numpy value
2051 if (PyArray_Check(value.ptr())) {
2052 // Cast to an array object and check that the numpy array
2053 PyArrayObject* arr = reinterpret_cast<decltype(arr)>(value.ptr());
2054 npy_intp ndims = PyArray_NDIM(arr);
2055 npy_intp* dims = PyArray_SHAPE(arr);
2056 npy_intp* strides = PyArray_STRIDES(arr);
2057
2058 if (ndims != 1)
2059 throw(rogue::GeneralError::create("Block::setFloat16Py",
2060 "Invalid number of dimensions (%" PRIu32 ") for passed ndarray for %s",
2061 ndims,
2062 var->name_.c_str()));
2063
2064 if ((index + dims[0]) > var->numValues_)
2065 throw(rogue::GeneralError::create("Block::setFloat16Py",
2066 "Overflow error for passed array with length %" PRIu32
2067 " at index %" PRIi32 ". Variable length = %" PRIu32 " for %s",
2068 dims[0],
2069 index,
2070 var->numValues_,
2071 var->name_.c_str()));
2072
2073 if (PyArray_TYPE(arr) == NPY_HALF) {
2074 npy_half* src = reinterpret_cast<npy_half*>(PyArray_DATA(arr));
2075 npy_intp stride = strides[0] / sizeof(npy_half);
2076 for (x = 0; x < dims[0]; x++) {
2077 float val = halfToFloat(src[x * stride]);
2078 setFloat16(val, var, index + x);
2079 }
2080 } else {
2081 throw(rogue::GeneralError::create("Block::setFloat16Py",
2082 "Passed nparray is not of type (float16) for %s",
2083 var->name_.c_str()));
2084 }
2085
2086 // Is passed value a list
2087 } else if (PyList_Check(value.ptr())) {
2088 bp::list vl = bp::extract<bp::list>(value);
2089 uint32_t vlen = len(vl);
2090
2091 if ((index + vlen) > var->numValues_)
2092 throw(rogue::GeneralError::create("Block::setFloat16Py",
2093 "Overflow error for passed array with length %" PRIu32
2094 " at index %" PRIi32 ". Variable length = %" PRIu32 " for %s",
2095 vlen,
2096 index,
2097 var->numValues_,
2098 var->name_.c_str()));
2099
2100 for (x = 0; x < vlen; x++) {
2101 bp::extract<float> tmp(vl[x]);
2102
2103 if (!tmp.check())
2104 throw(rogue::GeneralError::create("Block::setFloat16Py",
2105 "Failed to extract value for %s.",
2106 var->name_.c_str()));
2107
2108 setFloat16(tmp, var, index + x);
2109 }
2110
2111 // Passed scalar numpy value
2112 } else if (PyArray_CheckScalar(value.ptr())) {
2113 if (PyArray_DescrFromScalar(value.ptr())->type_num == NPY_HALF) {
2114 npy_half val;
2115 PyArray_ScalarAsCtype(value.ptr(), &val);
2116 float fval = halfToFloat(val);
2117 setFloat16(fval, var, index);
2118 } else {
2119 throw(rogue::GeneralError::create("Block::setFloat16Py",
2120 "Failed to extract value for %s.",
2121 var->name_.c_str()));
2122 }
2123 } else {
2124 bp::extract<float> tmp(value);
2125
2126 if (!tmp.check())
2127 throw(rogue::GeneralError::create("Block::setFloat16Py",
2128 "Failed to extract value for %s.",
2129 var->name_.c_str()));
2130
2131 setFloat16(tmp, var, index);
2132 }
2133}
2134
2135// Get data using float16
2136bp::object rim::Block::getFloat16Py(rim::Variable* var, int32_t index) {
2137 bp::object ret;
2138 uint32_t x;
2139
2140 // Unindexed with a list variable
2141 if (index < 0 && var->numValues_ > 0) {
2142 // Create a numpy array to receive it and locate the destination data buffer
2143 npy_intp dims[1] = {var->numValues_};
2144 PyObject* obj = PyArray_SimpleNew(1, dims, NPY_HALF);
2145 PyArrayObject* arr = reinterpret_cast<PyArrayObject*>(obj);
2146 npy_half* dst = reinterpret_cast<npy_half*>(PyArray_DATA(arr));
2147
2148 for (x = 0; x < var->numValues_; x++) dst[x] = floatToHalf(getFloat16(var, x));
2149
2150 boost::python::handle<> handle(obj);
2151 ret = bp::object(handle);
2152
2153 } else {
2154 PyObject* val = Py_BuildValue("f", getFloat16(var, index));
2155
2156 if (val == NULL) throw(rogue::GeneralError::create("Block::getFloat16Py", "Failed to generate Float16"));
2157
2158 bp::handle<> handle(val);
2159 ret = bp::object(handle);
2160 }
2161 return ret;
2162}
2163
2164#endif
2165
2166// Set data using float16
2167void rim::Block::setFloat16(const float& val, rim::Variable* var, int32_t index) {
2168 // Check range
2169 if ((var->minValue_ != 0 || var->maxValue_ != 0) && (val > var->maxValue_ || val < var->minValue_))
2170 throw(rogue::GeneralError::create("Block::setFloat16",
2171 "Value range error for %s. Value=%f, Min=%f, Max=%f",
2172 var->name_.c_str(),
2173 val,
2174 var->minValue_,
2175 var->maxValue_));
2176
2177 // Convert float to half-precision and store as 2 bytes
2178 uint16_t half = floatToHalf(val);
2179 setBytes(reinterpret_cast<uint8_t*>(&half), var, index);
2180}
2181
2182// Get data using float16
2183float rim::Block::getFloat16(rim::Variable* var, int32_t index) {
2184 uint16_t tmp = 0;
2185
2186 getBytes(reinterpret_cast<uint8_t*>(&tmp), var, index);
2187
2188 return halfToFloat(tmp);
2189}
2190
2192// Float8 (E4M3)
2194
2195#ifndef NO_PYTHON
2196
2197// Set data using float8
2198void rim::Block::setFloat8Py(bp::object& value, rim::Variable* var, int32_t index) {
2199 uint32_t x;
2200
2201 if (index == -1) index = 0;
2202
2203 // Passed value is a numpy value
2204 if (PyArray_Check(value.ptr())) {
2205 // Cast to an array object and check that the numpy array
2206 PyArrayObject* arr = reinterpret_cast<decltype(arr)>(value.ptr());
2207 npy_intp ndims = PyArray_NDIM(arr);
2208 npy_intp* dims = PyArray_SHAPE(arr);
2209 npy_intp* strides = PyArray_STRIDES(arr);
2210
2211 if (ndims != 1)
2212 throw(rogue::GeneralError::create("Block::setFloat8Py",
2213 "Invalid number of dimensions (%" PRIu32 ") for passed ndarray for %s",
2214 ndims,
2215 var->name_.c_str()));
2216
2217 if ((index + dims[0]) > var->numValues_)
2218 throw(rogue::GeneralError::create("Block::setFloat8Py",
2219 "Overflow error for passed array with length %" PRIu32
2220 " at index %" PRIi32 ". Variable length = %" PRIu32 " for %s",
2221 dims[0],
2222 index,
2223 var->numValues_,
2224 var->name_.c_str()));
2225
2226 if (PyArray_TYPE(arr) == NPY_FLOAT) {
2227 float* src = reinterpret_cast<float*>(PyArray_DATA(arr));
2228 npy_intp stride = strides[0] / sizeof(float);
2229 for (x = 0; x < dims[0]; x++) {
2230 float val = src[x * stride];
2231 setFloat8(val, var, index + x);
2232 }
2233 } else {
2234 throw(rogue::GeneralError::create("Block::setFloat8Py",
2235 "Passed nparray is not of type (float32) for %s",
2236 var->name_.c_str()));
2237 }
2238
2239 // Is passed value a list
2240 } else if (PyList_Check(value.ptr())) {
2241 bp::list vl = bp::extract<bp::list>(value);
2242 uint32_t vlen = len(vl);
2243
2244 if ((index + vlen) > var->numValues_)
2245 throw(rogue::GeneralError::create("Block::setFloat8Py",
2246 "Overflow error for passed array with length %" PRIu32
2247 " at index %" PRIi32 ". Variable length = %" PRIu32 " for %s",
2248 vlen,
2249 index,
2250 var->numValues_,
2251 var->name_.c_str()));
2252
2253 for (x = 0; x < vlen; x++) {
2254 bp::extract<float> tmp(vl[x]);
2255
2256 if (!tmp.check())
2257 throw(rogue::GeneralError::create("Block::setFloat8Py",
2258 "Failed to extract value for %s.",
2259 var->name_.c_str()));
2260
2261 setFloat8(tmp, var, index + x);
2262 }
2263
2264 } else {
2265 bp::extract<float> tmp(value);
2266
2267 if (!tmp.check())
2268 throw(rogue::GeneralError::create("Block::setFloat8Py",
2269 "Failed to extract value for %s.",
2270 var->name_.c_str()));
2271
2272 setFloat8(tmp, var, index);
2273 }
2274}
2275
2276// Get data using float8
2277bp::object rim::Block::getFloat8Py(rim::Variable* var, int32_t index) {
2278 bp::object ret;
2279 uint32_t x;
2280
2281 // Unindexed with a list variable
2282 if (index < 0 && var->numValues_ > 0) {
2283 npy_intp dims[1] = {var->numValues_};
2284 PyObject* obj = PyArray_SimpleNew(1, dims, NPY_FLOAT);
2285 PyArrayObject* arr = reinterpret_cast<PyArrayObject*>(obj);
2286 float* dst = reinterpret_cast<float*>(PyArray_DATA(arr));
2287
2288 for (x = 0; x < var->numValues_; x++) dst[x] = getFloat8(var, x);
2289
2290 boost::python::handle<> handle(obj);
2291 ret = bp::object(handle);
2292
2293 } else {
2294 PyObject* val = Py_BuildValue("f", getFloat8(var, index));
2295
2296 if (val == NULL) throw(rogue::GeneralError::create("Block::getFloat8Py", "Failed to generate Float8"));
2297
2298 bp::handle<> handle(val);
2299 ret = bp::object(handle);
2300 }
2301 return ret;
2302}
2303
2304#endif
2305
2306// Set data using float8
2307void rim::Block::setFloat8(const float& val, rim::Variable* var, int32_t index) {
2308 // Check range
2309 if ((var->minValue_ != 0 || var->maxValue_ != 0) && (val > var->maxValue_ || val < var->minValue_))
2310 throw(rogue::GeneralError::create("Block::setFloat8",
2311 "Value range error for %s. Value=%f, Min=%f, Max=%f",
2312 var->name_.c_str(),
2313 val,
2314 var->minValue_,
2315 var->maxValue_));
2316
2317 // Convert float to E4M3 and store as 1 byte
2318 uint8_t f8 = floatToFloat8(val);
2319 setBytes(reinterpret_cast<uint8_t*>(&f8), var, index);
2320}
2321
2322// Get data using float8
2323float rim::Block::getFloat8(rim::Variable* var, int32_t index) {
2324 uint8_t tmp = 0;
2325
2326 getBytes(reinterpret_cast<uint8_t*>(&tmp), var, index);
2327
2328 return float8ToFloat(tmp);
2329}
2330
2332// BFloat16 (Brain Float 16)
2334
2335#ifndef NO_PYTHON
2336
2337// Set data using bfloat16
2338void rim::Block::setBFloat16Py(bp::object& value, rim::Variable* var, int32_t index) {
2339 uint32_t x;
2340
2341 if (index == -1) index = 0;
2342
2343 // Passed value is a numpy value
2344 if (PyArray_Check(value.ptr())) {
2345 // Cast to an array object and check that the numpy array
2346 PyArrayObject* arr = reinterpret_cast<decltype(arr)>(value.ptr());
2347 npy_intp ndims = PyArray_NDIM(arr);
2348 npy_intp* dims = PyArray_SHAPE(arr);
2349 npy_intp* strides = PyArray_STRIDES(arr);
2350
2351 if (ndims != 1)
2352 throw(rogue::GeneralError::create("Block::setBFloat16Py",
2353 "Invalid number of dimensions (%" PRIu32 ") for passed ndarray for %s",
2354 ndims,
2355 var->name_.c_str()));
2356
2357 if ((index + dims[0]) > var->numValues_)
2358 throw(rogue::GeneralError::create("Block::setBFloat16Py",
2359 "Overflow error for passed array with length %" PRIu32
2360 " at index %" PRIi32 ". Variable length = %" PRIu32 " for %s",
2361 dims[0],
2362 index,
2363 var->numValues_,
2364 var->name_.c_str()));
2365
2366 if (PyArray_TYPE(arr) == NPY_FLOAT) {
2367 float* src = reinterpret_cast<float*>(PyArray_DATA(arr));
2368 npy_intp stride = strides[0] / sizeof(float);
2369 for (x = 0; x < dims[0]; x++) {
2370 float val = src[x * stride];
2371 setBFloat16(val, var, index + x);
2372 }
2373 } else {
2374 throw(rogue::GeneralError::create("Block::setBFloat16Py",
2375 "Passed nparray is not of type (float32) for %s",
2376 var->name_.c_str()));
2377 }
2378
2379 // Is passed value a list
2380 } else if (PyList_Check(value.ptr())) {
2381 bp::list vl = bp::extract<bp::list>(value);
2382 uint32_t vlen = len(vl);
2383
2384 if ((index + vlen) > var->numValues_)
2385 throw(rogue::GeneralError::create("Block::setBFloat16Py",
2386 "Overflow error for passed array with length %" PRIu32
2387 " at index %" PRIi32 ". Variable length = %" PRIu32 " for %s",
2388 vlen,
2389 index,
2390 var->numValues_,
2391 var->name_.c_str()));
2392
2393 for (x = 0; x < vlen; x++) {
2394 bp::extract<float> tmp(vl[x]);
2395
2396 if (!tmp.check())
2397 throw(rogue::GeneralError::create("Block::setBFloat16Py",
2398 "Failed to extract value for %s.",
2399 var->name_.c_str()));
2400
2401 setBFloat16(tmp, var, index + x);
2402 }
2403
2404 } else {
2405 bp::extract<float> tmp(value);
2406
2407 if (!tmp.check())
2408 throw(rogue::GeneralError::create("Block::setBFloat16Py",
2409 "Failed to extract value for %s.",
2410 var->name_.c_str()));
2411
2412 setBFloat16(tmp, var, index);
2413 }
2414}
2415
2416// Get data using bfloat16
2417bp::object rim::Block::getBFloat16Py(rim::Variable* var, int32_t index) {
2418 bp::object ret;
2419 uint32_t x;
2420
2421 // Unindexed with a list variable
2422 if (index < 0 && var->numValues_ > 0) {
2423 npy_intp dims[1] = {var->numValues_};
2424 PyObject* obj = PyArray_SimpleNew(1, dims, NPY_FLOAT);
2425 PyArrayObject* arr = reinterpret_cast<PyArrayObject*>(obj);
2426 float* dst = reinterpret_cast<float*>(PyArray_DATA(arr));
2427
2428 for (x = 0; x < var->numValues_; x++) dst[x] = getBFloat16(var, x);
2429
2430 boost::python::handle<> handle(obj);
2431 ret = bp::object(handle);
2432
2433 } else {
2434 PyObject* val = Py_BuildValue("f", getBFloat16(var, index));
2435
2436 if (val == NULL) throw(rogue::GeneralError::create("Block::getBFloat16Py", "Failed to generate BFloat16"));
2437
2438 bp::handle<> handle(val);
2439 ret = bp::object(handle);
2440 }
2441 return ret;
2442}
2443
2444#endif
2445
2446// Set data using bfloat16
2447void rim::Block::setBFloat16(const float& val, rim::Variable* var, int32_t index) {
2448 // Check range
2449 if ((var->minValue_ != 0 || var->maxValue_ != 0) && (val > var->maxValue_ || val < var->minValue_))
2450 throw(rogue::GeneralError::create("Block::setBFloat16",
2451 "Value range error for %s. Value=%f, Min=%f, Max=%f",
2452 var->name_.c_str(),
2453 val,
2454 var->minValue_,
2455 var->maxValue_));
2456
2457 // Convert float to BFloat16 and store as 2 bytes
2458 uint16_t bf16 = floatToBFloat16(val);
2459 setBytes(reinterpret_cast<uint8_t*>(&bf16), var, index);
2460}
2461
2462// Get data using bfloat16
2463float rim::Block::getBFloat16(rim::Variable* var, int32_t index) {
2464 uint16_t tmp = 0;
2465
2466 getBytes(reinterpret_cast<uint8_t*>(&tmp), var, index);
2467
2468 return bfloat16ToFloat(tmp);
2469}
2470
2472// TensorFloat32 (NVIDIA TF32)
2474
2475#ifndef NO_PYTHON
2476
2477// Set data using TensorFloat32
2478void rim::Block::setTensorFloat32Py(bp::object& value, rim::Variable* var, int32_t index) {
2479 uint32_t x;
2480
2481 if (index == -1) index = 0;
2482
2483 // Passed value is a numpy value
2484 if (PyArray_Check(value.ptr())) {
2485 // Cast to an array object and check that the numpy array
2486 PyArrayObject* arr = reinterpret_cast<decltype(arr)>(value.ptr());
2487 npy_intp ndims = PyArray_NDIM(arr);
2488 npy_intp* dims = PyArray_SHAPE(arr);
2489 npy_intp* strides = PyArray_STRIDES(arr);
2490
2491 if (ndims != 1)
2492 throw(rogue::GeneralError::create("Block::setTensorFloat32Py",
2493 "Invalid number of dimensions (%" PRIu32 ") for passed ndarray for %s",
2494 ndims,
2495 var->name_.c_str()));
2496
2497 if ((index + dims[0]) > var->numValues_)
2498 throw(rogue::GeneralError::create("Block::setTensorFloat32Py",
2499 "Overflow error for passed array with length %" PRIu32
2500 " at index %" PRIi32 ". Variable length = %" PRIu32 " for %s",
2501 dims[0],
2502 index,
2503 var->numValues_,
2504 var->name_.c_str()));
2505
2506 if (PyArray_TYPE(arr) == NPY_FLOAT) {
2507 float* src = reinterpret_cast<float*>(PyArray_DATA(arr));
2508 npy_intp stride = strides[0] / sizeof(float);
2509 for (x = 0; x < dims[0]; x++) {
2510 float val = src[x * stride];
2511 setTensorFloat32(val, var, index + x);
2512 }
2513 } else {
2514 throw(rogue::GeneralError::create("Block::setTensorFloat32Py",
2515 "Passed nparray is not of type (float32) for %s",
2516 var->name_.c_str()));
2517 }
2518
2519 // Is passed value a list
2520 } else if (PyList_Check(value.ptr())) {
2521 bp::list vl = bp::extract<bp::list>(value);
2522 uint32_t vlen = len(vl);
2523
2524 if ((index + vlen) > var->numValues_)
2525 throw(rogue::GeneralError::create("Block::setTensorFloat32Py",
2526 "Overflow error for passed array with length %" PRIu32
2527 " at index %" PRIi32 ". Variable length = %" PRIu32 " for %s",
2528 vlen,
2529 index,
2530 var->numValues_,
2531 var->name_.c_str()));
2532
2533 for (x = 0; x < vlen; x++) {
2534 bp::extract<float> tmp(vl[x]);
2535
2536 if (!tmp.check())
2537 throw(rogue::GeneralError::create("Block::setTensorFloat32Py",
2538 "Failed to extract value for %s.",
2539 var->name_.c_str()));
2540
2541 setTensorFloat32(tmp, var, index + x);
2542 }
2543
2544 } else {
2545 bp::extract<float> tmp(value);
2546
2547 if (!tmp.check())
2548 throw(rogue::GeneralError::create("Block::setTensorFloat32Py",
2549 "Failed to extract value for %s.",
2550 var->name_.c_str()));
2551
2552 setTensorFloat32(tmp, var, index);
2553 }
2554}
2555
2556// Get data using TensorFloat32
2557bp::object rim::Block::getTensorFloat32Py(rim::Variable* var, int32_t index) {
2558 bp::object ret;
2559 uint32_t x;
2560
2561 // Unindexed with a list variable
2562 if (index < 0 && var->numValues_ > 0) {
2563 npy_intp dims[1] = {var->numValues_};
2564 PyObject* obj = PyArray_SimpleNew(1, dims, NPY_FLOAT);
2565 PyArrayObject* arr = reinterpret_cast<PyArrayObject*>(obj);
2566 float* dst = reinterpret_cast<float*>(PyArray_DATA(arr));
2567
2568 for (x = 0; x < var->numValues_; x++) dst[x] = getTensorFloat32(var, x);
2569
2570 boost::python::handle<> handle(obj);
2571 ret = bp::object(handle);
2572
2573 } else {
2574 PyObject* val = Py_BuildValue("f", getTensorFloat32(var, index));
2575
2576 if (val == NULL) throw(rogue::GeneralError::create("Block::getTensorFloat32Py", "Failed to generate TensorFloat32"));
2577
2578 bp::handle<> handle(val);
2579 ret = bp::object(handle);
2580 }
2581 return ret;
2582}
2583
2584#endif
2585
2586// Set data using TensorFloat32
2587void rim::Block::setTensorFloat32(const float& val, rim::Variable* var, int32_t index) {
2588 // Check range
2589 if ((var->minValue_ != 0 || var->maxValue_ != 0) && (val > var->maxValue_ || val < var->minValue_))
2590 throw(rogue::GeneralError::create("Block::setTensorFloat32",
2591 "Value range error for %s. Value=%f, Min=%f, Max=%f",
2592 var->name_.c_str(),
2593 val,
2594 var->minValue_,
2595 var->maxValue_));
2596
2597 // Convert float to TensorFloat32 and store as 4 bytes
2598 uint32_t tf32 = floatToTensorFloat32(val);
2599 setBytes(reinterpret_cast<uint8_t*>(&tf32), var, index);
2600}
2601
2602// Get data using TensorFloat32
2603float rim::Block::getTensorFloat32(rim::Variable* var, int32_t index) {
2604 uint32_t tmp = 0;
2605
2606 getBytes(reinterpret_cast<uint8_t*>(&tmp), var, index);
2607
2608 return tensorFloat32ToFloat(tmp);
2609}
2610
2612// Float6 (E3M2)
2614
2615#ifndef NO_PYTHON
2616
2617// Set data using float6
2618void rim::Block::setFloat6Py(bp::object& value, rim::Variable* var, int32_t index) {
2619 uint32_t x;
2620
2621 if (index == -1) index = 0;
2622
2623 // Passed value is a numpy value
2624 if (PyArray_Check(value.ptr())) {
2625 // Cast to an array object and check that the numpy array
2626 PyArrayObject* arr = reinterpret_cast<decltype(arr)>(value.ptr());
2627 npy_intp ndims = PyArray_NDIM(arr);
2628 npy_intp* dims = PyArray_SHAPE(arr);
2629 npy_intp* strides = PyArray_STRIDES(arr);
2630
2631 if (ndims != 1)
2632 throw(rogue::GeneralError::create("Block::setFloat6Py",
2633 "Invalid number of dimensions (%" PRIu32 ") for passed ndarray for %s",
2634 ndims,
2635 var->name_.c_str()));
2636
2637 if ((index + dims[0]) > var->numValues_)
2638 throw(rogue::GeneralError::create("Block::setFloat6Py",
2639 "Overflow error for passed array with length %" PRIu32
2640 " at index %" PRIi32 ". Variable length = %" PRIu32 " for %s",
2641 dims[0],
2642 index,
2643 var->numValues_,
2644 var->name_.c_str()));
2645
2646 if (PyArray_TYPE(arr) == NPY_FLOAT) {
2647 float* src = reinterpret_cast<float*>(PyArray_DATA(arr));
2648 npy_intp stride = strides[0] / sizeof(float);
2649 for (x = 0; x < dims[0]; x++) {
2650 float val = src[x * stride];
2651 setFloat6(val, var, index + x);
2652 }
2653 } else {
2654 throw(rogue::GeneralError::create("Block::setFloat6Py",
2655 "Passed nparray is not of type (float32) for %s",
2656 var->name_.c_str()));
2657 }
2658
2659 // Is passed value a list
2660 } else if (PyList_Check(value.ptr())) {
2661 bp::list vl = bp::extract<bp::list>(value);
2662 uint32_t vlen = len(vl);
2663
2664 if ((index + vlen) > var->numValues_)
2665 throw(rogue::GeneralError::create("Block::setFloat6Py",
2666 "Overflow error for passed array with length %" PRIu32
2667 " at index %" PRIi32 ". Variable length = %" PRIu32 " for %s",
2668 vlen,
2669 index,
2670 var->numValues_,
2671 var->name_.c_str()));
2672
2673 for (x = 0; x < vlen; x++) {
2674 bp::extract<float> tmp(vl[x]);
2675
2676 if (!tmp.check())
2677 throw(rogue::GeneralError::create("Block::setFloat6Py",
2678 "Failed to extract value for %s.",
2679 var->name_.c_str()));
2680
2681 setFloat6(tmp, var, index + x);
2682 }
2683
2684 } else {
2685 bp::extract<float> tmp(value);
2686
2687 if (!tmp.check())
2688 throw(rogue::GeneralError::create("Block::setFloat6Py",
2689 "Failed to extract value for %s.",
2690 var->name_.c_str()));
2691
2692 setFloat6(tmp, var, index);
2693 }
2694}
2695
2696// Get data using float6
2697bp::object rim::Block::getFloat6Py(rim::Variable* var, int32_t index) {
2698 bp::object ret;
2699 uint32_t x;
2700
2701 // Unindexed with a list variable
2702 if (index < 0 && var->numValues_ > 0) {
2703 npy_intp dims[1] = {var->numValues_};
2704 PyObject* obj = PyArray_SimpleNew(1, dims, NPY_FLOAT);
2705 PyArrayObject* arr = reinterpret_cast<PyArrayObject*>(obj);
2706 float* dst = reinterpret_cast<float*>(PyArray_DATA(arr));
2707
2708 for (x = 0; x < var->numValues_; x++) dst[x] = getFloat6(var, x);
2709
2710 boost::python::handle<> handle(obj);
2711 ret = bp::object(handle);
2712
2713 } else {
2714 PyObject* val = Py_BuildValue("f", getFloat6(var, index));
2715
2716 if (val == NULL) throw(rogue::GeneralError::create("Block::getFloat6Py", "Failed to generate Float6"));
2717
2718 bp::handle<> handle(val);
2719 ret = bp::object(handle);
2720 }
2721 return ret;
2722}
2723
2724#endif
2725
2726// Set data using float6
2727void rim::Block::setFloat6(const float& val, rim::Variable* var, int32_t index) {
2728 // Check range
2729 if ((var->minValue_ != 0 || var->maxValue_ != 0) && (val > var->maxValue_ || val < var->minValue_))
2730 throw(rogue::GeneralError::create("Block::setFloat6",
2731 "Value range error for %s. Value=%f, Min=%f, Max=%f",
2732 var->name_.c_str(),
2733 val,
2734 var->minValue_,
2735 var->maxValue_));
2736
2737 // Convert float to E3M2 and store as 1 byte
2738 uint8_t f6 = floatToFloat6(val);
2739 setBytes(reinterpret_cast<uint8_t*>(&f6), var, index);
2740}
2741
2742// Get data using float6
2743float rim::Block::getFloat6(rim::Variable* var, int32_t index) {
2744 uint8_t tmp = 0;
2745
2746 getBytes(reinterpret_cast<uint8_t*>(&tmp), var, index);
2747
2748 return float6ToFloat(tmp);
2749}
2750
2752// Float4 (E2M1)
2754
2755#ifndef NO_PYTHON
2756
2757// Set data using float4
2758void rim::Block::setFloat4Py(bp::object& value, rim::Variable* var, int32_t index) {
2759 uint32_t x;
2760
2761 if (index == -1) index = 0;
2762
2763 // Passed value is a numpy value
2764 if (PyArray_Check(value.ptr())) {
2765 // Cast to an array object and check that the numpy array
2766 PyArrayObject* arr = reinterpret_cast<decltype(arr)>(value.ptr());
2767 npy_intp ndims = PyArray_NDIM(arr);
2768 npy_intp* dims = PyArray_SHAPE(arr);
2769 npy_intp* strides = PyArray_STRIDES(arr);
2770
2771 if (ndims != 1)
2772 throw(rogue::GeneralError::create("Block::setFloat4Py",
2773 "Invalid number of dimensions (%" PRIu32 ") for passed ndarray for %s",
2774 ndims,
2775 var->name_.c_str()));
2776
2777 if ((index + dims[0]) > var->numValues_)
2778 throw(rogue::GeneralError::create("Block::setFloat4Py",
2779 "Overflow error for passed array with length %" PRIu32
2780 " at index %" PRIi32 ". Variable length = %" PRIu32 " for %s",
2781 dims[0],
2782 index,
2783 var->numValues_,
2784 var->name_.c_str()));
2785
2786 if (PyArray_TYPE(arr) == NPY_FLOAT) {
2787 float* src = reinterpret_cast<float*>(PyArray_DATA(arr));
2788 npy_intp stride = strides[0] / sizeof(float);
2789 for (x = 0; x < dims[0]; x++) {
2790 float val = src[x * stride];
2791 setFloat4(val, var, index + x);
2792 }
2793 } else {
2794 throw(rogue::GeneralError::create("Block::setFloat4Py",
2795 "Passed nparray is not of type (float32) for %s",
2796 var->name_.c_str()));
2797 }
2798
2799 // Is passed value a list
2800 } else if (PyList_Check(value.ptr())) {
2801 bp::list vl = bp::extract<bp::list>(value);
2802 uint32_t vlen = len(vl);
2803
2804 if ((index + vlen) > var->numValues_)
2805 throw(rogue::GeneralError::create("Block::setFloat4Py",
2806 "Overflow error for passed array with length %" PRIu32
2807 " at index %" PRIi32 ". Variable length = %" PRIu32 " for %s",
2808 vlen,
2809 index,
2810 var->numValues_,
2811 var->name_.c_str()));
2812
2813 for (x = 0; x < vlen; x++) {
2814 bp::extract<float> tmp(vl[x]);
2815
2816 if (!tmp.check())
2817 throw(rogue::GeneralError::create("Block::setFloat4Py",
2818 "Failed to extract value for %s.",
2819 var->name_.c_str()));
2820
2821 setFloat4(tmp, var, index + x);
2822 }
2823
2824 } else {
2825 bp::extract<float> tmp(value);
2826
2827 if (!tmp.check())
2828 throw(rogue::GeneralError::create("Block::setFloat4Py",
2829 "Failed to extract value for %s.",
2830 var->name_.c_str()));
2831
2832 setFloat4(tmp, var, index);
2833 }
2834}
2835
2836// Get data using float4
2837bp::object rim::Block::getFloat4Py(rim::Variable* var, int32_t index) {
2838 bp::object ret;
2839 uint32_t x;
2840
2841 // Unindexed with a list variable
2842 if (index < 0 && var->numValues_ > 0) {
2843 npy_intp dims[1] = {var->numValues_};
2844 PyObject* obj = PyArray_SimpleNew(1, dims, NPY_FLOAT);
2845 PyArrayObject* arr = reinterpret_cast<PyArrayObject*>(obj);
2846 float* dst = reinterpret_cast<float*>(PyArray_DATA(arr));
2847
2848 for (x = 0; x < var->numValues_; x++) dst[x] = getFloat4(var, x);
2849
2850 boost::python::handle<> handle(obj);
2851 ret = bp::object(handle);
2852
2853 } else {
2854 PyObject* val = Py_BuildValue("f", getFloat4(var, index));
2855
2856 if (val == NULL) throw(rogue::GeneralError::create("Block::getFloat4Py", "Failed to generate Float4"));
2857
2858 bp::handle<> handle(val);
2859 ret = bp::object(handle);
2860 }
2861 return ret;
2862}
2863
2864#endif
2865
2866// Set data using float4
2867void rim::Block::setFloat4(const float& val, rim::Variable* var, int32_t index) {
2868 // Check range
2869 if ((var->minValue_ != 0 || var->maxValue_ != 0) && (val > var->maxValue_ || val < var->minValue_))
2870 throw(rogue::GeneralError::create("Block::setFloat4",
2871 "Value range error for %s. Value=%f, Min=%f, Max=%f",
2872 var->name_.c_str(),
2873 val,
2874 var->minValue_,
2875 var->maxValue_));
2876
2877 // Convert float to E2M1 and store as 1 byte
2878 uint8_t f4 = floatToFloat4(val);
2879 setBytes(reinterpret_cast<uint8_t*>(&f4), var, index);
2880}
2881
2882// Get data using float4
2883float rim::Block::getFloat4(rim::Variable* var, int32_t index) {
2884 uint8_t tmp = 0;
2885
2886 getBytes(reinterpret_cast<uint8_t*>(&tmp), var, index);
2887
2888 return float4ToFloat(tmp);
2889}
2890
2892// Double
2894
2895#ifndef NO_PYTHON
2896
2897// Set data using double
2898void rim::Block::setDoublePy(bp::object& value, rim::Variable* var, int32_t index) {
2899 uint32_t x;
2900
2901 if (index == -1) index = 0;
2902
2903 // Passed value is a numpy value
2904 if (PyArray_Check(value.ptr())) {
2905 // Cast to an array object and check that the numpy array
2906 PyArrayObject* arr = reinterpret_cast<decltype(arr)>(value.ptr());
2907 npy_intp ndims = PyArray_NDIM(arr);
2908 npy_intp* dims = PyArray_SHAPE(arr);
2909 npy_intp* strides = PyArray_STRIDES(arr);
2910
2911 if (ndims != 1)
2912 throw(rogue::GeneralError::create("Block::setDoublePy",
2913 "Invalid number of dimensions (%" PRIu32 ") for passed ndarray for %s",
2914 ndims,
2915 var->name_.c_str()));
2916
2917 if ((index + dims[0]) > var->numValues_)
2918 throw(rogue::GeneralError::create("Block::setDoublePy",
2919 "Overflow error for passed array with length %" PRIu32
2920 " at index %" PRIi32 ". Variable length = %" PRIu32 " for %s",
2921 dims[0],
2922 index,
2923 var->numValues_,
2924 var->name_.c_str()));
2925
2926 if (PyArray_TYPE(arr) == NPY_FLOAT64) {
2927 double* src = reinterpret_cast<double*>(PyArray_DATA(arr));
2928 npy_intp stride = strides[0] / sizeof(double);
2929 for (x = 0; x < dims[0]; x++) {
2930 setDouble(src[x * stride], var, index + x);
2931 }
2932 } else {
2933 throw(rogue::GeneralError::create("Block::setFLoatPy",
2934 "Passed nparray is not of type (double) for %s",
2935 var->name_.c_str()));
2936 }
2937
2938 // Is passed value a list
2939 } else if (PyList_Check(value.ptr())) {
2940 bp::list vl = bp::extract<bp::list>(value);
2941 uint32_t vlen = len(vl);
2942
2943 if ((index + vlen) > var->numValues_)
2944 throw(rogue::GeneralError::create("Block::setDoublePy",
2945 "Overflow error for passed array with length %" PRIu32
2946 " at index %" PRIi32 ". Variable length = %" PRIu32 " for %s",
2947 vlen,
2948 index,
2949 var->numValues_,
2950 var->name_.c_str()));
2951
2952 for (x = 0; x < vlen; x++) {
2953 bp::extract<double> tmp(vl[x]);
2954
2955 if (!tmp.check())
2956 throw(rogue::GeneralError::create("Block::setDoublePy",
2957 "Failed to extract value for %s.",
2958 var->name_.c_str()));
2959
2960 setDouble(tmp, var, index + x);
2961 }
2962
2963 // Passed scalar numpy value
2964 } else if (PyArray_CheckScalar(value.ptr())) {
2965 if (PyArray_DescrFromScalar(value.ptr())->type_num == NPY_FLOAT64) {
2966 double val;
2967 PyArray_ScalarAsCtype(value.ptr(), &val);
2968 setDouble(val, var, index);
2969 } else {
2970 throw(rogue::GeneralError::create("Block::setDoublePy",
2971 "Failed to extract value for %s.",
2972 var->name_.c_str()));
2973 }
2974 } else {
2975 bp::extract<double> tmp(value);
2976
2977 if (!tmp.check())
2978 throw(rogue::GeneralError::create("Block::setDoublePy",
2979 "Failed to extract value for %s.",
2980 var->name_.c_str()));
2981
2982 setDouble(tmp, var, index);
2983 }
2984}
2985
2986// Get data using double
2987bp::object rim::Block::getDoublePy(rim::Variable* var, int32_t index) {
2988 bp::object ret;
2989 uint32_t x;
2990
2991 // Unindexed with a list variable
2992 if (index < 0 && var->numValues_ > 0) {
2993 // Create a numpy array to receive it and locate the destination data buffer
2994 npy_intp dims[1] = {var->numValues_};
2995 PyObject* obj = PyArray_SimpleNew(1, dims, NPY_FLOAT64);
2996 PyArrayObject* arr = reinterpret_cast<PyArrayObject*>(obj);
2997 double* dst = reinterpret_cast<double*>(PyArray_DATA(arr));
2998
2999 for (x = 0; x < var->numValues_; x++) dst[x] = getDouble(var, x);
3000
3001 boost::python::handle<> handle(obj);
3002 ret = bp::object(handle);
3003
3004 } else {
3005 PyObject* val = Py_BuildValue("d", getDouble(var, index));
3006
3007 if (val == NULL) throw(rogue::GeneralError::create("Block::getDoublePy", "Failed to generate Double"));
3008
3009 bp::handle<> handle(val);
3010 ret = bp::object(handle);
3011 }
3012 return ret;
3013}
3014
3015#endif
3016
3017// Set data using double
3018void rim::Block::setDouble(const double& val, rim::Variable* var, int32_t index) {
3019 // Check range
3020 if ((var->minValue_ != 0 || var->maxValue_ != 0) && (val > var->maxValue_ || val < var->minValue_))
3021 throw(rogue::GeneralError::create("Block::setDouble",
3022 "Value range error for %s. Value=%f, Min=%f, Max=%f",
3023 var->name_.c_str(),
3024 val,
3025 var->minValue_,
3026 var->maxValue_));
3027
3028 setBytes(reinterpret_cast<uint8_t*>(const_cast<double*>(&val)), var, index);
3029}
3030
3031// Get data using double
3032double rim::Block::getDouble(rim::Variable* var, int32_t index) {
3033 double tmp = 0;
3034
3035 getBytes(reinterpret_cast<uint8_t*>(&tmp), var, index);
3036
3037 return tmp;
3038}
3039
3041// Fixed Point
3043
3044#ifndef NO_PYTHON
3045
3046// Set data using fixed point
3047void rim::Block::setFixedPy(bp::object& value, rim::Variable* var, int32_t index) {
3048 uint32_t x;
3049
3050 // Dispatch to the signed or unsigned fixed-point scalar setter based on modelId
3051 auto setScalar = [this, var](const double& v, int32_t idx) {
3052 if (var->modelId_ == rim::UFixed)
3053 setUFixed(v, var, idx);
3054 else
3055 setFixed(v, var, idx);
3056 };
3057
3058 if (index == -1) index = 0;
3059
3060 // Passed value is a numpy value
3061 if (PyArray_Check(value.ptr())) {
3062 // Cast to an array object and check that the numpy array
3063 PyArrayObject* arr = reinterpret_cast<decltype(arr)>(value.ptr());
3064 npy_intp ndims = PyArray_NDIM(arr);
3065 npy_intp* dims = PyArray_SHAPE(arr);
3066 npy_intp* strides = PyArray_STRIDES(arr);
3067
3068 if (ndims != 1)
3069 throw(rogue::GeneralError::create("Block::setFixedPy",
3070 "Invalid number of dimensions (%" PRIu32 ") for passed ndarray for %s",
3071 ndims,
3072 var->name_.c_str()));
3073
3074 if ((index + dims[0]) > var->numValues_)
3075 throw(rogue::GeneralError::create("Block::setFixedPy",
3076 "Overflow error for passed array with length %" PRIu32
3077 " at index %" PRIi32 ". Variable length = %" PRIu32 " for %s",
3078 dims[0],
3079 index,
3080 var->numValues_,
3081 var->name_.c_str()));
3082
3083 if (PyArray_TYPE(arr) == NPY_FLOAT64) {
3084 double* src = reinterpret_cast<double*>(PyArray_DATA(arr));
3085 npy_intp stride = strides[0] / sizeof(double);
3086 for (x = 0; x < dims[0]; x++) {
3087 setScalar(src[x * stride], index + x);
3088 }
3089 } else {
3090 throw(rogue::GeneralError::create("Block::setFixedPy",
3091 "Passed nparray is not of type (double) for %s",
3092 var->name_.c_str()));
3093 }
3094
3095 // Is passed value a list
3096 } else if (PyList_Check(value.ptr())) {
3097 bp::list vl = bp::extract<bp::list>(value);
3098 uint32_t vlen = len(vl);
3099
3100 if ((index + vlen) > var->numValues_)
3101 throw(rogue::GeneralError::create("Block::setFixedPy",
3102 "Overflow error for passed array with length %" PRIu32
3103 " at index %" PRIi32 ". Variable length = %" PRIu32 " for %s",
3104 vlen,
3105 index,
3106 var->numValues_,
3107 var->name_.c_str()));
3108
3109 for (x = 0; x < vlen; x++) {
3110 bp::extract<double> tmp(vl[x]);
3111
3112 if (!tmp.check())
3113 throw(rogue::GeneralError::create("Block::setFixedPy",
3114 "Failed to extract value for %s.",
3115 var->name_.c_str()));
3116
3117 setScalar(tmp, index + x);
3118 }
3119
3120 // Passed scalar numpy value
3121 } else if (PyArray_CheckScalar(value.ptr())) {
3122 if (PyArray_DescrFromScalar(value.ptr())->type_num == NPY_FLOAT64) {
3123 double val;
3124 PyArray_ScalarAsCtype(value.ptr(), &val);
3125 setScalar(val, index);
3126 } else {
3127 throw(rogue::GeneralError::create("Block::setFixedPy",
3128 "Failed to extract value for %s.",
3129 var->name_.c_str()));
3130 }
3131 } else {
3132 bp::extract<double> tmp(value);
3133
3134 if (!tmp.check())
3135 throw(rogue::GeneralError::create("Block::setFixedPy",
3136 "Failed to extract value for %s.",
3137 var->name_.c_str()));
3138
3139 setScalar(tmp, index);
3140 }
3141}
3142
3143// Get data using fixed point
3144bp::object rim::Block::getFixedPy(rim::Variable* var, int32_t index) {
3145 bp::object ret;
3146 uint32_t x;
3147
3148 // Dispatch to the signed or unsigned fixed-point scalar getter based on modelId
3149 auto getScalar = [this, var](int32_t idx) -> double {
3150 return (var->modelId_ == rim::UFixed) ? getUFixed(var, idx) : getFixed(var, idx);
3151 };
3152
3153 // Unindexed with a list variable
3154 if (index < 0 && var->numValues_ > 0) {
3155 // Create a numpy array to receive it and locate the destination data buffer
3156 npy_intp dims[1] = {var->numValues_};
3157 PyObject* obj = PyArray_SimpleNew(1, dims, NPY_FLOAT64);
3158 PyArrayObject* arr = reinterpret_cast<PyArrayObject*>(obj);
3159 double* dst = reinterpret_cast<double*>(PyArray_DATA(arr));
3160
3161 for (x = 0; x < var->numValues_; x++) dst[x] = getScalar(x);
3162
3163 boost::python::handle<> handle(obj);
3164 ret = bp::object(handle);
3165
3166 } else {
3167 PyObject* val = Py_BuildValue("d", getScalar(index));
3168
3169 if (val == NULL) throw(rogue::GeneralError::create("Block::getFixedPy", "Failed to generate Fixed"));
3170
3171 bp::handle<> handle(val);
3172 ret = bp::object(handle);
3173 }
3174 return ret;
3175}
3176
3177#endif
3178
3179// Set data using fixed point
3180void rim::Block::setFixed(const double& val, rim::Variable* var, int32_t index) {
3181 // Check range
3182 if ((var->minValue_ != 0 || var->maxValue_ != 0) && (val > var->maxValue_ || val < var->minValue_))
3183 throw(rogue::GeneralError::create("Block::setFixed",
3184 "Value range error for %s. Value=%f, Min=%f, Max=%f",
3185 var->name_.c_str(),
3186 val,
3187 var->minValue_,
3188 var->maxValue_));
3189
3190 // Convert
3191 int64_t fPoint = static_cast<int64_t>(round(val * pow(2, var->binPoint_)));
3192
3193 // Compute representable range in integer domain (use 1ULL to avoid UB for 64-bit widths)
3194 int64_t maxInt = static_cast<int64_t>((1ULL << (var->valueBits_ - 1)) - 1);
3195 int64_t minInt = -maxInt - 1;
3196
3197 // Check for overflow (rounding may push value beyond representable range)
3198 if (fPoint > maxInt || fPoint < minInt)
3199 throw(rogue::GeneralError::create("Block::setFixed",
3200 "Fixed point overflow for %s. Value=%f, Min=%f, Max=%f",
3201 var->name_.c_str(),
3202 val,
3203 static_cast<double>(minInt) / pow(2, var->binPoint_),
3204 static_cast<double>(maxInt) / pow(2, var->binPoint_)));
3205
3206 setBytes(reinterpret_cast<uint8_t*>(&fPoint), var, index);
3207}
3208
3209// Get data using fixed point
3210double rim::Block::getFixed(rim::Variable* var, int32_t index) {
3211 int64_t fPoint = 0;
3212 double tmp;
3213
3214 getBytes(reinterpret_cast<uint8_t*>(&fPoint), var, index);
3215 // Sign-extend from valueBits_ to 64 bits. Use 1LL to avoid shifting a
3216 // plain int by >= 32 (UB) and guard the valueBits_ == 64 case, where
3217 // fPoint already holds the signed two's-complement value directly.
3218 if (var->valueBits_ < 64) {
3219 const int64_t signBit = 1LL << (var->valueBits_ - 1);
3220 const int64_t modulus = 1LL << var->valueBits_;
3221 if ((fPoint & signBit) != 0) fPoint -= modulus;
3222 }
3223
3224 // Convert to float
3225 tmp = static_cast<double>(fPoint);
3226 tmp = tmp / pow(2, var->binPoint_);
3227 return tmp;
3228}
3229
3230// Set data using unsigned fixed point
3231void rim::Block::setUFixed(const double& val, rim::Variable* var, int32_t index) {
3232 // Check range
3233 if ((var->minValue_ != 0 || var->maxValue_ != 0) && (val > var->maxValue_ || val < var->minValue_))
3234 throw(rogue::GeneralError::create("Block::setUFixed",
3235 "Value range error for %s. Value=%f, Min=%f, Max=%f",
3236 var->name_.c_str(),
3237 val,
3238 var->minValue_,
3239 var->maxValue_));
3240
3241 // Convert (unsigned; reject negatives explicitly before the cast)
3242 if (val < 0)
3243 throw(rogue::GeneralError::create("Block::setUFixed",
3244 "Unsigned fixed-point underflow for %s. Value=%f",
3245 var->name_.c_str(),
3246 val));
3247
3248 uint64_t fPoint = static_cast<uint64_t>(round(val * pow(2, var->binPoint_)));
3249
3250 // Compute representable unsigned range (cap at 64 bits to avoid UB)
3251 uint64_t maxUInt =
3252 (var->valueBits_ >= 64) ? UINT64_MAX : ((1ULL << var->valueBits_) - 1ULL);
3253
3254 // Check for overflow (rounding may push value beyond representable range)
3255 if (fPoint > maxUInt)
3256 throw(rogue::GeneralError::create("Block::setUFixed",
3257 "Unsigned fixed-point overflow for %s. Value=%f, Min=0, Max=%f",
3258 var->name_.c_str(),
3259 val,
3260 static_cast<double>(maxUInt) / pow(2, var->binPoint_)));
3261
3262 setBytes(reinterpret_cast<uint8_t*>(&fPoint), var, index);
3263}
3264
3265// Get data using unsigned fixed point
3266double rim::Block::getUFixed(rim::Variable* var, int32_t index) {
3267 uint64_t fPoint = 0;
3268
3269 getBytes(reinterpret_cast<uint8_t*>(&fPoint), var, index);
3270 // No sign extension — treat stored bits as unsigned
3271 return static_cast<double>(fPoint) / pow(2, var->binPoint_);
3272}
3273
3275// Custom
3277
3278// Custom Init function called after addVariables
3279void rim::Block::customInit() {}
3280
3281// Custom Clean function called before delete
3282void rim::Block::customClean() {}
3283
3284void rim::Block::rateTest() {
3285 uint32_t x;
3286
3287 struct timeval stime;
3288 struct timeval etime;
3289 struct timeval dtime;
3290
3291 uint64_t count = 1000000;
3292 double durr;
3293 double rate;
3294 uint32_t value;
3295
3296 gettimeofday(&stime, NULL);
3297 waitTransaction(0);
3298 for (x = 0; x < count; ++x) {
3299 reqTransaction(0, 4, &value, rim::Read);
3300 waitTransaction(0);
3301 }
3302 gettimeofday(&etime, NULL);
3303
3304 timersub(&etime, &stime, &dtime);
3305 durr = dtime.tv_sec + static_cast<float>(dtime.tv_usec) / 1.0e6;
3306 rate = count / durr;
3307
3308 printf("\nBlock c++ raw: Read %" PRIu64 " times in %f seconds. Rate = %f\n", count, durr, rate);
3309
3310 gettimeofday(&stime, NULL);
3311 waitTransaction(0);
3312 for (x = 0; x < count; ++x) {
3313 reqTransaction(0, 4, reinterpret_cast<uint8_t*>(&count), rim::Write);
3314 waitTransaction(0);
3315 }
3316 gettimeofday(&etime, NULL);
3317
3318 timersub(&etime, &stime, &dtime);
3319 durr = dtime.tv_sec + static_cast<float>(dtime.tv_usec) / 1.0e6;
3320 rate = count / durr;
3321
3322 printf("\nBlock c++ raw: Wrote %" PRIu64 " times in %f seconds. Rate = %f\n", count, durr, rate);
3323}
Generic Rogue exception type.
char const * what() const
Returns exception text for standard exception handling.
static GeneralError create(std::string src, const char *fmt,...)
Creates a formatted error instance.
RAII helper that releases the Python GIL for a scope.
Definition GilRelease.h:36
static std::shared_ptr< rogue::Logging > create(const std::string &name, bool quiet=false)
Creates a logger instance.
Definition Logging.cpp:95
RAII helper that acquires the Python GIL for a scope.
Definition ScopedGil.h:35
uint32_t size()
Returns block size in bytes.
Definition Block.cpp:166
uint64_t offset()
Returns the local offset of this block.
Definition Block.cpp:156
Internal Boost.Python wrapper for rogue::interfaces::memory::Variable. Enables Python subclasses to o...
Definition Variable.h:1099
Memory variable descriptor and typed accessor facade.
Definition Variable.h:62
std::vector< uint32_t > bitSize_
Definition Variable.h:103
std::vector< uint32_t > bitOffset_
Definition Variable.h:100
std::shared_ptr< rogue::interfaces::memory::Block > BlockPtr
Shared pointer alias for Block.
Definition Block.h:1199
static const uint32_t Read
Memory read transaction type.
Definition Constants.h:36
static const uint32_t Write
Memory write transaction type.
Definition Constants.h:43
static const uint32_t Verify
Memory verify readback transaction type.
Definition Constants.h:57
static const uint8_t UFixed
Block access type for unsigned fixed-point numeric data.
Definition Constants.h:141
static const uint32_t Post
Memory posted write transaction type.
Definition Constants.h:50
std::shared_ptr< rogue::interfaces::memory::Variable > VariablePtr
Definition Variable.h:43