11import { stitch } from '../core/resolve/stitch.ts' ;
22import { UnknownData } from '../data/dataTypes.ts' ;
33import { undecorate } from '../data/dataTypes.ts' ;
4+ import { f32 } from '../data/numeric.ts' ;
45import { derefSnippet , RefOperator } from '../data/ref.ts' ;
56import { schemaCallWrapperGPU } from '../data/schemaCallWrapper.ts' ;
67import { snip , type Snippet } from '../data/snippet.ts' ;
@@ -120,12 +121,18 @@ function getImplicitConversionRank(src: BaseData, dest: BaseData): ConversionRan
120121 }
121122 }
122123
124+ if ( trueSrc . type in primitivePreference && trueDst . type === 'abstractFloat' ) {
125+ // When one of the types is a float (abstract or not), we don't want to cast it to a non-float type,
126+ // which would cause it to lose precision. We instead choose the common type to be f32.
127+ return { rank : 1 , action : 'cast' , targetType : f32 } ;
128+ }
129+
123130 if ( trueSrc . type === 'abstractFloat' ) {
124131 if ( trueDst . type === 'u32' ) {
125- return { rank : 2 , action : 'cast' , targetType : trueDst } ;
132+ return { rank : 3 , action : 'cast' , targetType : trueDst } ;
126133 }
127134 if ( trueDst . type === 'i32' ) {
128- return { rank : 1 , action : 'cast' , targetType : trueDst } ;
135+ return { rank : 2 , action : 'cast' , targetType : trueDst } ;
129136 }
130137 }
131138
@@ -167,6 +174,10 @@ function findBestType(
167174 let bestResult : { type : BaseData ; details : ConversionRankInfo [ ] ; sum : number } | undefined ;
168175
169176 for ( const targetType of uniqueTypes ) {
177+ /**
178+ * The type we end up converting to. Will be different than `targetType` if `targetType === abstractFloat`
179+ */
180+ let destType = targetType ;
170181 const details : ConversionRankInfo [ ] = [ ] ;
171182 let sum = 0 ;
172183 for ( const sourceType of types ) {
@@ -176,9 +187,12 @@ function findBestType(
176187 break ;
177188 }
178189 details . push ( conversion ) ;
190+ if ( conversion . action === 'cast' ) {
191+ destType = conversion . targetType ;
192+ }
179193 }
180194 if ( sum < ( bestResult ?. sum ?? Number . POSITIVE_INFINITY ) ) {
181- bestResult = { type : targetType , details, sum } ;
195+ bestResult = { type : destType , details, sum } ;
182196 }
183197 }
184198 if ( ! bestResult ) {
0 commit comments