Files
The Type-Casting Module

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.
 

Detailed Description

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.

int dst = cast_long_to_int_tx(LONG_MAX);
(picotm_error_as_errno() == ERANGE)) {
// An error happened during type casting.
}

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.

typedef float float100;
#define FLT100_MAX 100 // similar to FLT_MAX from <float.h>

We can now create a function for casting from float to float100 using PICOTM_CAST_TX().

PICOTM_CAST_TX(float, float, float100, float100, -FLT100_MAX, FLT100_MAX);

This macro expands to a function that casts from type float to type float100.

static inline float100
cast_float_to_float100_tx(float value)
{
... // safe type-casting code
}

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.

PICOTM_CAST_TX(uchar, unsigned char, float100, float100, -FLT100_MAX, FLT100_MAX);

This expands to the function shown below. Note that we used uchar for function names that involve unsigned char.

static inline float100
cast_uchar_to_float100_tx(unsigned char value)
{
... // safe type-casting code
}

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.

PICOTM_CAST_TX(float100, float100, uchar, unsigned char, 0, UCHAR_MAX);

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.