Operators.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620
  1. /*
  2. * Copyright (c) 2021-2023, Ali Mohammad Pur <mpfard@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <AK/BitCast.h>
  8. #include <AK/BuiltinWrappers.h>
  9. #include <AK/Result.h>
  10. #include <AK/SIMD.h>
  11. #include <AK/StringView.h>
  12. #include <AK/Types.h>
  13. #include <limits.h>
  14. #include <math.h>
  15. namespace Wasm::Operators {
  16. using namespace AK::SIMD;
  17. #define DEFINE_BINARY_OPERATOR(Name, operation) \
  18. struct Name { \
  19. template<typename Lhs, typename Rhs> \
  20. auto operator()(Lhs lhs, Rhs rhs) const \
  21. { \
  22. return lhs operation rhs; \
  23. } \
  24. \
  25. static StringView name() \
  26. { \
  27. return #operation##sv; \
  28. } \
  29. }
  30. DEFINE_BINARY_OPERATOR(Equals, ==);
  31. DEFINE_BINARY_OPERATOR(NotEquals, !=);
  32. DEFINE_BINARY_OPERATOR(GreaterThan, >);
  33. DEFINE_BINARY_OPERATOR(LessThan, <);
  34. DEFINE_BINARY_OPERATOR(LessThanOrEquals, <=);
  35. DEFINE_BINARY_OPERATOR(GreaterThanOrEquals, >=);
  36. DEFINE_BINARY_OPERATOR(Add, +);
  37. DEFINE_BINARY_OPERATOR(Subtract, -);
  38. DEFINE_BINARY_OPERATOR(Multiply, *);
  39. DEFINE_BINARY_OPERATOR(BitAnd, &);
  40. DEFINE_BINARY_OPERATOR(BitOr, |);
  41. DEFINE_BINARY_OPERATOR(BitXor, ^);
  42. #undef DEFINE_BINARY_OPERATOR
  43. struct Divide {
  44. template<typename Lhs, typename Rhs>
  45. auto operator()(Lhs lhs, Rhs rhs) const
  46. {
  47. if constexpr (IsFloatingPoint<Lhs>) {
  48. return lhs / rhs;
  49. } else {
  50. Checked value(lhs);
  51. value /= rhs;
  52. if (value.has_overflow())
  53. return AK::Result<Lhs, StringView>("Integer division overflow"sv);
  54. return AK::Result<Lhs, StringView>(value.value());
  55. }
  56. }
  57. static StringView name() { return "/"sv; }
  58. };
  59. struct Modulo {
  60. template<typename Lhs, typename Rhs>
  61. auto operator()(Lhs lhs, Rhs rhs) const
  62. {
  63. if (rhs == 0)
  64. return AK::Result<Lhs, StringView>("Integer division overflow"sv);
  65. if constexpr (IsSigned<Lhs>) {
  66. if (rhs == -1)
  67. return AK::Result<Lhs, StringView>(0); // Spec weirdness right here, signed division overflow is ignored.
  68. }
  69. return AK::Result<Lhs, StringView>(lhs % rhs);
  70. }
  71. static StringView name() { return "%"sv; }
  72. };
  73. struct BitShiftLeft {
  74. template<typename Lhs, typename Rhs>
  75. auto operator()(Lhs lhs, Rhs rhs) const { return lhs << (rhs % (sizeof(lhs) * 8)); }
  76. static StringView name() { return "<<"sv; }
  77. };
  78. struct BitShiftRight {
  79. template<typename Lhs, typename Rhs>
  80. auto operator()(Lhs lhs, Rhs rhs) const { return lhs >> (rhs % (sizeof(lhs) * 8)); }
  81. static StringView name() { return ">>"sv; }
  82. };
  83. struct BitRotateLeft {
  84. template<typename Lhs, typename Rhs>
  85. auto operator()(Lhs lhs, Rhs rhs) const
  86. {
  87. // generates a single 'rol' instruction if shift is positive
  88. // otherwise generate a `ror`
  89. auto const mask = CHAR_BIT * sizeof(Lhs) - 1;
  90. rhs &= mask;
  91. return (lhs << rhs) | (lhs >> ((-rhs) & mask));
  92. }
  93. static StringView name() { return "rotate_left"sv; }
  94. };
  95. struct BitRotateRight {
  96. template<typename Lhs, typename Rhs>
  97. auto operator()(Lhs lhs, Rhs rhs) const
  98. {
  99. // generates a single 'ror' instruction if shift is positive
  100. // otherwise generate a `rol`
  101. auto const mask = CHAR_BIT * sizeof(Lhs) - 1;
  102. rhs &= mask;
  103. return (lhs >> rhs) | (lhs << ((-rhs) & mask));
  104. }
  105. static StringView name() { return "rotate_right"sv; }
  106. };
  107. template<size_t VectorSize>
  108. struct VectorShiftLeft {
  109. auto operator()(u128 lhs, i32 rhs) const
  110. {
  111. auto shift_value = rhs % (sizeof(lhs) * 8 / VectorSize);
  112. return bit_cast<u128>(bit_cast<Native128ByteVectorOf<NativeIntegralType<128 / VectorSize>, MakeUnsigned>>(lhs) << shift_value);
  113. }
  114. static StringView name()
  115. {
  116. switch (VectorSize) {
  117. case 16:
  118. return "vec(8x16)<<"sv;
  119. case 8:
  120. return "vec(16x8)<<"sv;
  121. case 4:
  122. return "vec(32x4)<<"sv;
  123. case 2:
  124. return "vec(64x2)<<"sv;
  125. default:
  126. VERIFY_NOT_REACHED();
  127. }
  128. }
  129. };
  130. template<size_t VectorSize, template<typename> typename SetSign>
  131. struct VectorShiftRight {
  132. auto operator()(u128 lhs, i32 rhs) const
  133. {
  134. auto shift_value = rhs % (sizeof(lhs) * 8 / VectorSize);
  135. return bit_cast<u128>(bit_cast<Native128ByteVectorOf<NativeIntegralType<128 / VectorSize>, SetSign>>(lhs) >> shift_value);
  136. }
  137. static StringView name()
  138. {
  139. switch (VectorSize) {
  140. case 16:
  141. return "vec(8x16)>>"sv;
  142. case 8:
  143. return "vec(16x8)>>"sv;
  144. case 4:
  145. return "vec(32x4)>>"sv;
  146. case 2:
  147. return "vec(64x2)>>"sv;
  148. default:
  149. VERIFY_NOT_REACHED();
  150. }
  151. }
  152. };
  153. struct VectorSwizzle {
  154. auto operator()(u128 c1, u128 c2) const
  155. {
  156. // https://webassembly.github.io/spec/core/bikeshed/#-mathsfi8x16hrefsyntax-instr-vecmathsfswizzle%E2%91%A0
  157. auto i = bit_cast<Native128ByteVectorOf<i8, MakeSigned>>(c2);
  158. auto j = bit_cast<Native128ByteVectorOf<i8, MakeSigned>>(c1);
  159. auto result = AK::SIMD::shuffle(i, j);
  160. return bit_cast<u128>(result);
  161. }
  162. static StringView name() { return "vec(8x16).swizzle"sv; }
  163. };
  164. template<size_t VectorSize, template<typename> typename SetSign>
  165. struct VectorExtractLane {
  166. size_t lane;
  167. auto operator()(u128 c) const
  168. {
  169. auto result = bit_cast<Native128ByteVectorOf<NativeIntegralType<128 / VectorSize>, SetSign>>(c);
  170. return result[lane];
  171. }
  172. static StringView name()
  173. {
  174. switch (VectorSize) {
  175. case 16:
  176. return "vec(8x16).extract_lane"sv;
  177. case 8:
  178. return "vec(16x8).extract_lane"sv;
  179. case 4:
  180. return "vec(32x4).extract_lane"sv;
  181. case 2:
  182. return "vec(64x2).extract_lane"sv;
  183. default:
  184. VERIFY_NOT_REACHED();
  185. }
  186. }
  187. };
  188. template<size_t VectorSize>
  189. struct VectorExtractLaneFloat {
  190. size_t lane;
  191. auto operator()(u128 c) const
  192. {
  193. auto result = bit_cast<NativeFloatingVectorType<128 / VectorSize, VectorSize>>(c);
  194. return result[lane];
  195. }
  196. static StringView name()
  197. {
  198. switch (VectorSize) {
  199. case 16:
  200. return "vec(8x16).extract_lane"sv;
  201. case 8:
  202. return "vec(16x8).extract_lane"sv;
  203. case 4:
  204. return "vec(32x4).extract_lane"sv;
  205. case 2:
  206. return "vec(64x2).extract_lane"sv;
  207. default:
  208. VERIFY_NOT_REACHED();
  209. }
  210. }
  211. };
  212. template<size_t VectorSize, typename TrueValueType = NativeIntegralType<128 / VectorSize>>
  213. struct VectorReplaceLane {
  214. size_t lane;
  215. using ValueType = Conditional<IsFloatingPoint<TrueValueType>, NativeFloatingType<128 / VectorSize>, NativeIntegralType<128 / VectorSize>>;
  216. auto operator()(u128 c, TrueValueType value) const
  217. {
  218. auto result = bit_cast<Native128ByteVectorOf<ValueType, MakeUnsigned>>(c);
  219. result[lane] = static_cast<ValueType>(value);
  220. return bit_cast<u128>(result);
  221. }
  222. static StringView name()
  223. {
  224. switch (VectorSize) {
  225. case 16:
  226. return "vec(8x16).replace_lane"sv;
  227. case 8:
  228. return "vec(16x8).replace_lane"sv;
  229. case 4:
  230. return "vec(32x4).replace_lane"sv;
  231. case 2:
  232. return "vec(64x2).replace_lane"sv;
  233. default:
  234. VERIFY_NOT_REACHED();
  235. }
  236. }
  237. };
  238. struct Minimum {
  239. template<typename Lhs, typename Rhs>
  240. auto operator()(Lhs lhs, Rhs rhs) const
  241. {
  242. if constexpr (IsFloatingPoint<Lhs> || IsFloatingPoint<Rhs>) {
  243. if (isnan(lhs))
  244. return lhs;
  245. if (isnan(rhs))
  246. return rhs;
  247. if (isinf(lhs))
  248. return lhs > 0 ? rhs : lhs;
  249. if (isinf(rhs))
  250. return rhs > 0 ? lhs : rhs;
  251. }
  252. return min(lhs, rhs);
  253. }
  254. static StringView name() { return "minimum"sv; }
  255. };
  256. struct Maximum {
  257. template<typename Lhs, typename Rhs>
  258. auto operator()(Lhs lhs, Rhs rhs) const
  259. {
  260. if constexpr (IsFloatingPoint<Lhs> || IsFloatingPoint<Rhs>) {
  261. if (isnan(lhs))
  262. return lhs;
  263. if (isnan(rhs))
  264. return rhs;
  265. if (isinf(lhs))
  266. return lhs > 0 ? lhs : rhs;
  267. if (isinf(rhs))
  268. return rhs > 0 ? rhs : lhs;
  269. }
  270. return max(lhs, rhs);
  271. }
  272. static StringView name() { return "maximum"sv; }
  273. };
  274. struct CopySign {
  275. template<typename Lhs, typename Rhs>
  276. auto operator()(Lhs lhs, Rhs rhs) const
  277. {
  278. if constexpr (IsSame<Lhs, float>)
  279. return copysignf(lhs, rhs);
  280. else if constexpr (IsSame<Lhs, double>)
  281. return copysign(lhs, rhs);
  282. else
  283. static_assert(DependentFalse<Lhs, Rhs>, "Invalid types to CopySign");
  284. }
  285. static StringView name() { return "copysign"sv; }
  286. };
  287. // Unary
  288. struct EqualsZero {
  289. template<typename Lhs>
  290. auto operator()(Lhs lhs) const { return lhs == 0; }
  291. static StringView name() { return "== 0"sv; }
  292. };
  293. struct CountLeadingZeros {
  294. template<typename Lhs>
  295. i32 operator()(Lhs lhs) const
  296. {
  297. if (lhs == 0)
  298. return sizeof(Lhs) * CHAR_BIT;
  299. if constexpr (sizeof(Lhs) == 4 || sizeof(Lhs) == 8)
  300. return count_leading_zeroes(MakeUnsigned<Lhs>(lhs));
  301. else
  302. VERIFY_NOT_REACHED();
  303. }
  304. static StringView name() { return "clz"sv; }
  305. };
  306. struct CountTrailingZeros {
  307. template<typename Lhs>
  308. i32 operator()(Lhs lhs) const
  309. {
  310. if (lhs == 0)
  311. return sizeof(Lhs) * CHAR_BIT;
  312. if constexpr (sizeof(Lhs) == 4 || sizeof(Lhs) == 8)
  313. return count_trailing_zeroes(MakeUnsigned<Lhs>(lhs));
  314. else
  315. VERIFY_NOT_REACHED();
  316. }
  317. static StringView name() { return "ctz"sv; }
  318. };
  319. struct PopCount {
  320. template<typename Lhs>
  321. auto operator()(Lhs lhs) const
  322. {
  323. if constexpr (sizeof(Lhs) == 4 || sizeof(Lhs) == 8)
  324. return popcount(MakeUnsigned<Lhs>(lhs));
  325. else
  326. VERIFY_NOT_REACHED();
  327. }
  328. static StringView name() { return "popcnt"sv; }
  329. };
  330. struct Absolute {
  331. template<typename Lhs>
  332. auto operator()(Lhs lhs) const { return AK::abs(lhs); }
  333. static StringView name() { return "abs"sv; }
  334. };
  335. struct Negate {
  336. template<typename Lhs>
  337. auto operator()(Lhs lhs) const { return -lhs; }
  338. static StringView name() { return "== 0"sv; }
  339. };
  340. struct Ceil {
  341. template<typename Lhs>
  342. auto operator()(Lhs lhs) const
  343. {
  344. if constexpr (IsSame<Lhs, float>)
  345. return ceilf(lhs);
  346. else if constexpr (IsSame<Lhs, double>)
  347. return ceil(lhs);
  348. else
  349. VERIFY_NOT_REACHED();
  350. }
  351. static StringView name() { return "ceil"sv; }
  352. };
  353. struct Floor {
  354. template<typename Lhs>
  355. auto operator()(Lhs lhs) const
  356. {
  357. if constexpr (IsSame<Lhs, float>)
  358. return floorf(lhs);
  359. else if constexpr (IsSame<Lhs, double>)
  360. return floor(lhs);
  361. else
  362. VERIFY_NOT_REACHED();
  363. }
  364. static StringView name() { return "floor"sv; }
  365. };
  366. struct Truncate {
  367. template<typename Lhs>
  368. AK::Result<Lhs, StringView> operator()(Lhs lhs) const
  369. {
  370. if constexpr (IsSame<Lhs, float>)
  371. return truncf(lhs);
  372. else if constexpr (IsSame<Lhs, double>)
  373. return trunc(lhs);
  374. else
  375. VERIFY_NOT_REACHED();
  376. }
  377. static StringView name() { return "truncate"sv; }
  378. };
  379. struct NearbyIntegral {
  380. template<typename Lhs>
  381. auto operator()(Lhs lhs) const
  382. {
  383. if constexpr (IsSame<Lhs, float>)
  384. return nearbyintf(lhs);
  385. else if constexpr (IsSame<Lhs, double>)
  386. return nearbyint(lhs);
  387. else
  388. VERIFY_NOT_REACHED();
  389. }
  390. static StringView name() { return "round"sv; }
  391. };
  392. struct SquareRoot {
  393. template<typename Lhs>
  394. auto operator()(Lhs lhs) const
  395. {
  396. if constexpr (IsSame<Lhs, float>)
  397. return sqrtf(lhs);
  398. else if constexpr (IsSame<Lhs, double>)
  399. return sqrt(lhs);
  400. else
  401. VERIFY_NOT_REACHED();
  402. }
  403. static StringView name() { return "sqrt"sv; }
  404. };
  405. template<typename Result>
  406. struct Wrap {
  407. template<typename Lhs>
  408. Result operator()(Lhs lhs) const
  409. {
  410. return static_cast<MakeUnsigned<Result>>(bit_cast<MakeUnsigned<Lhs>>(lhs));
  411. }
  412. static StringView name() { return "wrap"sv; }
  413. };
  414. template<typename ResultT>
  415. struct CheckedTruncate {
  416. template<typename Lhs>
  417. AK::Result<ResultT, StringView> operator()(Lhs lhs) const
  418. {
  419. if (isnan(lhs) || isinf(lhs)) // "undefined", let's just trap.
  420. return "Truncation undefined behavior"sv;
  421. Lhs truncated;
  422. if constexpr (IsSame<float, Lhs>)
  423. truncated = truncf(lhs);
  424. else if constexpr (IsSame<double, Lhs>)
  425. truncated = trunc(lhs);
  426. else
  427. VERIFY_NOT_REACHED();
  428. // FIXME: This function assumes that all values of ResultT are representable in Lhs
  429. // the assumption comes from the fact that this was used exclusively by LibJS,
  430. // which only considers values that are all representable in 'double'.
  431. if (!AK::is_within_range<ResultT>(truncated))
  432. return "Truncation out of range"sv;
  433. return static_cast<ResultT>(truncated);
  434. }
  435. static StringView name() { return "truncate.checked"sv; }
  436. };
  437. template<typename ResultT>
  438. struct Extend {
  439. template<typename Lhs>
  440. ResultT operator()(Lhs lhs) const
  441. {
  442. return lhs;
  443. }
  444. static StringView name() { return "extend"sv; }
  445. };
  446. template<typename ResultT>
  447. struct Convert {
  448. template<typename Lhs>
  449. ResultT operator()(Lhs lhs) const
  450. {
  451. auto signed_interpretation = bit_cast<MakeSigned<Lhs>>(lhs);
  452. return static_cast<ResultT>(signed_interpretation);
  453. }
  454. static StringView name() { return "convert"sv; }
  455. };
  456. template<typename ResultT>
  457. struct Reinterpret {
  458. template<typename Lhs>
  459. ResultT operator()(Lhs lhs) const
  460. {
  461. return bit_cast<ResultT>(lhs);
  462. }
  463. static StringView name() { return "reinterpret"sv; }
  464. };
  465. struct Promote {
  466. double operator()(float lhs) const
  467. {
  468. if (isnan(lhs))
  469. return nan(""); // FIXME: Ensure canonical NaN remains canonical
  470. return static_cast<double>(lhs);
  471. }
  472. static StringView name() { return "promote"sv; }
  473. };
  474. struct Demote {
  475. float operator()(double lhs) const
  476. {
  477. if (isnan(lhs))
  478. return nanf(""); // FIXME: Ensure canonical NaN remains canonical
  479. if (isinf(lhs))
  480. return __builtin_huge_valf();
  481. return static_cast<float>(lhs);
  482. }
  483. static StringView name() { return "demote"sv; }
  484. };
  485. template<typename InitialType>
  486. struct SignExtend {
  487. template<typename Lhs>
  488. Lhs operator()(Lhs lhs) const
  489. {
  490. auto unsigned_representation = bit_cast<MakeUnsigned<Lhs>>(lhs);
  491. auto truncated_unsigned_representation = static_cast<MakeUnsigned<InitialType>>(unsigned_representation);
  492. auto initial_value = bit_cast<InitialType>(truncated_unsigned_representation);
  493. return static_cast<Lhs>(initial_value);
  494. }
  495. static StringView name() { return "extend"sv; }
  496. };
  497. template<typename ResultT>
  498. struct SaturatingTruncate {
  499. template<typename Lhs>
  500. ResultT operator()(Lhs lhs) const
  501. {
  502. if (isnan(lhs))
  503. return 0;
  504. if (isinf(lhs)) {
  505. if (lhs < 0)
  506. return NumericLimits<ResultT>::min();
  507. return NumericLimits<ResultT>::max();
  508. }
  509. // FIXME: This assumes that all values in ResultT are representable in 'double'.
  510. // that assumption is not correct, which makes this function yield incorrect values
  511. // for 'edge' values of type i64.
  512. constexpr auto convert = []<typename ConvertT>(ConvertT truncated_value) {
  513. if (truncated_value < NumericLimits<ResultT>::min())
  514. return NumericLimits<ResultT>::min();
  515. if constexpr (IsSame<ConvertT, float>) {
  516. if (truncated_value >= static_cast<ConvertT>(NumericLimits<ResultT>::max()))
  517. return NumericLimits<ResultT>::max();
  518. } else {
  519. if (static_cast<double>(truncated_value) >= static_cast<double>(NumericLimits<ResultT>::max()))
  520. return NumericLimits<ResultT>::max();
  521. }
  522. return static_cast<ResultT>(truncated_value);
  523. };
  524. if constexpr (IsSame<Lhs, float>)
  525. return convert(truncf(lhs));
  526. else
  527. return convert(trunc(lhs));
  528. }
  529. static StringView name() { return "truncate.saturating"sv; }
  530. };
  531. }