| 369 | }; |
| 370 | |
| 371 | Result<std::shared_ptr<FixedWidthType>> DataTypeFromDLPack(DLDataType dtype) { |
| 372 | if (dtype.lanes != 1) { |
| 373 | return Status::TypeError("Only type with one lane are supported."); |
| 374 | } |
| 375 | |
| 376 | auto constexpr as_fw = [](auto dt) { |
| 377 | return std::static_pointer_cast<FixedWidthType>(std::move(dt)); |
| 378 | }; |
| 379 | |
| 380 | switch (dtype.code) { |
| 381 | case kDLInt: { |
| 382 | switch (dtype.bits) { |
| 383 | case 8: |
| 384 | return as_fw(int8()); |
| 385 | case 16: |
| 386 | return as_fw(int16()); |
| 387 | case 32: |
| 388 | return as_fw(int32()); |
| 389 | case 64: |
| 390 | return as_fw(int64()); |
| 391 | default: |
| 392 | return Status::Invalid("unsupported integer bit width ", |
| 393 | static_cast<int>(dtype.bits)); |
| 394 | } |
| 395 | } |
| 396 | case kDLUInt: { |
| 397 | switch (dtype.bits) { |
| 398 | case 8: |
| 399 | return as_fw(uint8()); |
| 400 | case 16: |
| 401 | return as_fw(uint16()); |
| 402 | case 32: |
| 403 | return as_fw(uint32()); |
| 404 | case 64: |
| 405 | return as_fw(uint64()); |
| 406 | default: |
| 407 | return Status::Invalid("unsupported unsigned integer bit width ", |
| 408 | static_cast<int>(dtype.bits)); |
| 409 | } |
| 410 | } |
| 411 | case kDLFloat: { |
| 412 | switch (dtype.bits) { |
| 413 | case 16: |
| 414 | return as_fw(float16()); |
| 415 | case 32: |
| 416 | return as_fw(float32()); |
| 417 | case 64: |
| 418 | return as_fw(float64()); |
| 419 | default: |
| 420 | return Status::Invalid("unsupported float bit width ", |
| 421 | static_cast<int>(dtype.bits)); |
| 422 | } |
| 423 | } |
| 424 | default: { |
| 425 | return Status::Invalid("unsupported DLPack type ", static_cast<int>(dtype.code)); |
| 426 | } |
| 427 | } |
| 428 | } |