Skip to content

Utility helper to deduce scalar type from NSNumber. #9552

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 25, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions extension/apple/ExecuTorch/Internal/ExecuTorchUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,40 @@
*/

#import <Foundation/Foundation.h>

#ifdef __cplusplus

#import <executorch/runtime/core/exec_aten/exec_aten.h>

namespace executorch::extension::utils {
using namespace aten;

/**
* Deduces the scalar type for a given NSNumber based on its type encoding.
*
* @param number The NSNumber instance whose scalar type is to be deduced.
* @return The corresponding ScalarType.
*/
static inline ScalarType deduceScalarType(NSNumber *number) {
auto type = [number objCType][0];
type = (type >= 'A' && type <= 'Z') ? type + ('a' - 'A') : type;
if (type == 'c') {
return ScalarType::Byte;
} else if (type == 's') {
return ScalarType::Short;
} else if (type == 'i') {
return ScalarType::Int;
} else if (type == 'q' || type == 'l') {
return ScalarType::Long;
} else if (type == 'f') {
return ScalarType::Float;
} else if (type == 'd') {
return ScalarType::Double;
}
ET_CHECK_MSG(false, "Unsupported type: %c", type);
return ScalarType::Undefined;
}

} // namespace executorch::extension::utils

#endif // __cplusplus
Loading