+#define PSI_CALC_CAST_SET(in_type, in_val, op, out_val, out_var) \
+switch (in_type) { \
+case PSI_T_INT8: (out_val)->out_var op (in_val)->i8; break; \
+case PSI_T_UINT8: (out_val)->out_var op (in_val)->u8; break; \
+case PSI_T_INT16: (out_val)->out_var op (in_val)->i16; break; \
+case PSI_T_UINT16: (out_val)->out_var op (in_val)->u16; break; \
+case PSI_T_INT32: (out_val)->out_var op (in_val)->i32; break; \
+case PSI_T_UINT32: (out_val)->out_var op (in_val)->u32; break; \
+case PSI_T_INT64: (out_val)->out_var op (in_val)->i64; break; \
+case PSI_T_UINT64: (out_val)->out_var op (in_val)->u64; break; \
+case PSI_T_FLOAT: (out_val)->out_var op (in_val)->fval; break; \
+case PSI_T_DOUBLE: (out_val)->out_var op (in_val)->dval; break; \
+case PSI_T_LONG_DOUBLE: (out_val)->out_var op (in_val)->ldval; break; \
+default: \
+ assert(0); \
+}
+
+#define PSI_CALC_CAST(in_type, in_val, op, out_type, out_val) \
+switch (out_type) { \
+case PSI_T_INT8: PSI_CALC_CAST_SET(in_type, in_val, op, out_val, i8) break; \
+case PSI_T_UINT8: PSI_CALC_CAST_SET(in_type, in_val, op, out_val, u8) break; \
+case PSI_T_INT16: PSI_CALC_CAST_SET(in_type, in_val, op, out_val, i16) break; \
+case PSI_T_UINT16: PSI_CALC_CAST_SET(in_type, in_val, op, out_val, u16) break; \
+case PSI_T_INT32: PSI_CALC_CAST_SET(in_type, in_val, op, out_val, i32) break; \
+case PSI_T_UINT32: PSI_CALC_CAST_SET(in_type, in_val, op, out_val, u32) break; \
+case PSI_T_INT64: PSI_CALC_CAST_SET(in_type, in_val, op, out_val, i64) break; \
+case PSI_T_UINT64: PSI_CALC_CAST_SET(in_type, in_val, op, out_val, u64) break; \
+case PSI_T_FLOAT: PSI_CALC_CAST_SET(in_type, in_val, op, out_val, fval) break; \
+case PSI_T_DOUBLE: PSI_CALC_CAST_SET(in_type, in_val, op, out_val, dval) break; \
+case PSI_T_LONG_DOUBLE: PSI_CALC_CAST_SET(in_type, in_val, op, out_val, ldval) break; \
+default: \
+ assert(0); \
+}
+
+#define PSI_CALC_CAST_SET_INT(in_type, in_val, op, out_val, out_var) \
+switch (in_type) { \
+case PSI_T_INT8: (out_val)->out_var op (in_val)->i8; break; \
+case PSI_T_UINT8: (out_val)->out_var op (in_val)->u8; break; \
+case PSI_T_INT16: (out_val)->out_var op (in_val)->i16; break; \
+case PSI_T_UINT16: (out_val)->out_var op (in_val)->u16; break; \
+case PSI_T_INT32: (out_val)->out_var op (in_val)->i32; break; \
+case PSI_T_UINT32: (out_val)->out_var op (in_val)->u32; break; \
+case PSI_T_INT64: (out_val)->out_var op (in_val)->i64; break; \
+case PSI_T_UINT64: (out_val)->out_var op (in_val)->u64; break; \
+default: \
+ assert(0); \
+}
+#define PSI_CALC_CAST_INT(in_type, in_val, op, out_type, out_val) \
+switch (out_type) { \
+case PSI_T_INT8: PSI_CALC_CAST_SET_INT(in_type, in_val, op, out_val, i8) break; \
+case PSI_T_UINT8: PSI_CALC_CAST_SET_INT(in_type, in_val, op, out_val, u8) break; \
+case PSI_T_INT16: PSI_CALC_CAST_SET_INT(in_type, in_val, op, out_val, i16) break; \
+case PSI_T_UINT16: PSI_CALC_CAST_SET_INT(in_type, in_val, op, out_val, u16) break; \
+case PSI_T_INT32: PSI_CALC_CAST_SET_INT(in_type, in_val, op, out_val, i32) break; \
+case PSI_T_UINT32: PSI_CALC_CAST_SET_INT(in_type, in_val, op, out_val, u32) break; \
+case PSI_T_INT64: PSI_CALC_CAST_SET_INT(in_type, in_val, op, out_val, i64) break; \
+case PSI_T_UINT64: PSI_CALC_CAST_SET_INT(in_type, in_val, op, out_val, u64) break; \
+default: \
+ assert(0); \
+}
+
+void psi_calc_cast(token_t in_type, impl_val *in_val, token_t out_type, impl_val *out_val)
+{
+ PSI_CALC_CAST(in_type, in_val, =, out_type, out_val)
+}