alibaba / xquic

XQUIC Library released by Alibaba is a cross-platform implementation of QUIC and HTTP/3 protocol.
Apache License 2.0
1.65k stars 326 forks source link

xqc_pow2 cann't work in windows. #271

Closed keengo99 closed 7 months ago

keengo99 commented 1 year ago
#include <assert.h>
#include <math.h>
#ifdef _WIN32
#include <intrin.h>
#define XQC_SYS_WINDOWS 1
#endif
/* make n to 2 pow  */
static int xqc_pow2(unsigned int n)
{
    int count1 = 0;    
    unsigned long power = sizeof(n);
#ifdef __GNUC__
    count1 = __builtin_popcount(n);
#else
    unsigned int t = n;
    while (t) {
        count1 += t & 1;
        t >>= 1;
    }
#endif
    if (count1 <= 1) {
        return n;
    }
#if defined(XQC_SYS_WINDOWS) && defined(_MSC_VER)
    /* __lzcnt available beginning VS2008 or up*/
    _BitScanReverse((unsigned long *)&power, n);    
#else
    power = (power << 3) - __builtin_clz(n) - 1;    
#endif
    return (int)pow(2, power);
}
int main()
{
    assert(xqc_pow2(4) == 4);
    assert(xqc_pow2(12) == 8);
    assert(xqc_pow2(1024) == 1024);
    assert(xqc_pow2(0) == 0);
    assert(xqc_pow2(1) == 1);
}
keengo99 commented 1 year ago
keengo@ubuntu22:~/project/xquic/build$ ./tests/run_tests | tee -a ../xquic_test.log

     CUnit - A unit testing framework for C - Version 2.1-3
     http://cunit.sourceforge.net/

Suite: libxquic_TestSuite
  Test: xqc_test_get_random ...passed
  Test: xqc_test_engine_create ...passed
  Test: xqc_test_conn_create ...passed
  Test: xqc_test_timer ...passed
  Test: xqc_test_pq ...passed
  Test: xqc_test_common ...passed
  Test: xqc_test_vint ...passed
  Test: xqc_test_recv_record ...passed
  Test: xqc_test_reno ...passed
  Test: xqc_test_cubic ...passed
  Test: xqc_test_short_header_parse_cid ...passed
  Test: xqc_test_long_header_parse_cid ...passed
  Test: xqc_test_engine_packet_process ...passed
  Test: xqc_test_stream_frame ...passed
  Test: xqc_test_wakeup_pq ...passed
  Test: xqc_test_process_frame ...passed
  Test: xqc_test_parse_padding_frame ...passed
  Test: xqc_test_large_ack_frame ...passed
  Test: xqc_test_h3_frame ...passed
  Test: xqc_test_transport_params ...passed
  Test: xqc_test_tls ...passed
  Test: xqc_test_crypto ...passed
  Test: xqc_test_h3_stream ...passed
  Test: xqc_test_stable ...passed
  Test: xqc_test_dtable ...passed
  Test: test_2d_hash_table ...passed
  Test: test_ring_array ...passed
  Test: xqc_test_ring_mem ...passed
  Test: xqc_test_huffman ...passed
  Test: xqc_test_encoder ...passed
  Test: xqc_test_h3_ins ...passed
  Test: xqc_test_h3_rep ...passed
  Test: xqc_qpack_test ...passed
  Test: xqc_test_prefixed_str ...passed
  Test: xqc_cid_test ...passed
  Test: xqc_test_id_hash ...passed
  Test: xqc_test_retry ...passed

Run Summary:    Type   Total     Ran  Passed Failed Inactive
              suites       1       1     n/a      0        0
               tests      37      37      37      0        0
             asserts 9427497 9427497 9427497      0      n/a
yuanbo-zhang commented 7 months ago

xqc_pow2 can be replace with xqc_pow2_upper

Fixd:https://github.com/alibaba/xquic/pull/378/commits/cff8287003d1133993564edf74cf05538040ce81

Yanmei-Liu commented 7 months ago

As the function has been replaced and deleted, simply close this pr.