This macro defines a |UnaryScalarFoldingRule| that performs float to integer conversion. TODO(greg-lunarg): Support for 64-bit integer types.
| 865 | // integer conversion. |
| 866 | // TODO(greg-lunarg): Support for 64-bit integer types. |
| 867 | UnaryScalarFoldingRule FoldFToIOp() { |
| 868 | return [](const analysis::Type* result_type, const analysis::Constant* a, |
| 869 | analysis::ConstantManager* const_mgr) -> const analysis::Constant* { |
| 870 | assert(result_type != nullptr && a != nullptr); |
| 871 | const analysis::Integer* integer_type = result_type->AsInteger(); |
| 872 | const analysis::Float* float_type = a->type()->AsFloat(); |
| 873 | assert(float_type != nullptr); |
| 874 | assert(integer_type != nullptr); |
| 875 | if (integer_type->width() != 32) return nullptr; |
| 876 | if (float_type->width() == 32) { |
| 877 | float fa = a->GetFloat(); |
| 878 | uint32_t result = integer_type->IsSigned() |
| 879 | ? static_cast<uint32_t>(static_cast<int32_t>(fa)) |
| 880 | : static_cast<uint32_t>(fa); |
| 881 | std::vector<uint32_t> words = {result}; |
| 882 | return const_mgr->GetConstant(result_type, words); |
| 883 | } else if (float_type->width() == 64) { |
| 884 | double fa = a->GetDouble(); |
| 885 | uint32_t result = integer_type->IsSigned() |
| 886 | ? static_cast<uint32_t>(static_cast<int32_t>(fa)) |
| 887 | : static_cast<uint32_t>(fa); |
| 888 | std::vector<uint32_t> words = {result}; |
| 889 | return const_mgr->GetConstant(result_type, words); |
| 890 | } |
| 891 | return nullptr; |
| 892 | }; |
| 893 | } |
| 894 | |
| 895 | // This function defines a |UnaryScalarFoldingRule| that performs integer to |
| 896 | // float conversion. |