The type-casting module provides safe casting between C types. Overflows or underflows in the destination type's range are reported as errors to the transaction. More...
Files | |
| file | picotm-cast-ctypes.h |
| Transactional, safe type casting for native C types. | |
| file | picotm-cast.h |
| Public interfaces of picotm's type-casting module. | |
The type-casting module provides safe casting between C types. Type casting is subject to overflows or underflows of the destination type's range. For example, casting the value of 512 from 16-bit-wide short to 8-bit-wide signed char overflows the range of signed char. Similarly, casting from signed to unsigned types underflows the destination type's range if the source value is negative.
All type-casting functions are in the form of cast_<source type>_to_<destination type>_tx(), where <source type> and <destination type> are names for C types. Each function takes a value of the source type and returns the same value casted to the destination type.
Picotm's type-casting module comes with pre-defined functions for converting between all C-native integer types, and for converting from C-native integer to floating-point types. Here's an example transaction that casts a value from long to int.
Depending on the system's architecture, type long is either 32 bit of 64 bit in size. The example succeeds on 32-bit systems but reports an error on 64-bit systems.
The C preprocessor macro PICOTM_CAST_TX() creates a cast function for a specific pair of types and limits. We can use it to define cast functions for application-specific types. All pre-defined conversion function are created with this macro, so casts for application types share the same behaviour and features.
For example, our application might define the floating-point type float100, which is only defined from minus 100 to plus 100.
We can now create a function for casting from float to float100 using PICOTM_CAST_TX().
This macro expands to a function that casts from type float to type float100.
The return value of this function is of type float100 and spans values between -FLT_MAX and FLT_MAX, which has been defined to 100. If the parameter overflows or underflows this range, the function signals ERANGE to the transaction's recovery code.
Similarly, we can define a function for casting from unsigned char to float100.
This expands to the function shown below. Note that we used uchar for function names that involve unsigned char.
We can also cast from float100 to another type. The cast's minimum and maximum values are always relative to the destination type. So for a cast from float100 to unsigned char we have to use 0 and UCHAR_MAX as the limits.
Conversions from floating-point to integer types are not supported by the type-casting module, as they involve floating-point rounding modes. Transactional conversion functions from picotm's C Math Library module perform such operations.
Also not supported by the type-casting module are conversions from and to C strings. The sscanf_tx() and snprintf_tx() families of functions from picotm's C Standard Library Module perform these operations.
Results or parameters of type-casting operations should be loaded into the transaction or stored from the transaction using the Transactional memory module.