-
Notifications
You must be signed in to change notification settings - Fork 1
/
aiutp_util.erl
39 lines (31 loc) · 962 Bytes
/
aiutp_util.erl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
-module(aiutp_util).
-export([bit16/1,bit32/1,getaddr/1]).
-export([bit16_random/0,bit32_random/0]).
-export([microsecond/0,millisecond/0]).
-export([clamp/3,wrapping_compare_less/3]).
-spec bit16(integer()) -> integer().
bit16(N) when is_integer(N) ->
N band 16#FFFF.
-spec bit32(integer()) -> integer().
bit32(N) when is_integer(N) ->
N band 16#FFFFFFFF.
getaddr(S) when is_list(S) ->
{ok, CAddr} = inet:getaddr(S, inet),
CAddr;
getaddr({_, _, _, _} = Addr) ->
Addr.
microsecond()-> erlang:system_time(microsecond).
millisecond()-> erlang:system_time(millisecond).
bit16_random() ->
<<N:16/integer>> = crypto:strong_rand_bytes(2),
N.
bit32_random()->
<<N:32/integer>> = crypto:strong_rand_bytes(4),
N.
clamp(Val, Min, _Max) when Val < Min -> Min;
clamp(Val, _Min, Max) when Val > Max -> Max;
clamp(Val, _Min, _Max) -> Val.
wrapping_compare_less(L,R,Mask)->
Down = (L - R) band Mask,
Up = (R - L) band Mask,
Up < Down.