<html><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; "><br><div><div>On May 27, 2008, at 2:49 AM, Nicolas Capens wrote:</div><br class="Apple-interchange-newline"><blockquote type="cite"><span class="Apple-style-span" style="border-collapse: separate; color: rgb(0, 0, 0); font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-border-horizontal-spacing: 0px; -webkit-border-vertical-spacing: 0px; -webkit-text-decorations-in-effect: none; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0; "><div lang="EN-US" link="blue" vlink="purple"><div class="Section1"><div style="margin-top: 0cm; margin-right: 0cm; margin-left: 0cm; margin-bottom: 0.0001pt; font-size: 11pt; font-family: Calibri, sans-serif; ">Hi all,<o:p></o:p></div><div style="margin-top: 0cm; margin-right: 0cm; margin-left: 0cm; margin-bottom: 0.0001pt; font-size: 11pt; font-family: Calibri, sans-serif; "><o:p> </o:p></div><div style="margin-top: 0cm; margin-right: 0cm; margin-left: 0cm; margin-bottom: 0.0001pt; font-size: 11pt; font-family: Calibri, sans-serif; ">I’m trying to implement a floating-point ‘min’ and ‘max’ operation using select. For ‘min’ I get the expected x86 assembly minss instruction, but for ‘max’ I get a branch instead of maxss.<o:p></o:p></div><div style="margin-top: 0cm; margin-right: 0cm; margin-left: 0cm; margin-bottom: 0.0001pt; font-size: 11pt; font-family: Calibri, sans-serif; "><o:p> </o:p></div><div style="margin-top: 0cm; margin-right: 0cm; margin-left: 0cm; margin-bottom: 0.0001pt; font-size: 11pt; font-family: Calibri, sans-serif; ">The corresponding C syntax code looks like this:<o:p></o:p></div><div style="margin-top: 0cm; margin-right: 0cm; margin-left: 0cm; margin-bottom: 0.0001pt; font-size: 11pt; font-family: Calibri, sans-serif; "><o:p> </o:p></div><div style="margin-top: 0cm; margin-right: 0cm; margin-left: 0cm; margin-bottom: 0.0001pt; font-size: 11pt; font-family: Calibri, sans-serif; ">float z = (x > y) ? x : y;<o:p></o:p></div><div style="margin-top: 0cm; margin-right: 0cm; margin-left: 0cm; margin-bottom: 0.0001pt; font-size: 11pt; font-family: Calibri, sans-serif; "><o:p> </o:p></div><div style="margin-top: 0cm; margin-right: 0cm; margin-left: 0cm; margin-bottom: 0.0001pt; font-size: 11pt; font-family: Calibri, sans-serif; ">Any clues?<o:p></o:p></div><div style="margin-top: 0cm; margin-right: 0cm; margin-left: 0cm; margin-bottom: 0.0001pt; font-size: 11pt; font-family: Calibri, sans-serif; "><o:p></o:p></div></div></div></span></blockquote><div><br></div><div>Your code is not safe for NaNs.  This is the correct way to write maxss in C:</div><div><br></div><div><div>float max(float x, float y) {</div><div>  return !(x < y) ? x : y;</div><div>}</div><div><br></div></div>If you don't care about NaNs, you can pass -ffast-math to llvm-gcc, or set "UnsafeFPMath=true" from <llvm/Target/TargetOptions.h><br><br></div><div><blockquote type="cite"><span class="Apple-style-span" style="border-collapse: separate; color: rgb(0, 0, 0); font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-border-horizontal-spacing: 0px; -webkit-border-vertical-spacing: 0px; -webkit-text-decorations-in-effect: none; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0; "><div lang="EN-US" link="blue" vlink="purple"><div class="Section1"><div style="margin-top: 0cm; margin-right: 0cm; margin-left: 0cm; margin-bottom: 0.0001pt; font-size: 11pt; font-family: Calibri, sans-serif; ">Could someone maybe explain to me the basics of LLVM’s target specific optimizations and code generation? I’d love to analyze things like this myself but I don’t know where to start.</div></div></div></span></blockquote><br></div><div>This one specifically boils down to the semantics of maxss and LLVM IR instructions.  For example, this code:</div><div><br></div><div><div>float not_max(float x, float y) {</div><div>  return (x > y) ? x : y;</div><div>}</div><div><br></div><div>float really_max(float x, float y) {</div><div>  return !(x < y) ? x : y;</div><div>}</div><div><br></div></div><div>compiles into this LLVM IR (llvm-gcc t.c -S -o - -O -emit-llvm):</div><div><br></div><div><div>define float @not_max(float %x, float %y) nounwind  {</div><div>entry:</div><div><span class="Apple-tab-span" style="white-space:pre">  </span>%tmp3 = fcmp ogt float %x, %y<span class="Apple-tab-span" style="white-space:pre">               </span>; <i1> [#uses=1]</div><div><span class="Apple-tab-span" style="white-space:pre">       </span>%iftmp.0.0 = select i1 %tmp3, float %x, float %y<span class="Apple-tab-span" style="white-space:pre">            </span>; <float> [#uses=1]</div><div><span class="Apple-tab-span" style="white-space:pre">    </span>ret float %iftmp.0.0</div><div>}</div><div><br></div><div>define float @really_max(float %x, float %y) nounwind  {</div><div>entry:</div><div><span class="Apple-tab-span" style="white-space:pre">       </span>%tmp3 = fcmp uge float %x, %y<span class="Apple-tab-span" style="white-space:pre">               </span>; <i1> [#uses=1]</div><div><span class="Apple-tab-span" style="white-space:pre">       </span>%iftmp.1.0 = select i1 %tmp3, float %x, float %y<span class="Apple-tab-span" style="white-space:pre">            </span>; <float> [#uses=1]</div><div><span class="Apple-tab-span" style="white-space:pre">    </span>ret float %iftmp.1.0</div><div>}</div><div><br></div><div>If you're interested in target-specific x86 optimizations to be done, take a look at lib/Target/X86/README*.txt</div><div><br></div></div><div><div>-Chris</div></div></body></html>