@@ -5187,6 +5187,22 @@ export class Compiler extends DiagnosticEmitter {
5187
5187
}
5188
5188
case TypeKind . I32 :
5189
5189
case TypeKind . U32 : {
5190
+ if ( this . options . willOptimize ) {
5191
+ // Precompute power if LHS and RHS constants
5192
+ // TODO: move this optimization to AIR
5193
+ if (
5194
+ getExpressionId ( leftExpr ) == ExpressionId . Const &&
5195
+ getExpressionId ( rightExpr ) == ExpressionId . Const
5196
+ ) {
5197
+ let leftValue = getConstValueI32 ( leftExpr ) ;
5198
+ let rightValue = getConstValueI32 ( rightExpr ) ;
5199
+ this . currentType = type ;
5200
+ return module . i32 ( i64_low ( i64_pow (
5201
+ i64_new ( leftValue ) ,
5202
+ i64_new ( rightValue )
5203
+ ) ) ) ;
5204
+ }
5205
+ }
5190
5206
let instance = this . i32PowInstance ;
5191
5207
if ( ! instance ) {
5192
5208
let prototype = this . program . lookup ( CommonNames . ipow32 ) ;
@@ -5213,6 +5229,20 @@ export class Compiler extends DiagnosticEmitter {
5213
5229
}
5214
5230
case TypeKind . I64 :
5215
5231
case TypeKind . U64 : {
5232
+ if ( this . options . willOptimize ) {
5233
+ // Precompute power if LHS and RHS constants
5234
+ // TODO: move this optimization to AIR
5235
+ if (
5236
+ getExpressionId ( leftExpr ) == ExpressionId . Const &&
5237
+ getExpressionId ( rightExpr ) == ExpressionId . Const
5238
+ ) {
5239
+ let leftValue = i64_new ( getConstValueI64Low ( leftExpr ) , getConstValueI64High ( leftExpr ) ) ;
5240
+ let rightValue = i64_new ( getConstValueI64Low ( rightExpr ) , getConstValueI64High ( rightExpr ) ) ;
5241
+ let result = i64_pow ( leftValue , rightValue ) ;
5242
+ this . currentType = type ;
5243
+ return module . i64 ( i64_low ( result ) , i64_high ( result ) ) ;
5244
+ }
5245
+ }
5216
5246
let instance = this . i64PowInstance ;
5217
5247
if ( ! instance ) {
5218
5248
let prototype = this . program . lookup ( CommonNames . ipow64 ) ;
@@ -5234,6 +5264,30 @@ export class Compiler extends DiagnosticEmitter {
5234
5264
case TypeKind . ISIZE :
5235
5265
case TypeKind . USIZE : {
5236
5266
let isWasm64 = this . options . isWasm64 ;
5267
+ if ( this . options . willOptimize ) {
5268
+ // Precompute power if LHS and RHS constants
5269
+ // TODO: move this optimization to AIR
5270
+ if (
5271
+ getExpressionId ( leftExpr ) == ExpressionId . Const &&
5272
+ getExpressionId ( rightExpr ) == ExpressionId . Const
5273
+ ) {
5274
+ if ( isWasm64 ) {
5275
+ let leftValue = i64_new ( getConstValueI64Low ( leftExpr ) , getConstValueI64High ( leftExpr ) ) ;
5276
+ let rightValue = i64_new ( getConstValueI64Low ( rightExpr ) , getConstValueI64High ( rightExpr ) ) ;
5277
+ let result = i64_pow ( leftValue , rightValue ) ;
5278
+ this . currentType = type ;
5279
+ return module . i64 ( i64_low ( result ) , i64_high ( result ) ) ;
5280
+ } else {
5281
+ let leftValue = getConstValueI32 ( leftExpr ) ;
5282
+ let rightValue = getConstValueI32 ( rightExpr ) ;
5283
+ this . currentType = type ;
5284
+ return module . i32 ( i64_low ( i64_pow (
5285
+ i64_new ( leftValue ) ,
5286
+ i64_new ( rightValue )
5287
+ ) ) ) ;
5288
+ }
5289
+ }
5290
+ }
5237
5291
let instance = isWasm64 ? this . i64PowInstance : this . i32PowInstance ;
5238
5292
if ( ! instance ) {
5239
5293
let prototype = this . program . lookup ( isWasm64 ? CommonNames . ipow64 : CommonNames . ipow32 ) ;
@@ -5258,6 +5312,19 @@ export class Compiler extends DiagnosticEmitter {
5258
5312
return this . makeCallDirect ( instance , [ leftExpr , rightExpr ] , reportNode ) ;
5259
5313
}
5260
5314
case TypeKind . F32 : {
5315
+ if ( this . options . willOptimize ) {
5316
+ // Precompute power if LHS and RHS constants
5317
+ // TODO: move this optimization to AIR
5318
+ if (
5319
+ getExpressionId ( leftExpr ) == ExpressionId . Const &&
5320
+ getExpressionId ( rightExpr ) == ExpressionId . Const
5321
+ ) {
5322
+ let leftValue = getConstValueF32 ( leftExpr ) ;
5323
+ let rightValue = getConstValueF32 ( rightExpr ) ;
5324
+ this . currentType = type ;
5325
+ return module . f32 ( f32 ( Math . pow ( leftValue , rightValue ) ) ) ;
5326
+ }
5327
+ }
5261
5328
let instance = this . f32PowInstance ;
5262
5329
if ( ! instance ) {
5263
5330
let namespace = this . program . lookup ( CommonNames . Mathf ) ;
@@ -5287,6 +5354,19 @@ export class Compiler extends DiagnosticEmitter {
5287
5354
}
5288
5355
// Math.pow otherwise (result is f64)
5289
5356
case TypeKind . F64 : {
5357
+ if ( this . options . willOptimize ) {
5358
+ // Precompute power if LHS and RHS constants
5359
+ // TODO: move this optimization to AIR
5360
+ if (
5361
+ getExpressionId ( leftExpr ) == ExpressionId . Const &&
5362
+ getExpressionId ( rightExpr ) == ExpressionId . Const
5363
+ ) {
5364
+ let leftValue = getConstValueF64 ( leftExpr ) ;
5365
+ let rightValue = getConstValueF64 ( rightExpr ) ;
5366
+ this . currentType = type ;
5367
+ return module . f64 ( Math . pow ( leftValue , rightValue ) ) ;
5368
+ }
5369
+ }
5290
5370
let instance = this . f64PowInstance ;
5291
5371
if ( ! instance ) {
5292
5372
let namespace = this . program . lookup ( CommonNames . Math ) ;
0 commit comments