什么是有序和无序的LLVM CmpInst比较指令?

时间:2016-10-30 09:48:57

标签: llvm

CmpInst::Predicate"llvm/IR/InstrTypes.h"类型的定义描述了LLVM中比较指令的类型,如下所示:

enum Predicate {
  // Opcode              U L G E    Intuitive operation
  FCMP_FALSE =  0,  ///< 0 0 0 0    Always false (always folded)
  FCMP_OEQ   =  1,  ///< 0 0 0 1    True if ordered and equal
  FCMP_OGT   =  2,  ///< 0 0 1 0    True if ordered and greater than
  FCMP_OGE   =  3,  ///< 0 0 1 1    True if ordered and greater than or equal
  FCMP_OLT   =  4,  ///< 0 1 0 0    True if ordered and less than
  FCMP_OLE   =  5,  ///< 0 1 0 1    True if ordered and less than or equal
  FCMP_ONE   =  6,  ///< 0 1 1 0    True if ordered and operands are unequal
  FCMP_ORD   =  7,  ///< 0 1 1 1    True if ordered (no nans)
  FCMP_UNO   =  8,  ///< 1 0 0 0    True if unordered: isnan(X) | isnan(Y)
  FCMP_UEQ   =  9,  ///< 1 0 0 1    True if unordered or equal
  FCMP_UGT   = 10,  ///< 1 0 1 0    True if unordered or greater than
  FCMP_UGE   = 11,  ///< 1 0 1 1    True if unordered, greater than, or equal
  FCMP_ULT   = 12,  ///< 1 1 0 0    True if unordered or less than
  FCMP_ULE   = 13,  ///< 1 1 0 1    True if unordered, less than, or equal
  FCMP_UNE   = 14,  ///< 1 1 1 0    True if unordered or not equal
  FCMP_TRUE  = 15,  ///< 1 1 1 1    Always true (always folded)
  FIRST_FCMP_PREDICATE = FCMP_FALSE,
  LAST_FCMP_PREDICATE = FCMP_TRUE,
  BAD_FCMP_PREDICATE = FCMP_TRUE + 1,
  ICMP_EQ    = 32,  ///< equal
  ICMP_NE    = 33,  ///< not equal
  ICMP_UGT   = 34,  ///< unsigned greater than
  ICMP_UGE   = 35,  ///< unsigned greater or equal
  ICMP_ULT   = 36,  ///< unsigned less than
  ICMP_ULE   = 37,  ///< unsigned less or equal
  ICMP_SGT   = 38,  ///< signed greater than
  ICMP_SGE   = 39,  ///< signed greater or equal
  ICMP_SLT   = 40,  ///< signed less than
  ICMP_SLE   = 41,  ///< signed less or equal
  FIRST_ICMP_PREDICATE = ICMP_EQ,
  LAST_ICMP_PREDICATE = ICMP_SLE,
  BAD_ICMP_PREDICATE = ICMP_SLE + 1
};

我想知道&#34;命令是什么&#34;和#34;无序&#34;谓词(如&#34;如果有序和等于&#34;或&#34;如果无序,大于或等于&#34;与正常的那些相比,&#34;无符号大于或等于&#34;)

1 个答案:

答案 0 :(得分:5)

如果您不知道NaN是什么,请从最后一段开始:)

如果至少有一个操作数是NaN,则有序和无序浮点比较在比较结果上是不同的。有关详细信息,请参阅IR lang参考中的fcmp instruction。特别是这句话很重要:&#34; Ordered意味着两个操作数都不是QNAN,而无序意味着任一操作数都可以是QNAN &#34;。请注意,LLVM(AFAIK)不支持SNaN,这就是为什么lang ref专门讨论QNaN的原因。

命名的原因是NaN无法与浮点数进行比较。你不能说NaN小于或大于零。所以NaN是无序的。因此,如果其中一个操作数是NaN,则无序比较返回true。有序比较要求两个操作数都是数字。

如果你需要背景,这是wikipedia page on NaN。简而言之,当某些浮点计算的结果不是数字时,会生成一个称为NaN(非数字)的特殊结果。例如`std :: sqrt&#39;如果要求负数的平方根,则生成NaN。 NaN有两种变体。 SNaN和QNan。维基百科描述了它。出于您的问题的目的,您可以忽略差异,因为只有QNaN很重要。 LLVM AFAIK不支持SNaN。