最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

java - invert a FloatVector (1each element) - Stack Overflow

programmeradmin1浏览0评论

In java there is an API called VectorApi. It makes possible to do arithmetical operations on a whole float[] array in a single cpu cycle.

For Example:

FloatVector fv = FloatVector.fromArray(FloatVector.SPECIES_PREFERRED, new float[]{1, 2, 3, 4, 5, 6, 7}, 0);
//multiplies the wohle array in a single cycle by 2 (if the CPU supports this)
fv.mul(2f);

now I would like to calculate the result of 1f / FloatVector. For now I do it by

fv.pow(-1f);

I assume this could be a slow operation. Is there a better way to do this?

In java there is an API called VectorApi. It makes possible to do arithmetical operations on a whole float[] array in a single cpu cycle.

For Example:

FloatVector fv = FloatVector.fromArray(FloatVector.SPECIES_PREFERRED, new float[]{1, 2, 3, 4, 5, 6, 7}, 0);
//multiplies the wohle array in a single cycle by 2 (if the CPU supports this)
fv.mul(2f);

now I would like to calculate the result of 1f / FloatVector. For now I do it by

fv.pow(-1f);

I assume this could be a slow operation. Is there a better way to do this?

Share Improve this question edited Jan 21 at 7:51 neoexpert asked Jan 20 at 12:21 neoexpertneoexpert 4741 gold badge12 silver badges22 bronze badges 7
  • 1 Did you test this? – Green 绿色 Commented Jan 20 at 12:24
  • 2 You would have to look at the code generated to be sure. Pow in general is slow with an implicit log2x and 2^x hiding inside but some compilers special case certain values that they know how to do some more efficient way like -1, -1/2, -1/3 for example. BTW Division is never single cycle on any conventional hardware it is typically an order of magnitude slower than all of the other binary operations +, -, *. – Martin Brown Commented Jan 20 at 13:04
  • @MartinBrown Floating point division ain’t that slow. My Zen4 CPU takes 3 cycles to multiply FP32 numbers, less than 11 cycles to divide them. – Soonts Commented Jan 20 at 13:33
  • maybe worth mentioning that Vector API is an Incubating Feature since Java 16 (and proposed to be re-incubated in 24) – user85421 Commented Jan 20 at 14:02
  • 1 and eventually propose the addition of an invert (and even divInvert [value/vectorElement]) to Vector and to VectorOperators – user85421 Commented Jan 20 at 14:31
 |  Show 2 more comments

1 Answer 1

Reset to default 4

I got this code to run on an Intel architecture Windows Laptop (jshell, so no class)

import jdk.incubator.vector.FloatVector;
import jdk.incubator.vector.VectorSpecies;

VectorSpecies SPECIES = FloatVector.SPECIES_256;
FloatVector ONE = FloatVector.zero(SPECIES).add(1f);
FloatVector fv = FloatVector.fromArray(SPECIES, new float[]{1, 2, 3, 4, 5, 6, 7, 8}, 0);

fv.pow(-1f);

ONE.div(fv); // gives the same result as the above pow operation

I did not do any performance measurements, as they are probably also platform dependent, but as you can define ONE as constant and don't have to consider construction and addition as time consuming operations, you could do that yourself to find out if ONE.div(fv) performs better than fv.pow(-1f);

发布评论

评论列表(0)

  1. 暂无评论