1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845
|
//! # SpatialSimilarity - Hardware-Accelerated Similarity Metrics and Distance Functions
//!
//! * Targets ARM NEON, SVE, x86 AVX2, AVX-512 (VNNI, FP16) hardware backends.
//! * Handles `f64` double- and `f32` single-precision, integral, and binary vectors.
//! * Exposes half-precision (`f16`) and brain floating point (`bf16`) types.
//! * Zero-dependency header-only C 99 library with bindings for Rust and other languages.
//!
//! ## Implemented distance functions include:
//!
//! * Euclidean (L2), inner product, and cosine (angular) spatial distances.
//! * Hamming (~ Manhattan) and Jaccard (~ Tanimoto) binary distances.
//! * Kullback-Leibler and Jensen-Shannon divergences for probability distributions.
//!
//! ## Example
//!
//! ```rust
//! use simsimd::SpatialSimilarity;
//!
//! let a = &[1, 2, 3];
//! let b = &[4, 5, 6];
//!
//! // Compute cosine distance
//! let cos_dist = i8::cos(a, b);
//!
//! // Compute dot product distance
//! let dot_product = i8::dot(a, b);
//!
//! // Compute squared Euclidean distance
//! let l2sq_dist = i8::l2sq(a, b);
//!
//! // Optimize performance by flushing denormals
//! simsimd::capabilities::flush_denormals();
//! ```
//!
//! ## Mixed Precision Support
//!
//! ```rust
//! use simsimd::{SpatialSimilarity, f16, bf16};
//!
//! // Work with half-precision floats
//! let half_a: Vec<f16> = vec![1.0, 2.0, 3.0].iter().map(|&x| f16::from_f32(x)).collect();
//! let half_b: Vec<f16> = vec![4.0, 5.0, 6.0].iter().map(|&x| f16::from_f32(x)).collect();
//! let half_cos_dist = f16::cos(&half_a, &half_b);
//!
//! // Work with brain floats
//! let brain_a: Vec<bf16> = vec![1.0, 2.0, 3.0].iter().map(|&x| bf16::from_f32(x)).collect();
//! let brain_b: Vec<bf16> = vec![4.0, 5.0, 6.0].iter().map(|&x| bf16::from_f32(x)).collect();
//! let brain_cos_dist = bf16::cos(&brain_a, &brain_b);
//!
//! // Direct bit manipulation
//! let half = f16::from_f32(3.14);
//! let bits = half.0; // Access raw u16 representation
//! let reconstructed = f16(bits);
//! ```
//!
//! ## Traits
//!
//! The `SpatialSimilarity` trait covers following methods:
//!
//! - `cosine(a: &[Self], b: &[Self]) -> Option<Distance>`: Computes cosine distance (1 - similarity) between two slices.
//! - `dot(a: &[Self], b: &[Self]) -> Option<Distance>`: Computes dot product distance between two slices.
//! - `sqeuclidean(a: &[Self], b: &[Self]) -> Option<Distance>`: Computes squared Euclidean distance between two slices.
//!
//! The `BinarySimilarity` trait covers following methods:
//!
//! - `hamming(a: &[Self], b: &[Self]) -> Option<Distance>`: Computes Hamming distance between two slices.
//! - `jaccard(a: &[Self], b: &[Self]) -> Option<Distance>`: Computes Jaccard distance between two slices.
//!
//! The `ProbabilitySimilarity` trait covers following methods:
//!
//! - `jensenshannon(a: &[Self], b: &[Self]) -> Option<Distance>`: Computes Jensen-Shannon divergence between two slices.
//! - `kullbackleibler(a: &[Self], b: &[Self]) -> Option<Distance>`: Computes Kullback-Leibler divergence between two slices.
//!
#![allow(non_camel_case_types)]
#![cfg_attr(all(not(test), not(feature = "std")), no_std)]
pub type Distance = f64;
pub type ComplexProduct = (f64, f64);
/// Size type used in C FFI to match `simsimd_size_t` which is always `uint64_t`.
/// This is aliased to `u64` instead of `usize` to maintain ABI compatibility across
/// all platforms, including 32-bit architectures where `usize` is 32-bit but the
/// C library expects 64-bit size parameters.
///
/// TODO: In v7, change the C library to use `size_t` and this to `usize`.
type u64size = u64;
/// Compatibility function for pre 1.85 Rust versions lacking `f32::abs`.
#[inline(always)]
fn f32_abs_compat(x: f32) -> f32 {
f32::from_bits(x.to_bits() & 0x7FFF_FFFF)
}
#[link(name = "simsimd")]
extern "C" {
fn simsimd_dot_i8(a: *const i8, b: *const i8, c: u64size, d: *mut Distance);
fn simsimd_dot_f16(a: *const u16, b: *const u16, c: u64size, d: *mut Distance);
fn simsimd_dot_bf16(a: *const u16, b: *const u16, c: u64size, d: *mut Distance);
fn simsimd_dot_f32(a: *const f32, b: *const f32, c: u64size, d: *mut Distance);
fn simsimd_dot_f64(a: *const f64, b: *const f64, c: u64size, d: *mut Distance);
fn simsimd_dot_f16c(a: *const u16, b: *const u16, c: u64size, d: *mut Distance);
fn simsimd_dot_bf16c(a: *const u16, b: *const u16, c: u64size, d: *mut Distance);
fn simsimd_dot_f32c(a: *const f32, b: *const f32, c: u64size, d: *mut Distance);
fn simsimd_dot_f64c(a: *const f64, b: *const f64, c: u64size, d: *mut Distance);
fn simsimd_vdot_f16c(a: *const u16, b: *const u16, c: u64size, d: *mut Distance);
fn simsimd_vdot_bf16c(a: *const u16, b: *const u16, c: u64size, d: *mut Distance);
fn simsimd_vdot_f32c(a: *const f32, b: *const f32, c: u64size, d: *mut Distance);
fn simsimd_vdot_f64c(a: *const f64, b: *const f64, c: u64size, d: *mut Distance);
fn simsimd_cos_i8(a: *const i8, b: *const i8, c: u64size, d: *mut Distance);
fn simsimd_cos_f16(a: *const u16, b: *const u16, c: u64size, d: *mut Distance);
fn simsimd_cos_bf16(a: *const u16, b: *const u16, c: u64size, d: *mut Distance);
fn simsimd_cos_f32(a: *const f32, b: *const f32, c: u64size, d: *mut Distance);
fn simsimd_cos_f64(a: *const f64, b: *const f64, c: u64size, d: *mut Distance);
fn simsimd_l2sq_i8(a: *const i8, b: *const i8, c: u64size, d: *mut Distance);
fn simsimd_l2sq_f16(a: *const u16, b: *const u16, c: u64size, d: *mut Distance);
fn simsimd_l2sq_bf16(a: *const u16, b: *const u16, c: u64size, d: *mut Distance);
fn simsimd_l2sq_f32(a: *const f32, b: *const f32, c: u64size, d: *mut Distance);
fn simsimd_l2sq_f64(a: *const f64, b: *const f64, c: u64size, d: *mut Distance);
fn simsimd_l2_i8(a: *const i8, b: *const i8, c: u64size, d: *mut Distance);
fn simsimd_l2_f16(a: *const u16, b: *const u16, c: u64size, d: *mut Distance);
fn simsimd_l2_bf16(a: *const u16, b: *const u16, c: u64size, d: *mut Distance);
fn simsimd_l2_f32(a: *const f32, b: *const f32, c: u64size, d: *mut Distance);
fn simsimd_l2_f64(a: *const f64, b: *const f64, c: u64size, d: *mut Distance);
fn simsimd_hamming_b8(a: *const u8, b: *const u8, c: u64size, d: *mut Distance);
fn simsimd_jaccard_b8(a: *const u8, b: *const u8, c: u64size, d: *mut Distance);
fn simsimd_js_f16(a: *const u16, b: *const u16, c: u64size, d: *mut Distance);
fn simsimd_js_bf16(a: *const u16, b: *const u16, c: u64size, d: *mut Distance);
fn simsimd_js_f32(a: *const f32, b: *const f32, c: u64size, d: *mut Distance);
fn simsimd_js_f64(a: *const f64, b: *const f64, c: u64size, d: *mut Distance);
fn simsimd_kl_f16(a: *const u16, b: *const u16, c: u64size, d: *mut Distance);
fn simsimd_kl_bf16(a: *const u16, b: *const u16, c: u64size, d: *mut Distance);
fn simsimd_kl_f32(a: *const f32, b: *const f32, c: u64size, d: *mut Distance);
fn simsimd_kl_f64(a: *const f64, b: *const f64, c: u64size, d: *mut Distance);
fn simsimd_intersect_u16(
a: *const u16,
b: *const u16,
a_length: u64size,
b_length: u64size,
d: *mut Distance,
);
fn simsimd_intersect_u32(
a: *const u32,
b: *const u32,
a_length: u64size,
b_length: u64size,
d: *mut Distance,
);
fn simsimd_uses_neon() -> i32;
fn simsimd_uses_neon_f16() -> i32;
fn simsimd_uses_neon_bf16() -> i32;
fn simsimd_uses_neon_i8() -> i32;
fn simsimd_uses_sve() -> i32;
fn simsimd_uses_sve_f16() -> i32;
fn simsimd_uses_sve_bf16() -> i32;
fn simsimd_uses_sve_i8() -> i32;
fn simsimd_uses_haswell() -> i32;
fn simsimd_uses_skylake() -> i32;
fn simsimd_uses_ice() -> i32;
fn simsimd_uses_genoa() -> i32;
fn simsimd_uses_sapphire() -> i32;
fn simsimd_uses_turin() -> i32;
fn simsimd_uses_sierra() -> i32;
fn simsimd_flush_denormals() -> i32;
fn simsimd_uses_dynamic_dispatch() -> i32;
fn simsimd_f32_to_f16(f32_value: f32, result_ptr: *mut u16);
fn simsimd_f16_to_f32(f16_ptr: *const u16) -> f32;
fn simsimd_f32_to_bf16(f32_value: f32, result_ptr: *mut u16);
fn simsimd_bf16_to_f32(bf16_ptr: *const u16) -> f32;
}
/// A half-precision (16-bit) floating point number.
///
/// This type represents IEEE 754 half-precision binary floating-point format.
/// It provides conversion methods to and from f32, and the underlying u16
/// representation is publicly accessible for direct bit manipulation.
///
/// # Examples
///
/// ```
/// use simsimd::f16;
///
/// // Create from f32
/// let half = f16::from_f32(3.14);
///
/// // Convert back to f32
/// let float = half.to_f32();
///
/// // Direct access to bits
/// let bits = half.0;
/// ```
#[repr(transparent)]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct f16(pub u16);
impl f16 {
/// Positive zero.
pub const ZERO: Self = f16(0);
/// Positive one.
pub const ONE: Self = f16(0x3C00);
/// Negative one.
pub const NEG_ONE: Self = f16(0xBC00);
/// Converts an f32 to f16 representation.
///
/// # Examples
///
/// ```
/// use simsimd::f16;
/// let half = f16::from_f32(3.14159);
/// ```
#[inline(always)]
pub fn from_f32(value: f32) -> Self {
let mut result: u16 = 0;
unsafe { simsimd_f32_to_f16(value, &mut result) };
f16(result)
}
/// Converts the f16 to an f32.
///
/// # Examples
///
/// ```
/// use simsimd::f16;
/// let half = f16::from_f32(3.14159);
/// let float = half.to_f32();
/// ```
#[inline(always)]
pub fn to_f32(self) -> f32 {
unsafe { simsimd_f16_to_f32(&self.0) }
}
/// Returns true if this value is NaN.
#[inline(always)]
pub fn is_nan(self) -> bool {
self.to_f32().is_nan()
}
/// Returns true if this value is positive or negative infinity.
#[inline(always)]
pub fn is_infinite(self) -> bool {
self.to_f32().is_infinite()
}
/// Returns true if this number is neither infinite nor NaN.
#[inline(always)]
pub fn is_finite(self) -> bool {
self.to_f32().is_finite()
}
/// Returns the absolute value of self.
#[inline(always)]
pub fn abs(self) -> Self {
Self::from_f32(f32_abs_compat(self.to_f32()))
}
/// Returns the largest integer less than or equal to a number.
///
/// This method is only available when the `std` feature is enabled.
#[cfg(feature = "std")]
#[inline(always)]
pub fn floor(self) -> Self {
Self::from_f32(self.to_f32().floor())
}
/// Returns the smallest integer greater than or equal to a number.
///
/// This method is only available when the `std` feature is enabled.
#[cfg(feature = "std")]
#[inline(always)]
pub fn ceil(self) -> Self {
Self::from_f32(self.to_f32().ceil())
}
/// Returns the nearest integer to a number. Round half-way cases away from 0.0.
///
/// This method is only available when the `std` feature is enabled.
#[cfg(feature = "std")]
#[inline(always)]
pub fn round(self) -> Self {
Self::from_f32(self.to_f32().round())
}
}
#[cfg(feature = "std")]
impl core::fmt::Display for f16 {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
write!(f, "{}", self.to_f32())
}
}
impl core::ops::Add for f16 {
type Output = Self;
#[inline(always)]
fn add(self, rhs: Self) -> Self::Output {
Self::from_f32(self.to_f32() + rhs.to_f32())
}
}
impl core::ops::Sub for f16 {
type Output = Self;
#[inline(always)]
fn sub(self, rhs: Self) -> Self::Output {
Self::from_f32(self.to_f32() - rhs.to_f32())
}
}
impl core::ops::Mul for f16 {
type Output = Self;
#[inline(always)]
fn mul(self, rhs: Self) -> Self::Output {
Self::from_f32(self.to_f32() * rhs.to_f32())
}
}
impl core::ops::Div for f16 {
type Output = Self;
#[inline(always)]
fn div(self, rhs: Self) -> Self::Output {
Self::from_f32(self.to_f32() / rhs.to_f32())
}
}
impl core::ops::Neg for f16 {
type Output = Self;
#[inline(always)]
fn neg(self) -> Self::Output {
Self::from_f32(-self.to_f32())
}
}
impl core::cmp::PartialOrd for f16 {
#[inline(always)]
fn partial_cmp(&self, other: &Self) -> Option<core::cmp::Ordering> {
self.to_f32().partial_cmp(&other.to_f32())
}
}
/// A brain floating point (bfloat16) number.
///
/// This type represents Google's bfloat16 format, which truncates IEEE 754
/// single-precision to 16 bits by keeping the exponent bits but reducing
/// the mantissa. This provides a wider range than f16 but lower precision.
///
/// # Examples
///
/// ```
/// use simsimd::bf16;
///
/// // Create from f32
/// let brain_half = bf16::from_f32(3.14);
///
/// // Convert back to f32
/// let float = brain_half.to_f32();
///
/// // Direct access to bits
/// let bits = brain_half.0;
/// ```
#[repr(transparent)]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct bf16(pub u16);
impl bf16 {
/// Positive zero.
pub const ZERO: Self = bf16(0);
/// Positive one.
pub const ONE: Self = bf16(0x3F80);
/// Negative one.
pub const NEG_ONE: Self = bf16(0xBF80);
/// Converts an f32 to bf16 representation.
///
/// # Examples
///
/// ```
/// use simsimd::bf16;
/// let brain_half = bf16::from_f32(3.14159);
/// ```
#[inline(always)]
pub fn from_f32(value: f32) -> Self {
let mut result: u16 = 0;
unsafe { simsimd_f32_to_bf16(value, &mut result) };
bf16(result)
}
/// Converts the bf16 to an f32.
///
/// # Examples
///
/// ```
/// use simsimd::bf16;
/// let brain_half = bf16::from_f32(3.14159);
/// let float = brain_half.to_f32();
/// ```
#[inline(always)]
pub fn to_f32(self) -> f32 {
unsafe { simsimd_bf16_to_f32(&self.0) }
}
/// Returns true if this value is NaN.
#[inline(always)]
pub fn is_nan(self) -> bool {
self.to_f32().is_nan()
}
/// Returns true if this value is positive or negative infinity.
#[inline(always)]
pub fn is_infinite(self) -> bool {
self.to_f32().is_infinite()
}
/// Returns true if this number is neither infinite nor NaN.
#[inline(always)]
pub fn is_finite(self) -> bool {
self.to_f32().is_finite()
}
/// Returns the absolute value of self.
#[inline(always)]
pub fn abs(self) -> Self {
Self::from_f32(f32_abs_compat(self.to_f32()))
}
/// Returns the largest integer less than or equal to a number.
///
/// This method is only available when the `std` feature is enabled.
#[cfg(feature = "std")]
#[inline(always)]
pub fn floor(self) -> Self {
Self::from_f32(self.to_f32().floor())
}
/// Returns the smallest integer greater than or equal to a number.
///
/// This method is only available when the `std` feature is enabled.
#[cfg(feature = "std")]
#[inline(always)]
pub fn ceil(self) -> Self {
Self::from_f32(self.to_f32().ceil())
}
/// Returns the nearest integer to a number. Round half-way cases away from 0.0.
///
/// This method is only available when the `std` feature is enabled.
#[cfg(feature = "std")]
#[inline(always)]
pub fn round(self) -> Self {
Self::from_f32(self.to_f32().round())
}
}
#[cfg(feature = "std")]
impl core::fmt::Display for bf16 {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
write!(f, "{}", self.to_f32())
}
}
impl core::ops::Add for bf16 {
type Output = Self;
#[inline(always)]
fn add(self, rhs: Self) -> Self::Output {
Self::from_f32(self.to_f32() + rhs.to_f32())
}
}
impl core::ops::Sub for bf16 {
type Output = Self;
#[inline(always)]
fn sub(self, rhs: Self) -> Self::Output {
Self::from_f32(self.to_f32() - rhs.to_f32())
}
}
impl core::ops::Mul for bf16 {
type Output = Self;
#[inline(always)]
fn mul(self, rhs: Self) -> Self::Output {
Self::from_f32(self.to_f32() * rhs.to_f32())
}
}
impl core::ops::Div for bf16 {
type Output = Self;
#[inline(always)]
fn div(self, rhs: Self) -> Self::Output {
Self::from_f32(self.to_f32() / rhs.to_f32())
}
}
impl core::ops::Neg for bf16 {
type Output = Self;
#[inline(always)]
fn neg(self) -> Self::Output {
Self::from_f32(-self.to_f32())
}
}
impl core::cmp::PartialOrd for bf16 {
#[inline(always)]
fn partial_cmp(&self, other: &Self) -> Option<core::cmp::Ordering> {
self.to_f32().partial_cmp(&other.to_f32())
}
}
/// The `capabilities` module provides functions for detecting the hardware features
/// available on the current system.
pub mod capabilities {
pub fn uses_neon() -> bool {
unsafe { crate::simsimd_uses_neon() != 0 }
}
pub fn uses_neon_f16() -> bool {
unsafe { crate::simsimd_uses_neon_f16() != 0 }
}
pub fn uses_neon_bf16() -> bool {
unsafe { crate::simsimd_uses_neon_bf16() != 0 }
}
pub fn uses_neon_i8() -> bool {
unsafe { crate::simsimd_uses_neon_i8() != 0 }
}
pub fn uses_sve() -> bool {
unsafe { crate::simsimd_uses_sve() != 0 }
}
pub fn uses_sve_f16() -> bool {
unsafe { crate::simsimd_uses_sve_f16() != 0 }
}
pub fn uses_sve_bf16() -> bool {
unsafe { crate::simsimd_uses_sve_bf16() != 0 }
}
pub fn uses_sve_i8() -> bool {
unsafe { crate::simsimd_uses_sve_i8() != 0 }
}
pub fn uses_haswell() -> bool {
unsafe { crate::simsimd_uses_haswell() != 0 }
}
pub fn uses_skylake() -> bool {
unsafe { crate::simsimd_uses_skylake() != 0 }
}
pub fn uses_ice() -> bool {
unsafe { crate::simsimd_uses_ice() != 0 }
}
pub fn uses_genoa() -> bool {
unsafe { crate::simsimd_uses_genoa() != 0 }
}
pub fn uses_sapphire() -> bool {
unsafe { crate::simsimd_uses_sapphire() != 0 }
}
pub fn uses_turin() -> bool {
unsafe { crate::simsimd_uses_turin() != 0 }
}
pub fn uses_sierra() -> bool {
unsafe { crate::simsimd_uses_sierra() != 0 }
}
/// Flushes denormalized numbers to zero on the current CPU architecture.
///
/// This function should be called on each thread before any SIMD operations
/// to avoid performance penalties. When facing denormalized values,
/// Fused-Multiply-Add (FMA) operations can be up to 30x slower.
///
/// # Returns
///
/// Returns `true` if the operation was successful, `false` otherwise.
pub fn flush_denormals() -> bool {
unsafe { crate::simsimd_flush_denormals() != 0 }
}
/// Checks if the library is using dynamic dispatch for function selection.
///
/// # Returns
///
/// Returns `true` when the C backend is compiled with dynamic dispatch
/// (default for this crate via `build.rs`), otherwise `false`.
pub fn uses_dynamic_dispatch() -> bool {
unsafe { crate::simsimd_uses_dynamic_dispatch() != 0 }
}
}
/// `SpatialSimilarity` provides a set of trait methods for computing similarity
/// or distance between spatial data vectors in SIMD (Single Instruction, Multiple Data) context.
/// These methods can be used to calculate metrics like cosine distance, dot product,
/// and squared Euclidean distance between two slices of data.
///
/// Each method takes two slices of data (a and b) and returns an Option<Distance>.
/// The result is `None` if the slices are not of the same length, as these operations
/// require one-to-one correspondence between the elements of the slices.
/// Otherwise, it returns the computed similarity or distance as `Some(f64)`.
/// Convenience methods like `cosine`/`sqeuclidean` delegate to the core methods
/// `cos`/`l2sq` implemented by this trait.
pub trait SpatialSimilarity
where
Self: Sized,
{
/// Computes the cosine distance between two slices.
/// The cosine distance is 1 minus the cosine similarity between two non-zero vectors
/// of an dot product space that measures the cosine of the angle between them.
fn cos(a: &[Self], b: &[Self]) -> Option<Distance>;
/// Computes the inner product (also known as dot product) between two slices.
/// The dot product is the sum of the products of the corresponding entries
/// of the two sequences of numbers.
fn dot(a: &[Self], b: &[Self]) -> Option<Distance>;
/// Computes the squared Euclidean distance between two slices.
/// The squared Euclidean distance is the sum of the squared differences
/// between corresponding elements of the two slices.
fn l2sq(a: &[Self], b: &[Self]) -> Option<Distance>;
/// Computes the Euclidean distance between two slices.
/// The Euclidean distance is the square root of
// sum of the squared differences between corresponding
/// elements of the two slices.
fn l2(a: &[Self], b: &[Self]) -> Option<Distance>;
/// Computes the squared Euclidean distance between two slices.
/// The squared Euclidean distance is the sum of the squared differences
/// between corresponding elements of the two slices.
fn sqeuclidean(a: &[Self], b: &[Self]) -> Option<Distance> {
SpatialSimilarity::l2sq(a, b)
}
/// Computes the Euclidean distance between two slices.
/// The Euclidean distance is the square root of the
/// sum of the squared differences between corresponding
/// elements of the two slices.
fn euclidean(a: &[Self], b: &[Self]) -> Option<Distance> {
SpatialSimilarity::l2(a, b)
}
/// Computes the squared Euclidean distance between two slices.
/// The squared Euclidean distance is the sum of the squared differences
/// between corresponding elements of the two slices.
fn inner(a: &[Self], b: &[Self]) -> Option<Distance> {
SpatialSimilarity::dot(a, b)
}
/// Computes the cosine distance between two slices.
/// The cosine distance is 1 minus the cosine similarity between two non-zero vectors
/// of an dot product space that measures the cosine of the angle between them.
fn cosine(a: &[Self], b: &[Self]) -> Option<Distance> {
SpatialSimilarity::cos(a, b)
}
}
/// `BinarySimilarity` provides trait methods for computing similarity metrics
/// that are commonly used with binary data vectors, such as Hamming distance
/// and Jaccard index.
///
/// The methods accept two slices of binary data and return an Option<Distance>
/// indicating the computed similarity or distance, with `None` returned if the
/// slices differ in length.
pub trait BinarySimilarity
where
Self: Sized,
{
/// Computes the Hamming distance between two binary data slices.
/// The Hamming distance between two strings of equal length is the number of
/// bits at which the corresponding values are different.
fn hamming(a: &[Self], b: &[Self]) -> Option<Distance>;
/// Computes the Jaccard index between two bitsets represented by binary data slices.
/// The Jaccard index, also known as the Jaccard similarity coefficient, is a statistic
/// used for gauging the similarity and diversity of sample sets.
fn jaccard(a: &[Self], b: &[Self]) -> Option<Distance>;
}
/// `ProbabilitySimilarity` provides trait methods for computing similarity or divergence
/// measures between probability distributions, such as the Jensen-Shannon divergence
/// and the Kullback-Leibler divergence.
///
/// These methods are particularly useful in contexts such as information theory and
/// machine learning, where one often needs to measure how one probability distribution
/// differs from a second, reference probability distribution.
pub trait ProbabilitySimilarity
where
Self: Sized,
{
/// Computes the Jensen-Shannon divergence between two probability distributions.
/// The Jensen-Shannon divergence is a method of measuring the similarity between
/// two probability distributions. It is based on the Kullback-Leibler divergence,
/// but is symmetric and always has a finite value.
fn jensenshannon(a: &[Self], b: &[Self]) -> Option<Distance>;
/// Computes the Kullback-Leibler divergence between two probability distributions.
/// The Kullback-Leibler divergence is a measure of how one probability distribution
/// diverges from a second, expected probability distribution.
fn kullbackleibler(a: &[Self], b: &[Self]) -> Option<Distance>;
}
/// `ComplexProducts` provides trait methods for computing products between
/// complex number vectors. This includes standard and Hermitian dot products.
pub trait ComplexProducts
where
Self: Sized,
{
/// Computes the dot product between two complex number vectors.
fn dot(a: &[Self], b: &[Self]) -> Option<ComplexProduct>;
/// Computes the Hermitian dot product (conjugate dot product) between two complex number vectors.
fn vdot(a: &[Self], b: &[Self]) -> Option<ComplexProduct>;
}
/// `Sparse` provides trait methods for sparse vectors.
pub trait Sparse
where
Self: Sized,
{
/// Computes the number of common elements between two sparse vectors.
/// both vectors must be sorted in ascending order.
fn intersect(a: &[Self], b: &[Self]) -> Option<Distance>;
}
impl BinarySimilarity for u8 {
fn hamming(a: &[Self], b: &[Self]) -> Option<Distance> {
if a.len() != b.len() {
return None;
}
let mut distance_value: Distance = 0.0;
let distance_ptr: *mut Distance = &mut distance_value as *mut Distance;
unsafe { simsimd_hamming_b8(a.as_ptr(), b.as_ptr(), a.len() as u64size, distance_ptr) };
Some(distance_value)
}
fn jaccard(a: &[Self], b: &[Self]) -> Option<Distance> {
if a.len() != b.len() {
return None;
}
let mut distance_value: Distance = 0.0;
let distance_ptr: *mut Distance = &mut distance_value as *mut Distance;
unsafe { simsimd_jaccard_b8(a.as_ptr(), b.as_ptr(), a.len() as u64size, distance_ptr) };
Some(distance_value)
}
}
impl SpatialSimilarity for i8 {
fn cos(a: &[Self], b: &[Self]) -> Option<Distance> {
if a.len() != b.len() {
return None;
}
let mut distance_value: Distance = 0.0;
let distance_ptr: *mut Distance = &mut distance_value as *mut Distance;
unsafe { simsimd_cos_i8(a.as_ptr(), b.as_ptr(), a.len() as u64size, distance_ptr) };
Some(distance_value)
}
fn dot(a: &[Self], b: &[Self]) -> Option<Distance> {
if a.len() != b.len() {
return None;
}
let mut distance_value: Distance = 0.0;
let distance_ptr: *mut Distance = &mut distance_value as *mut Distance;
unsafe { simsimd_dot_i8(a.as_ptr(), b.as_ptr(), a.len() as u64size, distance_ptr) };
Some(distance_value)
}
fn l2sq(a: &[Self], b: &[Self]) -> Option<Distance> {
if a.len() != b.len() {
return None;
}
let mut distance_value: Distance = 0.0;
let distance_ptr: *mut Distance = &mut distance_value as *mut Distance;
unsafe { simsimd_l2sq_i8(a.as_ptr(), b.as_ptr(), a.len() as u64size, distance_ptr) };
Some(distance_value)
}
fn l2(a: &[Self], b: &[Self]) -> Option<Distance> {
if a.len() != b.len() {
return None;
}
let mut distance_value: Distance = 0.0;
let distance_ptr: *mut Distance = &mut distance_value as *mut Distance;
unsafe { simsimd_l2_i8(a.as_ptr(), b.as_ptr(), a.len() as u64size, distance_ptr) };
Some(distance_value)
}
}
impl Sparse for u16 {
fn intersect(a: &[Self], b: &[Self]) -> Option<Distance> {
let mut distance_value: Distance = 0.0;
let distance_ptr: *mut Distance = &mut distance_value as *mut Distance;
unsafe {
simsimd_intersect_u16(
a.as_ptr(),
b.as_ptr(),
a.len() as u64size,
b.len() as u64size,
distance_ptr,
)
};
Some(distance_value)
}
}
impl Sparse for u32 {
fn intersect(a: &[Self], b: &[Self]) -> Option<Distance> {
let mut distance_value: Distance = 0.0;
let distance_ptr: *mut Distance = &mut distance_value as *mut Distance;
unsafe {
simsimd_intersect_u32(
a.as_ptr(),
b.as_ptr(),
a.len() as u64size,
b.len() as u64size,
distance_ptr,
)
};
Some(distance_value)
}
}
impl SpatialSimilarity for f16 {
fn cos(a: &[Self], b: &[Self]) -> Option<Distance> {
if a.len() != b.len() {
return None;
}
// Explicitly cast `*const f16` to `*const u16`
let a_ptr = a.as_ptr() as *const u16;
let b_ptr = b.as_ptr() as *const u16;
let mut distance_value: Distance = 0.0;
let distance_ptr: *mut Distance = &mut distance_value as *mut Distance;
unsafe { simsimd_cos_f16(a_ptr, b_ptr, a.len() as u64size, distance_ptr) };
Some(distance_value)
}
fn dot(a: &[Self], b: &[Self]) -> Option<Distance> {
if a.len() != b.len() {
return None;
}
// Explicitly cast `*const f16` to `*const u16`
let a_ptr = a.as_ptr() as *const u16;
let b_ptr = b.as_ptr() as *const u16;
let mut distance_value: Distance = 0.0;
let distance_ptr: *mut Distance = &mut distance_value as *mut Distance;
unsafe { simsimd_dot_f16(a_ptr, b_ptr, a.len() as u64size, distance_ptr) };
Some(distance_value)
}
fn l2sq(a: &[Self], b: &[Self]) -> Option<Distance> {
if a.len() != b.len() {
return None;
}
// Explicitly cast `*const f16` to `*const u16`
let a_ptr = a.as_ptr() as *const u16;
let b_ptr = b.as_ptr() as *const u16;
let mut distance_value: Distance = 0.0;
let distance_ptr: *mut Distance = &mut distance_value as *mut Distance;
unsafe { simsimd_l2sq_f16(a_ptr, b_ptr, a.len() as u64size, distance_ptr) };
Some(distance_value)
}
fn l2(a: &[Self], b: &[Self]) -> Option<Distance> {
if a.len() != b.len() {
return None;
}
// Explicitly cast `*const f16` to `*const u16`
let a_ptr = a.as_ptr() as *const u16;
let b_ptr = b.as_ptr() as *const u16;
let mut distance_value: Distance = 0.0;
let distance_ptr: *mut Distance = &mut distance_value as *mut Distance;
unsafe { simsimd_l2_f16(a_ptr, b_ptr, a.len() as u64size, distance_ptr) };
Some(distance_value)
}
}
impl SpatialSimilarity for bf16 {
fn cos(a: &[Self], b: &[Self]) -> Option<Distance> {
if a.len() != b.len() {
return None;
}
// Explicitly cast `*const bf16` to `*const u16`
let a_ptr = a.as_ptr() as *const u16;
let b_ptr = b.as_ptr() as *const u16;
let mut distance_value: Distance = 0.0;
let distance_ptr: *mut Distance = &mut distance_value as *mut Distance;
unsafe { simsimd_cos_bf16(a_ptr, b_ptr, a.len() as u64size, distance_ptr) };
Some(distance_value)
}
fn dot(a: &[Self], b: &[Self]) -> Option<Distance> {
if a.len() != b.len() {
return None;
}
// Explicitly cast `*const bf16` to `*const u16`
let a_ptr = a.as_ptr() as *const u16;
let b_ptr = b.as_ptr() as *const u16;
let mut distance_value: Distance = 0.0;
let distance_ptr: *mut Distance = &mut distance_value as *mut Distance;
unsafe { simsimd_dot_bf16(a_ptr, b_ptr, a.len() as u64size, distance_ptr) };
Some(distance_value)
}
fn l2sq(a: &[Self], b: &[Self]) -> Option<Distance> {
if a.len() != b.len() {
return None;
}
// Explicitly cast `*const bf16` to `*const u16`
let a_ptr = a.as_ptr() as *const u16;
let b_ptr = b.as_ptr() as *const u16;
let mut distance_value: Distance = 0.0;
let distance_ptr: *mut Distance = &mut distance_value as *mut Distance;
unsafe { simsimd_l2sq_bf16(a_ptr, b_ptr, a.len() as u64size, distance_ptr) };
Some(distance_value)
}
fn l2(a: &[Self], b: &[Self]) -> Option<Distance> {
if a.len() != b.len() {
return None;
}
// Explicitly cast `*const bf16` to `*const u16`
let a_ptr = a.as_ptr() as *const u16;
let b_ptr = b.as_ptr() as *const u16;
let mut distance_value: Distance = 0.0;
let distance_ptr: *mut Distance = &mut distance_value as *mut Distance;
unsafe { simsimd_l2_bf16(a_ptr, b_ptr, a.len() as u64size, distance_ptr) };
Some(distance_value)
}
}
impl SpatialSimilarity for f32 {
fn cos(a: &[Self], b: &[Self]) -> Option<Distance> {
if a.len() != b.len() {
return None;
}
let mut distance_value: Distance = 0.0;
let distance_ptr: *mut Distance = &mut distance_value as *mut Distance;
unsafe { simsimd_cos_f32(a.as_ptr(), b.as_ptr(), a.len() as u64size, distance_ptr) };
Some(distance_value)
}
fn dot(a: &[Self], b: &[Self]) -> Option<Distance> {
if a.len() != b.len() {
return None;
}
let mut distance_value: Distance = 0.0;
let distance_ptr: *mut Distance = &mut distance_value as *mut Distance;
unsafe { simsimd_dot_f32(a.as_ptr(), b.as_ptr(), a.len() as u64size, distance_ptr) };
Some(distance_value)
}
fn l2sq(a: &[Self], b: &[Self]) -> Option<Distance> {
if a.len() != b.len() {
return None;
}
let mut distance_value: Distance = 0.0;
let distance_ptr: *mut Distance = &mut distance_value as *mut Distance;
unsafe { simsimd_l2sq_f32(a.as_ptr(), b.as_ptr(), a.len() as u64size, distance_ptr) };
Some(distance_value)
}
fn l2(a: &[Self], b: &[Self]) -> Option<Distance> {
if a.len() != b.len() {
return None;
}
let mut distance_value: Distance = 0.0;
let distance_ptr: *mut Distance = &mut distance_value as *mut Distance;
unsafe { simsimd_l2_f32(a.as_ptr(), b.as_ptr(), a.len() as u64size, distance_ptr) };
Some(distance_value)
}
}
impl SpatialSimilarity for f64 {
fn cos(a: &[Self], b: &[Self]) -> Option<Distance> {
if a.len() != b.len() {
return None;
}
let mut distance_value: Distance = 0.0;
let distance_ptr: *mut Distance = &mut distance_value as *mut Distance;
unsafe { simsimd_cos_f64(a.as_ptr(), b.as_ptr(), a.len() as u64size, distance_ptr) };
Some(distance_value)
}
fn dot(a: &[Self], b: &[Self]) -> Option<Distance> {
if a.len() != b.len() {
return None;
}
let mut distance_value: Distance = 0.0;
let distance_ptr: *mut Distance = &mut distance_value as *mut Distance;
unsafe { simsimd_dot_f64(a.as_ptr(), b.as_ptr(), a.len() as u64size, distance_ptr) };
Some(distance_value)
}
fn l2sq(a: &[Self], b: &[Self]) -> Option<Distance> {
if a.len() != b.len() {
return None;
}
let mut distance_value: Distance = 0.0;
let distance_ptr: *mut Distance = &mut distance_value as *mut Distance;
unsafe { simsimd_l2sq_f64(a.as_ptr(), b.as_ptr(), a.len() as u64size, distance_ptr) };
Some(distance_value)
}
fn l2(a: &[Self], b: &[Self]) -> Option<Distance> {
if a.len() != b.len() {
return None;
}
let mut distance_value: Distance = 0.0;
let distance_ptr: *mut Distance = &mut distance_value as *mut Distance;
unsafe { simsimd_l2_f64(a.as_ptr(), b.as_ptr(), a.len() as u64size, distance_ptr) };
Some(distance_value)
}
}
impl ProbabilitySimilarity for f16 {
fn jensenshannon(a: &[Self], b: &[Self]) -> Option<Distance> {
if a.len() != b.len() {
return None;
}
// Explicitly cast `*const f16` to `*const u16`
let a_ptr = a.as_ptr() as *const u16;
let b_ptr = b.as_ptr() as *const u16;
let mut distance_value: Distance = 0.0;
let distance_ptr: *mut Distance = &mut distance_value as *mut Distance;
unsafe { simsimd_js_f16(a_ptr, b_ptr, a.len() as u64size, distance_ptr) };
Some(distance_value)
}
fn kullbackleibler(a: &[Self], b: &[Self]) -> Option<Distance> {
if a.len() != b.len() {
return None;
}
// Explicitly cast `*const f16` to `*const u16`
let a_ptr = a.as_ptr() as *const u16;
let b_ptr = b.as_ptr() as *const u16;
let mut distance_value: Distance = 0.0;
let distance_ptr: *mut Distance = &mut distance_value as *mut Distance;
unsafe { simsimd_kl_f16(a_ptr, b_ptr, a.len() as u64size, distance_ptr) };
Some(distance_value)
}
}
impl ProbabilitySimilarity for bf16 {
fn jensenshannon(a: &[Self], b: &[Self]) -> Option<Distance> {
if a.len() != b.len() {
return None;
}
// Explicitly cast `*const bf16` to `*const u16`
let a_ptr = a.as_ptr() as *const u16;
let b_ptr = b.as_ptr() as *const u16;
let mut distance_value: Distance = 0.0;
let distance_ptr: *mut Distance = &mut distance_value as *mut Distance;
unsafe { simsimd_js_bf16(a_ptr, b_ptr, a.len() as u64size, distance_ptr) };
Some(distance_value)
}
fn kullbackleibler(a: &[Self], b: &[Self]) -> Option<Distance> {
if a.len() != b.len() {
return None;
}
// Explicitly cast `*const bf16` to `*const u16`
let a_ptr = a.as_ptr() as *const u16;
let b_ptr = b.as_ptr() as *const u16;
let mut distance_value: Distance = 0.0;
let distance_ptr: *mut Distance = &mut distance_value as *mut Distance;
unsafe { simsimd_kl_bf16(a_ptr, b_ptr, a.len() as u64size, distance_ptr) };
Some(distance_value)
}
}
impl ProbabilitySimilarity for f32 {
fn jensenshannon(a: &[Self], b: &[Self]) -> Option<Distance> {
if a.len() != b.len() {
return None;
}
let mut distance_value: Distance = 0.0;
let distance_ptr: *mut Distance = &mut distance_value as *mut Distance;
unsafe { simsimd_js_f32(a.as_ptr(), b.as_ptr(), a.len() as u64size, distance_ptr) };
Some(distance_value)
}
fn kullbackleibler(a: &[Self], b: &[Self]) -> Option<Distance> {
if a.len() != b.len() {
return None;
}
let mut distance_value: Distance = 0.0;
let distance_ptr: *mut Distance = &mut distance_value as *mut Distance;
unsafe { simsimd_kl_f32(a.as_ptr(), b.as_ptr(), a.len() as u64size, distance_ptr) };
Some(distance_value)
}
}
impl ProbabilitySimilarity for f64 {
fn jensenshannon(a: &[Self], b: &[Self]) -> Option<Distance> {
if a.len() != b.len() {
return None;
}
let mut distance_value: Distance = 0.0;
let distance_ptr: *mut Distance = &mut distance_value as *mut Distance;
unsafe { simsimd_js_f64(a.as_ptr(), b.as_ptr(), a.len() as u64size, distance_ptr) };
Some(distance_value)
}
fn kullbackleibler(a: &[Self], b: &[Self]) -> Option<Distance> {
if a.len() != b.len() {
return None;
}
let mut distance_value: Distance = 0.0;
let distance_ptr: *mut Distance = &mut distance_value as *mut Distance;
unsafe { simsimd_kl_f64(a.as_ptr(), b.as_ptr(), a.len() as u64size, distance_ptr) };
Some(distance_value)
}
}
impl ComplexProducts for f16 {
fn dot(a: &[Self], b: &[Self]) -> Option<ComplexProduct> {
if a.len() != b.len() || a.len() % 2 != 0 {
return None;
}
// Prepare the output array where the real and imaginary parts will be stored
let mut product: [Distance; 2] = [0.0, 0.0];
let product_ptr: *mut Distance = &mut product[0] as *mut _;
// Explicitly cast `*const f16` to `*const u16`
let a_ptr = a.as_ptr() as *const u16;
let b_ptr = b.as_ptr() as *const u16;
// The C function expects the number of complex pairs, not the total number of f16 elements
unsafe { simsimd_dot_f16c(a_ptr, b_ptr, a.len() as u64size / 2, product_ptr) };
Some((product[0], product[1]))
}
fn vdot(a: &[Self], b: &[Self]) -> Option<ComplexProduct> {
if a.len() != b.len() || a.len() % 2 != 0 {
return None;
}
let mut product: [Distance; 2] = [0.0, 0.0];
let product_ptr: *mut Distance = &mut product[0] as *mut _;
let a_ptr = a.as_ptr() as *const u16;
let b_ptr = b.as_ptr() as *const u16;
// The C function expects the number of complex pairs, not the total number of f16 elements
unsafe { simsimd_vdot_f16c(a_ptr, b_ptr, a.len() as u64size / 2, product_ptr) };
Some((product[0], product[1]))
}
}
impl ComplexProducts for bf16 {
fn dot(a: &[Self], b: &[Self]) -> Option<ComplexProduct> {
if a.len() != b.len() || a.len() % 2 != 0 {
return None;
}
// Prepare the output array where the real and imaginary parts will be stored
let mut product: [Distance; 2] = [0.0, 0.0];
let product_ptr: *mut Distance = &mut product[0] as *mut _;
// Explicitly cast `*const bf16` to `*const u16`
let a_ptr = a.as_ptr() as *const u16;
let b_ptr = b.as_ptr() as *const u16;
// The C function expects the number of complex pairs, not the total number of bf16 elements
unsafe { simsimd_dot_bf16c(a_ptr, b_ptr, a.len() as u64size / 2, product_ptr) };
Some((product[0], product[1]))
}
fn vdot(a: &[Self], b: &[Self]) -> Option<ComplexProduct> {
if a.len() != b.len() || a.len() % 2 != 0 {
return None;
}
// Prepare the output array where the real and imaginary parts will be stored
let mut product: [Distance; 2] = [0.0, 0.0];
let product_ptr: *mut Distance = &mut product[0] as *mut _;
// Explicitly cast `*const bf16` to `*const u16`
let a_ptr = a.as_ptr() as *const u16;
let b_ptr = b.as_ptr() as *const u16;
// The C function expects the number of complex pairs, not the total number of bf16 elements
unsafe { simsimd_vdot_bf16c(a_ptr, b_ptr, a.len() as u64size / 2, product_ptr) };
Some((product[0], product[1]))
}
}
impl ComplexProducts for f32 {
fn dot(a: &[Self], b: &[Self]) -> Option<ComplexProduct> {
if a.len() != b.len() || a.len() % 2 != 0 {
return None;
}
let mut product: [Distance; 2] = [0.0, 0.0];
let product_ptr: *mut Distance = &mut product[0] as *mut _;
// The C function expects the number of complex pairs, not the total number of floats
unsafe { simsimd_dot_f32c(a.as_ptr(), b.as_ptr(), a.len() as u64size / 2, product_ptr) };
Some((product[0], product[1]))
}
fn vdot(a: &[Self], b: &[Self]) -> Option<ComplexProduct> {
if a.len() != b.len() || a.len() % 2 != 0 {
return None;
}
let mut product: [Distance; 2] = [0.0, 0.0];
let product_ptr: *mut Distance = &mut product[0] as *mut _;
// The C function expects the number of complex pairs, not the total number of floats
unsafe { simsimd_vdot_f32c(a.as_ptr(), b.as_ptr(), a.len() as u64size / 2, product_ptr) };
Some((product[0], product[1]))
}
}
impl ComplexProducts for f64 {
fn dot(a: &[Self], b: &[Self]) -> Option<ComplexProduct> {
if a.len() != b.len() || a.len() % 2 != 0 {
return None;
}
let mut product: [Distance; 2] = [0.0, 0.0];
let product_ptr: *mut Distance = &mut product[0] as *mut _;
// The C function expects the number of complex pairs, not the total number of floats
unsafe { simsimd_dot_f64c(a.as_ptr(), b.as_ptr(), a.len() as u64size / 2, product_ptr) };
Some((product[0], product[1]))
}
fn vdot(a: &[Self], b: &[Self]) -> Option<ComplexProduct> {
if a.len() != b.len() || a.len() % 2 != 0 {
return None;
}
let mut product: [Distance; 2] = [0.0, 0.0];
let product_ptr: *mut Distance = &mut product[0] as *mut _;
// The C function expects the number of complex pairs, not the total number of floats
unsafe { simsimd_vdot_f64c(a.as_ptr(), b.as_ptr(), a.len() as u64size / 2, product_ptr) };
Some((product[0], product[1]))
}
}
#[cfg(test)]
mod tests {
use super::*;
use half::bf16 as HalfBF16;
use half::f16 as HalfF16;
#[test]
fn hardware_features_detection() {
let uses_arm = capabilities::uses_neon() || capabilities::uses_sve();
let uses_x86 = capabilities::uses_haswell()
|| capabilities::uses_skylake()
|| capabilities::uses_ice()
|| capabilities::uses_genoa()
|| capabilities::uses_sapphire()
|| capabilities::uses_turin();
// The CPU can't simultaneously support ARM and x86 SIMD extensions
if uses_arm {
assert!(!uses_x86);
}
if uses_x86 {
assert!(!uses_arm);
}
println!("- uses_neon: {}", capabilities::uses_neon());
println!("- uses_neon_f16: {}", capabilities::uses_neon_f16());
println!("- uses_neon_bf16: {}", capabilities::uses_neon_bf16());
println!("- uses_neon_i8: {}", capabilities::uses_neon_i8());
println!("- uses_sve: {}", capabilities::uses_sve());
println!("- uses_sve_f16: {}", capabilities::uses_sve_f16());
println!("- uses_sve_bf16: {}", capabilities::uses_sve_bf16());
println!("- uses_sve_i8: {}", capabilities::uses_sve_i8());
println!("- uses_haswell: {}", capabilities::uses_haswell());
println!("- uses_skylake: {}", capabilities::uses_skylake());
println!("- uses_ice: {}", capabilities::uses_ice());
println!("- uses_genoa: {}", capabilities::uses_genoa());
println!("- uses_sapphire: {}", capabilities::uses_sapphire());
println!("- uses_turin: {}", capabilities::uses_turin());
println!("- uses_sierra: {}", capabilities::uses_sierra());
}
//
fn assert_almost_equal(left: Distance, right: Distance, tolerance: Distance) {
let lower = right - tolerance;
let upper = right + tolerance;
assert!(left >= lower && left <= upper);
}
#[test]
fn cos_i8() {
let a = &[3, 97, 127];
let b = &[3, 97, 127];
if let Some(result) = SpatialSimilarity::cosine(a, b) {
assert_almost_equal(0.00012027938, result, 0.01);
}
}
#[test]
fn cos_f32() {
let a = &[1.0, 2.0, 3.0];
let b = &[4.0, 5.0, 6.0];
if let Some(result) = SpatialSimilarity::cosine(a, b) {
assert_almost_equal(0.025, result, 0.01);
}
}
#[test]
fn dot_i8() {
let a = &[1, 2, 3];
let b = &[4, 5, 6];
if let Some(result) = SpatialSimilarity::dot(a, b) {
assert_almost_equal(32.0, result, 0.01);
}
}
#[test]
fn dot_f32() {
let a = &[1.0, 2.0, 3.0];
let b = &[4.0, 5.0, 6.0];
if let Some(result) = SpatialSimilarity::dot(a, b) {
assert_almost_equal(32.0, result, 0.01);
}
}
#[test]
fn dot_f32_complex() {
// Let's consider these as complex numbers where every pair is (real, imaginary)
let a: &[f32; 4] = &[1.0, 2.0, 3.0, 4.0]; // Represents two complex numbers: 1+2i, 3+4i
let b: &[f32; 4] = &[5.0, 6.0, 7.0, 8.0]; // Represents two complex numbers: 5+6i, 7+8i
if let Some((real, imag)) = ComplexProducts::dot(a, b) {
assert_almost_equal(-18.0, real, 0.01);
assert_almost_equal(68.0, imag, 0.01);
}
}
#[test]
fn vdot_f32_complex() {
// Here we're assuming a similar setup to the previous test, but for the Hermitian (conjugate) dot product
let a: &[f32; 4] = &[1.0, 2.0, 3.0, 4.0]; // Represents two complex numbers: 1+2i, 3+4i
let b: &[f32; 4] = &[5.0, 6.0, 7.0, 8.0]; // Represents two complex numbers: 5+6i, 7+8i
if let Some((real, imag)) = ComplexProducts::vdot(a, b) {
assert_almost_equal(70.0, real, 0.01);
assert_almost_equal(-8.0, imag, 0.01);
}
}
#[test]
fn l2sq_i8() {
let a = &[1, 2, 3];
let b = &[4, 5, 6];
if let Some(result) = SpatialSimilarity::sqeuclidean(a, b) {
assert_almost_equal(27.0, result, 0.01);
}
}
#[test]
fn l2sq_f32() {
let a = &[1.0, 2.0, 3.0];
let b = &[4.0, 5.0, 6.0];
if let Some(result) = SpatialSimilarity::sqeuclidean(a, b) {
assert_almost_equal(27.0, result, 0.01);
}
}
#[test]
fn l2_f32() {
let a: &[f32; 3] = &[1.0, 2.0, 3.0];
let b: &[f32; 3] = &[4.0, 5.0, 6.0];
if let Some(result) = SpatialSimilarity::euclidean(a, b) {
assert_almost_equal(5.2, result, 0.01);
}
}
#[test]
fn l2_f64() {
let a: &[f64; 3] = &[1.0, 2.0, 3.0];
let b: &[f64; 3] = &[4.0, 5.0, 6.0];
if let Some(result) = SpatialSimilarity::euclidean(a, b) {
assert_almost_equal(5.2, result, 0.01);
}
}
#[test]
fn l2_f16() {
let a_half: Vec<HalfF16> = vec![1.0, 2.0, 3.0]
.iter()
.map(|&x| HalfF16::from_f32(x))
.collect();
let b_half: Vec<HalfF16> = vec![4.0, 5.0, 6.0]
.iter()
.map(|&x| HalfF16::from_f32(x))
.collect();
let a_simsimd: &[f16] =
unsafe { std::slice::from_raw_parts(a_half.as_ptr() as *const f16, a_half.len()) };
let b_simsimd: &[f16] =
unsafe { std::slice::from_raw_parts(b_half.as_ptr() as *const f16, b_half.len()) };
if let Some(result) = SpatialSimilarity::euclidean(&a_simsimd, &b_simsimd) {
assert_almost_equal(5.2, result, 0.01);
}
}
#[test]
fn l2_i8() {
let a = &[1, 2, 3];
let b = &[4, 5, 6];
if let Some(result) = SpatialSimilarity::euclidean(a, b) {
assert_almost_equal(5.2, result, 0.01);
}
}
// Adding new tests for bit-level distances
#[test]
fn hamming_u8() {
let a = &[0b01010101, 0b11110000, 0b10101010];
let b = &[0b01010101, 0b11110000, 0b10101010];
if let Some(result) = BinarySimilarity::hamming(a, b) {
assert_almost_equal(0.0, result, 0.01);
}
}
#[test]
fn jaccard_u8() {
// For binary data, treat each byte as a set of bits
let a = &[0b11110000, 0b00001111, 0b10101010];
let b = &[0b11110000, 0b00001111, 0b01010101];
if let Some(result) = BinarySimilarity::jaccard(a, b) {
assert_almost_equal(0.5, result, 0.01);
}
}
// Adding new tests for probability similarities
#[test]
fn js_f32() {
let a: &[f32; 3] = &[0.1, 0.9, 0.0];
let b: &[f32; 3] = &[0.2, 0.8, 0.0];
if let Some(result) = ProbabilitySimilarity::jensenshannon(a, b) {
assert_almost_equal(0.099, result, 0.01);
}
}
#[test]
fn kl_f32() {
let a: &[f32; 3] = &[0.1, 0.9, 0.0];
let b: &[f32; 3] = &[0.2, 0.8, 0.0];
if let Some(result) = ProbabilitySimilarity::kullbackleibler(a, b) {
assert_almost_equal(0.036, result, 0.01);
}
}
#[test]
fn cos_f16_same() {
// Assuming these u16 values represent f16 bit patterns, and they are identical
let a_u16: &[u16] = &[15360, 16384, 17408]; // Corresponding to some f16 values
let b_u16: &[u16] = &[15360, 16384, 17408]; // Same as above for simplicity
// Reinterpret cast from &[u16] to &[f16]
let a_f16: &[f16] =
unsafe { std::slice::from_raw_parts(a_u16.as_ptr() as *const f16, a_u16.len()) };
let b_f16: &[f16] =
unsafe { std::slice::from_raw_parts(b_u16.as_ptr() as *const f16, b_u16.len()) };
if let Some(result) = SpatialSimilarity::cosine(a_f16, b_f16) {
assert_almost_equal(0.0, result, 0.01);
}
}
#[test]
fn cos_bf16_same() {
// Assuming these u16 values represent bf16 bit patterns, and they are identical
let a_u16: &[u16] = &[15360, 16384, 17408]; // Corresponding to some bf16 values
let b_u16: &[u16] = &[15360, 16384, 17408]; // Same as above for simplicity
// Reinterpret cast from &[u16] to &[bf16]
let a_bf16: &[bf16] =
unsafe { std::slice::from_raw_parts(a_u16.as_ptr() as *const bf16, a_u16.len()) };
let b_bf16: &[bf16] =
unsafe { std::slice::from_raw_parts(b_u16.as_ptr() as *const bf16, b_u16.len()) };
if let Some(result) = SpatialSimilarity::cosine(a_bf16, b_bf16) {
assert_almost_equal(0.0, result, 0.01);
}
}
#[test]
fn cos_f16_interop() {
let a_half: Vec<HalfF16> = vec![1.0, 2.0, 3.0]
.iter()
.map(|&x| HalfF16::from_f32(x))
.collect();
let b_half: Vec<HalfF16> = vec![4.0, 5.0, 6.0]
.iter()
.map(|&x| HalfF16::from_f32(x))
.collect();
// SAFETY: This is safe as long as the memory representations are guaranteed to be identical,
// which they are due to both being #[repr(transparent)] wrappers around u16.
let a_simsimd: &[f16] =
unsafe { std::slice::from_raw_parts(a_half.as_ptr() as *const f16, a_half.len()) };
let b_simsimd: &[f16] =
unsafe { std::slice::from_raw_parts(b_half.as_ptr() as *const f16, b_half.len()) };
// Use the reinterpret-casted slices with your SpatialSimilarity implementation
if let Some(result) = SpatialSimilarity::cosine(a_simsimd, b_simsimd) {
assert_almost_equal(0.025, result, 0.01);
}
}
#[test]
fn cos_bf16_interop() {
let a_half: Vec<HalfBF16> = vec![1.0, 2.0, 3.0]
.iter()
.map(|&x| HalfBF16::from_f32(x))
.collect();
let b_half: Vec<HalfBF16> = vec![4.0, 5.0, 6.0]
.iter()
.map(|&x| HalfBF16::from_f32(x))
.collect();
// SAFETY: This is safe as long as the memory representations are guaranteed to be identical,
// which they are due to both being #[repr(transparent)] wrappers around u16.
let a_simsimd: &[bf16] =
unsafe { std::slice::from_raw_parts(a_half.as_ptr() as *const bf16, a_half.len()) };
let b_simsimd: &[bf16] =
unsafe { std::slice::from_raw_parts(b_half.as_ptr() as *const bf16, b_half.len()) };
// Use the reinterpret-casted slices with your SpatialSimilarity implementation
if let Some(result) = SpatialSimilarity::cosine(a_simsimd, b_simsimd) {
assert_almost_equal(0.025, result, 0.01);
}
}
#[test]
fn intersect_u16() {
{
let a_u16: &[u16] = &[153, 16384, 17408];
let b_u16: &[u16] = &[7408, 15360, 16384];
if let Some(result) = Sparse::intersect(a_u16, b_u16) {
assert_almost_equal(1.0, result, 0.0001);
}
}
{
let a_u16: &[u16] = &[8, 153, 11638];
let b_u16: &[u16] = &[7408, 15360, 16384];
if let Some(result) = Sparse::intersect(a_u16, b_u16) {
assert_almost_equal(0.0, result, 0.0001);
}
}
}
#[test]
fn intersect_u32() {
{
let a_u32: &[u32] = &[11, 153];
let b_u32: &[u32] = &[11, 153, 7408, 16384];
if let Some(result) = Sparse::intersect(a_u32, b_u32) {
assert_almost_equal(2.0, result, 0.0001);
}
}
{
let a_u32: &[u32] = &[153, 7408, 11638];
let b_u32: &[u32] = &[153, 7408, 11638];
if let Some(result) = Sparse::intersect(a_u32, b_u32) {
assert_almost_equal(3.0, result, 0.0001);
}
}
}
/// Reference implementation of set intersection using Rust's standard library
fn reference_intersect<T: Ord>(a: &[T], b: &[T]) -> usize {
let mut a_iter = a.iter();
let mut b_iter = b.iter();
let mut a_current = a_iter.next();
let mut b_current = b_iter.next();
let mut count = 0;
while let (Some(a_val), Some(b_val)) = (a_current, b_current) {
match a_val.cmp(b_val) {
core::cmp::Ordering::Less => a_current = a_iter.next(),
core::cmp::Ordering::Greater => b_current = b_iter.next(),
core::cmp::Ordering::Equal => {
count += 1;
a_current = a_iter.next();
b_current = b_iter.next();
}
}
}
count
}
/// Generate test arrays with various sizes and patterns for intersection testing
/// Includes empty, small, medium, large arrays with different overlap characteristics
fn generate_intersection_test_arrays<T>() -> Vec<Vec<T>>
where
T: core::convert::TryFrom<u32> + Copy,
<T as core::convert::TryFrom<u32>>::Error: core::fmt::Debug,
{
vec![
// Empty array
vec![],
// Single element
vec![T::try_from(42).unwrap()],
// Very small arrays (< 16 elements) - tests serial fallback
vec![
T::try_from(1).unwrap(),
T::try_from(5).unwrap(),
T::try_from(10).unwrap(),
],
vec![
T::try_from(2).unwrap(),
T::try_from(4).unwrap(),
T::try_from(6).unwrap(),
T::try_from(8).unwrap(),
T::try_from(10).unwrap(),
T::try_from(12).unwrap(),
T::try_from(14).unwrap(),
],
// Small arrays (< 32 elements) - boundary case for Turin
(0..14).map(|x| T::try_from(x * 10).unwrap()).collect(),
(5..20).map(|x| T::try_from(x * 10).unwrap()).collect(),
// Medium arrays (32-64 elements) - tests one or two SIMD iterations
(0..40).map(|x| T::try_from(x * 2).unwrap()).collect(),
(10..50).map(|x| T::try_from(x * 2).unwrap()).collect(), // 50% overlap with previous
(0..45).map(|x| T::try_from(x * 3).unwrap()).collect(), // Different stride
// Large arrays (> 64 elements) - tests main SIMD loop
(0..100).map(|x| T::try_from(x * 2).unwrap()).collect(),
(50..150).map(|x| T::try_from(x * 2).unwrap()).collect(), // 50% overlap
(0..100).map(|x| T::try_from(x * 5).unwrap()).collect(), // Sparse overlap
(0..150)
.filter(|x| x % 7 == 0)
.map(|x| T::try_from(x).unwrap())
.collect(),
// Very large arrays (> 256 elements) - stress test
(0..500).map(|x| T::try_from(x * 3).unwrap()).collect(),
(100..600).map(|x| T::try_from(x * 3).unwrap()).collect(), // Large overlap
(0..600).map(|x| T::try_from(x * 7).unwrap()).collect(), // Minimal overlap
// Edge cases: no overlap at all
(0..50).map(|x| T::try_from(x * 2).unwrap()).collect(),
(1000..1050).map(|x| T::try_from(x * 2).unwrap()).collect(), // Completely disjoint
// Dense arrays at boundaries
(0..16).map(|x| T::try_from(x).unwrap()).collect(), // Exactly 16 elements
(0..32).map(|x| T::try_from(x).unwrap()).collect(), // Exactly 32 elements
(0..64).map(|x| T::try_from(x).unwrap()).collect(), // Exactly 64 elements
]
}
#[test]
fn intersect_u32_comprehensive() {
let test_arrays: Vec<Vec<u32>> = generate_intersection_test_arrays();
for (i, array_a) in test_arrays.iter().enumerate() {
for (j, array_b) in test_arrays.iter().enumerate() {
let expected = reference_intersect(array_a, array_b);
let result =
Sparse::intersect(array_a.as_slice(), array_b.as_slice()).unwrap() as usize;
assert_eq!(
expected,
result,
"Intersection mismatch for arrays[{}] (len={}) and arrays[{}] (len={})",
i,
array_a.len(),
j,
array_b.len()
);
}
}
}
#[test]
fn intersect_u16_comprehensive() {
let test_arrays: Vec<Vec<u16>> = generate_intersection_test_arrays();
for (i, array_a) in test_arrays.iter().enumerate() {
for (j, array_b) in test_arrays.iter().enumerate() {
let expected = reference_intersect(array_a, array_b);
let result =
Sparse::intersect(array_a.as_slice(), array_b.as_slice()).unwrap() as usize;
assert_eq!(
expected,
result,
"Intersection mismatch for arrays[{}] (len={}) and arrays[{}] (len={})",
i,
array_a.len(),
j,
array_b.len()
);
}
}
}
#[test]
fn intersect_edge_cases() {
// Test empty arrays
let empty: &[u32] = &[];
let non_empty: &[u32] = &[1, 2, 3];
assert_eq!(Sparse::intersect(empty, empty), Some(0.0));
assert_eq!(Sparse::intersect(empty, non_empty), Some(0.0));
assert_eq!(Sparse::intersect(non_empty, empty), Some(0.0));
// Test single element matches
assert_eq!(Sparse::intersect(&[42u32], &[42u32]), Some(1.0));
assert_eq!(Sparse::intersect(&[42u32], &[43u32]), Some(0.0));
// Test no overlap
let a: &[u32] = &[1, 2, 3, 4, 5];
let b: &[u32] = &[10, 20, 30, 40, 50];
assert_eq!(Sparse::intersect(a, b), Some(0.0));
// Test complete overlap
let c: &[u32] = &[10, 20, 30, 40, 50];
assert_eq!(Sparse::intersect(c, c), Some(5.0));
// Test one element at boundary (exactly at 16, 32, 64 element boundaries)
let boundary_16: Vec<u32> = (0..16).collect();
let boundary_32: Vec<u32> = (0..32).collect();
let boundary_64: Vec<u32> = (0..64).collect();
assert_eq!(Sparse::intersect(&boundary_16, &boundary_16), Some(16.0));
assert_eq!(Sparse::intersect(&boundary_32, &boundary_32), Some(32.0));
assert_eq!(Sparse::intersect(&boundary_64, &boundary_64), Some(64.0));
// Test partial overlap at boundaries
let first_half: Vec<u32> = (0..32).collect();
let second_half: Vec<u32> = (16..48).collect();
assert_eq!(Sparse::intersect(&first_half, &second_half), Some(16.0));
}
#[test]
fn f16_arithmetic() {
let a = f16::from_f32(3.5);
let b = f16::from_f32(2.0);
// Test basic arithmetic
assert!((a + b).to_f32() - 5.5 < 0.01);
assert!((a - b).to_f32() - 1.5 < 0.01);
assert!((a * b).to_f32() - 7.0 < 0.01);
assert!((a / b).to_f32() - 1.75 < 0.01);
assert!((-a).to_f32() + 3.5 < 0.01);
// Test constants
assert!(f16::ZERO.to_f32() == 0.0);
assert!((f16::ONE.to_f32() - 1.0).abs() < 0.01);
assert!((f16::NEG_ONE.to_f32() + 1.0).abs() < 0.01);
// Test comparisons
assert!(a > b);
assert!(!(a < b));
assert!(a == a);
// Test utility methods
assert!((-a).abs().to_f32() - 3.5 < 0.01);
assert!(a.is_finite());
assert!(!a.is_nan());
assert!(!a.is_infinite());
}
#[test]
fn bf16_arithmetic() {
let a = bf16::from_f32(3.5);
let b = bf16::from_f32(2.0);
// Test basic arithmetic
assert!((a + b).to_f32() - 5.5 < 0.1);
assert!((a - b).to_f32() - 1.5 < 0.1);
assert!((a * b).to_f32() - 7.0 < 0.1);
assert!((a / b).to_f32() - 1.75 < 0.1);
assert!((-a).to_f32() + 3.5 < 0.1);
// Test constants
assert!(bf16::ZERO.to_f32() == 0.0);
assert!((bf16::ONE.to_f32() - 1.0).abs() < 0.01);
assert!((bf16::NEG_ONE.to_f32() + 1.0).abs() < 0.01);
// Test comparisons
assert!(a > b);
assert!(!(a < b));
assert!(a == a);
// Test utility methods
assert!((-a).abs().to_f32() - 3.5 < 0.1);
assert!(a.is_finite());
assert!(!a.is_nan());
assert!(!a.is_infinite());
}
#[test]
fn bf16_dot() {
let brain_a: Vec<bf16> = vec![1.0, 2.0, 3.0, 1.0, 2.0]
.iter()
.map(|&x| bf16::from_f32(x))
.collect();
let brain_b: Vec<bf16> = vec![4.0, 5.0, 6.0, 4.0, 5.0]
.iter()
.map(|&x| bf16::from_f32(x))
.collect();
if let Some(result) = <bf16 as SpatialSimilarity>::dot(&brain_a, &brain_b) {
assert_eq!(46.0, result);
}
}
}
|