3 ms·
I'm not sure you can avoid the check, but you can avoid a branch. An AVX-512 extension has a `vfixupimm` instruction[1] which can adjust special floating point
by sparkie 1y ago
I'm not sure you can avoid the check, but you can avoid a branch.
An AVX-512 extension has a `vfixupimm` instruction[1] which can adjust special floating point values. You could use this to adjust all zeroes to -0 but leave any non-zeroes untouched. It isn't very obvious how to use though.
vfixupimmsd dst, src, fixup, flag
* The `flag` is for error reporting - we can set it to zero to ignore errors.
* `dst` and `src` are a floating point value - they can be the same register.
* The instruction first checks `src` and turns any denormals into zero if the MXCSR.DAZ flag is set.
* It then categorizes `src` as one of {QNAN, SNAN, ZERO, ONE, NEG_INF, POS_ING, NEG_VALUE, POS_VALUE}
* `fixup` is an array of 8 nybbles (a 32-bit int) and is looked up based on the categorization of `src` {QNAN = 0 ... POS_VALUE = 7}
* The values of each nybble denote which value to place into `dst`:
0x0 : dst (unchanged)
0x1 : src (with denormals as zero if MXCSR.DAZ is set)
0x2 : QNaN(src)
0x3 : QNAN_Indefinite
0x4 : -INF
0x5 : +INF
0x6 : src < 0 ? -INF : +INF
0x7 : -0
0x8 : +0
0x9 : -1
0xA : +1
0xB : 1/2
0xC : 90.0
0xD : PI/2
0xE : MAX_FLOAT
0xF : -MAX_FLOAT
You want to set the nybble for categorization ZERO (bits 11..8) to 0x7 (-0) in `fixup`. This would mean you want `fixup` to be equal to `0x00000700`. So usage would be:
static __m128i zerofixup = { 0x700 };
double fixnum_to_flonum(int64_t fixnum) {
__m128d flonum = { (double)fixnum };
return _mm_cvtsd_f64(_mm_fixupimm_sd(flonum, flonum, zerofixup, 0));
}
Which compiles to just 4 instructions, with no branches:
.FIXUP:
.long 1792 # 0x700
.long 0 # 0x0
.long 0 # 0x0
.long 0 # 0x0
fixnum_to_flonum:
vcvtsi2sd xmm0, xmm0, rdi
vmovq xmm0, xmm0
vfixupimmsd xmm0, xmm0, qword ptr [rip + .FIXUP], 0
ret
It can be extended to operate on 8 int64->double at a time (__m512d) with little extra cost.
You could maybe use this optimization where the instruction is available and just stick with a branch version otherwise, or figure out some other way to make it branchless - though I can't think of any other way which would be any faster than a branch.
[1]:https://www.intel.com/content/www/us/en/docs/intrinsics-guide/index.html#text=_mm_fixupimm_sd https://www.intel.com/content/www/us/en/docs/intrinsics-guid...
- gus_massa 1y agoSo, if I use 0x00450000 I can swap -inf.0 and +inf.0 without modifying any other value? (I don't expect this swap operation to be useful, but I'm trying to understand the details.) --- Thanks again, it's very interesting. I used assembler a long time ago, for the Z80 and 80?86, when the coprosesor was like 2 inches away :) . The problem is that Chez Scheme emits it's own assembler, and support many platforms. So after going into the rabbit hole, you get to asm-fpt https://github.com/search?q=repo%3Acisco%2FChezScheme+asm-fpt&type=code https://github.com/search?q=repo%3Acisco%2FChezScheme+asm-fp... (expand and look for "define asm-fpt" near line 1300-2000) This is like 2 or 3 layers below the level I usually modify, so I'm not sure about the details and quirks in that layer. I'll link to this discussion in github in case some of the maintainers wants to add something like this. My particular case is a very small corner cases and I'm not sure they'd like to add more complexity, but it's nice to have this info in case there are similar cases because once you notice them, they star to appear everywhere. You can tag yourself in case someone wants to ask more questions or just get updates, but I expect that I'll go in the oposite direction.
- sparkie 1y ago> So, if I use 0x00450000 I can swap -inf.0 and +inf.0 without modifying any other value? Yeah, you got it. See test: https://ce.billsun.dev/#g:!((g:!((g:!((h:codeEditor,i:(filename:'1',fontScale:14,fontUsePx:'0',j:1,lang:___c,selection:(endColumn:1,endLineNumber:14,positionColumn:1,positionLineNumber:14,selectionStartColumn:1,selectionStartLineNumber:14,startColumn:1,startLineNumber:14),source:'%23include+%3Cstdio.h%3E%0A%23include+%3Cimmintrin.h%3E%0A%23include+%3Cmath.h%3E%0A%0A__m128i+fixup+%3D+%7B+(int64_t)0x00450700+%7D%3B%0A__m512i+fixup_x8%3B%0A%0A__attribute__((constructor))%0Avoid+init()+%7B%0A++++//+Enable+MXCSR.DAZ+(denormals+as+zero)%0A++++_MM_SET_DENORMALS_ZERO_MODE(_MM_DENORMALS_ZERO_ON)%3B%0A++++fixup_x8+%3D+_mm512_broadcastq_epi64(fixup)%3B%0A%7D%0A%0A__m512d+fixup_double_x8(__m512d+values)+%7B%0A++++return+_mm512_fixupimm_pd(values,+values,+fixup_x8,+0)%3B%0A%7D%0A%0A__attribute__((noinline))%0Avoid+print_values(__m512d+values,+__m512d+fixedupvalues)+%7B%0A++++for+(int+i%3D0%3Bi%3C8%3Bi%2B%2B)%0A++++++++printf(%22Value:+%25.20g%3B%5CtFixedup:+%25.20g%5Cn%22,+values%5Bi%5D,+fixedupvalues%5Bi%5D)%3B%0A%7D%0A%0Aint+main+()+%7B%0A++++int64_t+denormal+%3D+0x0000000012340000LL%3B%0A++++__m512d+values+%3D+%0A++++++++%7B+INFINITY%0A++++++++,+(double)-1%0A++++++++,+(double)0%0A++++++++,+(double)%2B1%0A++++++++,+-INFINITY%0A++++++++,+0.5%0A++++++++,+-1.5%0A++++++++,+*(double*)%26denormal%0A++++++++%7D%3B%0A%0A++++print_values(values,+fixup_double_x8(values))%3B%0A%0A++++return+0%3B%0A%7D'),l:'5',n:'0',o:'C+source+%231',t:'0')),k:50,l:'4',n:'0',o:'',s:0,t:'0'),(g:!((g:!((h:compiler,i:(compiler:cclang11,filters:(b:'0',binary:'1',commentOnly:'0',demangle:'0',directives:'0',execute:'0',intel:'0',libraryCode:'0',trim:'1'),flagsViewOpen:'1',fontScale:14,fontUsePx:'0',j:1,lang:___c,libs:!(),options:'-march%3Dnative+-O2',selection:(endColumn:24,endLineNumber:4,positionColumn:24,positionLineNumber:4,selectionStartColumn:24,selectionStartLineNumber:4,startColumn:24,startLineNumber:4),source:1,tree:'1'),l:'5',n:'0',o:'clang+11+(C,+Editor+%231,+Compiler+%231)',t:'0')),k:50,l:'4',m:66.70212765957447,n:'0',o:'',s:0,t:'0'),(g:!((h:output,i:(compilerName:'clang+11',editorid:1,fontScale:14,fontUsePx:'0',j:1,wrap:'1'),l:'5',n:'0',o:'Output+of+clang+11+(Compiler+%231)',t:'0')),header:(),l:'4',m:33.29787234042553,n:'0',o:'',s:0,t:'0')),k:50,l:'3',n:'0',o:'',t:'0')),l:'2',n:'0',o:'',t:'0')),version:4 https://ce.billsun.dev/#g:!((g:!((g:!((h:codeEditor,i:(filen... If you are going to implement something like this you basically need a fallback for where it is not supported. In C you write: #ifdef __AVX5125F__ // optimized code #else // fallback code #endif The optimized code will only be emitted if -mavx512f is passed to the compiler. This flag is implied if `-march=native` and the host compiling the code supports it, or if `-march=specificarch` and specificarch supports it. Otherwise the fallback code will be used. If using the custom assembler you would need to test whether AVX512F is available by using the CPUID instruction.