github.com/zuoyebang/bitalosdb@v1.1.1-0.20240516111551-79a8c4d8ce20/internal/utils/float.go (about)

     1  // Copyright 2021 The Bitalosdb author(hustxrb@163.com) and other contributors.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package utils
    16  
    17  import (
    18  	"encoding/binary"
    19  	"math"
    20  )
    21  
    22  func Float64ToByteSort(float float64, buf []byte) []byte {
    23  	bits := math.Float64bits(float)
    24  	if buf == nil {
    25  		buf = make([]byte, 8, 8)
    26  	}
    27  	_ = buf[7]
    28  	binary.BigEndian.PutUint64(buf, bits)
    29  	if buf[0] < 1<<7 {
    30  		buf[0] ^= 1 << 7
    31  	} else {
    32  		buf[0] ^= 0xff
    33  		buf[1] ^= 0xff
    34  		buf[2] ^= 0xff
    35  		buf[3] ^= 0xff
    36  		buf[4] ^= 0xff
    37  		buf[5] ^= 0xff
    38  		buf[6] ^= 0xff
    39  		buf[7] ^= 0xff
    40  	}
    41  	return buf
    42  }