|
1 | 1 | // Licensed to the .NET Foundation under one or more agreements. |
2 | 2 | // The .NET Foundation licenses this file to you under the MIT license. |
3 | 3 |
|
| 4 | +using System.Runtime.CompilerServices; |
| 5 | + |
4 | 6 | namespace System.Numerics.Tensors |
5 | 7 | { |
6 | 8 | /// <summary>Performs primitive tensor operations over spans of memory.</summary> |
@@ -488,6 +490,249 @@ public static void Ceiling<T>(ReadOnlySpan<T> x, Span<T> destination) |
488 | 490 | where T : IFloatingPoint<T> => |
489 | 491 | InvokeSpanIntoSpan<T, CeilingOperator<T>>(x, destination); |
490 | 492 |
|
| 493 | + /// <summary> |
| 494 | + /// Copies <paramref name="source"/> to <paramref name="destination"/>, converting each <typeparamref name="TFrom"/> |
| 495 | + /// value to a <typeparamref name="TTo"/> value. |
| 496 | + /// </summary> |
| 497 | + /// <param name="source">The source span from which to copy values.</param> |
| 498 | + /// <param name="destination">The destination span into which the converted values should be written.</param> |
| 499 | + /// <exception cref="ArgumentException">Destination is too short.</exception> |
| 500 | + /// <remarks> |
| 501 | + /// <para> |
| 502 | + /// This method effectively computes <c><paramref name="destination" />[i] = TTo.CreateChecked(<paramref name="source"/>[i])</c>. |
| 503 | + /// </para> |
| 504 | + /// </remarks> |
| 505 | + public static void ConvertChecked<TFrom, TTo>(ReadOnlySpan<TFrom> source, Span<TTo> destination) |
| 506 | + where TFrom : INumberBase<TFrom> |
| 507 | + where TTo : INumberBase<TTo> |
| 508 | + { |
| 509 | + if (!TryConvertUniversal(source, destination)) |
| 510 | + { |
| 511 | + InvokeSpanIntoSpan<TFrom, TTo, ConvertCheckedFallbackOperator<TFrom, TTo>>(source, destination); |
| 512 | + } |
| 513 | + } |
| 514 | + |
| 515 | + /// <summary> |
| 516 | + /// Copies <paramref name="source"/> to <paramref name="destination"/>, converting each <typeparamref name="TFrom"/> |
| 517 | + /// value to a <typeparamref name="TTo"/> value. |
| 518 | + /// </summary> |
| 519 | + /// <param name="source">The source span from which to copy values.</param> |
| 520 | + /// <param name="destination">The destination span into which the converted values should be written.</param> |
| 521 | + /// <exception cref="ArgumentException">Destination is too short.</exception> |
| 522 | + /// <remarks> |
| 523 | + /// <para> |
| 524 | + /// This method effectively computes <c><paramref name="destination" />[i] = TTo.CreateSaturating(<paramref name="source"/>[i])</c>. |
| 525 | + /// </para> |
| 526 | + /// </remarks> |
| 527 | + public static void ConvertSaturating<TFrom, TTo>(ReadOnlySpan<TFrom> source, Span<TTo> destination) |
| 528 | + where TFrom : INumberBase<TFrom> |
| 529 | + where TTo : INumberBase<TTo> |
| 530 | + { |
| 531 | + if (!TryConvertUniversal(source, destination)) |
| 532 | + { |
| 533 | + InvokeSpanIntoSpan<TFrom, TTo, ConvertSaturatingFallbackOperator<TFrom, TTo>>(source, destination); |
| 534 | + } |
| 535 | + } |
| 536 | + |
| 537 | + /// <summary> |
| 538 | + /// Copies <paramref name="source"/> to <paramref name="destination"/>, converting each <typeparamref name="TFrom"/> |
| 539 | + /// value to a <typeparamref name="TTo"/> value. |
| 540 | + /// </summary> |
| 541 | + /// <param name="source">The source span from which to copy values.</param> |
| 542 | + /// <param name="destination">The destination span into which the converted values should be written.</param> |
| 543 | + /// <exception cref="ArgumentException">Destination is too short.</exception> |
| 544 | + /// <remarks> |
| 545 | + /// <para> |
| 546 | + /// This method effectively computes <c><paramref name="destination" />[i] = TTo.CreateTruncating(<paramref name="source"/>[i])</c>. |
| 547 | + /// </para> |
| 548 | + /// </remarks> |
| 549 | + public static void ConvertTruncating<TFrom, TTo>(ReadOnlySpan<TFrom> source, Span<TTo> destination) |
| 550 | + where TFrom : INumberBase<TFrom> |
| 551 | + where TTo : INumberBase<TTo> |
| 552 | + { |
| 553 | + if (TryConvertUniversal(source, destination)) |
| 554 | + { |
| 555 | + return; |
| 556 | + } |
| 557 | + |
| 558 | + if (((typeof(TFrom) == typeof(byte) || typeof(TFrom) == typeof(sbyte)) && (typeof(TTo) == typeof(byte) || typeof(TTo) == typeof(sbyte))) || |
| 559 | + ((typeof(TFrom) == typeof(ushort) || typeof(TFrom) == typeof(short)) && (typeof(TTo) == typeof(ushort) || typeof(TTo) == typeof(short))) || |
| 560 | + ((IsUInt32Like<TFrom>() || IsInt32Like<TFrom>()) && (IsUInt32Like<TTo>() || IsInt32Like<TTo>())) || |
| 561 | + ((IsUInt64Like<TFrom>() || IsInt64Like<TFrom>()) && (IsUInt64Like<TTo>() || IsInt64Like<TTo>()))) |
| 562 | + { |
| 563 | + source.CopyTo(Rename<TTo, TFrom>(destination)); |
| 564 | + return; |
| 565 | + } |
| 566 | + |
| 567 | + if (typeof(TFrom) == typeof(float) && IsUInt32Like<TTo>()) |
| 568 | + { |
| 569 | + InvokeSpanIntoSpan<float, uint, ConvertSingleToUInt32>(Rename<TFrom, float>(source), Rename<TTo, uint>(destination)); |
| 570 | + return; |
| 571 | + } |
| 572 | + |
| 573 | + if (typeof(TFrom) == typeof(float) && IsInt32Like<TTo>()) |
| 574 | + { |
| 575 | + InvokeSpanIntoSpan<float, int, ConvertSingleToInt32>(Rename<TFrom, float>(source), Rename<TTo, int>(destination)); |
| 576 | + return; |
| 577 | + } |
| 578 | + |
| 579 | + if (typeof(TFrom) == typeof(double) && IsUInt64Like<TTo>()) |
| 580 | + { |
| 581 | + InvokeSpanIntoSpan<double, ulong, ConvertDoubleToUInt64>(Rename<TFrom, double>(source), Rename<TTo, ulong>(destination)); |
| 582 | + return; |
| 583 | + } |
| 584 | + |
| 585 | + if (typeof(TFrom) == typeof(double) && IsInt64Like<TTo>()) |
| 586 | + { |
| 587 | + InvokeSpanIntoSpan<double, long, ConvertDoubleToInt64>(Rename<TFrom, double>(source), Rename<TTo, long>(destination)); |
| 588 | + return; |
| 589 | + } |
| 590 | + |
| 591 | + if (typeof(TFrom) == typeof(ushort) && typeof(TTo) == typeof(byte)) |
| 592 | + { |
| 593 | + InvokeSpanIntoSpan_2to1<ushort, byte, NarrowUInt16ToByteOperator>(Rename<TFrom, ushort>(source), Rename<TTo, byte>(destination)); |
| 594 | + return; |
| 595 | + } |
| 596 | + |
| 597 | + if (typeof(TFrom) == typeof(short) && typeof(TTo) == typeof(sbyte)) |
| 598 | + { |
| 599 | + InvokeSpanIntoSpan_2to1<short, sbyte, NarrowInt16ToSByteOperator>(Rename<TFrom, short>(source), Rename<TTo, sbyte>(destination)); |
| 600 | + return; |
| 601 | + } |
| 602 | + |
| 603 | + if (IsUInt32Like<TFrom>() && typeof(TTo) == typeof(ushort)) |
| 604 | + { |
| 605 | + InvokeSpanIntoSpan_2to1<uint, ushort, NarrowUInt32ToUInt16Operator>(Rename<TFrom, uint>(source), Rename<TTo, ushort>(destination)); |
| 606 | + return; |
| 607 | + } |
| 608 | + |
| 609 | + if (IsInt32Like<TFrom>() && typeof(TTo) == typeof(short)) |
| 610 | + { |
| 611 | + InvokeSpanIntoSpan_2to1<int, short, NarrowInt32ToInt16Operator>(Rename<TFrom, int>(source), Rename<TTo, short>(destination)); |
| 612 | + return; |
| 613 | + } |
| 614 | + |
| 615 | + if (IsUInt64Like<TFrom>() && IsUInt32Like<TTo>()) |
| 616 | + { |
| 617 | + InvokeSpanIntoSpan_2to1<ulong, uint, NarrowUInt64ToUInt32Operator>(Rename<TFrom, ulong>(source), Rename<TTo, uint>(destination)); |
| 618 | + return; |
| 619 | + } |
| 620 | + |
| 621 | + if (IsInt64Like<TFrom>() && IsInt32Like<TTo>()) |
| 622 | + { |
| 623 | + InvokeSpanIntoSpan_2to1<long, int, NarrowInt64ToInt32Operator>(Rename<TFrom, long>(source), Rename<TTo, int>(destination)); |
| 624 | + return; |
| 625 | + } |
| 626 | + |
| 627 | + InvokeSpanIntoSpan<TFrom, TTo, ConvertTruncatingFallbackOperator<TFrom, TTo>>(source, destination); |
| 628 | + } |
| 629 | + |
| 630 | + /// <summary>Performs conversions that are the same regardless of checked, truncating, or saturation.</summary> |
| 631 | + [MethodImpl(MethodImplOptions.AggressiveInlining)] // at most one of the branches will be kept |
| 632 | + private static bool TryConvertUniversal<TFrom, TTo>(ReadOnlySpan<TFrom> source, Span<TTo> destination) |
| 633 | + where TFrom : INumberBase<TFrom> |
| 634 | + where TTo : INumberBase<TTo> |
| 635 | + { |
| 636 | + if (typeof(TFrom) == typeof(TTo)) |
| 637 | + { |
| 638 | + if (source.Length > destination.Length) |
| 639 | + { |
| 640 | + ThrowHelper.ThrowArgument_DestinationTooShort(); |
| 641 | + } |
| 642 | + |
| 643 | + ValidateInputOutputSpanNonOverlapping(source, Rename<TTo, TFrom>(destination)); |
| 644 | + |
| 645 | + source.CopyTo(Rename<TTo, TFrom>(destination)); |
| 646 | + return true; |
| 647 | + } |
| 648 | + |
| 649 | + if (IsInt32Like<TFrom>() && typeof(TTo) == typeof(float)) |
| 650 | + { |
| 651 | + InvokeSpanIntoSpan<int, float, ConvertInt32ToSingle>(Rename<TFrom, int>(source), Rename<TTo, float>(destination)); |
| 652 | + return true; |
| 653 | + } |
| 654 | + |
| 655 | + if (IsUInt32Like<TFrom>() && typeof(TTo) == typeof(float)) |
| 656 | + { |
| 657 | + InvokeSpanIntoSpan<uint, float, ConvertUInt32ToSingle>(Rename<TFrom, uint>(source), Rename<TTo, float>(destination)); |
| 658 | + return true; |
| 659 | + } |
| 660 | + |
| 661 | + if (IsInt64Like<TFrom>() && typeof(TTo) == typeof(double)) |
| 662 | + { |
| 663 | + InvokeSpanIntoSpan<long, double, ConvertInt64ToDouble>(Rename<TFrom, long>(source), Rename<TTo, double>(destination)); |
| 664 | + return true; |
| 665 | + } |
| 666 | + |
| 667 | + if (IsUInt64Like<TFrom>() && typeof(TTo) == typeof(double)) |
| 668 | + { |
| 669 | + InvokeSpanIntoSpan<ulong, double, ConvertUInt64ToDouble>(Rename<TFrom, ulong>(source), Rename<TTo, double>(destination)); |
| 670 | + return true; |
| 671 | + } |
| 672 | + |
| 673 | + if (typeof(TFrom) == typeof(float) && typeof(TTo) == typeof(Half)) |
| 674 | + { |
| 675 | + ConvertToHalf(Rename<TFrom, float>(source), Rename<TTo, Half>(destination)); |
| 676 | + return true; |
| 677 | + } |
| 678 | + |
| 679 | + if (typeof(TFrom) == typeof(Half) && typeof(TTo) == typeof(float)) |
| 680 | + { |
| 681 | + ConvertToSingle(Rename<TFrom, Half>(source), Rename<TTo, float>(destination)); |
| 682 | + return true; |
| 683 | + } |
| 684 | + |
| 685 | + if (typeof(TFrom) == typeof(float) && typeof(TTo) == typeof(double)) |
| 686 | + { |
| 687 | + InvokeSpanIntoSpan_1to2<float, double, WidenSingleToDoubleOperator>(Rename<TFrom, float>(source), Rename<TTo, double>(destination)); |
| 688 | + return true; |
| 689 | + } |
| 690 | + |
| 691 | + if (typeof(TFrom) == typeof(double) && typeof(TTo) == typeof(float)) |
| 692 | + { |
| 693 | + InvokeSpanIntoSpan_2to1<double, float, NarrowDoubleToSingleOperator>(Rename<TFrom, double>(source), Rename<TTo, float>(destination)); |
| 694 | + return true; |
| 695 | + } |
| 696 | + |
| 697 | + if (typeof(TFrom) == typeof(byte) && typeof(TTo) == typeof(ushort)) |
| 698 | + { |
| 699 | + InvokeSpanIntoSpan_1to2<byte, ushort, WidenByteToUInt16Operator>(Rename<TFrom, byte>(source), Rename<TTo, ushort>(destination)); |
| 700 | + return true; |
| 701 | + } |
| 702 | + |
| 703 | + if (typeof(TFrom) == typeof(sbyte) && typeof(TTo) == typeof(short)) |
| 704 | + { |
| 705 | + InvokeSpanIntoSpan_1to2<sbyte, short, WidenSByteToInt16Operator>(Rename<TFrom, sbyte>(source), Rename<TTo, short>(destination)); |
| 706 | + return true; |
| 707 | + } |
| 708 | + |
| 709 | + if (typeof(TFrom) == typeof(ushort) && IsUInt32Like<TTo>()) |
| 710 | + { |
| 711 | + InvokeSpanIntoSpan_1to2<ushort, uint, WidenUInt16ToUInt32Operator>(Rename<TFrom, ushort>(source), Rename<TTo, uint>(destination)); |
| 712 | + return true; |
| 713 | + } |
| 714 | + |
| 715 | + if (typeof(TFrom) == typeof(short) && IsInt32Like<TTo>()) |
| 716 | + { |
| 717 | + InvokeSpanIntoSpan_1to2<short, int, WidenInt16ToInt32Operator>(Rename<TFrom, short>(source), Rename<TTo, int>(destination)); |
| 718 | + return true; |
| 719 | + } |
| 720 | + |
| 721 | + if (IsUInt32Like<TTo>() && IsUInt64Like<TTo>()) |
| 722 | + { |
| 723 | + InvokeSpanIntoSpan_1to2<uint, ulong, WidenUInt32ToUInt64Operator>(Rename<TFrom, uint>(source), Rename<TTo, ulong>(destination)); |
| 724 | + return true; |
| 725 | + } |
| 726 | + |
| 727 | + if (IsInt32Like<TFrom>() && IsInt64Like<TTo>()) |
| 728 | + { |
| 729 | + InvokeSpanIntoSpan_1to2<int, long, WidenInt32ToInt64Operator>(Rename<TFrom, int>(source), Rename<TTo, long>(destination)); |
| 730 | + return true; |
| 731 | + } |
| 732 | + |
| 733 | + return false; |
| 734 | + } |
| 735 | + |
491 | 736 | /// <summary>Computes the element-wise result of copying the sign from one number to another number in the specified tensors.</summary> |
492 | 737 | /// <param name="x">The first tensor, represented as a span.</param> |
493 | 738 | /// <param name="sign">The second tensor, represented as a span.</param> |
@@ -963,15 +1208,14 @@ public static void Ieee754Remainder<T>(T x, ReadOnlySpan<T> y, Span<T> destinati |
963 | 1208 | public static void ILogB<T>(ReadOnlySpan<T> x, Span<int> destination) |
964 | 1209 | where T : IFloatingPointIeee754<T> |
965 | 1210 | { |
966 | | - if (x.Length > destination.Length) |
| 1211 | + if (typeof(T) == typeof(double)) |
967 | 1212 | { |
968 | | - ThrowHelper.ThrowArgument_DestinationTooShort(); |
| 1213 | + // Special-case double as the only vectorizable floating-point type whose size != sizeof(int). |
| 1214 | + InvokeSpanIntoSpan_2to1<double, int, ILogBDoubleOperator>(Rename<T, double>(x), destination); |
969 | 1215 | } |
970 | | - |
971 | | - // TODO: Vectorize |
972 | | - for (int i = 0; i < x.Length; i++) |
| 1216 | + else |
973 | 1217 | { |
974 | | - destination[i] = T.ILogB(x[i]); |
| 1218 | + InvokeSpanIntoSpan<T, int, ILogBOperator<T>>(x, destination); |
975 | 1219 | } |
976 | 1220 | } |
977 | 1221 |
|
|
0 commit comments