Results 1 to 4 of 4

Thread: using ?: with vectors

  1. #1
    Junior Member
    Join Date
    Dec 2009
    Posts
    16

    using ?: with vectors

    What is the correct way to use the selection operator with vectors?
    For example, this works fine when a and b are floats:
    a = (b==.0f) ? .0f : a/b;
    but if they are float4 it errors when building:
    "used type 'int __attribute__((ext_vector_type(4)))' where arithmetic or pointer type is required"

    Making the zeros vectors doesn't help:
    a = (b==(float4)(0,0,0,0)) ? (float4)(0,0,0,0) : a / b;

    The reference guide makes it sound like this should work, what am I missing?

    Thanks!

  2. #2
    Senior Member
    Join Date
    May 2010
    Location
    Toronto, Canada
    Posts
    845

    Re: using ?: with vectors

    This is what the spec says:

    The ternary selection operator (? operates on three expressions (exp1 ? exp2 : exp3). This operator evaluates the first expression exp1, which can be a scalar or vector result except float. If the result is a scalar value then it selects to evaluate the second expression if the result compares unequal to 0, otherwise it selects to evaluate the third expression. If the result is a vector value, then this is equivalent to calling select(exp3, exp2, exp1). The select function is described in table 6.14. The second and third expressions can be any type, as long their types match, or there is a conversion in section 6.2.1 Implicit conversions that can be applied to one of the expressions to make their types match, or one is a vector and the other is a scalar and the scalar may be subject to the usual arithmetic conversion to the element type used by the vector operand and widened to the same type as the vector type. This resulting matching type is the type of the entire expression.
    If you are still as confused as I am we can start a club

    It sounds like what you want to do can be achieved as well with the select() builtin. Have you tried that?
    Disclaimer: Employee of Qualcomm Canada. Any opinions expressed here are personal and do not necessarily reflect the views of my employer. LinkedIn profile.

  3. #3
    Junior Member
    Join Date
    Dec 2009
    Posts
    16

    Re: using ?: with vectors

    Yes, the select function is working, so I can proceed with that, thanks.

    However it seems like a bug since, as you quoted above, the spec claims: "If the [exp1] result is a vector value, then this is equivalent to calling select(exp3, exp2, exp1)"
    but (exp1 ? exp2 : exp3) fails where select(exp3, exp2, exp1) works.
    I'm on OS X so I'll submit a bug report to Apple.

  4. #4
    Senior Member
    Join Date
    May 2010
    Location
    Toronto, Canada
    Posts
    845

    Re: using ?: with vectors

    Well done submitting an error to the vendor
    Disclaimer: Employee of Qualcomm Canada. Any opinions expressed here are personal and do not necessarily reflect the views of my employer. LinkedIn profile.

Similar Threads

  1. 2D Vectors
    By BratSinot in forum OpenCL - parallel programming of heterogeneous systems
    Replies: 2
    Last Post: 09-13-2011, 04:21 AM
  2. Testing vectors for equality
    By matts in forum OpenCL - parallel programming of heterogeneous systems
    Replies: 2
    Last Post: 04-04-2011, 11:51 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •