github.com/primecitizens/pcz/std@v0.2.1/core/cmp/bs_wasm.s (about)

     1  // SPDX-License-Identifier: Apache-2.0
     2  // Copyright 2023 The Prime Citizens
     3  // 
     4  // Copyright 2018 The Go Authors. All rights reserved.
     5  // Use of this source code is governed by a BSD-style
     6  // license that can be found in the LICENSE file.
     7  
     8  //go:build pcz && wasm
     9  
    10  #include "textflag.h"
    11  
    12  TEXT ·Bytes(SB),NOSPLIT,$0-56
    13  	Get SP
    14  	I64Load a_base+0(FP)
    15  	I64Load a_len+8(FP)
    16  	I64Load b_base+24(FP)
    17  	I64Load b_len+32(FP)
    18  	Call cmpbody<>(SB)
    19  	I64Store ret+48(FP)
    20  	RET
    21  
    22  TEXT ·String(SB),NOSPLIT,$0-40
    23  	Get SP
    24  	I64Load a_base+0(FP)
    25  	I64Load a_len+8(FP)
    26  	I64Load b_base+16(FP)
    27  	I64Load b_len+24(FP)
    28  	Call cmpbody<>(SB)
    29  	I64Store ret+32(FP)
    30  	RET
    31  
    32  // params: a, alen, b, blen
    33  // ret: -1/0/1
    34  TEXT cmpbody<>(SB),NOSPLIT,$0-0
    35  	// len = min(alen, blen)
    36  	Get R1
    37  	Get R3
    38  	Get R1
    39  	Get R3
    40  	I64LtU
    41  	Select
    42  	Set R4
    43  
    44  	Get R0
    45  	I32WrapI64
    46  	Get R2
    47  	I32WrapI64
    48  	Get R4
    49  	I32WrapI64
    50  	Call memcmp<>(SB)
    51  	I64ExtendI32S
    52  	Tee R5
    53  
    54  	I64Eqz
    55  	If
    56  		// check length
    57  		Get R1
    58  		Get R3
    59  		I64Sub
    60  		Set R5
    61  	End
    62  
    63  	I64Const $0
    64  	I64Const $-1
    65  	I64Const $1
    66  	Get R5
    67  	I64Const $0
    68  	I64LtS
    69  	Select
    70  	Get R5
    71  	I64Eqz
    72  	Select
    73  	Return
    74  
    75  // compiled with emscripten
    76  // params: a, b, len
    77  // ret: <0/0/>0
    78  TEXT memcmp<>(SB),NOSPLIT,$0-0
    79  	Get R2
    80  	If $1
    81  	Loop
    82  	Get R0
    83  	I32Load8S $0
    84  	Tee R3
    85  	Get R1
    86  	I32Load8S $0
    87  	Tee R4
    88  	I32Eq
    89  	If
    90  	Get R0
    91  	I32Const $1
    92  	I32Add
    93  	Set R0
    94  	Get R1
    95  	I32Const $1
    96  	I32Add
    97  	Set R1
    98  	I32Const $0
    99  	Get R2
   100  	I32Const $-1
   101  	I32Add
   102  	Tee R2
   103  	I32Eqz
   104  	BrIf $3
   105  	Drop
   106  	Br $1
   107  	End
   108  	End
   109  	Get R3
   110  	I32Const $255
   111  	I32And
   112  	Get R4
   113  	I32Const $255
   114  	I32And
   115  	I32Sub
   116  	Else
   117  	I32Const $0
   118  	End
   119  	Return